blob: 8316bc56b3b121b3c3a7358f3ca4710b72b408a0 [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);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200296 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800297}
298
Ian Rogers62d6c772013-02-27 08:32:07 -0800299void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) {
300 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
301 bool require_entry_exit_stubs = false;
302 bool require_interpreter = false;
303
304 if ((events & kMethodEntered) != 0) {
305 bool contains = std::find(method_entry_listeners_.begin(), method_entry_listeners_.end(),
306 listener) != method_entry_listeners_.end();
307 if (contains) {
308 method_entry_listeners_.remove(listener);
309 }
310 have_method_entry_listeners_ = method_entry_listeners_.size() > 0;
Ian Rogers816432e2013-09-06 15:47:45 -0700311 require_entry_exit_stubs |= have_method_entry_listeners_ &&
312 !kDeoptimizeForAccurateMethodEntryExitListeners;
313 require_interpreter = have_method_entry_listeners_ &&
314 kDeoptimizeForAccurateMethodEntryExitListeners;
Ian Rogers62d6c772013-02-27 08:32:07 -0800315 }
316 if ((events & kMethodExited) != 0) {
317 bool contains = std::find(method_exit_listeners_.begin(), method_exit_listeners_.end(),
318 listener) != method_exit_listeners_.end();
319 if (contains) {
320 method_exit_listeners_.remove(listener);
321 }
322 have_method_exit_listeners_ = method_exit_listeners_.size() > 0;
Ian Rogers816432e2013-09-06 15:47:45 -0700323 require_entry_exit_stubs |= have_method_exit_listeners_ &&
324 !kDeoptimizeForAccurateMethodEntryExitListeners;
325 require_interpreter = have_method_exit_listeners_ &&
326 kDeoptimizeForAccurateMethodEntryExitListeners;
Ian Rogers62d6c772013-02-27 08:32:07 -0800327 }
328 if ((events & kMethodUnwind) != 0) {
329 method_unwind_listeners_.remove(listener);
330 }
331 if ((events & kDexPcMoved) != 0) {
332 bool contains = std::find(dex_pc_listeners_.begin(), dex_pc_listeners_.end(),
333 listener) != dex_pc_listeners_.end();
334 if (contains) {
335 dex_pc_listeners_.remove(listener);
336 }
337 have_dex_pc_listeners_ = dex_pc_listeners_.size() > 0;
338 require_interpreter |= have_dex_pc_listeners_;
339 }
Jeff Hao14dd5a82013-04-11 10:23:36 -0700340 if ((events & kExceptionCaught) != 0) {
341 exception_caught_listeners_.remove(listener);
342 have_exception_caught_listeners_ = exception_caught_listeners_.size() > 0;
343 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800344 ConfigureStubs(require_entry_exit_stubs, require_interpreter);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200345 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800346}
347
Ian Rogers62d6c772013-02-27 08:32:07 -0800348void Instrumentation::ConfigureStubs(bool require_entry_exit_stubs, bool require_interpreter) {
349 interpret_only_ = require_interpreter || forced_interpret_only_;
350 // Compute what level of instrumentation is required and compare to current.
351 int desired_level, current_level;
352 if (require_interpreter) {
353 desired_level = 2;
354 } else if (require_entry_exit_stubs) {
355 desired_level = 1;
356 } else {
357 desired_level = 0;
358 }
359 if (interpreter_stubs_installed_) {
360 current_level = 2;
361 } else if (entry_exit_stubs_installed_) {
362 current_level = 1;
363 } else {
364 current_level = 0;
365 }
366 if (desired_level == current_level) {
367 // We're already set.
368 return;
369 }
370 Thread* self = Thread::Current();
371 Runtime* runtime = Runtime::Current();
372 Locks::thread_list_lock_->AssertNotHeld(self);
373 if (desired_level > 0) {
374 if (require_interpreter) {
375 interpreter_stubs_installed_ = true;
376 } else {
377 CHECK(require_entry_exit_stubs);
378 entry_exit_stubs_installed_ = true;
379 }
380 runtime->GetClassLinker()->VisitClasses(InstallStubsClassVisitor, this);
381 instrumentation_stubs_installed_ = true;
382 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
383 runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this);
384 } else {
385 interpreter_stubs_installed_ = false;
386 entry_exit_stubs_installed_ = false;
387 runtime->GetClassLinker()->VisitClasses(InstallStubsClassVisitor, this);
388 instrumentation_stubs_installed_ = false;
389 MutexLock mu(self, *Locks::thread_list_lock_);
390 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
391 }
jeffhao725a9572012-11-13 18:20:12 -0800392}
393
Brian Carlstromea46f952013-07-30 01:26:50 -0700394void Instrumentation::UpdateMethodsCode(mirror::ArtMethod* method, const void* code) const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800395 if (LIKELY(!instrumentation_stubs_installed_)) {
Jeff Haoaa4a7932013-05-13 11:28:27 -0700396 method->SetEntryPointFromCompiledCode(code);
Jeff Hao65d15d92013-07-16 16:39:33 -0700397 } else {
398 if (!interpreter_stubs_installed_ || method->IsNative()) {
Ian Rogers848871b2013-08-05 10:56:33 -0700399 method->SetEntryPointFromCompiledCode(GetQuickInstrumentationEntryPoint());
Jeff Hao65d15d92013-07-16 16:39:33 -0700400 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700401 method->SetEntryPointFromCompiledCode(GetCompiledCodeToInterpreterBridge());
Jeff Hao65d15d92013-07-16 16:39:33 -0700402 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800403 }
jeffhao725a9572012-11-13 18:20:12 -0800404}
405
Brian Carlstromea46f952013-07-30 01:26:50 -0700406const void* Instrumentation::GetQuickCodeFor(const mirror::ArtMethod* method) const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800407 Runtime* runtime = Runtime::Current();
408 if (LIKELY(!instrumentation_stubs_installed_)) {
Jeff Haoaa4a7932013-05-13 11:28:27 -0700409 const void* code = method->GetEntryPointFromCompiledCode();
Ian Rogers62d6c772013-02-27 08:32:07 -0800410 DCHECK(code != NULL);
Ian Rogers848871b2013-08-05 10:56:33 -0700411 if (LIKELY(code != GetQuickResolutionTrampoline(runtime->GetClassLinker()) &&
412 code != GetQuickToInterpreterBridge())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800413 return code;
414 }
415 }
416 return runtime->GetClassLinker()->GetOatCodeFor(method);
jeffhao725a9572012-11-13 18:20:12 -0800417}
418
Ian Rogers62d6c772013-02-27 08:32:07 -0800419void Instrumentation::MethodEnterEventImpl(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700420 const mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800421 uint32_t dex_pc) const {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700422 auto it = method_entry_listeners_.begin();
Jeff Hao65d15d92013-07-16 16:39:33 -0700423 bool is_end = (it == method_entry_listeners_.end());
424 // Implemented this way to prevent problems caused by modification of the list while iterating.
425 while (!is_end) {
426 InstrumentationListener* cur = *it;
427 ++it;
428 is_end = (it == method_entry_listeners_.end());
429 cur->MethodEntered(thread, this_object, method, dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800430 }
431}
432
433void Instrumentation::MethodExitEventImpl(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700434 const mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800435 uint32_t dex_pc, const JValue& return_value) const {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700436 auto it = method_exit_listeners_.begin();
Jeff Hao65d15d92013-07-16 16:39:33 -0700437 bool is_end = (it == method_exit_listeners_.end());
438 // Implemented this way to prevent problems caused by modification of the list while iterating.
439 while (!is_end) {
440 InstrumentationListener* cur = *it;
441 ++it;
442 is_end = (it == method_exit_listeners_.end());
443 cur->MethodExited(thread, this_object, method, dex_pc, return_value);
Ian Rogers62d6c772013-02-27 08:32:07 -0800444 }
445}
446
447void Instrumentation::MethodUnwindEvent(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700448 const mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800449 uint32_t dex_pc) const {
450 if (have_method_unwind_listeners_) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700451 for (InstrumentationListener* listener : method_unwind_listeners_) {
452 listener->MethodUnwind(thread, method, dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800453 }
454 }
455}
456
457void Instrumentation::DexPcMovedEventImpl(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700458 const mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800459 uint32_t dex_pc) const {
460 // TODO: STL copy-on-write collection? The copy below is due to the debug listener having an
461 // action where it can remove itself as a listener and break the iterator. The copy only works
462 // around the problem and in general we may have to move to something like reference counting to
463 // ensure listeners are deleted correctly.
464 std::list<InstrumentationListener*> copy(dex_pc_listeners_);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700465 for (InstrumentationListener* listener : copy) {
466 listener->DexPcMoved(thread, this_object, method, dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800467 }
468}
469
470void Instrumentation::ExceptionCaughtEvent(Thread* thread, const ThrowLocation& throw_location,
Brian Carlstromea46f952013-07-30 01:26:50 -0700471 mirror::ArtMethod* catch_method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800472 uint32_t catch_dex_pc,
Sebastien Hertz947ff082013-09-17 14:10:13 +0200473 mirror::Throwable* exception_object) const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800474 if (have_exception_caught_listeners_) {
Jeff Haoc0bd4da2013-04-11 15:52:28 -0700475 DCHECK_EQ(thread->GetException(NULL), exception_object);
476 thread->ClearException();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700477 for (InstrumentationListener* listener : exception_caught_listeners_) {
478 listener->ExceptionCaught(thread, throw_location, catch_method, catch_dex_pc, exception_object);
Ian Rogers62d6c772013-02-27 08:32:07 -0800479 }
Jeff Haoc0bd4da2013-04-11 15:52:28 -0700480 thread->SetException(throw_location, exception_object);
Ian Rogers62d6c772013-02-27 08:32:07 -0800481 }
482}
483
484static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame,
485 int delta)
486 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
487 size_t frame_id = StackVisitor::ComputeNumFrames(self) + delta;
488 if (frame_id != instrumentation_frame.frame_id_) {
489 LOG(ERROR) << "Expected frame_id=" << frame_id << " but found "
490 << instrumentation_frame.frame_id_;
491 StackVisitor::DescribeStack(self);
492 CHECK_EQ(frame_id, instrumentation_frame.frame_id_);
493 }
494}
495
496void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700497 mirror::ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -0700498 uintptr_t lr, bool interpreter_entry) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800499 // We have a callee-save frame meaning this value is guaranteed to never be 0.
500 size_t frame_id = StackVisitor::ComputeNumFrames(self);
501 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
502 if (kVerboseInstrumentation) {
Brian Carlstrom2d888622013-07-18 17:02:00 -0700503 LOG(INFO) << "Entering " << PrettyMethod(method) << " from PC " << reinterpret_cast<void*>(lr);
Ian Rogers62d6c772013-02-27 08:32:07 -0800504 }
505 instrumentation::InstrumentationStackFrame instrumentation_frame(this_object, method, lr,
Jeff Hao9a916d32013-06-27 18:45:37 -0700506 frame_id, interpreter_entry);
Ian Rogers62d6c772013-02-27 08:32:07 -0800507 stack->push_front(instrumentation_frame);
508
509 MethodEnterEvent(self, this_object, method, 0);
510}
511
512uint64_t Instrumentation::PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc,
513 uint64_t gpr_result, uint64_t fpr_result) {
514 // Do the pop.
515 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
516 CHECK_GT(stack->size(), 0U);
517 InstrumentationStackFrame instrumentation_frame = stack->front();
518 stack->pop_front();
519
520 // Set return PC and check the sanity of the stack.
521 *return_pc = instrumentation_frame.return_pc_;
522 CheckStackDepth(self, instrumentation_frame, 0);
523
Brian Carlstromea46f952013-07-30 01:26:50 -0700524 mirror::ArtMethod* method = instrumentation_frame.method_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800525 char return_shorty = MethodHelper(method).GetShorty()[0];
526 JValue return_value;
527 if (return_shorty == 'V') {
528 return_value.SetJ(0);
529 } else if (return_shorty == 'F' || return_shorty == 'D') {
530 return_value.SetJ(fpr_result);
531 } else {
532 return_value.SetJ(gpr_result);
533 }
534 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
535 // return_pc.
536 uint32_t dex_pc = DexFile::kDexNoIndex;
537 mirror::Object* this_object = instrumentation_frame.this_object_;
538 MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value);
jeffhao725a9572012-11-13 18:20:12 -0800539
Ian Rogers62d6c772013-02-27 08:32:07 -0800540 bool deoptimize = false;
541 if (interpreter_stubs_installed_) {
542 // Deoptimize unless we're returning to an upcall.
543 NthCallerVisitor visitor(self, 1, true);
544 visitor.WalkStack(true);
545 deoptimize = visitor.caller != NULL;
546 if (deoptimize && kVerboseInstrumentation) {
547 LOG(INFO) << "Deoptimizing into " << PrettyMethod(visitor.caller);
548 }
549 }
550 if (deoptimize) {
551 if (kVerboseInstrumentation) {
552 LOG(INFO) << "Deoptimizing from " << PrettyMethod(method)
553 << " result is " << std::hex << return_value.GetJ();
554 }
555 self->SetDeoptimizationReturnValue(return_value);
Ian Rogers848871b2013-08-05 10:56:33 -0700556 return static_cast<uint64_t>(GetQuickDeoptimizationEntryPoint()) |
Ian Rogers62d6c772013-02-27 08:32:07 -0800557 (static_cast<uint64_t>(*return_pc) << 32);
558 } else {
559 if (kVerboseInstrumentation) {
Brian Carlstrom2d888622013-07-18 17:02:00 -0700560 LOG(INFO) << "Returning from " << PrettyMethod(method)
561 << " to PC " << reinterpret_cast<void*>(*return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800562 }
563 return *return_pc;
564 }
jeffhao725a9572012-11-13 18:20:12 -0800565}
566
Ian Rogers62d6c772013-02-27 08:32:07 -0800567void Instrumentation::PopMethodForUnwind(Thread* self, bool is_deoptimization) const {
568 // Do the pop.
569 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
570 CHECK_GT(stack->size(), 0U);
571 InstrumentationStackFrame instrumentation_frame = stack->front();
572 // TODO: bring back CheckStackDepth(self, instrumentation_frame, 2);
573 stack->pop_front();
574
Brian Carlstromea46f952013-07-30 01:26:50 -0700575 mirror::ArtMethod* method = instrumentation_frame.method_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800576 if (is_deoptimization) {
577 if (kVerboseInstrumentation) {
578 LOG(INFO) << "Popping for deoptimization " << PrettyMethod(method);
579 }
580 } else {
581 if (kVerboseInstrumentation) {
582 LOG(INFO) << "Popping for unwind " << PrettyMethod(method);
583 }
584
585 // Notify listeners of method unwind.
586 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
587 // return_pc.
588 uint32_t dex_pc = DexFile::kDexNoIndex;
589 MethodUnwindEvent(self, instrumentation_frame.this_object_, method, dex_pc);
590 }
591}
592
593std::string InstrumentationStackFrame::Dump() const {
594 std::ostringstream os;
595 os << "Frame " << frame_id_ << " " << PrettyMethod(method_) << ":"
596 << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_);
597 return os.str();
598}
599
600} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -0800601} // namespace art