blob: 481cbad3b8400bee848db3d9963f335c3e0e230a [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 Rogers62d6c772013-02-27 08:32:07 -080021#include "atomic_integer.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"
Brian Carlstromea46f952013-07-30 01:26:50 -070026#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "mirror/class-inl.h"
28#include "mirror/dex_cache.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "mirror/object_array-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070030#include "mirror/object-inl.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080031#include "nth_caller_visitor.h"
Ian Rogersc928de92013-02-27 14:30:44 -080032#if !defined(ART_USE_PORTABLE_COMPILER)
Ian Rogers166db042013-07-26 12:05:57 -070033#include "entrypoints/quick/quick_entrypoints.h"
jeffhao725a9572012-11-13 18:20:12 -080034#endif
35#include "object_utils.h"
36#include "os.h"
37#include "scoped_thread_state_change.h"
38#include "thread.h"
39#include "thread_list.h"
jeffhao725a9572012-11-13 18:20:12 -080040
41namespace art {
Ian Rogers62d6c772013-02-27 08:32:07 -080042namespace instrumentation {
jeffhao725a9572012-11-13 18:20:12 -080043
Ian Rogers816432e2013-09-06 15:47:45 -070044// Do we want to deoptimize for method entry and exit listeners or just try to intercept
45// invocations? Deoptimization forces all code to run in the interpreter and considerably hurts the
46// application's performance.
Ian Rogers7b6da362013-09-11 09:29:40 -070047static constexpr bool kDeoptimizeForAccurateMethodEntryExitListeners = false;
Ian Rogers816432e2013-09-06 15:47:45 -070048
Ian Rogers62d6c772013-02-27 08:32:07 -080049static bool InstallStubsClassVisitor(mirror::Class* klass, void* arg)
jeffhao725a9572012-11-13 18:20:12 -080050 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -080051 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
52 return instrumentation->InstallStubsForClass(klass);
53}
54
55bool Instrumentation::InstallStubsForClass(mirror::Class* klass) {
56 bool uninstall = !entry_exit_stubs_installed_ && !interpreter_stubs_installed_;
57 ClassLinker* class_linker = NULL;
58 if (uninstall) {
59 class_linker = Runtime::Current()->GetClassLinker();
60 }
61 bool is_initialized = klass->IsInitialized();
jeffhao725a9572012-11-13 18:20:12 -080062 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -070063 mirror::ArtMethod* method = klass->GetDirectMethod(i);
Ian Rogers62d6c772013-02-27 08:32:07 -080064 if (!method->IsAbstract()) {
65 const void* new_code;
66 if (uninstall) {
Jeff Hao65d15d92013-07-16 16:39:33 -070067 if (forced_interpret_only_ && !method->IsNative() && !method->IsProxyMethod()) {
Ian Rogers848871b2013-08-05 10:56:33 -070068 new_code = GetCompiledCodeToInterpreterBridge();
Jeff Hao65d15d92013-07-16 16:39:33 -070069 } else if (is_initialized || !method->IsStatic() || method->IsConstructor()) {
Ian Rogers62d6c772013-02-27 08:32:07 -080070 new_code = class_linker->GetOatCodeFor(method);
71 } else {
Jeff Hao0aba0ba2013-06-03 14:49:28 -070072 new_code = GetResolutionTrampoline(class_linker);
Ian Rogers62d6c772013-02-27 08:32:07 -080073 }
74 } else { // !uninstall
75 if (!interpreter_stubs_installed_ || method->IsNative()) {
Ian Rogers848871b2013-08-05 10:56:33 -070076 new_code = GetQuickInstrumentationEntryPoint();
Ian Rogers62d6c772013-02-27 08:32:07 -080077 } else {
Ian Rogers848871b2013-08-05 10:56:33 -070078 new_code = GetCompiledCodeToInterpreterBridge();
Ian Rogers62d6c772013-02-27 08:32:07 -080079 }
80 }
Jeff Haoaa4a7932013-05-13 11:28:27 -070081 method->SetEntryPointFromCompiledCode(new_code);
jeffhao725a9572012-11-13 18:20:12 -080082 }
83 }
jeffhao725a9572012-11-13 18:20:12 -080084 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -070085 mirror::ArtMethod* method = klass->GetVirtualMethod(i);
Ian Rogers62d6c772013-02-27 08:32:07 -080086 if (!method->IsAbstract()) {
87 const void* new_code;
88 if (uninstall) {
Jeff Hao65d15d92013-07-16 16:39:33 -070089 if (forced_interpret_only_ && !method->IsNative() && !method->IsProxyMethod()) {
Ian Rogers848871b2013-08-05 10:56:33 -070090 new_code = GetCompiledCodeToInterpreterBridge();
Jeff Hao65d15d92013-07-16 16:39:33 -070091 } else {
92 new_code = class_linker->GetOatCodeFor(method);
93 }
Ian Rogers62d6c772013-02-27 08:32:07 -080094 } else { // !uninstall
95 if (!interpreter_stubs_installed_ || method->IsNative()) {
Ian Rogers848871b2013-08-05 10:56:33 -070096 new_code = GetQuickInstrumentationEntryPoint();
Ian Rogers62d6c772013-02-27 08:32:07 -080097 } else {
Ian Rogers848871b2013-08-05 10:56:33 -070098 new_code = GetCompiledCodeToInterpreterBridge();
Ian Rogers62d6c772013-02-27 08:32:07 -080099 }
100 }
Jeff Haoaa4a7932013-05-13 11:28:27 -0700101 method->SetEntryPointFromCompiledCode(new_code);
jeffhao725a9572012-11-13 18:20:12 -0800102 }
103 }
104 return true;
105}
106
Ian Rogers62d6c772013-02-27 08:32:07 -0800107// Places the instrumentation exit pc as the return PC for every quick frame. This also allows
108// deoptimization of quick frames to interpreter frames.
109static void InstrumentationInstallStack(Thread* thread, void* arg)
Ian Rogers306057f2012-11-26 12:45:53 -0800110 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
111 struct InstallStackVisitor : public StackVisitor {
Ian Rogers62d6c772013-02-27 08:32:07 -0800112 InstallStackVisitor(Thread* thread, Context* context, uintptr_t instrumentation_exit_pc)
113 : StackVisitor(thread, context), instrumentation_stack_(thread->GetInstrumentationStack()),
114 instrumentation_exit_pc_(instrumentation_exit_pc), last_return_pc_(0) {}
jeffhao725a9572012-11-13 18:20:12 -0800115
Ian Rogers306057f2012-11-26 12:45:53 -0800116 virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700117 mirror::ArtMethod* m = GetMethod();
Ian Rogers306057f2012-11-26 12:45:53 -0800118 if (GetCurrentQuickFrame() == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800119 if (kVerboseInstrumentation) {
120 LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId()
121 << " Method=" << PrettyMethod(m);
122 }
Ian Rogers306057f2012-11-26 12:45:53 -0800123 return true; // Ignore shadow frames.
124 }
Ian Rogers306057f2012-11-26 12:45:53 -0800125 if (m == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800126 if (kVerboseInstrumentation) {
127 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
128 }
129 last_return_pc_ = 0;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700130 return true; // Ignore upcalls.
Ian Rogers306057f2012-11-26 12:45:53 -0800131 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800132 if (m->IsRuntimeMethod()) {
133 if (kVerboseInstrumentation) {
134 LOG(INFO) << " Skipping runtime method. Frame " << GetFrameId();
135 }
136 last_return_pc_ = GetReturnPc();
Ian Rogers306057f2012-11-26 12:45:53 -0800137 return true; // Ignore unresolved methods since they will be instrumented after resolution.
138 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800139 if (kVerboseInstrumentation) {
140 LOG(INFO) << " Installing exit stub in " << DescribeLocation();
141 }
142 uintptr_t return_pc = GetReturnPc();
143 CHECK_NE(return_pc, instrumentation_exit_pc_);
144 CHECK_NE(return_pc, 0U);
Jeff Hao9a916d32013-06-27 18:45:37 -0700145 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, return_pc, GetFrameId(),
146 false);
Ian Rogers62d6c772013-02-27 08:32:07 -0800147 if (kVerboseInstrumentation) {
148 LOG(INFO) << "Pushing frame " << instrumentation_frame.Dump();
149 }
150 instrumentation_stack_->push_back(instrumentation_frame);
151 dex_pcs_.push_back(m->ToDexPc(last_return_pc_));
Ian Rogers306057f2012-11-26 12:45:53 -0800152 SetReturnPc(instrumentation_exit_pc_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800153 last_return_pc_ = return_pc;
Ian Rogers306057f2012-11-26 12:45:53 -0800154 return true; // Continue.
155 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800156 std::deque<InstrumentationStackFrame>* const instrumentation_stack_;
157 std::vector<uint32_t> dex_pcs_;
Ian Rogers306057f2012-11-26 12:45:53 -0800158 const uintptr_t instrumentation_exit_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800159 uintptr_t last_return_pc_;
Ian Rogers306057f2012-11-26 12:45:53 -0800160 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800161 if (kVerboseInstrumentation) {
162 std::string thread_name;
163 thread->GetThreadName(thread_name);
164 LOG(INFO) << "Installing exit stubs in " << thread_name;
Ian Rogers306057f2012-11-26 12:45:53 -0800165 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800166 UniquePtr<Context> context(Context::Create());
Ian Rogers848871b2013-08-05 10:56:33 -0700167 uintptr_t instrumentation_exit_pc = GetQuickInstrumentationExitPc();
Ian Rogers62d6c772013-02-27 08:32:07 -0800168 InstallStackVisitor visitor(thread, context.get(), instrumentation_exit_pc);
169 visitor.WalkStack(true);
170
171 // Create method enter events for all methods current on the thread's stack.
172 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
173 typedef std::deque<InstrumentationStackFrame>::const_reverse_iterator It;
174 for (It it = thread->GetInstrumentationStack()->rbegin(),
175 end = thread->GetInstrumentationStack()->rend(); it != end; ++it) {
176 mirror::Object* this_object = (*it).this_object_;
Brian Carlstromea46f952013-07-30 01:26:50 -0700177 mirror::ArtMethod* method = (*it).method_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800178 uint32_t dex_pc = visitor.dex_pcs_.back();
179 visitor.dex_pcs_.pop_back();
180 instrumentation->MethodEnterEvent(thread, this_object, method, dex_pc);
181 }
182 thread->VerifyStack();
Ian Rogers306057f2012-11-26 12:45:53 -0800183}
184
Ian Rogers62d6c772013-02-27 08:32:07 -0800185// Removes the instrumentation exit pc as the return PC for every quick frame.
186static void InstrumentationRestoreStack(Thread* thread, void* arg)
Ian Rogers306057f2012-11-26 12:45:53 -0800187 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
188 struct RestoreStackVisitor : public StackVisitor {
Ian Rogers62d6c772013-02-27 08:32:07 -0800189 RestoreStackVisitor(Thread* thread, uintptr_t instrumentation_exit_pc,
190 Instrumentation* instrumentation)
191 : StackVisitor(thread, NULL), thread_(thread),
192 instrumentation_exit_pc_(instrumentation_exit_pc),
193 instrumentation_(instrumentation),
194 instrumentation_stack_(thread->GetInstrumentationStack()),
195 frames_removed_(0) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800196
197 virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800198 if (instrumentation_stack_->size() == 0) {
jeffhao725a9572012-11-13 18:20:12 -0800199 return false; // Stop.
200 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700201 mirror::ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800202 if (GetCurrentQuickFrame() == NULL) {
203 if (kVerboseInstrumentation) {
204 LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId() << " Method=" << PrettyMethod(m);
205 }
206 return true; // Ignore shadow frames.
207 }
Ian Rogers306057f2012-11-26 12:45:53 -0800208 if (m == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800209 if (kVerboseInstrumentation) {
210 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
211 }
Ian Rogers306057f2012-11-26 12:45:53 -0800212 return true; // Ignore upcalls.
213 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800214 bool removed_stub = false;
215 // TODO: make this search more efficient?
Mathieu Chartier02e25112013-08-14 16:14:24 -0700216 for (InstrumentationStackFrame instrumentation_frame : *instrumentation_stack_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800217 if (instrumentation_frame.frame_id_ == GetFrameId()) {
218 if (kVerboseInstrumentation) {
219 LOG(INFO) << " Removing exit stub in " << DescribeLocation();
220 }
Jeff Hao9a916d32013-06-27 18:45:37 -0700221 if (instrumentation_frame.interpreter_entry_) {
222 CHECK(m == Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
223 } else {
224 CHECK(m == instrumentation_frame.method_) << PrettyMethod(m);
225 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800226 SetReturnPc(instrumentation_frame.return_pc_);
227 // Create the method exit events. As the methods didn't really exit the result is 0.
228 instrumentation_->MethodExitEvent(thread_, instrumentation_frame.this_object_, m,
229 GetDexPc(), JValue());
230 frames_removed_++;
231 removed_stub = true;
232 break;
233 }
234 }
235 if (!removed_stub) {
236 if (kVerboseInstrumentation) {
237 LOG(INFO) << " No exit stub in " << DescribeLocation();
Ian Rogers306057f2012-11-26 12:45:53 -0800238 }
jeffhao725a9572012-11-13 18:20:12 -0800239 }
240 return true; // Continue.
241 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800242 Thread* const thread_;
Ian Rogers306057f2012-11-26 12:45:53 -0800243 const uintptr_t instrumentation_exit_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800244 Instrumentation* const instrumentation_;
245 std::deque<instrumentation::InstrumentationStackFrame>* const instrumentation_stack_;
246 size_t frames_removed_;
jeffhao725a9572012-11-13 18:20:12 -0800247 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800248 if (kVerboseInstrumentation) {
249 std::string thread_name;
250 thread->GetThreadName(thread_name);
251 LOG(INFO) << "Removing exit stubs in " << thread_name;
252 }
253 std::deque<instrumentation::InstrumentationStackFrame>* stack = thread->GetInstrumentationStack();
254 if (stack->size() > 0) {
255 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers848871b2013-08-05 10:56:33 -0700256 uintptr_t instrumentation_exit_pc = GetQuickInstrumentationExitPc();
Ian Rogers62d6c772013-02-27 08:32:07 -0800257 RestoreStackVisitor visitor(thread, instrumentation_exit_pc, instrumentation);
258 visitor.WalkStack(true);
259 CHECK_EQ(visitor.frames_removed_, stack->size());
260 while (stack->size() > 0) {
261 stack->pop_front();
262 }
jeffhao725a9572012-11-13 18:20:12 -0800263 }
264}
265
Ian Rogers62d6c772013-02-27 08:32:07 -0800266void Instrumentation::AddListener(InstrumentationListener* listener, uint32_t events) {
267 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
268 bool require_entry_exit_stubs = false;
269 bool require_interpreter = false;
270 if ((events & kMethodEntered) != 0) {
271 method_entry_listeners_.push_back(listener);
Ian Rogers816432e2013-09-06 15:47:45 -0700272 require_interpreter = kDeoptimizeForAccurateMethodEntryExitListeners;
273 require_entry_exit_stubs = !kDeoptimizeForAccurateMethodEntryExitListeners;
Ian Rogers62d6c772013-02-27 08:32:07 -0800274 have_method_entry_listeners_ = true;
275 }
276 if ((events & kMethodExited) != 0) {
277 method_exit_listeners_.push_back(listener);
Ian Rogers816432e2013-09-06 15:47:45 -0700278 require_interpreter = kDeoptimizeForAccurateMethodEntryExitListeners;
279 require_entry_exit_stubs = !kDeoptimizeForAccurateMethodEntryExitListeners;
Ian Rogers62d6c772013-02-27 08:32:07 -0800280 have_method_exit_listeners_ = true;
281 }
282 if ((events & kMethodUnwind) != 0) {
283 method_unwind_listeners_.push_back(listener);
284 have_method_unwind_listeners_ = true;
285 }
286 if ((events & kDexPcMoved) != 0) {
287 dex_pc_listeners_.push_back(listener);
288 require_interpreter = true;
289 have_dex_pc_listeners_ = true;
290 }
Jeff Hao14dd5a82013-04-11 10:23:36 -0700291 if ((events & kExceptionCaught) != 0) {
292 exception_caught_listeners_.push_back(listener);
293 have_exception_caught_listeners_ = true;
294 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800295 ConfigureStubs(require_entry_exit_stubs, require_interpreter);
jeffhao725a9572012-11-13 18:20:12 -0800296}
297
Ian Rogers62d6c772013-02-27 08:32:07 -0800298void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) {
299 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
300 bool require_entry_exit_stubs = false;
301 bool require_interpreter = false;
302
303 if ((events & kMethodEntered) != 0) {
304 bool contains = std::find(method_entry_listeners_.begin(), method_entry_listeners_.end(),
305 listener) != method_entry_listeners_.end();
306 if (contains) {
307 method_entry_listeners_.remove(listener);
308 }
309 have_method_entry_listeners_ = method_entry_listeners_.size() > 0;
Ian Rogers816432e2013-09-06 15:47:45 -0700310 require_entry_exit_stubs |= have_method_entry_listeners_ &&
311 !kDeoptimizeForAccurateMethodEntryExitListeners;
312 require_interpreter = have_method_entry_listeners_ &&
313 kDeoptimizeForAccurateMethodEntryExitListeners;
Ian Rogers62d6c772013-02-27 08:32:07 -0800314 }
315 if ((events & kMethodExited) != 0) {
316 bool contains = std::find(method_exit_listeners_.begin(), method_exit_listeners_.end(),
317 listener) != method_exit_listeners_.end();
318 if (contains) {
319 method_exit_listeners_.remove(listener);
320 }
321 have_method_exit_listeners_ = method_exit_listeners_.size() > 0;
Ian Rogers816432e2013-09-06 15:47:45 -0700322 require_entry_exit_stubs |= have_method_exit_listeners_ &&
323 !kDeoptimizeForAccurateMethodEntryExitListeners;
324 require_interpreter = have_method_exit_listeners_ &&
325 kDeoptimizeForAccurateMethodEntryExitListeners;
Ian Rogers62d6c772013-02-27 08:32:07 -0800326 }
327 if ((events & kMethodUnwind) != 0) {
328 method_unwind_listeners_.remove(listener);
329 }
330 if ((events & kDexPcMoved) != 0) {
331 bool contains = std::find(dex_pc_listeners_.begin(), dex_pc_listeners_.end(),
332 listener) != dex_pc_listeners_.end();
333 if (contains) {
334 dex_pc_listeners_.remove(listener);
335 }
336 have_dex_pc_listeners_ = dex_pc_listeners_.size() > 0;
337 require_interpreter |= have_dex_pc_listeners_;
338 }
Jeff Hao14dd5a82013-04-11 10:23:36 -0700339 if ((events & kExceptionCaught) != 0) {
340 exception_caught_listeners_.remove(listener);
341 have_exception_caught_listeners_ = exception_caught_listeners_.size() > 0;
342 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800343 ConfigureStubs(require_entry_exit_stubs, require_interpreter);
jeffhao725a9572012-11-13 18:20:12 -0800344}
345
Ian Rogers62d6c772013-02-27 08:32:07 -0800346void Instrumentation::ConfigureStubs(bool require_entry_exit_stubs, bool require_interpreter) {
347 interpret_only_ = require_interpreter || forced_interpret_only_;
348 // Compute what level of instrumentation is required and compare to current.
349 int desired_level, current_level;
350 if (require_interpreter) {
351 desired_level = 2;
352 } else if (require_entry_exit_stubs) {
353 desired_level = 1;
354 } else {
355 desired_level = 0;
356 }
357 if (interpreter_stubs_installed_) {
358 current_level = 2;
359 } else if (entry_exit_stubs_installed_) {
360 current_level = 1;
361 } else {
362 current_level = 0;
363 }
364 if (desired_level == current_level) {
365 // We're already set.
366 return;
367 }
368 Thread* self = Thread::Current();
369 Runtime* runtime = Runtime::Current();
370 Locks::thread_list_lock_->AssertNotHeld(self);
371 if (desired_level > 0) {
372 if (require_interpreter) {
373 interpreter_stubs_installed_ = true;
374 } else {
375 CHECK(require_entry_exit_stubs);
376 entry_exit_stubs_installed_ = true;
377 }
378 runtime->GetClassLinker()->VisitClasses(InstallStubsClassVisitor, this);
379 instrumentation_stubs_installed_ = true;
380 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
381 runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this);
382 } else {
383 interpreter_stubs_installed_ = false;
384 entry_exit_stubs_installed_ = false;
385 runtime->GetClassLinker()->VisitClasses(InstallStubsClassVisitor, this);
386 instrumentation_stubs_installed_ = false;
387 MutexLock mu(self, *Locks::thread_list_lock_);
388 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
389 }
jeffhao725a9572012-11-13 18:20:12 -0800390}
391
Brian Carlstromea46f952013-07-30 01:26:50 -0700392void Instrumentation::UpdateMethodsCode(mirror::ArtMethod* method, const void* code) const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800393 if (LIKELY(!instrumentation_stubs_installed_)) {
Jeff Haoaa4a7932013-05-13 11:28:27 -0700394 method->SetEntryPointFromCompiledCode(code);
Jeff Hao65d15d92013-07-16 16:39:33 -0700395 } else {
396 if (!interpreter_stubs_installed_ || method->IsNative()) {
Ian Rogers848871b2013-08-05 10:56:33 -0700397 method->SetEntryPointFromCompiledCode(GetQuickInstrumentationEntryPoint());
Jeff Hao65d15d92013-07-16 16:39:33 -0700398 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700399 method->SetEntryPointFromCompiledCode(GetCompiledCodeToInterpreterBridge());
Jeff Hao65d15d92013-07-16 16:39:33 -0700400 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800401 }
jeffhao725a9572012-11-13 18:20:12 -0800402}
403
Brian Carlstromea46f952013-07-30 01:26:50 -0700404const void* Instrumentation::GetQuickCodeFor(const mirror::ArtMethod* method) const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800405 Runtime* runtime = Runtime::Current();
406 if (LIKELY(!instrumentation_stubs_installed_)) {
Jeff Haoaa4a7932013-05-13 11:28:27 -0700407 const void* code = method->GetEntryPointFromCompiledCode();
Ian Rogers62d6c772013-02-27 08:32:07 -0800408 DCHECK(code != NULL);
Ian Rogers848871b2013-08-05 10:56:33 -0700409 if (LIKELY(code != GetQuickResolutionTrampoline(runtime->GetClassLinker()) &&
410 code != GetQuickToInterpreterBridge())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800411 return code;
412 }
413 }
414 return runtime->GetClassLinker()->GetOatCodeFor(method);
jeffhao725a9572012-11-13 18:20:12 -0800415}
416
Ian Rogers62d6c772013-02-27 08:32:07 -0800417void Instrumentation::MethodEnterEventImpl(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700418 const mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800419 uint32_t dex_pc) const {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700420 auto it = method_entry_listeners_.begin();
Jeff Hao65d15d92013-07-16 16:39:33 -0700421 bool is_end = (it == method_entry_listeners_.end());
422 // Implemented this way to prevent problems caused by modification of the list while iterating.
423 while (!is_end) {
424 InstrumentationListener* cur = *it;
425 ++it;
426 is_end = (it == method_entry_listeners_.end());
427 cur->MethodEntered(thread, this_object, method, dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800428 }
429}
430
431void Instrumentation::MethodExitEventImpl(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700432 const mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800433 uint32_t dex_pc, const JValue& return_value) const {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700434 auto it = method_exit_listeners_.begin();
Jeff Hao65d15d92013-07-16 16:39:33 -0700435 bool is_end = (it == method_exit_listeners_.end());
436 // Implemented this way to prevent problems caused by modification of the list while iterating.
437 while (!is_end) {
438 InstrumentationListener* cur = *it;
439 ++it;
440 is_end = (it == method_exit_listeners_.end());
441 cur->MethodExited(thread, this_object, method, dex_pc, return_value);
Ian Rogers62d6c772013-02-27 08:32:07 -0800442 }
443}
444
445void Instrumentation::MethodUnwindEvent(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700446 const mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800447 uint32_t dex_pc) const {
448 if (have_method_unwind_listeners_) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700449 for (InstrumentationListener* listener : method_unwind_listeners_) {
450 listener->MethodUnwind(thread, method, dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800451 }
452 }
453}
454
455void Instrumentation::DexPcMovedEventImpl(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700456 const mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800457 uint32_t dex_pc) const {
458 // TODO: STL copy-on-write collection? The copy below is due to the debug listener having an
459 // action where it can remove itself as a listener and break the iterator. The copy only works
460 // around the problem and in general we may have to move to something like reference counting to
461 // ensure listeners are deleted correctly.
462 std::list<InstrumentationListener*> copy(dex_pc_listeners_);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700463 for (InstrumentationListener* listener : copy) {
464 listener->DexPcMoved(thread, this_object, method, dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800465 }
466}
467
468void Instrumentation::ExceptionCaughtEvent(Thread* thread, const ThrowLocation& throw_location,
Brian Carlstromea46f952013-07-30 01:26:50 -0700469 mirror::ArtMethod* catch_method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800470 uint32_t catch_dex_pc,
Sebastien Hertz947ff082013-09-17 14:10:13 +0200471 mirror::Throwable* exception_object) const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800472 if (have_exception_caught_listeners_) {
Jeff Haoc0bd4da2013-04-11 15:52:28 -0700473 DCHECK_EQ(thread->GetException(NULL), exception_object);
474 thread->ClearException();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700475 for (InstrumentationListener* listener : exception_caught_listeners_) {
476 listener->ExceptionCaught(thread, throw_location, catch_method, catch_dex_pc, exception_object);
Ian Rogers62d6c772013-02-27 08:32:07 -0800477 }
Jeff Haoc0bd4da2013-04-11 15:52:28 -0700478 thread->SetException(throw_location, exception_object);
Ian Rogers62d6c772013-02-27 08:32:07 -0800479 }
480}
481
482static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame,
483 int delta)
484 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
485 size_t frame_id = StackVisitor::ComputeNumFrames(self) + delta;
486 if (frame_id != instrumentation_frame.frame_id_) {
487 LOG(ERROR) << "Expected frame_id=" << frame_id << " but found "
488 << instrumentation_frame.frame_id_;
489 StackVisitor::DescribeStack(self);
490 CHECK_EQ(frame_id, instrumentation_frame.frame_id_);
491 }
492}
493
494void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700495 mirror::ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -0700496 uintptr_t lr, bool interpreter_entry) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800497 // We have a callee-save frame meaning this value is guaranteed to never be 0.
498 size_t frame_id = StackVisitor::ComputeNumFrames(self);
499 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
500 if (kVerboseInstrumentation) {
Brian Carlstrom2d888622013-07-18 17:02:00 -0700501 LOG(INFO) << "Entering " << PrettyMethod(method) << " from PC " << reinterpret_cast<void*>(lr);
Ian Rogers62d6c772013-02-27 08:32:07 -0800502 }
503 instrumentation::InstrumentationStackFrame instrumentation_frame(this_object, method, lr,
Jeff Hao9a916d32013-06-27 18:45:37 -0700504 frame_id, interpreter_entry);
Ian Rogers62d6c772013-02-27 08:32:07 -0800505 stack->push_front(instrumentation_frame);
506
507 MethodEnterEvent(self, this_object, method, 0);
508}
509
510uint64_t Instrumentation::PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc,
511 uint64_t gpr_result, uint64_t fpr_result) {
512 // Do the pop.
513 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
514 CHECK_GT(stack->size(), 0U);
515 InstrumentationStackFrame instrumentation_frame = stack->front();
516 stack->pop_front();
517
518 // Set return PC and check the sanity of the stack.
519 *return_pc = instrumentation_frame.return_pc_;
520 CheckStackDepth(self, instrumentation_frame, 0);
521
Brian Carlstromea46f952013-07-30 01:26:50 -0700522 mirror::ArtMethod* method = instrumentation_frame.method_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800523 char return_shorty = MethodHelper(method).GetShorty()[0];
524 JValue return_value;
525 if (return_shorty == 'V') {
526 return_value.SetJ(0);
527 } else if (return_shorty == 'F' || return_shorty == 'D') {
528 return_value.SetJ(fpr_result);
529 } else {
530 return_value.SetJ(gpr_result);
531 }
532 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
533 // return_pc.
534 uint32_t dex_pc = DexFile::kDexNoIndex;
535 mirror::Object* this_object = instrumentation_frame.this_object_;
536 MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value);
jeffhao725a9572012-11-13 18:20:12 -0800537
Ian Rogers62d6c772013-02-27 08:32:07 -0800538 bool deoptimize = false;
539 if (interpreter_stubs_installed_) {
540 // Deoptimize unless we're returning to an upcall.
541 NthCallerVisitor visitor(self, 1, true);
542 visitor.WalkStack(true);
543 deoptimize = visitor.caller != NULL;
544 if (deoptimize && kVerboseInstrumentation) {
545 LOG(INFO) << "Deoptimizing into " << PrettyMethod(visitor.caller);
546 }
547 }
548 if (deoptimize) {
549 if (kVerboseInstrumentation) {
550 LOG(INFO) << "Deoptimizing from " << PrettyMethod(method)
551 << " result is " << std::hex << return_value.GetJ();
552 }
553 self->SetDeoptimizationReturnValue(return_value);
Ian Rogers848871b2013-08-05 10:56:33 -0700554 return static_cast<uint64_t>(GetQuickDeoptimizationEntryPoint()) |
Ian Rogers62d6c772013-02-27 08:32:07 -0800555 (static_cast<uint64_t>(*return_pc) << 32);
556 } else {
557 if (kVerboseInstrumentation) {
Brian Carlstrom2d888622013-07-18 17:02:00 -0700558 LOG(INFO) << "Returning from " << PrettyMethod(method)
559 << " to PC " << reinterpret_cast<void*>(*return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800560 }
561 return *return_pc;
562 }
jeffhao725a9572012-11-13 18:20:12 -0800563}
564
Ian Rogers62d6c772013-02-27 08:32:07 -0800565void Instrumentation::PopMethodForUnwind(Thread* self, bool is_deoptimization) const {
566 // Do the pop.
567 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
568 CHECK_GT(stack->size(), 0U);
569 InstrumentationStackFrame instrumentation_frame = stack->front();
570 // TODO: bring back CheckStackDepth(self, instrumentation_frame, 2);
571 stack->pop_front();
572
Brian Carlstromea46f952013-07-30 01:26:50 -0700573 mirror::ArtMethod* method = instrumentation_frame.method_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800574 if (is_deoptimization) {
575 if (kVerboseInstrumentation) {
576 LOG(INFO) << "Popping for deoptimization " << PrettyMethod(method);
577 }
578 } else {
579 if (kVerboseInstrumentation) {
580 LOG(INFO) << "Popping for unwind " << PrettyMethod(method);
581 }
582
583 // Notify listeners of method unwind.
584 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
585 // return_pc.
586 uint32_t dex_pc = DexFile::kDexNoIndex;
587 MethodUnwindEvent(self, instrumentation_frame.this_object_, method, dex_pc);
588 }
589}
590
591std::string InstrumentationStackFrame::Dump() const {
592 std::ostringstream os;
593 os << "Frame " << frame_id_ << " " << PrettyMethod(method_) << ":"
594 << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_);
595 return os.str();
596}
597
598} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -0800599} // namespace art