blob: f459b590eb3f22fec2349aff1a3cb7d3e64a7422 [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
Sebastien Hertzbae182c2013-12-17 10:42:03 +0100140 if ((interpreter_stubs_installed_ || forced_interpret_only_ || IsDeoptimized(method)) &&
141 !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800142 new_portable_code = GetPortableToInterpreterBridge();
143 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100144 } else {
145 // Do not overwrite resolution trampoline. When the trampoline initializes the method's
146 // class, all its static methods code will be set to the instrumentation entry point.
147 // For more details, see ClassLinker::FixupStaticTrampolines.
148 if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
149 // Do not overwrite interpreter to prevent from posting method entry/exit events twice.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800150 new_portable_code = class_linker->GetPortableOatCodeFor(method, &have_portable_code);
151 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Vladimir Marko8a630572014-04-09 18:45:35 +0100152 DCHECK(new_quick_code != GetQuickToInterpreterBridgeTrampoline(class_linker));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800153 if (entry_exit_stubs_installed_ && new_quick_code != GetQuickToInterpreterBridge()) {
Sebastien Hertzbae182c2013-12-17 10:42:03 +0100154 // TODO: portable to quick bridge. Bug: 8196384. We cannot enable the check below as long
155 // as GetPortableToQuickBridge() == GetPortableToInterpreterBridge().
156 // DCHECK(new_portable_code != GetPortableToInterpreterBridge());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800157 new_portable_code = GetPortableToInterpreterBridge();
158 new_quick_code = GetQuickInstrumentationEntryPoint();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100159 }
160 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800161 new_portable_code = GetPortableResolutionTrampoline(class_linker);
162 new_quick_code = GetQuickResolutionTrampoline(class_linker);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100163 }
164 }
165 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800166 UpdateEntrypoints(method, new_quick_code, new_portable_code, have_portable_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100167}
168
Ian Rogers62d6c772013-02-27 08:32:07 -0800169// Places the instrumentation exit pc as the return PC for every quick frame. This also allows
170// deoptimization of quick frames to interpreter frames.
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100171// Since we may already have done this previously, we need to push new instrumentation frame before
172// existing instrumentation frames.
Ian Rogers62d6c772013-02-27 08:32:07 -0800173static void InstrumentationInstallStack(Thread* thread, void* arg)
Ian Rogers306057f2012-11-26 12:45:53 -0800174 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
175 struct InstallStackVisitor : public StackVisitor {
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100176 InstallStackVisitor(Thread* thread, Context* context, uintptr_t instrumentation_exit_pc)
Ian Rogers62d6c772013-02-27 08:32:07 -0800177 : StackVisitor(thread, context), instrumentation_stack_(thread->GetInstrumentationStack()),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100178 existing_instrumentation_frames_count_(instrumentation_stack_->size()),
179 instrumentation_exit_pc_(instrumentation_exit_pc),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100180 reached_existing_instrumentation_frames_(false), instrumentation_stack_depth_(0),
181 last_return_pc_(0) {
182 }
jeffhao725a9572012-11-13 18:20:12 -0800183
Ian Rogers306057f2012-11-26 12:45:53 -0800184 virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700185 mirror::ArtMethod* m = GetMethod();
Ian Rogers306057f2012-11-26 12:45:53 -0800186 if (m == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800187 if (kVerboseInstrumentation) {
188 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
189 }
190 last_return_pc_ = 0;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700191 return true; // Ignore upcalls.
Ian Rogers306057f2012-11-26 12:45:53 -0800192 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800193 if (m->IsRuntimeMethod()) {
194 if (kVerboseInstrumentation) {
195 LOG(INFO) << " Skipping runtime method. Frame " << GetFrameId();
196 }
197 last_return_pc_ = GetReturnPc();
Ian Rogers306057f2012-11-26 12:45:53 -0800198 return true; // Ignore unresolved methods since they will be instrumented after resolution.
199 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800200 if (kVerboseInstrumentation) {
201 LOG(INFO) << " Installing exit stub in " << DescribeLocation();
202 }
Jeff Haoa15a81b2014-05-27 18:25:47 -0700203 if (GetCurrentQuickFrame() == NULL) {
204 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, 0, GetFrameId(), false);
205 if (kVerboseInstrumentation) {
206 LOG(INFO) << "Pushing shadow frame " << instrumentation_frame.Dump();
207 }
208 shadow_stack_.push_back(instrumentation_frame);
209 return true; // Continue.
210 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800211 uintptr_t return_pc = GetReturnPc();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100212 if (return_pc == instrumentation_exit_pc_) {
213 // We've reached a frame which has already been installed with instrumentation exit stub.
214 // We should have already installed instrumentation on previous frames.
215 reached_existing_instrumentation_frames_ = true;
216
217 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
218 const InstrumentationStackFrame& frame = instrumentation_stack_->at(instrumentation_stack_depth_);
219 CHECK_EQ(m, frame.method_) << "Expected " << PrettyMethod(m)
220 << ", Found " << PrettyMethod(frame.method_);
221 return_pc = frame.return_pc_;
222 if (kVerboseInstrumentation) {
223 LOG(INFO) << "Ignoring already instrumented " << frame.Dump();
224 }
225 } else {
226 CHECK_NE(return_pc, 0U);
227 CHECK(!reached_existing_instrumentation_frames_);
228 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, return_pc, GetFrameId(),
229 false);
230 if (kVerboseInstrumentation) {
231 LOG(INFO) << "Pushing frame " << instrumentation_frame.Dump();
232 }
233
234 // Insert frame before old ones so we do not corrupt the instrumentation stack.
235 auto it = instrumentation_stack_->end() - existing_instrumentation_frames_count_;
236 instrumentation_stack_->insert(it, instrumentation_frame);
237 SetReturnPc(instrumentation_exit_pc_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800238 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800239 dex_pcs_.push_back(m->ToDexPc(last_return_pc_));
Ian Rogers62d6c772013-02-27 08:32:07 -0800240 last_return_pc_ = return_pc;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100241 ++instrumentation_stack_depth_;
Ian Rogers306057f2012-11-26 12:45:53 -0800242 return true; // Continue.
243 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800244 std::deque<InstrumentationStackFrame>* const instrumentation_stack_;
Jeff Haoa15a81b2014-05-27 18:25:47 -0700245 std::vector<InstrumentationStackFrame> shadow_stack_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100246 const size_t existing_instrumentation_frames_count_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800247 std::vector<uint32_t> dex_pcs_;
Ian Rogers306057f2012-11-26 12:45:53 -0800248 const uintptr_t instrumentation_exit_pc_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100249 bool reached_existing_instrumentation_frames_;
250 size_t instrumentation_stack_depth_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800251 uintptr_t last_return_pc_;
Ian Rogers306057f2012-11-26 12:45:53 -0800252 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800253 if (kVerboseInstrumentation) {
254 std::string thread_name;
255 thread->GetThreadName(thread_name);
256 LOG(INFO) << "Installing exit stubs in " << thread_name;
Ian Rogers306057f2012-11-26 12:45:53 -0800257 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100258
259 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers700a4022014-05-19 16:49:03 -0700260 std::unique_ptr<Context> context(Context::Create());
Ian Rogers848871b2013-08-05 10:56:33 -0700261 uintptr_t instrumentation_exit_pc = GetQuickInstrumentationExitPc();
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100262 InstallStackVisitor visitor(thread, context.get(), instrumentation_exit_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800263 visitor.WalkStack(true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100264 CHECK_EQ(visitor.dex_pcs_.size(), thread->GetInstrumentationStack()->size());
Ian Rogers62d6c772013-02-27 08:32:07 -0800265
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100266 if (instrumentation->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100267 // Create method enter events for all methods currently on the thread's stack. We only do this
268 // if no debugger is attached to prevent from posting events twice.
Jeff Haoa15a81b2014-05-27 18:25:47 -0700269 auto ssi = visitor.shadow_stack_.rbegin();
270 for (auto isi = thread->GetInstrumentationStack()->rbegin(),
271 end = thread->GetInstrumentationStack()->rend(); isi != end; ++isi) {
272 while (ssi != visitor.shadow_stack_.rend() && (*ssi).frame_id_ < (*isi).frame_id_) {
273 instrumentation->MethodEnterEvent(thread, (*ssi).this_object_, (*ssi).method_, 0);
274 ++ssi;
275 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100276 uint32_t dex_pc = visitor.dex_pcs_.back();
277 visitor.dex_pcs_.pop_back();
Jeff Haoa15a81b2014-05-27 18:25:47 -0700278 instrumentation->MethodEnterEvent(thread, (*isi).this_object_, (*isi).method_, dex_pc);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100279 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800280 }
281 thread->VerifyStack();
Ian Rogers306057f2012-11-26 12:45:53 -0800282}
283
Ian Rogers62d6c772013-02-27 08:32:07 -0800284// Removes the instrumentation exit pc as the return PC for every quick frame.
285static void InstrumentationRestoreStack(Thread* thread, void* arg)
Ian Rogers306057f2012-11-26 12:45:53 -0800286 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
287 struct RestoreStackVisitor : public StackVisitor {
Ian Rogers62d6c772013-02-27 08:32:07 -0800288 RestoreStackVisitor(Thread* thread, uintptr_t instrumentation_exit_pc,
289 Instrumentation* instrumentation)
290 : StackVisitor(thread, NULL), thread_(thread),
291 instrumentation_exit_pc_(instrumentation_exit_pc),
292 instrumentation_(instrumentation),
293 instrumentation_stack_(thread->GetInstrumentationStack()),
294 frames_removed_(0) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800295
296 virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800297 if (instrumentation_stack_->size() == 0) {
jeffhao725a9572012-11-13 18:20:12 -0800298 return false; // Stop.
299 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700300 mirror::ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800301 if (GetCurrentQuickFrame() == NULL) {
302 if (kVerboseInstrumentation) {
303 LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId() << " Method=" << PrettyMethod(m);
304 }
305 return true; // Ignore shadow frames.
306 }
Ian Rogers306057f2012-11-26 12:45:53 -0800307 if (m == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800308 if (kVerboseInstrumentation) {
309 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
310 }
Ian Rogers306057f2012-11-26 12:45:53 -0800311 return true; // Ignore upcalls.
312 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800313 bool removed_stub = false;
314 // TODO: make this search more efficient?
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100315 const size_t frameId = GetFrameId();
316 for (const InstrumentationStackFrame& instrumentation_frame : *instrumentation_stack_) {
317 if (instrumentation_frame.frame_id_ == frameId) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800318 if (kVerboseInstrumentation) {
319 LOG(INFO) << " Removing exit stub in " << DescribeLocation();
320 }
Jeff Hao9a916d32013-06-27 18:45:37 -0700321 if (instrumentation_frame.interpreter_entry_) {
322 CHECK(m == Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
323 } else {
324 CHECK(m == instrumentation_frame.method_) << PrettyMethod(m);
325 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800326 SetReturnPc(instrumentation_frame.return_pc_);
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100327 if (instrumentation_->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100328 // Create the method exit events. As the methods didn't really exit the result is 0.
329 // We only do this if no debugger is attached to prevent from posting events twice.
330 instrumentation_->MethodExitEvent(thread_, instrumentation_frame.this_object_, m,
331 GetDexPc(), JValue());
332 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800333 frames_removed_++;
334 removed_stub = true;
335 break;
336 }
337 }
338 if (!removed_stub) {
339 if (kVerboseInstrumentation) {
340 LOG(INFO) << " No exit stub in " << DescribeLocation();
Ian Rogers306057f2012-11-26 12:45:53 -0800341 }
jeffhao725a9572012-11-13 18:20:12 -0800342 }
343 return true; // Continue.
344 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800345 Thread* const thread_;
Ian Rogers306057f2012-11-26 12:45:53 -0800346 const uintptr_t instrumentation_exit_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800347 Instrumentation* const instrumentation_;
348 std::deque<instrumentation::InstrumentationStackFrame>* const instrumentation_stack_;
349 size_t frames_removed_;
jeffhao725a9572012-11-13 18:20:12 -0800350 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800351 if (kVerboseInstrumentation) {
352 std::string thread_name;
353 thread->GetThreadName(thread_name);
354 LOG(INFO) << "Removing exit stubs in " << thread_name;
355 }
356 std::deque<instrumentation::InstrumentationStackFrame>* stack = thread->GetInstrumentationStack();
357 if (stack->size() > 0) {
358 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers848871b2013-08-05 10:56:33 -0700359 uintptr_t instrumentation_exit_pc = GetQuickInstrumentationExitPc();
Ian Rogers62d6c772013-02-27 08:32:07 -0800360 RestoreStackVisitor visitor(thread, instrumentation_exit_pc, instrumentation);
361 visitor.WalkStack(true);
362 CHECK_EQ(visitor.frames_removed_, stack->size());
363 while (stack->size() > 0) {
364 stack->pop_front();
365 }
jeffhao725a9572012-11-13 18:20:12 -0800366 }
367}
368
Ian Rogers62d6c772013-02-27 08:32:07 -0800369void Instrumentation::AddListener(InstrumentationListener* listener, uint32_t events) {
370 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Ian Rogers62d6c772013-02-27 08:32:07 -0800371 if ((events & kMethodEntered) != 0) {
372 method_entry_listeners_.push_back(listener);
Ian Rogers62d6c772013-02-27 08:32:07 -0800373 have_method_entry_listeners_ = true;
374 }
375 if ((events & kMethodExited) != 0) {
376 method_exit_listeners_.push_back(listener);
Ian Rogers62d6c772013-02-27 08:32:07 -0800377 have_method_exit_listeners_ = true;
378 }
379 if ((events & kMethodUnwind) != 0) {
380 method_unwind_listeners_.push_back(listener);
381 have_method_unwind_listeners_ = true;
382 }
383 if ((events & kDexPcMoved) != 0) {
384 dex_pc_listeners_.push_back(listener);
Ian Rogers62d6c772013-02-27 08:32:07 -0800385 have_dex_pc_listeners_ = true;
386 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200387 if ((events & kFieldRead) != 0) {
388 field_read_listeners_.push_back(listener);
389 have_field_read_listeners_ = true;
390 }
391 if ((events & kFieldWritten) != 0) {
392 field_write_listeners_.push_back(listener);
393 have_field_write_listeners_ = true;
394 }
Jeff Hao14dd5a82013-04-11 10:23:36 -0700395 if ((events & kExceptionCaught) != 0) {
396 exception_caught_listeners_.push_back(listener);
397 have_exception_caught_listeners_ = true;
398 }
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200399 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800400}
401
Ian Rogers62d6c772013-02-27 08:32:07 -0800402void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) {
403 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Ian Rogers62d6c772013-02-27 08:32:07 -0800404
405 if ((events & kMethodEntered) != 0) {
406 bool contains = std::find(method_entry_listeners_.begin(), method_entry_listeners_.end(),
407 listener) != method_entry_listeners_.end();
408 if (contains) {
409 method_entry_listeners_.remove(listener);
410 }
411 have_method_entry_listeners_ = method_entry_listeners_.size() > 0;
Ian Rogers62d6c772013-02-27 08:32:07 -0800412 }
413 if ((events & kMethodExited) != 0) {
414 bool contains = std::find(method_exit_listeners_.begin(), method_exit_listeners_.end(),
415 listener) != method_exit_listeners_.end();
416 if (contains) {
417 method_exit_listeners_.remove(listener);
418 }
419 have_method_exit_listeners_ = method_exit_listeners_.size() > 0;
Ian Rogers62d6c772013-02-27 08:32:07 -0800420 }
421 if ((events & kMethodUnwind) != 0) {
422 method_unwind_listeners_.remove(listener);
423 }
424 if ((events & kDexPcMoved) != 0) {
425 bool contains = std::find(dex_pc_listeners_.begin(), dex_pc_listeners_.end(),
426 listener) != dex_pc_listeners_.end();
427 if (contains) {
428 dex_pc_listeners_.remove(listener);
429 }
430 have_dex_pc_listeners_ = dex_pc_listeners_.size() > 0;
Ian Rogers62d6c772013-02-27 08:32:07 -0800431 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200432 if ((events & kFieldRead) != 0) {
433 bool contains = std::find(field_read_listeners_.begin(), field_read_listeners_.end(),
434 listener) != field_read_listeners_.end();
435 if (contains) {
436 field_read_listeners_.remove(listener);
437 }
438 have_field_read_listeners_ = field_read_listeners_.size() > 0;
439 }
440 if ((events & kFieldWritten) != 0) {
441 bool contains = std::find(field_write_listeners_.begin(), field_write_listeners_.end(),
442 listener) != field_write_listeners_.end();
443 if (contains) {
444 field_write_listeners_.remove(listener);
445 }
446 have_field_write_listeners_ = field_write_listeners_.size() > 0;
447 }
Jeff Hao14dd5a82013-04-11 10:23:36 -0700448 if ((events & kExceptionCaught) != 0) {
449 exception_caught_listeners_.remove(listener);
450 have_exception_caught_listeners_ = exception_caught_listeners_.size() > 0;
451 }
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200452 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800453}
454
Ian Rogers62d6c772013-02-27 08:32:07 -0800455void Instrumentation::ConfigureStubs(bool require_entry_exit_stubs, bool require_interpreter) {
456 interpret_only_ = require_interpreter || forced_interpret_only_;
457 // Compute what level of instrumentation is required and compare to current.
458 int desired_level, current_level;
459 if (require_interpreter) {
460 desired_level = 2;
461 } else if (require_entry_exit_stubs) {
462 desired_level = 1;
463 } else {
464 desired_level = 0;
465 }
466 if (interpreter_stubs_installed_) {
467 current_level = 2;
468 } else if (entry_exit_stubs_installed_) {
469 current_level = 1;
470 } else {
471 current_level = 0;
472 }
473 if (desired_level == current_level) {
474 // We're already set.
475 return;
476 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100477 Thread* const self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -0800478 Runtime* runtime = Runtime::Current();
479 Locks::thread_list_lock_->AssertNotHeld(self);
480 if (desired_level > 0) {
481 if (require_interpreter) {
482 interpreter_stubs_installed_ = true;
483 } else {
484 CHECK(require_entry_exit_stubs);
485 entry_exit_stubs_installed_ = true;
486 }
487 runtime->GetClassLinker()->VisitClasses(InstallStubsClassVisitor, this);
488 instrumentation_stubs_installed_ = true;
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100489 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800490 runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this);
491 } else {
492 interpreter_stubs_installed_ = false;
493 entry_exit_stubs_installed_ = false;
494 runtime->GetClassLinker()->VisitClasses(InstallStubsClassVisitor, this);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100495 // Restore stack only if there is no method currently deoptimized.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700496 bool empty;
497 {
498 ReaderMutexLock mu(self, deoptimized_methods_lock_);
499 empty = deoptimized_methods_.empty(); // Avoid lock violation.
500 }
501 if (empty) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100502 instrumentation_stubs_installed_ = false;
503 MutexLock mu(self, *Locks::thread_list_lock_);
504 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
505 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800506 }
jeffhao725a9572012-11-13 18:20:12 -0800507}
508
Ian Rogersfa824272013-11-05 16:12:57 -0800509static void ResetQuickAllocEntryPointsForThread(Thread* thread, void* arg) {
510 thread->ResetQuickAllocEntryPointsForThread();
511}
512
Mathieu Chartier661974a2014-01-09 11:23:53 -0800513void Instrumentation::SetEntrypointsInstrumented(bool instrumented) {
514 Runtime* runtime = Runtime::Current();
515 ThreadList* tl = runtime->GetThreadList();
516 if (runtime->IsStarted()) {
517 tl->SuspendAll();
518 }
519 {
520 MutexLock mu(Thread::Current(), *Locks::runtime_shutdown_lock_);
521 SetQuickAllocEntryPointsInstrumented(instrumented);
522 ResetQuickAllocEntryPoints();
523 }
524 if (runtime->IsStarted()) {
525 tl->ResumeAll();
526 }
527}
528
Ian Rogersfa824272013-11-05 16:12:57 -0800529void Instrumentation::InstrumentQuickAllocEntryPoints() {
530 // TODO: the read of quick_alloc_entry_points_instrumentation_counter_ is racey and this code
531 // should be guarded by a lock.
Ian Rogers3e5cf302014-05-20 16:40:37 -0700532 DCHECK_GE(quick_alloc_entry_points_instrumentation_counter_.LoadSequentiallyConsistent(), 0);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800533 const bool enable_instrumentation =
Ian Rogers3e5cf302014-05-20 16:40:37 -0700534 quick_alloc_entry_points_instrumentation_counter_.FetchAndAddSequentiallyConsistent(1) == 0;
Ian Rogersfa824272013-11-05 16:12:57 -0800535 if (enable_instrumentation) {
Mathieu Chartier661974a2014-01-09 11:23:53 -0800536 SetEntrypointsInstrumented(true);
Ian Rogersfa824272013-11-05 16:12:57 -0800537 }
538}
539
540void Instrumentation::UninstrumentQuickAllocEntryPoints() {
541 // TODO: the read of quick_alloc_entry_points_instrumentation_counter_ is racey and this code
542 // should be guarded by a lock.
Ian Rogers3e5cf302014-05-20 16:40:37 -0700543 DCHECK_GT(quick_alloc_entry_points_instrumentation_counter_.LoadSequentiallyConsistent(), 0);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800544 const bool disable_instrumentation =
Ian Rogers3e5cf302014-05-20 16:40:37 -0700545 quick_alloc_entry_points_instrumentation_counter_.FetchAndSubSequentiallyConsistent(1) == 1;
Ian Rogersfa824272013-11-05 16:12:57 -0800546 if (disable_instrumentation) {
Mathieu Chartier661974a2014-01-09 11:23:53 -0800547 SetEntrypointsInstrumented(false);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800548 }
549}
550
551void Instrumentation::ResetQuickAllocEntryPoints() {
552 Runtime* runtime = Runtime::Current();
553 if (runtime->IsStarted()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800554 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
555 runtime->GetThreadList()->ForEach(ResetQuickAllocEntryPointsForThread, NULL);
Ian Rogersfa824272013-11-05 16:12:57 -0800556 }
557}
558
Ian Rogersef7d42f2014-01-06 12:55:46 -0800559void Instrumentation::UpdateMethodsCode(mirror::ArtMethod* method, const void* quick_code,
560 const void* portable_code, bool have_portable_code) const {
561 const void* new_portable_code;
562 const void* new_quick_code;
563 bool new_have_portable_code;
Ian Rogers62d6c772013-02-27 08:32:07 -0800564 if (LIKELY(!instrumentation_stubs_installed_)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800565 new_portable_code = portable_code;
566 new_quick_code = quick_code;
567 new_have_portable_code = have_portable_code;
Jeff Hao65d15d92013-07-16 16:39:33 -0700568 } else {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100569 if ((interpreter_stubs_installed_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800570 new_portable_code = GetPortableToInterpreterBridge();
571 new_quick_code = GetQuickToInterpreterBridge();
572 new_have_portable_code = false;
573 } else if (quick_code == GetQuickResolutionTrampoline(Runtime::Current()->GetClassLinker()) ||
Vladimir Marko8a630572014-04-09 18:45:35 +0100574 quick_code == GetQuickToInterpreterBridgeTrampoline(Runtime::Current()->GetClassLinker()) ||
575 quick_code == GetQuickToInterpreterBridge()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800576 DCHECK((portable_code == GetPortableResolutionTrampoline(Runtime::Current()->GetClassLinker())) ||
577 (portable_code == GetPortableToInterpreterBridge()));
578 new_portable_code = portable_code;
579 new_quick_code = quick_code;
580 new_have_portable_code = have_portable_code;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100581 } else if (entry_exit_stubs_installed_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800582 new_quick_code = GetQuickInstrumentationEntryPoint();
583 new_portable_code = GetPortableToInterpreterBridge();
584 new_have_portable_code = false;
Jeff Hao65d15d92013-07-16 16:39:33 -0700585 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800586 new_portable_code = portable_code;
587 new_quick_code = quick_code;
588 new_have_portable_code = have_portable_code;
Jeff Hao65d15d92013-07-16 16:39:33 -0700589 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800590 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800591 UpdateEntrypoints(method, new_quick_code, new_portable_code, new_have_portable_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100592}
593
594void Instrumentation::Deoptimize(mirror::ArtMethod* method) {
595 CHECK(!method->IsNative());
596 CHECK(!method->IsProxyMethod());
597 CHECK(!method->IsAbstract());
598
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700599 Thread* self = Thread::Current();
600 std::pair<std::set<mirror::ArtMethod*>::iterator, bool> pair;
601 {
602 WriterMutexLock mu(self, deoptimized_methods_lock_);
603 pair = deoptimized_methods_.insert(method);
604 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100605 bool already_deoptimized = !pair.second;
606 CHECK(!already_deoptimized) << "Method " << PrettyMethod(method) << " is already deoptimized";
607
608 if (!interpreter_stubs_installed_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800609 UpdateEntrypoints(method, GetQuickToInterpreterBridge(), GetPortableToInterpreterBridge(),
610 false);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100611
612 // Install instrumentation exit stub and instrumentation frames. We may already have installed
613 // these previously so it will only cover the newly created frames.
614 instrumentation_stubs_installed_ = true;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700615 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100616 Runtime::Current()->GetThreadList()->ForEach(InstrumentationInstallStack, this);
617 }
618}
619
620void Instrumentation::Undeoptimize(mirror::ArtMethod* method) {
621 CHECK(!method->IsNative());
622 CHECK(!method->IsProxyMethod());
623 CHECK(!method->IsAbstract());
624
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700625 Thread* self = Thread::Current();
626 bool empty;
627 {
628 WriterMutexLock mu(self, deoptimized_methods_lock_);
629 auto it = deoptimized_methods_.find(method);
630 CHECK(it != deoptimized_methods_.end()) << "Method " << PrettyMethod(method)
631 << " is not deoptimized";
632 deoptimized_methods_.erase(it);
633 empty = deoptimized_methods_.empty();
634 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100635
636 // Restore code and possibly stack only if we did not deoptimize everything.
637 if (!interpreter_stubs_installed_) {
638 // Restore its code or resolution trampoline.
639 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800640 if (method->IsStatic() && !method->IsConstructor() &&
641 !method->GetDeclaringClass()->IsInitialized()) {
642 UpdateEntrypoints(method, GetQuickResolutionTrampoline(class_linker),
643 GetPortableResolutionTrampoline(class_linker), false);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100644 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800645 bool have_portable_code = false;
646 const void* quick_code = class_linker->GetQuickOatCodeFor(method);
647 const void* portable_code = class_linker->GetPortableOatCodeFor(method, &have_portable_code);
648 UpdateEntrypoints(method, quick_code, portable_code, have_portable_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100649 }
650
651 // If there is no deoptimized method left, we can restore the stack of each thread.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700652 if (empty) {
653 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100654 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
655 instrumentation_stubs_installed_ = false;
656 }
657 }
658}
659
660bool Instrumentation::IsDeoptimized(mirror::ArtMethod* method) const {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700661 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100662 DCHECK(method != nullptr);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700663 return deoptimized_methods_.find(method) != deoptimized_methods_.end();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100664}
665
666void Instrumentation::EnableDeoptimization() {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700667 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100668 CHECK(deoptimized_methods_.empty());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100669 CHECK_EQ(deoptimization_enabled_, false);
670 deoptimization_enabled_ = true;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100671}
672
673void Instrumentation::DisableDeoptimization() {
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100674 CHECK_EQ(deoptimization_enabled_, true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100675 // If we deoptimized everything, undo it.
676 if (interpreter_stubs_installed_) {
677 UndeoptimizeEverything();
678 }
679 // Undeoptimized selected methods.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700680 while (true) {
681 mirror::ArtMethod* method;
682 {
683 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
684 if (deoptimized_methods_.empty()) {
685 break;
686 }
687 method = *deoptimized_methods_.begin();
688 }
689 Undeoptimize(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100690 }
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100691 deoptimization_enabled_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100692}
693
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100694// Indicates if instrumentation should notify method enter/exit events to the listeners.
695bool Instrumentation::ShouldNotifyMethodEnterExitEvents() const {
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100696 return !deoptimization_enabled_ && !interpreter_stubs_installed_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100697}
698
699void Instrumentation::DeoptimizeEverything() {
700 CHECK(!interpreter_stubs_installed_);
701 ConfigureStubs(false, true);
702}
703
704void Instrumentation::UndeoptimizeEverything() {
705 CHECK(interpreter_stubs_installed_);
706 ConfigureStubs(false, false);
707}
708
709void Instrumentation::EnableMethodTracing() {
710 bool require_interpreter = kDeoptimizeForAccurateMethodEntryExitListeners;
711 ConfigureStubs(!require_interpreter, require_interpreter);
712}
713
714void Instrumentation::DisableMethodTracing() {
715 ConfigureStubs(false, false);
jeffhao725a9572012-11-13 18:20:12 -0800716}
717
Ian Rogersef7d42f2014-01-06 12:55:46 -0800718const void* Instrumentation::GetQuickCodeFor(mirror::ArtMethod* method) const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800719 Runtime* runtime = Runtime::Current();
720 if (LIKELY(!instrumentation_stubs_installed_)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800721 const void* code = method->GetEntryPointFromQuickCompiledCode();
Vladimir Marko8a630572014-04-09 18:45:35 +0100722 DCHECK(code != nullptr);
723 if (LIKELY(code != GetQuickResolutionTrampoline(runtime->GetClassLinker())) &&
724 LIKELY(code != GetQuickToInterpreterBridgeTrampoline(runtime->GetClassLinker())) &&
725 LIKELY(code != GetQuickToInterpreterBridge())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800726 return code;
727 }
728 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800729 return runtime->GetClassLinker()->GetQuickOatCodeFor(method);
jeffhao725a9572012-11-13 18:20:12 -0800730}
731
Ian Rogers62d6c772013-02-27 08:32:07 -0800732void Instrumentation::MethodEnterEventImpl(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800733 mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800734 uint32_t dex_pc) const {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700735 auto it = method_entry_listeners_.begin();
Jeff Hao65d15d92013-07-16 16:39:33 -0700736 bool is_end = (it == method_entry_listeners_.end());
737 // Implemented this way to prevent problems caused by modification of the list while iterating.
738 while (!is_end) {
739 InstrumentationListener* cur = *it;
740 ++it;
741 is_end = (it == method_entry_listeners_.end());
742 cur->MethodEntered(thread, this_object, method, dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800743 }
744}
745
746void Instrumentation::MethodExitEventImpl(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800747 mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800748 uint32_t dex_pc, const JValue& return_value) const {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700749 auto it = method_exit_listeners_.begin();
Jeff Hao65d15d92013-07-16 16:39:33 -0700750 bool is_end = (it == method_exit_listeners_.end());
751 // Implemented this way to prevent problems caused by modification of the list while iterating.
752 while (!is_end) {
753 InstrumentationListener* cur = *it;
754 ++it;
755 is_end = (it == method_exit_listeners_.end());
756 cur->MethodExited(thread, this_object, method, dex_pc, return_value);
Ian Rogers62d6c772013-02-27 08:32:07 -0800757 }
758}
759
760void Instrumentation::MethodUnwindEvent(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800761 mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800762 uint32_t dex_pc) const {
763 if (have_method_unwind_listeners_) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700764 for (InstrumentationListener* listener : method_unwind_listeners_) {
Sebastien Hertz51db44a2013-11-19 10:00:29 +0100765 listener->MethodUnwind(thread, this_object, method, dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800766 }
767 }
768}
769
770void Instrumentation::DexPcMovedEventImpl(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800771 mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800772 uint32_t dex_pc) const {
773 // TODO: STL copy-on-write collection? The copy below is due to the debug listener having an
774 // action where it can remove itself as a listener and break the iterator. The copy only works
775 // around the problem and in general we may have to move to something like reference counting to
776 // ensure listeners are deleted correctly.
777 std::list<InstrumentationListener*> copy(dex_pc_listeners_);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700778 for (InstrumentationListener* listener : copy) {
779 listener->DexPcMoved(thread, this_object, method, dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800780 }
781}
782
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200783void Instrumentation::FieldReadEventImpl(Thread* thread, mirror::Object* this_object,
784 mirror::ArtMethod* method, uint32_t dex_pc,
785 mirror::ArtField* field) const {
Sebastien Hertz9f102032014-05-23 08:59:42 +0200786 // TODO: same comment than DexPcMovedEventImpl.
787 std::list<InstrumentationListener*> copy(field_read_listeners_);
788 for (InstrumentationListener* listener : copy) {
789 listener->FieldRead(thread, this_object, method, dex_pc, field);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200790 }
791}
792
793void Instrumentation::FieldWriteEventImpl(Thread* thread, mirror::Object* this_object,
794 mirror::ArtMethod* method, uint32_t dex_pc,
795 mirror::ArtField* field, const JValue& field_value) const {
Sebastien Hertz9f102032014-05-23 08:59:42 +0200796 // 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);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200800 }
801}
802
Ian Rogers62d6c772013-02-27 08:32:07 -0800803void Instrumentation::ExceptionCaughtEvent(Thread* thread, const ThrowLocation& throw_location,
Brian Carlstromea46f952013-07-30 01:26:50 -0700804 mirror::ArtMethod* catch_method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800805 uint32_t catch_dex_pc,
Sebastien Hertz947ff082013-09-17 14:10:13 +0200806 mirror::Throwable* exception_object) const {
Sebastien Hertz9f102032014-05-23 08:59:42 +0200807 if (HasExceptionCaughtListeners()) {
808 DCHECK_EQ(thread->GetException(nullptr), exception_object);
809 bool is_exception_reported = thread->IsExceptionReportedToInstrumentation();
Jeff Haoc0bd4da2013-04-11 15:52:28 -0700810 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);
Sebastien Hertz9f102032014-05-23 08:59:42 +0200818 thread->SetExceptionReportedToInstrumentation(is_exception_reported);
Ian Rogers62d6c772013-02-27 08:32:07 -0800819 }
820}
821
822static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame,
823 int delta)
824 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
825 size_t frame_id = StackVisitor::ComputeNumFrames(self) + delta;
826 if (frame_id != instrumentation_frame.frame_id_) {
827 LOG(ERROR) << "Expected frame_id=" << frame_id << " but found "
828 << instrumentation_frame.frame_id_;
829 StackVisitor::DescribeStack(self);
830 CHECK_EQ(frame_id, instrumentation_frame.frame_id_);
831 }
832}
833
834void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700835 mirror::ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -0700836 uintptr_t lr, bool interpreter_entry) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800837 // We have a callee-save frame meaning this value is guaranteed to never be 0.
838 size_t frame_id = StackVisitor::ComputeNumFrames(self);
839 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
840 if (kVerboseInstrumentation) {
Brian Carlstrom2d888622013-07-18 17:02:00 -0700841 LOG(INFO) << "Entering " << PrettyMethod(method) << " from PC " << reinterpret_cast<void*>(lr);
Ian Rogers62d6c772013-02-27 08:32:07 -0800842 }
843 instrumentation::InstrumentationStackFrame instrumentation_frame(this_object, method, lr,
Jeff Hao9a916d32013-06-27 18:45:37 -0700844 frame_id, interpreter_entry);
Ian Rogers62d6c772013-02-27 08:32:07 -0800845 stack->push_front(instrumentation_frame);
846
847 MethodEnterEvent(self, this_object, method, 0);
848}
849
Andreas Gamped58342c2014-06-05 14:18:08 -0700850TwoWordReturn Instrumentation::PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc,
851 uint64_t gpr_result,
852 uint64_t fpr_result) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800853 // Do the pop.
854 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
855 CHECK_GT(stack->size(), 0U);
856 InstrumentationStackFrame instrumentation_frame = stack->front();
857 stack->pop_front();
858
859 // Set return PC and check the sanity of the stack.
860 *return_pc = instrumentation_frame.return_pc_;
861 CheckStackDepth(self, instrumentation_frame, 0);
862
Brian Carlstromea46f952013-07-30 01:26:50 -0700863 mirror::ArtMethod* method = instrumentation_frame.method_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700864 uint32_t length;
865 char return_shorty = method->GetShorty(&length)[0];
Ian Rogers62d6c772013-02-27 08:32:07 -0800866 JValue return_value;
867 if (return_shorty == 'V') {
868 return_value.SetJ(0);
869 } else if (return_shorty == 'F' || return_shorty == 'D') {
870 return_value.SetJ(fpr_result);
871 } else {
872 return_value.SetJ(gpr_result);
873 }
874 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
875 // return_pc.
876 uint32_t dex_pc = DexFile::kDexNoIndex;
877 mirror::Object* this_object = instrumentation_frame.this_object_;
878 MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value);
jeffhao725a9572012-11-13 18:20:12 -0800879
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100880 // Deoptimize if the caller needs to continue execution in the interpreter. Do nothing if we get
881 // back to an upcall.
882 NthCallerVisitor visitor(self, 1, true);
883 visitor.WalkStack(true);
884 bool deoptimize = (visitor.caller != NULL) &&
885 (interpreter_stubs_installed_ || IsDeoptimized(visitor.caller));
886 if (deoptimize && kVerboseInstrumentation) {
887 LOG(INFO) << "Deoptimizing into " << PrettyMethod(visitor.caller);
Ian Rogers62d6c772013-02-27 08:32:07 -0800888 }
889 if (deoptimize) {
890 if (kVerboseInstrumentation) {
891 LOG(INFO) << "Deoptimizing from " << PrettyMethod(method)
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100892 << " result is " << std::hex << return_value.GetJ();
Ian Rogers62d6c772013-02-27 08:32:07 -0800893 }
894 self->SetDeoptimizationReturnValue(return_value);
Andreas Gamped58342c2014-06-05 14:18:08 -0700895 return GetTwoWordSuccessValue(*return_pc,
896 reinterpret_cast<uintptr_t>(GetQuickDeoptimizationEntryPoint()));
Ian Rogers62d6c772013-02-27 08:32:07 -0800897 } else {
898 if (kVerboseInstrumentation) {
Brian Carlstrom2d888622013-07-18 17:02:00 -0700899 LOG(INFO) << "Returning from " << PrettyMethod(method)
900 << " to PC " << reinterpret_cast<void*>(*return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800901 }
Andreas Gamped58342c2014-06-05 14:18:08 -0700902 return GetTwoWordSuccessValue(0, *return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800903 }
jeffhao725a9572012-11-13 18:20:12 -0800904}
905
Ian Rogers62d6c772013-02-27 08:32:07 -0800906void Instrumentation::PopMethodForUnwind(Thread* self, bool is_deoptimization) const {
907 // Do the pop.
908 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
909 CHECK_GT(stack->size(), 0U);
910 InstrumentationStackFrame instrumentation_frame = stack->front();
911 // TODO: bring back CheckStackDepth(self, instrumentation_frame, 2);
912 stack->pop_front();
913
Brian Carlstromea46f952013-07-30 01:26:50 -0700914 mirror::ArtMethod* method = instrumentation_frame.method_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800915 if (is_deoptimization) {
916 if (kVerboseInstrumentation) {
917 LOG(INFO) << "Popping for deoptimization " << PrettyMethod(method);
918 }
919 } else {
920 if (kVerboseInstrumentation) {
921 LOG(INFO) << "Popping for unwind " << PrettyMethod(method);
922 }
923
924 // Notify listeners of method unwind.
925 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
926 // return_pc.
927 uint32_t dex_pc = DexFile::kDexNoIndex;
928 MethodUnwindEvent(self, instrumentation_frame.this_object_, method, dex_pc);
929 }
930}
931
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700932void Instrumentation::VisitRoots(RootCallback* callback, void* arg) {
933 WriterMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
934 if (deoptimized_methods_.empty()) {
935 return;
936 }
937 std::set<mirror::ArtMethod*> new_deoptimized_methods;
938 for (mirror::ArtMethod* method : deoptimized_methods_) {
939 DCHECK(method != nullptr);
940 callback(reinterpret_cast<mirror::Object**>(&method), arg, 0, kRootVMInternal);
941 new_deoptimized_methods.insert(method);
942 }
943 deoptimized_methods_ = new_deoptimized_methods;
944}
945
Ian Rogers62d6c772013-02-27 08:32:07 -0800946std::string InstrumentationStackFrame::Dump() const {
947 std::ostringstream os;
948 os << "Frame " << frame_id_ << " " << PrettyMethod(method_) << ":"
949 << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_);
950 return os.str();
951}
952
953} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -0800954} // namespace art