blob: 194cb18078670593a765ff99f1bf7d5f35782a28 [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
19#include <sys/uio.h>
20
Ian Rogersef7d42f2014-01-06 12:55:46 -080021#include "atomic.h"
Elliott Hughes76160052012-12-12 16:31:20 -080022#include "base/unix_file/fd_file.h"
jeffhao725a9572012-11-13 18:20:12 -080023#include "class_linker.h"
24#include "debugger.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080025#include "dex_file-inl.h"
Mathieu Chartierd8891782014-03-02 13:28:37 -080026#include "entrypoints/quick/quick_alloc_entrypoints.h"
Sebastien Hertz138dbfc2013-12-04 18:15:25 +010027#include "interpreter/interpreter.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070028#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "mirror/class-inl.h"
30#include "mirror/dex_cache.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031#include "mirror/object_array-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070032#include "mirror/object-inl.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080033#include "nth_caller_visitor.h"
Ian Rogersc928de92013-02-27 14:30:44 -080034#if !defined(ART_USE_PORTABLE_COMPILER)
Ian Rogers166db042013-07-26 12:05:57 -070035#include "entrypoints/quick/quick_entrypoints.h"
jeffhao725a9572012-11-13 18:20:12 -080036#endif
37#include "object_utils.h"
38#include "os.h"
39#include "scoped_thread_state_change.h"
40#include "thread.h"
41#include "thread_list.h"
jeffhao725a9572012-11-13 18:20:12 -080042
43namespace art {
Ian Rogersfa824272013-11-05 16:12:57 -080044
Ian Rogers62d6c772013-02-27 08:32:07 -080045namespace instrumentation {
jeffhao725a9572012-11-13 18:20:12 -080046
Sebastien Hertz5bfd5c92013-11-15 11:36:07 +010047const bool kVerboseInstrumentation = false;
48
Ian Rogers816432e2013-09-06 15:47:45 -070049// Do we want to deoptimize for method entry and exit listeners or just try to intercept
50// invocations? Deoptimization forces all code to run in the interpreter and considerably hurts the
51// application's performance.
Ian Rogers7b6da362013-09-11 09:29:40 -070052static constexpr bool kDeoptimizeForAccurateMethodEntryExitListeners = false;
Ian Rogers816432e2013-09-06 15:47:45 -070053
Ian Rogers62d6c772013-02-27 08:32:07 -080054static bool InstallStubsClassVisitor(mirror::Class* klass, void* arg)
jeffhao725a9572012-11-13 18:20:12 -080055 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -080056 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
57 return instrumentation->InstallStubsForClass(klass);
58}
59
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070060Instrumentation::Instrumentation()
61 : instrumentation_stubs_installed_(false), entry_exit_stubs_installed_(false),
62 interpreter_stubs_installed_(false),
63 interpret_only_(false), forced_interpret_only_(false),
64 have_method_entry_listeners_(false), have_method_exit_listeners_(false),
65 have_method_unwind_listeners_(false), have_dex_pc_listeners_(false),
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +020066 have_field_read_listeners_(false), have_field_write_listeners_(false),
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070067 have_exception_caught_listeners_(false),
68 deoptimized_methods_lock_("deoptimized methods lock"),
69 deoptimization_enabled_(false),
70 interpreter_handler_table_(kMainHandlerTable),
71 quick_alloc_entry_points_instrumentation_counter_(0) {
72}
73
Ian Rogers62d6c772013-02-27 08:32:07 -080074bool Instrumentation::InstallStubsForClass(mirror::Class* klass) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +010075 for (size_t i = 0, e = klass->NumDirectMethods(); i < e; i++) {
76 InstallStubsForMethod(klass->GetDirectMethod(i));
jeffhao725a9572012-11-13 18:20:12 -080077 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +010078 for (size_t i = 0, e = klass->NumVirtualMethods(); i < e; i++) {
79 InstallStubsForMethod(klass->GetVirtualMethod(i));
jeffhao725a9572012-11-13 18:20:12 -080080 }
81 return true;
82}
83
Ian Rogersef7d42f2014-01-06 12:55:46 -080084static void UpdateEntrypoints(mirror::ArtMethod* method, const void* quick_code,
85 const void* portable_code, bool have_portable_code)
86 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
87 method->SetEntryPointFromPortableCompiledCode(portable_code);
88 method->SetEntryPointFromQuickCompiledCode(quick_code);
89 bool portable_enabled = method->IsPortableCompiled();
90 if (have_portable_code && !portable_enabled) {
91 method->SetIsPortableCompiled();
92 } else if (portable_enabled) {
93 method->ClearIsPortableCompiled();
94 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +010095 if (!method->IsResolutionMethod()) {
Hiroshi Yamauchi563b47c2014-02-28 17:18:37 -080096 if (quick_code == GetQuickToInterpreterBridge() ||
Vladimir Marko8a630572014-04-09 18:45:35 +010097 quick_code == GetQuickToInterpreterBridgeTrampoline(Runtime::Current()->GetClassLinker()) ||
Hiroshi Yamauchi563b47c2014-02-28 17:18:37 -080098 (quick_code == GetQuickResolutionTrampoline(Runtime::Current()->GetClassLinker()) &&
99 Runtime::Current()->GetInstrumentation()->IsForcedInterpretOnly()
100 && !method->IsNative() && !method->IsProxyMethod())) {
101 if (kIsDebugBuild) {
102 if (quick_code == GetQuickToInterpreterBridge()) {
103 DCHECK(portable_code == GetPortableToInterpreterBridge());
104 } else if (quick_code == GetQuickResolutionTrampoline(Runtime::Current()->GetClassLinker())) {
105 DCHECK(portable_code == GetPortableResolutionTrampoline(Runtime::Current()->GetClassLinker()));
106 }
107 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800108 DCHECK(!method->IsNative()) << PrettyMethod(method);
Hiroshi Yamauchi563b47c2014-02-28 17:18:37 -0800109 DCHECK(!method->IsProxyMethod()) << PrettyMethod(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100110 method->SetEntryPointFromInterpreter(art::interpreter::artInterpreterToInterpreterBridge);
111 } else {
112 method->SetEntryPointFromInterpreter(art::artInterpreterToCompiledCodeBridge);
113 }
114 }
115}
116
117void Instrumentation::InstallStubsForMethod(mirror::ArtMethod* method) {
118 if (method->IsAbstract() || method->IsProxyMethod()) {
119 // Do not change stubs for these methods.
120 return;
121 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800122 const void* new_portable_code;
123 const void* new_quick_code;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100124 bool uninstall = !entry_exit_stubs_installed_ && !interpreter_stubs_installed_;
125 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
126 bool is_class_initialized = method->GetDeclaringClass()->IsInitialized();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800127 bool have_portable_code = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100128 if (uninstall) {
129 if ((forced_interpret_only_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800130 new_portable_code = GetPortableToInterpreterBridge();
131 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100132 } else if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800133 new_portable_code = class_linker->GetPortableOatCodeFor(method, &have_portable_code);
134 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100135 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800136 new_portable_code = GetPortableResolutionTrampoline(class_linker);
137 new_quick_code = GetQuickResolutionTrampoline(class_linker);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100138 }
139 } else { // !uninstall
140 if ((interpreter_stubs_installed_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800141 new_portable_code = GetPortableToInterpreterBridge();
142 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100143 } else {
144 // Do not overwrite resolution trampoline. When the trampoline initializes the method's
145 // class, all its static methods code will be set to the instrumentation entry point.
146 // For more details, see ClassLinker::FixupStaticTrampolines.
147 if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
148 // Do not overwrite interpreter to prevent from posting method entry/exit events twice.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800149 new_portable_code = class_linker->GetPortableOatCodeFor(method, &have_portable_code);
150 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Vladimir Marko8a630572014-04-09 18:45:35 +0100151 DCHECK(new_quick_code != GetQuickToInterpreterBridgeTrampoline(class_linker));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800152 if (entry_exit_stubs_installed_ && new_quick_code != GetQuickToInterpreterBridge()) {
153 DCHECK(new_portable_code != GetPortableToInterpreterBridge());
154 new_portable_code = GetPortableToInterpreterBridge();
155 new_quick_code = GetQuickInstrumentationEntryPoint();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100156 }
157 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800158 new_portable_code = GetPortableResolutionTrampoline(class_linker);
159 new_quick_code = GetQuickResolutionTrampoline(class_linker);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100160 }
161 }
162 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800163 UpdateEntrypoints(method, new_quick_code, new_portable_code, have_portable_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)
Ian Rogers306057f2012-11-26 12:45:53 -0800171 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
172 struct InstallStackVisitor : public StackVisitor {
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100173 InstallStackVisitor(Thread* thread, Context* context, uintptr_t instrumentation_exit_pc)
Ian Rogers62d6c772013-02-27 08:32:07 -0800174 : StackVisitor(thread, context), instrumentation_stack_(thread->GetInstrumentationStack()),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100175 existing_instrumentation_frames_count_(instrumentation_stack_->size()),
176 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
Ian Rogers306057f2012-11-26 12:45:53 -0800181 virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700182 mirror::ArtMethod* m = GetMethod();
Ian Rogers306057f2012-11-26 12:45:53 -0800183 if (m == NULL) {
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 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800190 if (m->IsRuntimeMethod()) {
191 if (kVerboseInstrumentation) {
192 LOG(INFO) << " Skipping runtime method. Frame " << GetFrameId();
193 }
194 last_return_pc_ = GetReturnPc();
Ian Rogers306057f2012-11-26 12:45:53 -0800195 return true; // Ignore unresolved methods since they will be instrumented after resolution.
196 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800197 if (kVerboseInstrumentation) {
198 LOG(INFO) << " Installing exit stub in " << DescribeLocation();
199 }
Jeff Haoa15a81b2014-05-27 18:25:47 -0700200 if (GetCurrentQuickFrame() == NULL) {
201 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, 0, GetFrameId(), false);
202 if (kVerboseInstrumentation) {
203 LOG(INFO) << "Pushing shadow frame " << instrumentation_frame.Dump();
204 }
205 shadow_stack_.push_back(instrumentation_frame);
206 return true; // Continue.
207 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800208 uintptr_t return_pc = GetReturnPc();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100209 if (return_pc == instrumentation_exit_pc_) {
210 // We've reached a frame which has already been installed with instrumentation exit stub.
211 // We should have already installed instrumentation on previous frames.
212 reached_existing_instrumentation_frames_ = true;
213
214 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
215 const InstrumentationStackFrame& frame = instrumentation_stack_->at(instrumentation_stack_depth_);
216 CHECK_EQ(m, frame.method_) << "Expected " << PrettyMethod(m)
217 << ", Found " << PrettyMethod(frame.method_);
218 return_pc = frame.return_pc_;
219 if (kVerboseInstrumentation) {
220 LOG(INFO) << "Ignoring already instrumented " << frame.Dump();
221 }
222 } else {
223 CHECK_NE(return_pc, 0U);
224 CHECK(!reached_existing_instrumentation_frames_);
225 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, return_pc, GetFrameId(),
226 false);
227 if (kVerboseInstrumentation) {
228 LOG(INFO) << "Pushing frame " << instrumentation_frame.Dump();
229 }
230
231 // Insert frame before old ones so we do not corrupt the instrumentation stack.
232 auto it = instrumentation_stack_->end() - existing_instrumentation_frames_count_;
233 instrumentation_stack_->insert(it, instrumentation_frame);
234 SetReturnPc(instrumentation_exit_pc_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800235 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800236 dex_pcs_.push_back(m->ToDexPc(last_return_pc_));
Ian Rogers62d6c772013-02-27 08:32:07 -0800237 last_return_pc_ = return_pc;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100238 ++instrumentation_stack_depth_;
Ian Rogers306057f2012-11-26 12:45:53 -0800239 return true; // Continue.
240 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800241 std::deque<InstrumentationStackFrame>* const instrumentation_stack_;
Jeff Haoa15a81b2014-05-27 18:25:47 -0700242 std::vector<InstrumentationStackFrame> shadow_stack_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100243 const size_t existing_instrumentation_frames_count_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800244 std::vector<uint32_t> dex_pcs_;
Ian Rogers306057f2012-11-26 12:45:53 -0800245 const uintptr_t instrumentation_exit_pc_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100246 bool reached_existing_instrumentation_frames_;
247 size_t instrumentation_stack_depth_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800248 uintptr_t last_return_pc_;
Ian Rogers306057f2012-11-26 12:45:53 -0800249 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800250 if (kVerboseInstrumentation) {
251 std::string thread_name;
252 thread->GetThreadName(thread_name);
253 LOG(INFO) << "Installing exit stubs in " << thread_name;
Ian Rogers306057f2012-11-26 12:45:53 -0800254 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100255
256 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers700a4022014-05-19 16:49:03 -0700257 std::unique_ptr<Context> context(Context::Create());
Ian Rogers848871b2013-08-05 10:56:33 -0700258 uintptr_t instrumentation_exit_pc = GetQuickInstrumentationExitPc();
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100259 InstallStackVisitor visitor(thread, context.get(), instrumentation_exit_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800260 visitor.WalkStack(true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100261 CHECK_EQ(visitor.dex_pcs_.size(), thread->GetInstrumentationStack()->size());
Ian Rogers62d6c772013-02-27 08:32:07 -0800262
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100263 if (instrumentation->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100264 // Create method enter events for all methods currently on the thread's stack. We only do this
265 // if no debugger is attached to prevent from posting events twice.
Jeff Haoa15a81b2014-05-27 18:25:47 -0700266 auto ssi = visitor.shadow_stack_.rbegin();
267 for (auto isi = thread->GetInstrumentationStack()->rbegin(),
268 end = thread->GetInstrumentationStack()->rend(); isi != end; ++isi) {
269 while (ssi != visitor.shadow_stack_.rend() && (*ssi).frame_id_ < (*isi).frame_id_) {
270 instrumentation->MethodEnterEvent(thread, (*ssi).this_object_, (*ssi).method_, 0);
271 ++ssi;
272 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100273 uint32_t dex_pc = visitor.dex_pcs_.back();
274 visitor.dex_pcs_.pop_back();
Jeff Haoa15a81b2014-05-27 18:25:47 -0700275 instrumentation->MethodEnterEvent(thread, (*isi).this_object_, (*isi).method_, dex_pc);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100276 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800277 }
278 thread->VerifyStack();
Ian Rogers306057f2012-11-26 12:45:53 -0800279}
280
Ian Rogers62d6c772013-02-27 08:32:07 -0800281// Removes the instrumentation exit pc as the return PC for every quick frame.
282static void InstrumentationRestoreStack(Thread* thread, void* arg)
Ian Rogers306057f2012-11-26 12:45:53 -0800283 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
284 struct RestoreStackVisitor : public StackVisitor {
Ian Rogers62d6c772013-02-27 08:32:07 -0800285 RestoreStackVisitor(Thread* thread, uintptr_t instrumentation_exit_pc,
286 Instrumentation* instrumentation)
287 : StackVisitor(thread, NULL), thread_(thread),
288 instrumentation_exit_pc_(instrumentation_exit_pc),
289 instrumentation_(instrumentation),
290 instrumentation_stack_(thread->GetInstrumentationStack()),
291 frames_removed_(0) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800292
293 virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800294 if (instrumentation_stack_->size() == 0) {
jeffhao725a9572012-11-13 18:20:12 -0800295 return false; // Stop.
296 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700297 mirror::ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800298 if (GetCurrentQuickFrame() == NULL) {
299 if (kVerboseInstrumentation) {
300 LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId() << " Method=" << PrettyMethod(m);
301 }
302 return true; // Ignore shadow frames.
303 }
Ian Rogers306057f2012-11-26 12:45:53 -0800304 if (m == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800305 if (kVerboseInstrumentation) {
306 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
307 }
Ian Rogers306057f2012-11-26 12:45:53 -0800308 return true; // Ignore upcalls.
309 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800310 bool removed_stub = false;
311 // TODO: make this search more efficient?
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100312 const size_t frameId = GetFrameId();
313 for (const InstrumentationStackFrame& instrumentation_frame : *instrumentation_stack_) {
314 if (instrumentation_frame.frame_id_ == frameId) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800315 if (kVerboseInstrumentation) {
316 LOG(INFO) << " Removing exit stub in " << DescribeLocation();
317 }
Jeff Hao9a916d32013-06-27 18:45:37 -0700318 if (instrumentation_frame.interpreter_entry_) {
319 CHECK(m == Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
320 } else {
321 CHECK(m == instrumentation_frame.method_) << PrettyMethod(m);
322 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800323 SetReturnPc(instrumentation_frame.return_pc_);
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100324 if (instrumentation_->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100325 // Create the method exit events. As the methods didn't really exit the result is 0.
326 // We only do this if no debugger is attached to prevent from posting events twice.
327 instrumentation_->MethodExitEvent(thread_, instrumentation_frame.this_object_, m,
328 GetDexPc(), JValue());
329 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800330 frames_removed_++;
331 removed_stub = true;
332 break;
333 }
334 }
335 if (!removed_stub) {
336 if (kVerboseInstrumentation) {
337 LOG(INFO) << " No exit stub in " << DescribeLocation();
Ian Rogers306057f2012-11-26 12:45:53 -0800338 }
jeffhao725a9572012-11-13 18:20:12 -0800339 }
340 return true; // Continue.
341 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800342 Thread* const thread_;
Ian Rogers306057f2012-11-26 12:45:53 -0800343 const uintptr_t instrumentation_exit_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800344 Instrumentation* const instrumentation_;
345 std::deque<instrumentation::InstrumentationStackFrame>* const instrumentation_stack_;
346 size_t frames_removed_;
jeffhao725a9572012-11-13 18:20:12 -0800347 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800348 if (kVerboseInstrumentation) {
349 std::string thread_name;
350 thread->GetThreadName(thread_name);
351 LOG(INFO) << "Removing exit stubs in " << thread_name;
352 }
353 std::deque<instrumentation::InstrumentationStackFrame>* stack = thread->GetInstrumentationStack();
354 if (stack->size() > 0) {
355 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers848871b2013-08-05 10:56:33 -0700356 uintptr_t instrumentation_exit_pc = GetQuickInstrumentationExitPc();
Ian Rogers62d6c772013-02-27 08:32:07 -0800357 RestoreStackVisitor visitor(thread, instrumentation_exit_pc, instrumentation);
358 visitor.WalkStack(true);
359 CHECK_EQ(visitor.frames_removed_, stack->size());
360 while (stack->size() > 0) {
361 stack->pop_front();
362 }
jeffhao725a9572012-11-13 18:20:12 -0800363 }
364}
365
Ian Rogers62d6c772013-02-27 08:32:07 -0800366void Instrumentation::AddListener(InstrumentationListener* listener, uint32_t events) {
367 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Ian Rogers62d6c772013-02-27 08:32:07 -0800368 if ((events & kMethodEntered) != 0) {
369 method_entry_listeners_.push_back(listener);
Ian Rogers62d6c772013-02-27 08:32:07 -0800370 have_method_entry_listeners_ = true;
371 }
372 if ((events & kMethodExited) != 0) {
373 method_exit_listeners_.push_back(listener);
Ian Rogers62d6c772013-02-27 08:32:07 -0800374 have_method_exit_listeners_ = true;
375 }
376 if ((events & kMethodUnwind) != 0) {
377 method_unwind_listeners_.push_back(listener);
378 have_method_unwind_listeners_ = true;
379 }
380 if ((events & kDexPcMoved) != 0) {
381 dex_pc_listeners_.push_back(listener);
Ian Rogers62d6c772013-02-27 08:32:07 -0800382 have_dex_pc_listeners_ = true;
383 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200384 if ((events & kFieldRead) != 0) {
385 field_read_listeners_.push_back(listener);
386 have_field_read_listeners_ = true;
387 }
388 if ((events & kFieldWritten) != 0) {
389 field_write_listeners_.push_back(listener);
390 have_field_write_listeners_ = true;
391 }
Jeff Hao14dd5a82013-04-11 10:23:36 -0700392 if ((events & kExceptionCaught) != 0) {
393 exception_caught_listeners_.push_back(listener);
394 have_exception_caught_listeners_ = true;
395 }
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200396 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800397}
398
Ian Rogers62d6c772013-02-27 08:32:07 -0800399void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) {
400 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Ian Rogers62d6c772013-02-27 08:32:07 -0800401
402 if ((events & kMethodEntered) != 0) {
403 bool contains = std::find(method_entry_listeners_.begin(), method_entry_listeners_.end(),
404 listener) != method_entry_listeners_.end();
405 if (contains) {
406 method_entry_listeners_.remove(listener);
407 }
408 have_method_entry_listeners_ = method_entry_listeners_.size() > 0;
Ian Rogers62d6c772013-02-27 08:32:07 -0800409 }
410 if ((events & kMethodExited) != 0) {
411 bool contains = std::find(method_exit_listeners_.begin(), method_exit_listeners_.end(),
412 listener) != method_exit_listeners_.end();
413 if (contains) {
414 method_exit_listeners_.remove(listener);
415 }
416 have_method_exit_listeners_ = method_exit_listeners_.size() > 0;
Ian Rogers62d6c772013-02-27 08:32:07 -0800417 }
418 if ((events & kMethodUnwind) != 0) {
419 method_unwind_listeners_.remove(listener);
420 }
421 if ((events & kDexPcMoved) != 0) {
422 bool contains = std::find(dex_pc_listeners_.begin(), dex_pc_listeners_.end(),
423 listener) != dex_pc_listeners_.end();
424 if (contains) {
425 dex_pc_listeners_.remove(listener);
426 }
427 have_dex_pc_listeners_ = dex_pc_listeners_.size() > 0;
Ian Rogers62d6c772013-02-27 08:32:07 -0800428 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200429 if ((events & kFieldRead) != 0) {
430 bool contains = std::find(field_read_listeners_.begin(), field_read_listeners_.end(),
431 listener) != field_read_listeners_.end();
432 if (contains) {
433 field_read_listeners_.remove(listener);
434 }
435 have_field_read_listeners_ = field_read_listeners_.size() > 0;
436 }
437 if ((events & kFieldWritten) != 0) {
438 bool contains = std::find(field_write_listeners_.begin(), field_write_listeners_.end(),
439 listener) != field_write_listeners_.end();
440 if (contains) {
441 field_write_listeners_.remove(listener);
442 }
443 have_field_write_listeners_ = field_write_listeners_.size() > 0;
444 }
Jeff Hao14dd5a82013-04-11 10:23:36 -0700445 if ((events & kExceptionCaught) != 0) {
446 exception_caught_listeners_.remove(listener);
447 have_exception_caught_listeners_ = exception_caught_listeners_.size() > 0;
448 }
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200449 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800450}
451
Ian Rogers62d6c772013-02-27 08:32:07 -0800452void Instrumentation::ConfigureStubs(bool require_entry_exit_stubs, bool require_interpreter) {
453 interpret_only_ = require_interpreter || forced_interpret_only_;
454 // Compute what level of instrumentation is required and compare to current.
455 int desired_level, current_level;
456 if (require_interpreter) {
457 desired_level = 2;
458 } else if (require_entry_exit_stubs) {
459 desired_level = 1;
460 } else {
461 desired_level = 0;
462 }
463 if (interpreter_stubs_installed_) {
464 current_level = 2;
465 } else if (entry_exit_stubs_installed_) {
466 current_level = 1;
467 } else {
468 current_level = 0;
469 }
470 if (desired_level == current_level) {
471 // We're already set.
472 return;
473 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100474 Thread* const self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -0800475 Runtime* runtime = Runtime::Current();
476 Locks::thread_list_lock_->AssertNotHeld(self);
477 if (desired_level > 0) {
478 if (require_interpreter) {
479 interpreter_stubs_installed_ = true;
480 } else {
481 CHECK(require_entry_exit_stubs);
482 entry_exit_stubs_installed_ = true;
483 }
484 runtime->GetClassLinker()->VisitClasses(InstallStubsClassVisitor, this);
485 instrumentation_stubs_installed_ = true;
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100486 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800487 runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this);
488 } else {
489 interpreter_stubs_installed_ = false;
490 entry_exit_stubs_installed_ = false;
491 runtime->GetClassLinker()->VisitClasses(InstallStubsClassVisitor, this);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100492 // Restore stack only if there is no method currently deoptimized.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700493 bool empty;
494 {
495 ReaderMutexLock mu(self, deoptimized_methods_lock_);
496 empty = deoptimized_methods_.empty(); // Avoid lock violation.
497 }
498 if (empty) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100499 instrumentation_stubs_installed_ = false;
500 MutexLock mu(self, *Locks::thread_list_lock_);
501 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
502 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800503 }
jeffhao725a9572012-11-13 18:20:12 -0800504}
505
Ian Rogersfa824272013-11-05 16:12:57 -0800506static void ResetQuickAllocEntryPointsForThread(Thread* thread, void* arg) {
507 thread->ResetQuickAllocEntryPointsForThread();
508}
509
Mathieu Chartier661974a2014-01-09 11:23:53 -0800510void Instrumentation::SetEntrypointsInstrumented(bool instrumented) {
511 Runtime* runtime = Runtime::Current();
512 ThreadList* tl = runtime->GetThreadList();
513 if (runtime->IsStarted()) {
514 tl->SuspendAll();
515 }
516 {
517 MutexLock mu(Thread::Current(), *Locks::runtime_shutdown_lock_);
518 SetQuickAllocEntryPointsInstrumented(instrumented);
519 ResetQuickAllocEntryPoints();
520 }
521 if (runtime->IsStarted()) {
522 tl->ResumeAll();
523 }
524}
525
Ian Rogersfa824272013-11-05 16:12:57 -0800526void Instrumentation::InstrumentQuickAllocEntryPoints() {
527 // TODO: the read of quick_alloc_entry_points_instrumentation_counter_ is racey and this code
528 // should be guarded by a lock.
Ian Rogers3e5cf302014-05-20 16:40:37 -0700529 DCHECK_GE(quick_alloc_entry_points_instrumentation_counter_.LoadSequentiallyConsistent(), 0);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800530 const bool enable_instrumentation =
Ian Rogers3e5cf302014-05-20 16:40:37 -0700531 quick_alloc_entry_points_instrumentation_counter_.FetchAndAddSequentiallyConsistent(1) == 0;
Ian Rogersfa824272013-11-05 16:12:57 -0800532 if (enable_instrumentation) {
Mathieu Chartier661974a2014-01-09 11:23:53 -0800533 SetEntrypointsInstrumented(true);
Ian Rogersfa824272013-11-05 16:12:57 -0800534 }
535}
536
537void Instrumentation::UninstrumentQuickAllocEntryPoints() {
538 // TODO: the read of quick_alloc_entry_points_instrumentation_counter_ is racey and this code
539 // should be guarded by a lock.
Ian Rogers3e5cf302014-05-20 16:40:37 -0700540 DCHECK_GT(quick_alloc_entry_points_instrumentation_counter_.LoadSequentiallyConsistent(), 0);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800541 const bool disable_instrumentation =
Ian Rogers3e5cf302014-05-20 16:40:37 -0700542 quick_alloc_entry_points_instrumentation_counter_.FetchAndSubSequentiallyConsistent(1) == 1;
Ian Rogersfa824272013-11-05 16:12:57 -0800543 if (disable_instrumentation) {
Mathieu Chartier661974a2014-01-09 11:23:53 -0800544 SetEntrypointsInstrumented(false);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800545 }
546}
547
548void Instrumentation::ResetQuickAllocEntryPoints() {
549 Runtime* runtime = Runtime::Current();
550 if (runtime->IsStarted()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800551 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
552 runtime->GetThreadList()->ForEach(ResetQuickAllocEntryPointsForThread, NULL);
Ian Rogersfa824272013-11-05 16:12:57 -0800553 }
554}
555
Ian Rogersef7d42f2014-01-06 12:55:46 -0800556void Instrumentation::UpdateMethodsCode(mirror::ArtMethod* method, const void* quick_code,
557 const void* portable_code, bool have_portable_code) const {
558 const void* new_portable_code;
559 const void* new_quick_code;
560 bool new_have_portable_code;
Ian Rogers62d6c772013-02-27 08:32:07 -0800561 if (LIKELY(!instrumentation_stubs_installed_)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800562 new_portable_code = portable_code;
563 new_quick_code = quick_code;
564 new_have_portable_code = have_portable_code;
Jeff Hao65d15d92013-07-16 16:39:33 -0700565 } else {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100566 if ((interpreter_stubs_installed_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800567 new_portable_code = GetPortableToInterpreterBridge();
568 new_quick_code = GetQuickToInterpreterBridge();
569 new_have_portable_code = false;
570 } else if (quick_code == GetQuickResolutionTrampoline(Runtime::Current()->GetClassLinker()) ||
Vladimir Marko8a630572014-04-09 18:45:35 +0100571 quick_code == GetQuickToInterpreterBridgeTrampoline(Runtime::Current()->GetClassLinker()) ||
572 quick_code == GetQuickToInterpreterBridge()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800573 DCHECK((portable_code == GetPortableResolutionTrampoline(Runtime::Current()->GetClassLinker())) ||
574 (portable_code == GetPortableToInterpreterBridge()));
575 new_portable_code = portable_code;
576 new_quick_code = quick_code;
577 new_have_portable_code = have_portable_code;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100578 } else if (entry_exit_stubs_installed_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800579 new_quick_code = GetQuickInstrumentationEntryPoint();
580 new_portable_code = GetPortableToInterpreterBridge();
581 new_have_portable_code = false;
Jeff Hao65d15d92013-07-16 16:39:33 -0700582 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800583 new_portable_code = portable_code;
584 new_quick_code = quick_code;
585 new_have_portable_code = have_portable_code;
Jeff Hao65d15d92013-07-16 16:39:33 -0700586 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800587 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800588 UpdateEntrypoints(method, new_quick_code, new_portable_code, new_have_portable_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100589}
590
591void Instrumentation::Deoptimize(mirror::ArtMethod* method) {
592 CHECK(!method->IsNative());
593 CHECK(!method->IsProxyMethod());
594 CHECK(!method->IsAbstract());
595
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700596 Thread* self = Thread::Current();
597 std::pair<std::set<mirror::ArtMethod*>::iterator, bool> pair;
598 {
599 WriterMutexLock mu(self, deoptimized_methods_lock_);
600 pair = deoptimized_methods_.insert(method);
601 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100602 bool already_deoptimized = !pair.second;
603 CHECK(!already_deoptimized) << "Method " << PrettyMethod(method) << " is already deoptimized";
604
605 if (!interpreter_stubs_installed_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800606 UpdateEntrypoints(method, GetQuickToInterpreterBridge(), GetPortableToInterpreterBridge(),
607 false);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100608
609 // Install instrumentation exit stub and instrumentation frames. We may already have installed
610 // these previously so it will only cover the newly created frames.
611 instrumentation_stubs_installed_ = true;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700612 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100613 Runtime::Current()->GetThreadList()->ForEach(InstrumentationInstallStack, this);
614 }
615}
616
617void Instrumentation::Undeoptimize(mirror::ArtMethod* method) {
618 CHECK(!method->IsNative());
619 CHECK(!method->IsProxyMethod());
620 CHECK(!method->IsAbstract());
621
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700622 Thread* self = Thread::Current();
623 bool empty;
624 {
625 WriterMutexLock mu(self, deoptimized_methods_lock_);
626 auto it = deoptimized_methods_.find(method);
627 CHECK(it != deoptimized_methods_.end()) << "Method " << PrettyMethod(method)
628 << " is not deoptimized";
629 deoptimized_methods_.erase(it);
630 empty = deoptimized_methods_.empty();
631 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100632
633 // Restore code and possibly stack only if we did not deoptimize everything.
634 if (!interpreter_stubs_installed_) {
635 // Restore its code or resolution trampoline.
636 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800637 if (method->IsStatic() && !method->IsConstructor() &&
638 !method->GetDeclaringClass()->IsInitialized()) {
639 UpdateEntrypoints(method, GetQuickResolutionTrampoline(class_linker),
640 GetPortableResolutionTrampoline(class_linker), false);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100641 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800642 bool have_portable_code = false;
643 const void* quick_code = class_linker->GetQuickOatCodeFor(method);
644 const void* portable_code = class_linker->GetPortableOatCodeFor(method, &have_portable_code);
645 UpdateEntrypoints(method, quick_code, portable_code, have_portable_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100646 }
647
648 // If there is no deoptimized method left, we can restore the stack of each thread.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700649 if (empty) {
650 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100651 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
652 instrumentation_stubs_installed_ = false;
653 }
654 }
655}
656
657bool Instrumentation::IsDeoptimized(mirror::ArtMethod* method) const {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700658 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100659 DCHECK(method != nullptr);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700660 return deoptimized_methods_.find(method) != deoptimized_methods_.end();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100661}
662
663void Instrumentation::EnableDeoptimization() {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700664 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100665 CHECK(deoptimized_methods_.empty());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100666 CHECK_EQ(deoptimization_enabled_, false);
667 deoptimization_enabled_ = true;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100668}
669
670void Instrumentation::DisableDeoptimization() {
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100671 CHECK_EQ(deoptimization_enabled_, true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100672 // If we deoptimized everything, undo it.
673 if (interpreter_stubs_installed_) {
674 UndeoptimizeEverything();
675 }
676 // Undeoptimized selected methods.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700677 while (true) {
678 mirror::ArtMethod* method;
679 {
680 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
681 if (deoptimized_methods_.empty()) {
682 break;
683 }
684 method = *deoptimized_methods_.begin();
685 }
686 Undeoptimize(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100687 }
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100688 deoptimization_enabled_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100689}
690
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100691// Indicates if instrumentation should notify method enter/exit events to the listeners.
692bool Instrumentation::ShouldNotifyMethodEnterExitEvents() const {
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100693 return !deoptimization_enabled_ && !interpreter_stubs_installed_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100694}
695
696void Instrumentation::DeoptimizeEverything() {
697 CHECK(!interpreter_stubs_installed_);
698 ConfigureStubs(false, true);
699}
700
701void Instrumentation::UndeoptimizeEverything() {
702 CHECK(interpreter_stubs_installed_);
703 ConfigureStubs(false, false);
704}
705
706void Instrumentation::EnableMethodTracing() {
707 bool require_interpreter = kDeoptimizeForAccurateMethodEntryExitListeners;
708 ConfigureStubs(!require_interpreter, require_interpreter);
709}
710
711void Instrumentation::DisableMethodTracing() {
712 ConfigureStubs(false, false);
jeffhao725a9572012-11-13 18:20:12 -0800713}
714
Ian Rogersef7d42f2014-01-06 12:55:46 -0800715const void* Instrumentation::GetQuickCodeFor(mirror::ArtMethod* method) const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800716 Runtime* runtime = Runtime::Current();
717 if (LIKELY(!instrumentation_stubs_installed_)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800718 const void* code = method->GetEntryPointFromQuickCompiledCode();
Vladimir Marko8a630572014-04-09 18:45:35 +0100719 DCHECK(code != nullptr);
720 if (LIKELY(code != GetQuickResolutionTrampoline(runtime->GetClassLinker())) &&
721 LIKELY(code != GetQuickToInterpreterBridgeTrampoline(runtime->GetClassLinker())) &&
722 LIKELY(code != GetQuickToInterpreterBridge())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800723 return code;
724 }
725 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800726 return runtime->GetClassLinker()->GetQuickOatCodeFor(method);
jeffhao725a9572012-11-13 18:20:12 -0800727}
728
Ian Rogers62d6c772013-02-27 08:32:07 -0800729void Instrumentation::MethodEnterEventImpl(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800730 mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800731 uint32_t dex_pc) const {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700732 auto it = method_entry_listeners_.begin();
Jeff Hao65d15d92013-07-16 16:39:33 -0700733 bool is_end = (it == method_entry_listeners_.end());
734 // Implemented this way to prevent problems caused by modification of the list while iterating.
735 while (!is_end) {
736 InstrumentationListener* cur = *it;
737 ++it;
738 is_end = (it == method_entry_listeners_.end());
739 cur->MethodEntered(thread, this_object, method, dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800740 }
741}
742
743void Instrumentation::MethodExitEventImpl(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800744 mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800745 uint32_t dex_pc, const JValue& return_value) const {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700746 auto it = method_exit_listeners_.begin();
Jeff Hao65d15d92013-07-16 16:39:33 -0700747 bool is_end = (it == method_exit_listeners_.end());
748 // Implemented this way to prevent problems caused by modification of the list while iterating.
749 while (!is_end) {
750 InstrumentationListener* cur = *it;
751 ++it;
752 is_end = (it == method_exit_listeners_.end());
753 cur->MethodExited(thread, this_object, method, dex_pc, return_value);
Ian Rogers62d6c772013-02-27 08:32:07 -0800754 }
755}
756
757void Instrumentation::MethodUnwindEvent(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800758 mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800759 uint32_t dex_pc) const {
760 if (have_method_unwind_listeners_) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700761 for (InstrumentationListener* listener : method_unwind_listeners_) {
Sebastien Hertz51db44a2013-11-19 10:00:29 +0100762 listener->MethodUnwind(thread, this_object, method, dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800763 }
764 }
765}
766
767void Instrumentation::DexPcMovedEventImpl(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800768 mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800769 uint32_t dex_pc) const {
770 // TODO: STL copy-on-write collection? The copy below is due to the debug listener having an
771 // action where it can remove itself as a listener and break the iterator. The copy only works
772 // around the problem and in general we may have to move to something like reference counting to
773 // ensure listeners are deleted correctly.
774 std::list<InstrumentationListener*> copy(dex_pc_listeners_);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700775 for (InstrumentationListener* listener : copy) {
776 listener->DexPcMoved(thread, this_object, method, dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800777 }
778}
779
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200780void Instrumentation::FieldReadEventImpl(Thread* thread, mirror::Object* this_object,
781 mirror::ArtMethod* method, uint32_t dex_pc,
782 mirror::ArtField* field) const {
783 if (have_field_read_listeners_) {
784 // TODO: same comment than DexPcMovedEventImpl.
785 std::list<InstrumentationListener*> copy(field_read_listeners_);
786 for (InstrumentationListener* listener : copy) {
787 listener->FieldRead(thread, this_object, method, dex_pc, field);
788 }
789 }
790}
791
792void Instrumentation::FieldWriteEventImpl(Thread* thread, mirror::Object* this_object,
793 mirror::ArtMethod* method, uint32_t dex_pc,
794 mirror::ArtField* field, const JValue& field_value) const {
795 if (have_field_write_listeners_) {
796 // TODO: same comment than DexPcMovedEventImpl.
797 std::list<InstrumentationListener*> copy(field_write_listeners_);
798 for (InstrumentationListener* listener : copy) {
799 listener->FieldWritten(thread, this_object, method, dex_pc, field, field_value);
800 }
801 }
802}
803
Ian Rogers62d6c772013-02-27 08:32:07 -0800804void Instrumentation::ExceptionCaughtEvent(Thread* thread, const ThrowLocation& throw_location,
Brian Carlstromea46f952013-07-30 01:26:50 -0700805 mirror::ArtMethod* catch_method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800806 uint32_t catch_dex_pc,
Sebastien Hertz947ff082013-09-17 14:10:13 +0200807 mirror::Throwable* exception_object) const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800808 if (have_exception_caught_listeners_) {
Jeff Haoc0bd4da2013-04-11 15:52:28 -0700809 DCHECK_EQ(thread->GetException(NULL), exception_object);
810 thread->ClearException();
Sebastien Hertzbf079fe2014-04-01 15:31:05 +0200811 // TODO: The copy below is due to the debug listener having an action where it can remove
812 // itself as a listener and break the iterator. The copy only works around the problem.
813 std::list<InstrumentationListener*> copy(exception_caught_listeners_);
814 for (InstrumentationListener* listener : copy) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700815 listener->ExceptionCaught(thread, throw_location, catch_method, catch_dex_pc, exception_object);
Ian Rogers62d6c772013-02-27 08:32:07 -0800816 }
Jeff Haoc0bd4da2013-04-11 15:52:28 -0700817 thread->SetException(throw_location, exception_object);
Ian Rogers62d6c772013-02-27 08:32:07 -0800818 }
819}
820
821static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame,
822 int delta)
823 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
824 size_t frame_id = StackVisitor::ComputeNumFrames(self) + delta;
825 if (frame_id != instrumentation_frame.frame_id_) {
826 LOG(ERROR) << "Expected frame_id=" << frame_id << " but found "
827 << instrumentation_frame.frame_id_;
828 StackVisitor::DescribeStack(self);
829 CHECK_EQ(frame_id, instrumentation_frame.frame_id_);
830 }
831}
832
833void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700834 mirror::ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -0700835 uintptr_t lr, bool interpreter_entry) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800836 // We have a callee-save frame meaning this value is guaranteed to never be 0.
837 size_t frame_id = StackVisitor::ComputeNumFrames(self);
838 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
839 if (kVerboseInstrumentation) {
Brian Carlstrom2d888622013-07-18 17:02:00 -0700840 LOG(INFO) << "Entering " << PrettyMethod(method) << " from PC " << reinterpret_cast<void*>(lr);
Ian Rogers62d6c772013-02-27 08:32:07 -0800841 }
842 instrumentation::InstrumentationStackFrame instrumentation_frame(this_object, method, lr,
Jeff Hao9a916d32013-06-27 18:45:37 -0700843 frame_id, interpreter_entry);
Ian Rogers62d6c772013-02-27 08:32:07 -0800844 stack->push_front(instrumentation_frame);
845
846 MethodEnterEvent(self, this_object, method, 0);
847}
848
849uint64_t Instrumentation::PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc,
850 uint64_t gpr_result, uint64_t fpr_result) {
851 // Do the pop.
852 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
853 CHECK_GT(stack->size(), 0U);
854 InstrumentationStackFrame instrumentation_frame = stack->front();
855 stack->pop_front();
856
857 // Set return PC and check the sanity of the stack.
858 *return_pc = instrumentation_frame.return_pc_;
859 CheckStackDepth(self, instrumentation_frame, 0);
860
Brian Carlstromea46f952013-07-30 01:26:50 -0700861 mirror::ArtMethod* method = instrumentation_frame.method_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800862 char return_shorty = MethodHelper(method).GetShorty()[0];
863 JValue return_value;
864 if (return_shorty == 'V') {
865 return_value.SetJ(0);
866 } else if (return_shorty == 'F' || return_shorty == 'D') {
867 return_value.SetJ(fpr_result);
868 } else {
869 return_value.SetJ(gpr_result);
870 }
871 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
872 // return_pc.
873 uint32_t dex_pc = DexFile::kDexNoIndex;
874 mirror::Object* this_object = instrumentation_frame.this_object_;
875 MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value);
jeffhao725a9572012-11-13 18:20:12 -0800876
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100877 // Deoptimize if the caller needs to continue execution in the interpreter. Do nothing if we get
878 // back to an upcall.
879 NthCallerVisitor visitor(self, 1, true);
880 visitor.WalkStack(true);
881 bool deoptimize = (visitor.caller != NULL) &&
882 (interpreter_stubs_installed_ || IsDeoptimized(visitor.caller));
883 if (deoptimize && kVerboseInstrumentation) {
884 LOG(INFO) << "Deoptimizing into " << PrettyMethod(visitor.caller);
Ian Rogers62d6c772013-02-27 08:32:07 -0800885 }
886 if (deoptimize) {
887 if (kVerboseInstrumentation) {
888 LOG(INFO) << "Deoptimizing from " << PrettyMethod(method)
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100889 << " result is " << std::hex << return_value.GetJ();
Ian Rogers62d6c772013-02-27 08:32:07 -0800890 }
891 self->SetDeoptimizationReturnValue(return_value);
Ian Rogers848871b2013-08-05 10:56:33 -0700892 return static_cast<uint64_t>(GetQuickDeoptimizationEntryPoint()) |
Ian Rogers62d6c772013-02-27 08:32:07 -0800893 (static_cast<uint64_t>(*return_pc) << 32);
894 } else {
895 if (kVerboseInstrumentation) {
Brian Carlstrom2d888622013-07-18 17:02:00 -0700896 LOG(INFO) << "Returning from " << PrettyMethod(method)
897 << " to PC " << reinterpret_cast<void*>(*return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800898 }
899 return *return_pc;
900 }
jeffhao725a9572012-11-13 18:20:12 -0800901}
902
Ian Rogers62d6c772013-02-27 08:32:07 -0800903void Instrumentation::PopMethodForUnwind(Thread* self, bool is_deoptimization) const {
904 // Do the pop.
905 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
906 CHECK_GT(stack->size(), 0U);
907 InstrumentationStackFrame instrumentation_frame = stack->front();
908 // TODO: bring back CheckStackDepth(self, instrumentation_frame, 2);
909 stack->pop_front();
910
Brian Carlstromea46f952013-07-30 01:26:50 -0700911 mirror::ArtMethod* method = instrumentation_frame.method_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800912 if (is_deoptimization) {
913 if (kVerboseInstrumentation) {
914 LOG(INFO) << "Popping for deoptimization " << PrettyMethod(method);
915 }
916 } else {
917 if (kVerboseInstrumentation) {
918 LOG(INFO) << "Popping for unwind " << PrettyMethod(method);
919 }
920
921 // Notify listeners of method unwind.
922 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
923 // return_pc.
924 uint32_t dex_pc = DexFile::kDexNoIndex;
925 MethodUnwindEvent(self, instrumentation_frame.this_object_, method, dex_pc);
926 }
927}
928
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700929void Instrumentation::VisitRoots(RootCallback* callback, void* arg) {
930 WriterMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
931 if (deoptimized_methods_.empty()) {
932 return;
933 }
934 std::set<mirror::ArtMethod*> new_deoptimized_methods;
935 for (mirror::ArtMethod* method : deoptimized_methods_) {
936 DCHECK(method != nullptr);
937 callback(reinterpret_cast<mirror::Object**>(&method), arg, 0, kRootVMInternal);
938 new_deoptimized_methods.insert(method);
939 }
940 deoptimized_methods_ = new_deoptimized_methods;
941}
942
Ian Rogers62d6c772013-02-27 08:32:07 -0800943std::string InstrumentationStackFrame::Dump() const {
944 std::ostringstream os;
945 os << "Frame " << frame_id_ << " " << PrettyMethod(method_) << ":"
946 << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_);
947 return os.str();
948}
949
950} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -0800951} // namespace art