blob: e937397d80e1cc969cd0034f26c9812e75008c80 [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
58 bool Visit(mirror::Class* klass) OVERRIDE REQUIRES(Locks::mutator_lock_) {
59 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()
69 : instrumentation_stubs_installed_(false), entry_exit_stubs_installed_(false),
70 interpreter_stubs_installed_(false),
71 interpret_only_(false), forced_interpret_only_(false),
72 have_method_entry_listeners_(false), have_method_exit_listeners_(false),
73 have_method_unwind_listeners_(false), have_dex_pc_listeners_(false),
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +020074 have_field_read_listeners_(false), have_field_write_listeners_(false),
Sebastien Hertz0462c4c2015-04-01 16:34:17 +020075 have_exception_caught_listeners_(false), have_backward_branch_listeners_(false),
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070076 deoptimized_methods_lock_("deoptimized methods lock"),
77 deoptimization_enabled_(false),
78 interpreter_handler_table_(kMainHandlerTable),
79 quick_alloc_entry_points_instrumentation_counter_(0) {
80}
81
Sebastien Hertza10aa372015-01-21 17:30:58 +010082void Instrumentation::InstallStubsForClass(mirror::Class* klass) {
Sebastien Hertza8a697f2015-01-15 12:28:47 +010083 if (klass->IsErroneous()) {
84 // We can't execute code in a erroneous class: do nothing.
85 } else if (!klass->IsResolved()) {
86 // We need the class to be resolved to install/uninstall stubs. Otherwise its methods
87 // could not be initialized or linked with regards to class inheritance.
88 } else {
89 for (size_t i = 0, e = klass->NumDirectMethods(); i < e; i++) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070090 InstallStubsForMethod(klass->GetDirectMethod(i, sizeof(void*)));
Sebastien Hertza8a697f2015-01-15 12:28:47 +010091 }
92 for (size_t i = 0, e = klass->NumVirtualMethods(); i < e; i++) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070093 InstallStubsForMethod(klass->GetVirtualMethod(i, sizeof(void*)));
Sebastien Hertza8a697f2015-01-15 12:28:47 +010094 }
jeffhao725a9572012-11-13 18:20:12 -080095 }
jeffhao725a9572012-11-13 18:20:12 -080096}
97
Mathieu Chartiere401d142015-04-22 13:56:20 -070098static void UpdateEntrypoints(ArtMethod* method, const void* quick_code)
Mathieu Chartier90443472015-07-16 20:32:27 -070099 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800100 method->SetEntryPointFromQuickCompiledCode(quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100101}
102
Mathieu Chartiere401d142015-04-22 13:56:20 -0700103void Instrumentation::InstallStubsForMethod(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100104 if (method->IsAbstract() || method->IsProxyMethod()) {
105 // Do not change stubs for these methods.
106 return;
107 }
Jeff Hao56802772014-08-19 10:17:36 -0700108 // Don't stub Proxy.<init>. Note that the Proxy class itself is not a proxy class.
109 if (method->IsConstructor() &&
110 method->GetDeclaringClass()->DescriptorEquals("Ljava/lang/reflect/Proxy;")) {
Jeff Haodb8a6642014-08-14 17:18:52 -0700111 return;
112 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800113 const void* new_quick_code;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100114 bool uninstall = !entry_exit_stubs_installed_ && !interpreter_stubs_installed_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800115 Runtime* const runtime = Runtime::Current();
116 ClassLinker* const class_linker = runtime->GetClassLinker();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100117 bool is_class_initialized = method->GetDeclaringClass()->IsInitialized();
118 if (uninstall) {
119 if ((forced_interpret_only_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800120 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100121 } else if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800122 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100123 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700124 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100125 }
126 } else { // !uninstall
Sebastien Hertzbae182c2013-12-17 10:42:03 +0100127 if ((interpreter_stubs_installed_ || forced_interpret_only_ || IsDeoptimized(method)) &&
128 !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800129 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100130 } else {
131 // Do not overwrite resolution trampoline. When the trampoline initializes the method's
132 // class, all its static methods code will be set to the instrumentation entry point.
133 // For more details, see ClassLinker::FixupStaticTrampolines.
134 if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Sebastien Hertz320deb22014-06-11 19:45:05 +0200135 if (entry_exit_stubs_installed_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800136 new_quick_code = GetQuickInstrumentationEntryPoint();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200137 } else {
Sebastien Hertz320deb22014-06-11 19:45:05 +0200138 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100139 }
140 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700141 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100142 }
143 }
144 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800145 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100146}
147
Ian Rogers62d6c772013-02-27 08:32:07 -0800148// Places the instrumentation exit pc as the return PC for every quick frame. This also allows
149// deoptimization of quick frames to interpreter frames.
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100150// Since we may already have done this previously, we need to push new instrumentation frame before
151// existing instrumentation frames.
Ian Rogers62d6c772013-02-27 08:32:07 -0800152static void InstrumentationInstallStack(Thread* thread, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700153 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200154 struct InstallStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800155 InstallStackVisitor(Thread* thread_in, Context* context, uintptr_t instrumentation_exit_pc)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100156 : StackVisitor(thread_in, context, kInstrumentationStackWalk),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800157 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100158 instrumentation_exit_pc_(instrumentation_exit_pc),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100159 reached_existing_instrumentation_frames_(false), instrumentation_stack_depth_(0),
160 last_return_pc_(0) {
161 }
jeffhao725a9572012-11-13 18:20:12 -0800162
Mathieu Chartier90443472015-07-16 20:32:27 -0700163 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700164 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700165 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800166 if (kVerboseInstrumentation) {
167 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
168 }
169 last_return_pc_ = 0;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700170 return true; // Ignore upcalls.
Ian Rogers306057f2012-11-26 12:45:53 -0800171 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700172 if (GetCurrentQuickFrame() == nullptr) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800173 bool interpreter_frame = true;
Sebastien Hertz320deb22014-06-11 19:45:05 +0200174 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, 0, GetFrameId(),
175 interpreter_frame);
Jeff Haoa15a81b2014-05-27 18:25:47 -0700176 if (kVerboseInstrumentation) {
177 LOG(INFO) << "Pushing shadow frame " << instrumentation_frame.Dump();
178 }
179 shadow_stack_.push_back(instrumentation_frame);
180 return true; // Continue.
181 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800182 uintptr_t return_pc = GetReturnPc();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200183 if (m->IsRuntimeMethod()) {
184 if (return_pc == instrumentation_exit_pc_) {
185 if (kVerboseInstrumentation) {
186 LOG(INFO) << " Handling quick to interpreter transition. Frame " << GetFrameId();
187 }
188 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200189 const InstrumentationStackFrame& frame =
190 instrumentation_stack_->at(instrumentation_stack_depth_);
Sebastien Hertz320deb22014-06-11 19:45:05 +0200191 CHECK(frame.interpreter_entry_);
192 // This is an interpreter frame so method enter event must have been reported. However we
193 // need to push a DEX pc into the dex_pcs_ list to match size of instrumentation stack.
194 // Since we won't report method entry here, we can safely push any DEX pc.
195 dex_pcs_.push_back(0);
196 last_return_pc_ = frame.return_pc_;
197 ++instrumentation_stack_depth_;
198 return true;
199 } else {
200 if (kVerboseInstrumentation) {
201 LOG(INFO) << " Skipping runtime method. Frame " << GetFrameId();
202 }
203 last_return_pc_ = GetReturnPc();
204 return true; // Ignore unresolved methods since they will be instrumented after resolution.
205 }
206 }
207 if (kVerboseInstrumentation) {
208 LOG(INFO) << " Installing exit stub in " << DescribeLocation();
209 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100210 if (return_pc == instrumentation_exit_pc_) {
211 // We've reached a frame which has already been installed with instrumentation exit stub.
212 // We should have already installed instrumentation on previous frames.
213 reached_existing_instrumentation_frames_ = true;
214
215 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200216 const InstrumentationStackFrame& frame =
217 instrumentation_stack_->at(instrumentation_stack_depth_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100218 CHECK_EQ(m, frame.method_) << "Expected " << PrettyMethod(m)
219 << ", Found " << PrettyMethod(frame.method_);
220 return_pc = frame.return_pc_;
221 if (kVerboseInstrumentation) {
222 LOG(INFO) << "Ignoring already instrumented " << frame.Dump();
223 }
224 } else {
225 CHECK_NE(return_pc, 0U);
226 CHECK(!reached_existing_instrumentation_frames_);
227 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, return_pc, GetFrameId(),
228 false);
229 if (kVerboseInstrumentation) {
230 LOG(INFO) << "Pushing frame " << instrumentation_frame.Dump();
231 }
232
Sebastien Hertz320deb22014-06-11 19:45:05 +0200233 // Insert frame at the right position so we do not corrupt the instrumentation stack.
234 // Instrumentation stack frames are in descending frame id order.
235 auto it = instrumentation_stack_->begin();
236 for (auto end = instrumentation_stack_->end(); it != end; ++it) {
237 const InstrumentationStackFrame& current = *it;
238 if (instrumentation_frame.frame_id_ >= current.frame_id_) {
239 break;
240 }
241 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100242 instrumentation_stack_->insert(it, instrumentation_frame);
243 SetReturnPc(instrumentation_exit_pc_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800244 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100245 dex_pcs_.push_back((GetCurrentOatQuickMethodHeader() == nullptr)
246 ? DexFile::kDexNoIndex
247 : GetCurrentOatQuickMethodHeader()->ToDexPc(m, last_return_pc_));
Ian Rogers62d6c772013-02-27 08:32:07 -0800248 last_return_pc_ = return_pc;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100249 ++instrumentation_stack_depth_;
Ian Rogers306057f2012-11-26 12:45:53 -0800250 return true; // Continue.
251 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800252 std::deque<InstrumentationStackFrame>* const instrumentation_stack_;
Jeff Haoa15a81b2014-05-27 18:25:47 -0700253 std::vector<InstrumentationStackFrame> shadow_stack_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800254 std::vector<uint32_t> dex_pcs_;
Ian Rogers306057f2012-11-26 12:45:53 -0800255 const uintptr_t instrumentation_exit_pc_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100256 bool reached_existing_instrumentation_frames_;
257 size_t instrumentation_stack_depth_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800258 uintptr_t last_return_pc_;
Ian Rogers306057f2012-11-26 12:45:53 -0800259 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800260 if (kVerboseInstrumentation) {
261 std::string thread_name;
262 thread->GetThreadName(thread_name);
263 LOG(INFO) << "Installing exit stubs in " << thread_name;
Ian Rogers306057f2012-11-26 12:45:53 -0800264 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100265
266 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers700a4022014-05-19 16:49:03 -0700267 std::unique_ptr<Context> context(Context::Create());
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700268 uintptr_t instrumentation_exit_pc = reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100269 InstallStackVisitor visitor(thread, context.get(), instrumentation_exit_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800270 visitor.WalkStack(true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100271 CHECK_EQ(visitor.dex_pcs_.size(), thread->GetInstrumentationStack()->size());
Ian Rogers62d6c772013-02-27 08:32:07 -0800272
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100273 if (instrumentation->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100274 // Create method enter events for all methods currently on the thread's stack. We only do this
275 // if no debugger is attached to prevent from posting events twice.
Jeff Haoa15a81b2014-05-27 18:25:47 -0700276 auto ssi = visitor.shadow_stack_.rbegin();
277 for (auto isi = thread->GetInstrumentationStack()->rbegin(),
278 end = thread->GetInstrumentationStack()->rend(); isi != end; ++isi) {
279 while (ssi != visitor.shadow_stack_.rend() && (*ssi).frame_id_ < (*isi).frame_id_) {
280 instrumentation->MethodEnterEvent(thread, (*ssi).this_object_, (*ssi).method_, 0);
281 ++ssi;
282 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100283 uint32_t dex_pc = visitor.dex_pcs_.back();
284 visitor.dex_pcs_.pop_back();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200285 if (!isi->interpreter_entry_) {
286 instrumentation->MethodEnterEvent(thread, (*isi).this_object_, (*isi).method_, dex_pc);
287 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100288 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800289 }
290 thread->VerifyStack();
Ian Rogers306057f2012-11-26 12:45:53 -0800291}
292
Mingyao Yang99170c62015-07-06 11:10:37 -0700293void Instrumentation::InstrumentThreadStack(Thread* thread) {
294 instrumentation_stubs_installed_ = true;
295 InstrumentationInstallStack(thread, this);
296}
297
Ian Rogers62d6c772013-02-27 08:32:07 -0800298// Removes the instrumentation exit pc as the return PC for every quick frame.
299static void InstrumentationRestoreStack(Thread* thread, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700300 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200301 struct RestoreStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800302 RestoreStackVisitor(Thread* thread_in, uintptr_t instrumentation_exit_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800303 Instrumentation* instrumentation)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100304 : StackVisitor(thread_in, nullptr, kInstrumentationStackWalk),
305 thread_(thread_in),
Ian Rogers62d6c772013-02-27 08:32:07 -0800306 instrumentation_exit_pc_(instrumentation_exit_pc),
307 instrumentation_(instrumentation),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800308 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Ian Rogers62d6c772013-02-27 08:32:07 -0800309 frames_removed_(0) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800310
Mathieu Chartier90443472015-07-16 20:32:27 -0700311 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800312 if (instrumentation_stack_->size() == 0) {
jeffhao725a9572012-11-13 18:20:12 -0800313 return false; // Stop.
314 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700315 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700316 if (GetCurrentQuickFrame() == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800317 if (kVerboseInstrumentation) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200318 LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId()
319 << " Method=" << PrettyMethod(m);
Ian Rogers62d6c772013-02-27 08:32:07 -0800320 }
321 return true; // Ignore shadow frames.
322 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700323 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800324 if (kVerboseInstrumentation) {
325 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
326 }
Ian Rogers306057f2012-11-26 12:45:53 -0800327 return true; // Ignore upcalls.
328 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800329 bool removed_stub = false;
330 // TODO: make this search more efficient?
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100331 const size_t frameId = GetFrameId();
332 for (const InstrumentationStackFrame& instrumentation_frame : *instrumentation_stack_) {
333 if (instrumentation_frame.frame_id_ == frameId) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800334 if (kVerboseInstrumentation) {
335 LOG(INFO) << " Removing exit stub in " << DescribeLocation();
336 }
Jeff Hao9a916d32013-06-27 18:45:37 -0700337 if (instrumentation_frame.interpreter_entry_) {
338 CHECK(m == Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
339 } else {
340 CHECK(m == instrumentation_frame.method_) << PrettyMethod(m);
341 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800342 SetReturnPc(instrumentation_frame.return_pc_);
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100343 if (instrumentation_->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100344 // Create the method exit events. As the methods didn't really exit the result is 0.
345 // We only do this if no debugger is attached to prevent from posting events twice.
346 instrumentation_->MethodExitEvent(thread_, instrumentation_frame.this_object_, m,
347 GetDexPc(), JValue());
348 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800349 frames_removed_++;
350 removed_stub = true;
351 break;
352 }
353 }
354 if (!removed_stub) {
355 if (kVerboseInstrumentation) {
356 LOG(INFO) << " No exit stub in " << DescribeLocation();
Ian Rogers306057f2012-11-26 12:45:53 -0800357 }
jeffhao725a9572012-11-13 18:20:12 -0800358 }
359 return true; // Continue.
360 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800361 Thread* const thread_;
Ian Rogers306057f2012-11-26 12:45:53 -0800362 const uintptr_t instrumentation_exit_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800363 Instrumentation* const instrumentation_;
364 std::deque<instrumentation::InstrumentationStackFrame>* const instrumentation_stack_;
365 size_t frames_removed_;
jeffhao725a9572012-11-13 18:20:12 -0800366 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800367 if (kVerboseInstrumentation) {
368 std::string thread_name;
369 thread->GetThreadName(thread_name);
370 LOG(INFO) << "Removing exit stubs in " << thread_name;
371 }
372 std::deque<instrumentation::InstrumentationStackFrame>* stack = thread->GetInstrumentationStack();
373 if (stack->size() > 0) {
374 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700375 uintptr_t instrumentation_exit_pc =
376 reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Ian Rogers62d6c772013-02-27 08:32:07 -0800377 RestoreStackVisitor visitor(thread, instrumentation_exit_pc, instrumentation);
378 visitor.WalkStack(true);
379 CHECK_EQ(visitor.frames_removed_, stack->size());
380 while (stack->size() > 0) {
381 stack->pop_front();
382 }
jeffhao725a9572012-11-13 18:20:12 -0800383 }
384}
385
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200386static bool HasEvent(Instrumentation::InstrumentationEvent expected, uint32_t events) {
387 return (events & expected) != 0;
388}
389
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000390static void PotentiallyAddListenerTo(Instrumentation::InstrumentationEvent event,
391 uint32_t events,
392 std::list<InstrumentationListener*>& list,
393 InstrumentationListener* listener,
394 bool* has_listener)
395 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) {
396 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
397 if (!HasEvent(event, events)) {
398 return;
399 }
400 // If there is a free slot in the list, we insert the listener in that slot.
401 // Otherwise we add it to the end of the list.
402 auto it = std::find(list.begin(), list.end(), nullptr);
403 if (it != list.end()) {
404 *it = listener;
405 } else {
406 list.push_back(listener);
407 }
408 *has_listener = true;
409}
410
Ian Rogers62d6c772013-02-27 08:32:07 -0800411void Instrumentation::AddListener(InstrumentationListener* listener, uint32_t events) {
412 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000413 PotentiallyAddListenerTo(kMethodEntered,
414 events,
415 method_entry_listeners_,
416 listener,
417 &have_method_entry_listeners_);
418 PotentiallyAddListenerTo(kMethodExited,
419 events,
420 method_exit_listeners_,
421 listener,
422 &have_method_exit_listeners_);
423 PotentiallyAddListenerTo(kMethodUnwind,
424 events,
425 method_unwind_listeners_,
426 listener,
427 &have_method_unwind_listeners_);
428 PotentiallyAddListenerTo(kBackwardBranch,
429 events,
430 backward_branch_listeners_,
431 listener,
432 &have_backward_branch_listeners_);
433 PotentiallyAddListenerTo(kInvokeVirtualOrInterface,
434 events,
435 invoke_virtual_or_interface_listeners_,
436 listener,
437 &have_invoke_virtual_or_interface_listeners_);
438 PotentiallyAddListenerTo(kDexPcMoved,
439 events,
440 dex_pc_listeners_,
441 listener,
442 &have_dex_pc_listeners_);
443 PotentiallyAddListenerTo(kFieldRead,
444 events,
445 field_read_listeners_,
446 listener,
447 &have_field_read_listeners_);
448 PotentiallyAddListenerTo(kFieldWritten,
449 events,
450 field_write_listeners_,
451 listener,
452 &have_field_write_listeners_);
453 PotentiallyAddListenerTo(kExceptionCaught,
454 events,
455 exception_caught_listeners_,
456 listener,
457 &have_exception_caught_listeners_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200458 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800459}
460
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000461static void PotentiallyRemoveListenerFrom(Instrumentation::InstrumentationEvent event,
462 uint32_t events,
463 std::list<InstrumentationListener*>& list,
464 InstrumentationListener* listener,
465 bool* has_listener)
466 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) {
467 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
468 if (!HasEvent(event, events)) {
469 return;
470 }
471 auto it = std::find(list.begin(), list.end(), listener);
472 if (it != list.end()) {
473 // Just update the entry, do not remove from the list. Removing entries in the list
474 // is unsafe when mutators are iterating over it.
475 *it = nullptr;
476 }
477
478 // Check if the list contains any non-null listener, and update 'has_listener'.
479 for (InstrumentationListener* l : list) {
480 if (l != nullptr) {
481 *has_listener = true;
482 return;
483 }
484 }
485 *has_listener = false;
486}
487
Ian Rogers62d6c772013-02-27 08:32:07 -0800488void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) {
489 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000490 PotentiallyRemoveListenerFrom(kMethodEntered,
491 events,
492 method_entry_listeners_,
493 listener,
494 &have_method_entry_listeners_);
495 PotentiallyRemoveListenerFrom(kMethodExited,
496 events,
497 method_exit_listeners_,
498 listener,
499 &have_method_exit_listeners_);
500 PotentiallyRemoveListenerFrom(kMethodUnwind,
501 events,
502 method_unwind_listeners_,
503 listener,
504 &have_method_unwind_listeners_);
505 PotentiallyRemoveListenerFrom(kBackwardBranch,
506 events,
507 backward_branch_listeners_,
508 listener,
509 &have_backward_branch_listeners_);
510 PotentiallyRemoveListenerFrom(kInvokeVirtualOrInterface,
511 events,
512 invoke_virtual_or_interface_listeners_,
513 listener,
514 &have_invoke_virtual_or_interface_listeners_);
515 PotentiallyRemoveListenerFrom(kDexPcMoved,
516 events,
517 dex_pc_listeners_,
518 listener,
519 &have_dex_pc_listeners_);
520 PotentiallyRemoveListenerFrom(kFieldRead,
521 events,
522 field_read_listeners_,
523 listener,
524 &have_field_read_listeners_);
525 PotentiallyRemoveListenerFrom(kFieldWritten,
526 events,
527 field_write_listeners_,
528 listener,
529 &have_field_write_listeners_);
530 PotentiallyRemoveListenerFrom(kExceptionCaught,
531 events,
532 exception_caught_listeners_,
533 listener,
534 &have_exception_caught_listeners_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200535 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800536}
537
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200538Instrumentation::InstrumentationLevel Instrumentation::GetCurrentInstrumentationLevel() const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800539 if (interpreter_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200540 return InstrumentationLevel::kInstrumentWithInterpreter;
Ian Rogers62d6c772013-02-27 08:32:07 -0800541 } else if (entry_exit_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200542 return InstrumentationLevel::kInstrumentWithInstrumentationStubs;
Ian Rogers62d6c772013-02-27 08:32:07 -0800543 } else {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200544 return InstrumentationLevel::kInstrumentNothing;
Ian Rogers62d6c772013-02-27 08:32:07 -0800545 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200546}
547
548void Instrumentation::ConfigureStubs(const char* key, InstrumentationLevel desired_level) {
549 // Store the instrumentation level for this key or remove it.
550 if (desired_level == InstrumentationLevel::kInstrumentNothing) {
551 // The client no longer needs instrumentation.
552 requested_instrumentation_levels_.erase(key);
553 } else {
554 // The client needs instrumentation.
555 requested_instrumentation_levels_.Overwrite(key, desired_level);
556 }
557
558 // Look for the highest required instrumentation level.
559 InstrumentationLevel requested_level = InstrumentationLevel::kInstrumentNothing;
560 for (const auto& v : requested_instrumentation_levels_) {
561 requested_level = std::max(requested_level, v.second);
562 }
563
564 interpret_only_ = (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) ||
565 forced_interpret_only_;
566
567 InstrumentationLevel current_level = GetCurrentInstrumentationLevel();
568 if (requested_level == current_level) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800569 // We're already set.
570 return;
571 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100572 Thread* const self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -0800573 Runtime* runtime = Runtime::Current();
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100574 Locks::mutator_lock_->AssertExclusiveHeld(self);
Ian Rogers62d6c772013-02-27 08:32:07 -0800575 Locks::thread_list_lock_->AssertNotHeld(self);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200576 if (requested_level > InstrumentationLevel::kInstrumentNothing) {
577 if (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800578 interpreter_stubs_installed_ = true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800579 entry_exit_stubs_installed_ = true;
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200580 } else {
581 CHECK_EQ(requested_level, InstrumentationLevel::kInstrumentWithInstrumentationStubs);
582 entry_exit_stubs_installed_ = true;
583 interpreter_stubs_installed_ = false;
Ian Rogers62d6c772013-02-27 08:32:07 -0800584 }
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700585 InstallStubsClassVisitor visitor(this);
586 runtime->GetClassLinker()->VisitClasses(&visitor);
Ian Rogers62d6c772013-02-27 08:32:07 -0800587 instrumentation_stubs_installed_ = true;
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100588 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800589 runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this);
590 } else {
591 interpreter_stubs_installed_ = false;
592 entry_exit_stubs_installed_ = false;
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700593 InstallStubsClassVisitor visitor(this);
594 runtime->GetClassLinker()->VisitClasses(&visitor);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100595 // Restore stack only if there is no method currently deoptimized.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700596 bool empty;
597 {
598 ReaderMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700599 empty = IsDeoptimizedMethodsEmpty(); // Avoid lock violation.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700600 }
601 if (empty) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100602 instrumentation_stubs_installed_ = false;
603 MutexLock mu(self, *Locks::thread_list_lock_);
604 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
605 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800606 }
jeffhao725a9572012-11-13 18:20:12 -0800607}
608
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200609static void ResetQuickAllocEntryPointsForThread(Thread* thread, void* arg ATTRIBUTE_UNUSED) {
Ian Rogersfa824272013-11-05 16:12:57 -0800610 thread->ResetQuickAllocEntryPointsForThread();
611}
612
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700613void Instrumentation::SetEntrypointsInstrumented(bool instrumented) {
614 Thread* self = Thread::Current();
Mathieu Chartier661974a2014-01-09 11:23:53 -0800615 Runtime* runtime = Runtime::Current();
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700616 Locks::mutator_lock_->AssertNotHeld(self);
617 Locks::instrument_entrypoints_lock_->AssertHeld(self);
618 if (runtime->IsStarted()) {
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700619 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700620 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
Mathieu Chartier661974a2014-01-09 11:23:53 -0800621 SetQuickAllocEntryPointsInstrumented(instrumented);
622 ResetQuickAllocEntryPoints();
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700623 } else {
624 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
625 SetQuickAllocEntryPointsInstrumented(instrumented);
626 ResetQuickAllocEntryPoints();
Mathieu Chartier661974a2014-01-09 11:23:53 -0800627 }
628}
629
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700630void Instrumentation::InstrumentQuickAllocEntryPoints() {
631 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
632 InstrumentQuickAllocEntryPointsLocked();
Ian Rogersfa824272013-11-05 16:12:57 -0800633}
634
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700635void Instrumentation::UninstrumentQuickAllocEntryPoints() {
636 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
637 UninstrumentQuickAllocEntryPointsLocked();
638}
639
640void Instrumentation::InstrumentQuickAllocEntryPointsLocked() {
641 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
642 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
643 SetEntrypointsInstrumented(true);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800644 }
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700645 ++quick_alloc_entry_points_instrumentation_counter_;
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700646}
647
648void Instrumentation::UninstrumentQuickAllocEntryPointsLocked() {
649 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
650 CHECK_GT(quick_alloc_entry_points_instrumentation_counter_, 0U);
651 --quick_alloc_entry_points_instrumentation_counter_;
652 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
653 SetEntrypointsInstrumented(false);
654 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800655}
656
657void Instrumentation::ResetQuickAllocEntryPoints() {
658 Runtime* runtime = Runtime::Current();
659 if (runtime->IsStarted()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800660 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700661 runtime->GetThreadList()->ForEach(ResetQuickAllocEntryPointsForThread, nullptr);
Ian Rogersfa824272013-11-05 16:12:57 -0800662 }
663}
664
Mathieu Chartiere401d142015-04-22 13:56:20 -0700665void Instrumentation::UpdateMethodsCode(ArtMethod* method, const void* quick_code) {
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100666 DCHECK(method->GetDeclaringClass()->IsResolved());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800667 const void* new_quick_code;
Ian Rogers62d6c772013-02-27 08:32:07 -0800668 if (LIKELY(!instrumentation_stubs_installed_)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800669 new_quick_code = quick_code;
Jeff Hao65d15d92013-07-16 16:39:33 -0700670 } else {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100671 if ((interpreter_stubs_installed_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800672 new_quick_code = GetQuickToInterpreterBridge();
Jeff Hao65d15d92013-07-16 16:39:33 -0700673 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700674 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700675 if (class_linker->IsQuickResolutionStub(quick_code) ||
676 class_linker->IsQuickToInterpreterBridge(quick_code)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700677 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700678 } else if (entry_exit_stubs_installed_) {
679 new_quick_code = GetQuickInstrumentationEntryPoint();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700680 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700681 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700682 }
Jeff Hao65d15d92013-07-16 16:39:33 -0700683 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800684 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800685 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100686}
687
Mathieu Chartiere401d142015-04-22 13:56:20 -0700688bool Instrumentation::AddDeoptimizedMethod(ArtMethod* method) {
689 if (IsDeoptimizedMethod(method)) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700690 // Already in the map. Return.
691 return false;
692 }
693 // Not found. Add it.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700694 deoptimized_methods_.insert(method);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700695 return true;
696}
697
Mathieu Chartiere401d142015-04-22 13:56:20 -0700698bool Instrumentation::IsDeoptimizedMethod(ArtMethod* method) {
699 return deoptimized_methods_.find(method) != deoptimized_methods_.end();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700700}
701
Mathieu Chartiere401d142015-04-22 13:56:20 -0700702ArtMethod* Instrumentation::BeginDeoptimizedMethod() {
703 if (deoptimized_methods_.empty()) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700704 // Empty.
705 return nullptr;
706 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700707 return *deoptimized_methods_.begin();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700708}
709
Mathieu Chartiere401d142015-04-22 13:56:20 -0700710bool Instrumentation::RemoveDeoptimizedMethod(ArtMethod* method) {
711 auto it = deoptimized_methods_.find(method);
712 if (it == deoptimized_methods_.end()) {
713 return false;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700714 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700715 deoptimized_methods_.erase(it);
716 return true;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700717}
718
719bool Instrumentation::IsDeoptimizedMethodsEmpty() const {
720 return deoptimized_methods_.empty();
721}
722
Mathieu Chartiere401d142015-04-22 13:56:20 -0700723void Instrumentation::Deoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100724 CHECK(!method->IsNative());
725 CHECK(!method->IsProxyMethod());
726 CHECK(!method->IsAbstract());
727
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700728 Thread* self = Thread::Current();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700729 {
730 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700731 bool has_not_been_deoptimized = AddDeoptimizedMethod(method);
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200732 CHECK(has_not_been_deoptimized) << "Method " << PrettyMethod(method)
733 << " is already deoptimized";
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700734 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100735 if (!interpreter_stubs_installed_) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800736 UpdateEntrypoints(method, GetQuickInstrumentationEntryPoint());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100737
738 // Install instrumentation exit stub and instrumentation frames. We may already have installed
739 // these previously so it will only cover the newly created frames.
740 instrumentation_stubs_installed_ = true;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700741 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100742 Runtime::Current()->GetThreadList()->ForEach(InstrumentationInstallStack, this);
743 }
744}
745
Mathieu Chartiere401d142015-04-22 13:56:20 -0700746void Instrumentation::Undeoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100747 CHECK(!method->IsNative());
748 CHECK(!method->IsProxyMethod());
749 CHECK(!method->IsAbstract());
750
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700751 Thread* self = Thread::Current();
752 bool empty;
753 {
754 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700755 bool found_and_erased = RemoveDeoptimizedMethod(method);
756 CHECK(found_and_erased) << "Method " << PrettyMethod(method)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700757 << " is not deoptimized";
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700758 empty = IsDeoptimizedMethodsEmpty();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700759 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100760
761 // Restore code and possibly stack only if we did not deoptimize everything.
762 if (!interpreter_stubs_installed_) {
763 // Restore its code or resolution trampoline.
764 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800765 if (method->IsStatic() && !method->IsConstructor() &&
766 !method->GetDeclaringClass()->IsInitialized()) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800767 UpdateEntrypoints(method, GetQuickResolutionStub());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100768 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800769 const void* quick_code = class_linker->GetQuickOatCodeFor(method);
Elliott Hughes956af0f2014-12-11 14:34:28 -0800770 UpdateEntrypoints(method, quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100771 }
772
773 // If there is no deoptimized method left, we can restore the stack of each thread.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700774 if (empty) {
775 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100776 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
777 instrumentation_stubs_installed_ = false;
778 }
779 }
780}
781
Mathieu Chartiere401d142015-04-22 13:56:20 -0700782bool Instrumentation::IsDeoptimized(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100783 DCHECK(method != nullptr);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700784 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700785 return IsDeoptimizedMethod(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100786}
787
788void Instrumentation::EnableDeoptimization() {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700789 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700790 CHECK(IsDeoptimizedMethodsEmpty());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100791 CHECK_EQ(deoptimization_enabled_, false);
792 deoptimization_enabled_ = true;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100793}
794
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200795void Instrumentation::DisableDeoptimization(const char* key) {
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100796 CHECK_EQ(deoptimization_enabled_, true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100797 // If we deoptimized everything, undo it.
798 if (interpreter_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200799 UndeoptimizeEverything(key);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100800 }
801 // Undeoptimized selected methods.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700802 while (true) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700803 ArtMethod* method;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700804 {
805 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700806 if (IsDeoptimizedMethodsEmpty()) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700807 break;
808 }
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700809 method = BeginDeoptimizedMethod();
810 CHECK(method != nullptr);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700811 }
812 Undeoptimize(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100813 }
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100814 deoptimization_enabled_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100815}
816
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100817// Indicates if instrumentation should notify method enter/exit events to the listeners.
818bool Instrumentation::ShouldNotifyMethodEnterExitEvents() const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200819 if (!HasMethodEntryListeners() && !HasMethodExitListeners()) {
820 return false;
821 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100822 return !deoptimization_enabled_ && !interpreter_stubs_installed_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100823}
824
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200825void Instrumentation::DeoptimizeEverything(const char* key) {
826 CHECK(deoptimization_enabled_);
827 ConfigureStubs(key, InstrumentationLevel::kInstrumentWithInterpreter);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100828}
829
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200830void Instrumentation::UndeoptimizeEverything(const char* key) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100831 CHECK(interpreter_stubs_installed_);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200832 CHECK(deoptimization_enabled_);
833 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100834}
835
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200836void Instrumentation::EnableMethodTracing(const char* key, bool needs_interpreter) {
837 InstrumentationLevel level;
838 if (needs_interpreter) {
839 level = InstrumentationLevel::kInstrumentWithInterpreter;
840 } else {
841 level = InstrumentationLevel::kInstrumentWithInstrumentationStubs;
842 }
843 ConfigureStubs(key, level);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100844}
845
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200846void Instrumentation::DisableMethodTracing(const char* key) {
847 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
jeffhao725a9572012-11-13 18:20:12 -0800848}
849
Mathieu Chartiere401d142015-04-22 13:56:20 -0700850const void* Instrumentation::GetQuickCodeFor(ArtMethod* method, size_t pointer_size) const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800851 Runtime* runtime = Runtime::Current();
852 if (LIKELY(!instrumentation_stubs_installed_)) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800853 const void* code = method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Vladimir Marko8a630572014-04-09 18:45:35 +0100854 DCHECK(code != nullptr);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700855 ClassLinker* class_linker = runtime->GetClassLinker();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700856 if (LIKELY(!class_linker->IsQuickResolutionStub(code) &&
857 !class_linker->IsQuickToInterpreterBridge(code)) &&
858 !class_linker->IsQuickResolutionStub(code) &&
859 !class_linker->IsQuickToInterpreterBridge(code)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800860 return code;
861 }
862 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800863 return runtime->GetClassLinker()->GetQuickOatCodeFor(method);
jeffhao725a9572012-11-13 18:20:12 -0800864}
865
Ian Rogers62d6c772013-02-27 08:32:07 -0800866void Instrumentation::MethodEnterEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700867 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800868 uint32_t dex_pc) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000869 if (HasMethodEntryListeners()) {
870 for (InstrumentationListener* listener : method_entry_listeners_) {
871 if (listener != nullptr) {
872 listener->MethodEntered(thread, this_object, method, dex_pc);
873 }
874 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800875 }
876}
877
878void Instrumentation::MethodExitEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700879 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800880 uint32_t dex_pc, const JValue& return_value) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000881 if (HasMethodExitListeners()) {
882 for (InstrumentationListener* listener : method_exit_listeners_) {
883 if (listener != nullptr) {
884 listener->MethodExited(thread, this_object, method, dex_pc, return_value);
885 }
886 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800887 }
888}
889
890void Instrumentation::MethodUnwindEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700891 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800892 uint32_t dex_pc) const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200893 if (HasMethodUnwindListeners()) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700894 for (InstrumentationListener* listener : method_unwind_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000895 if (listener != nullptr) {
896 listener->MethodUnwind(thread, this_object, method, dex_pc);
897 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800898 }
899 }
900}
901
902void Instrumentation::DexPcMovedEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700903 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800904 uint32_t dex_pc) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000905 for (InstrumentationListener* listener : dex_pc_listeners_) {
906 if (listener != nullptr) {
907 listener->DexPcMoved(thread, this_object, method, dex_pc);
908 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800909 }
910}
911
Mathieu Chartiere401d142015-04-22 13:56:20 -0700912void Instrumentation::BackwardBranchImpl(Thread* thread, ArtMethod* method,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800913 int32_t offset) const {
914 for (InstrumentationListener* listener : backward_branch_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000915 if (listener != nullptr) {
916 listener->BackwardBranch(thread, method, offset);
917 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800918 }
919}
920
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100921void Instrumentation::InvokeVirtualOrInterfaceImpl(Thread* thread,
922 mirror::Object* this_object,
923 ArtMethod* caller,
924 uint32_t dex_pc,
925 ArtMethod* callee) const {
926 for (InstrumentationListener* listener : invoke_virtual_or_interface_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000927 if (listener != nullptr) {
928 listener->InvokeVirtualOrInterface(thread, this_object, caller, dex_pc, callee);
929 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100930 }
931}
932
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200933void Instrumentation::FieldReadEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700934 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700935 ArtField* field) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000936 for (InstrumentationListener* listener : field_read_listeners_) {
937 if (listener != nullptr) {
938 listener->FieldRead(thread, this_object, method, dex_pc, field);
939 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200940 }
941}
942
943void Instrumentation::FieldWriteEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700944 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700945 ArtField* field, const JValue& field_value) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000946 for (InstrumentationListener* listener : field_write_listeners_) {
947 if (listener != nullptr) {
948 listener->FieldWritten(thread, this_object, method, dex_pc, field, field_value);
949 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200950 }
951}
952
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000953void Instrumentation::ExceptionCaughtEvent(Thread* thread,
Sebastien Hertz947ff082013-09-17 14:10:13 +0200954 mirror::Throwable* exception_object) const {
Sebastien Hertz9f102032014-05-23 08:59:42 +0200955 if (HasExceptionCaughtListeners()) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000956 DCHECK_EQ(thread->GetException(), exception_object);
Jeff Haoc0bd4da2013-04-11 15:52:28 -0700957 thread->ClearException();
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000958 for (InstrumentationListener* listener : exception_caught_listeners_) {
959 if (listener != nullptr) {
960 listener->ExceptionCaught(thread, exception_object);
961 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800962 }
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000963 thread->SetException(exception_object);
Ian Rogers62d6c772013-02-27 08:32:07 -0800964 }
965}
966
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000967// Computes a frame ID by ignoring inlined frames.
968size_t Instrumentation::ComputeFrameId(Thread* self,
969 size_t frame_depth,
970 size_t inlined_frames_before_frame) {
971 CHECK_GE(frame_depth, inlined_frames_before_frame);
972 size_t no_inline_depth = frame_depth - inlined_frames_before_frame;
973 return StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) - no_inline_depth;
974}
975
Ian Rogers62d6c772013-02-27 08:32:07 -0800976static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame,
977 int delta)
Mathieu Chartier90443472015-07-16 20:32:27 -0700978 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100979 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) + delta;
Ian Rogers62d6c772013-02-27 08:32:07 -0800980 if (frame_id != instrumentation_frame.frame_id_) {
981 LOG(ERROR) << "Expected frame_id=" << frame_id << " but found "
982 << instrumentation_frame.frame_id_;
983 StackVisitor::DescribeStack(self);
984 CHECK_EQ(frame_id, instrumentation_frame.frame_id_);
985 }
986}
987
988void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700989 ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -0700990 uintptr_t lr, bool interpreter_entry) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800991 // We have a callee-save frame meaning this value is guaranteed to never be 0.
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100992 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk);
Ian Rogers62d6c772013-02-27 08:32:07 -0800993 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
994 if (kVerboseInstrumentation) {
Brian Carlstrom2d888622013-07-18 17:02:00 -0700995 LOG(INFO) << "Entering " << PrettyMethod(method) << " from PC " << reinterpret_cast<void*>(lr);
Ian Rogers62d6c772013-02-27 08:32:07 -0800996 }
997 instrumentation::InstrumentationStackFrame instrumentation_frame(this_object, method, lr,
Jeff Hao9a916d32013-06-27 18:45:37 -0700998 frame_id, interpreter_entry);
Ian Rogers62d6c772013-02-27 08:32:07 -0800999 stack->push_front(instrumentation_frame);
1000
Sebastien Hertz320deb22014-06-11 19:45:05 +02001001 if (!interpreter_entry) {
1002 MethodEnterEvent(self, this_object, method, 0);
1003 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001004}
1005
Andreas Gamped58342c2014-06-05 14:18:08 -07001006TwoWordReturn Instrumentation::PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc,
1007 uint64_t gpr_result,
1008 uint64_t fpr_result) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001009 // Do the pop.
1010 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1011 CHECK_GT(stack->size(), 0U);
1012 InstrumentationStackFrame instrumentation_frame = stack->front();
1013 stack->pop_front();
1014
1015 // Set return PC and check the sanity of the stack.
1016 *return_pc = instrumentation_frame.return_pc_;
1017 CheckStackDepth(self, instrumentation_frame, 0);
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001018 self->VerifyStack();
Ian Rogers62d6c772013-02-27 08:32:07 -08001019
Mathieu Chartiere401d142015-04-22 13:56:20 -07001020 ArtMethod* method = instrumentation_frame.method_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001021 uint32_t length;
Mingyao Yang99170c62015-07-06 11:10:37 -07001022 const size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
1023 char return_shorty = method->GetInterfaceMethodIfProxy(pointer_size)->GetShorty(&length)[0];
Ian Rogers62d6c772013-02-27 08:32:07 -08001024 JValue return_value;
1025 if (return_shorty == 'V') {
1026 return_value.SetJ(0);
1027 } else if (return_shorty == 'F' || return_shorty == 'D') {
1028 return_value.SetJ(fpr_result);
1029 } else {
1030 return_value.SetJ(gpr_result);
1031 }
1032 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1033 // return_pc.
1034 uint32_t dex_pc = DexFile::kDexNoIndex;
1035 mirror::Object* this_object = instrumentation_frame.this_object_;
Sebastien Hertz320deb22014-06-11 19:45:05 +02001036 if (!instrumentation_frame.interpreter_entry_) {
1037 MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value);
1038 }
jeffhao725a9572012-11-13 18:20:12 -08001039
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001040 // Deoptimize if the caller needs to continue execution in the interpreter. Do nothing if we get
1041 // back to an upcall.
1042 NthCallerVisitor visitor(self, 1, true);
1043 visitor.WalkStack(true);
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001044 bool deoptimize = (visitor.caller != nullptr) &&
Daniel Mihalyieb076692014-08-22 17:33:31 +02001045 (interpreter_stubs_installed_ || IsDeoptimized(visitor.caller) ||
1046 Dbg::IsForcedInterpreterNeededForUpcall(self, visitor.caller));
Ian Rogers62d6c772013-02-27 08:32:07 -08001047 if (deoptimize) {
1048 if (kVerboseInstrumentation) {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001049 LOG(INFO) << StringPrintf("Deoptimizing %s by returning from %s with result %#" PRIx64 " in ",
1050 PrettyMethod(visitor.caller).c_str(),
1051 PrettyMethod(method).c_str(),
1052 return_value.GetJ()) << *self;
Ian Rogers62d6c772013-02-27 08:32:07 -08001053 }
Sebastien Hertz07474662015-08-25 15:12:33 +00001054 self->PushDeoptimizationContext(return_value, return_shorty == 'L',
1055 nullptr /* no pending exception */);
Andreas Gamped58342c2014-06-05 14:18:08 -07001056 return GetTwoWordSuccessValue(*return_pc,
1057 reinterpret_cast<uintptr_t>(GetQuickDeoptimizationEntryPoint()));
Ian Rogers62d6c772013-02-27 08:32:07 -08001058 } else {
1059 if (kVerboseInstrumentation) {
Brian Carlstrom2d888622013-07-18 17:02:00 -07001060 LOG(INFO) << "Returning from " << PrettyMethod(method)
1061 << " to PC " << reinterpret_cast<void*>(*return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001062 }
Andreas Gamped58342c2014-06-05 14:18:08 -07001063 return GetTwoWordSuccessValue(0, *return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001064 }
jeffhao725a9572012-11-13 18:20:12 -08001065}
1066
Ian Rogers62d6c772013-02-27 08:32:07 -08001067void Instrumentation::PopMethodForUnwind(Thread* self, bool is_deoptimization) const {
1068 // Do the pop.
1069 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1070 CHECK_GT(stack->size(), 0U);
1071 InstrumentationStackFrame instrumentation_frame = stack->front();
1072 // TODO: bring back CheckStackDepth(self, instrumentation_frame, 2);
1073 stack->pop_front();
1074
Mathieu Chartiere401d142015-04-22 13:56:20 -07001075 ArtMethod* method = instrumentation_frame.method_;
Ian Rogers62d6c772013-02-27 08:32:07 -08001076 if (is_deoptimization) {
1077 if (kVerboseInstrumentation) {
1078 LOG(INFO) << "Popping for deoptimization " << PrettyMethod(method);
1079 }
1080 } else {
1081 if (kVerboseInstrumentation) {
1082 LOG(INFO) << "Popping for unwind " << PrettyMethod(method);
1083 }
1084
1085 // Notify listeners of method unwind.
1086 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1087 // return_pc.
1088 uint32_t dex_pc = DexFile::kDexNoIndex;
1089 MethodUnwindEvent(self, instrumentation_frame.this_object_, method, dex_pc);
1090 }
1091}
1092
1093std::string InstrumentationStackFrame::Dump() const {
1094 std::ostringstream os;
1095 os << "Frame " << frame_id_ << " " << PrettyMethod(method_) << ":"
1096 << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_);
1097 return os.str();
1098}
1099
1100} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -08001101} // namespace art