blob: 4070324fafb06a7fb6e552b1a7dbebd381febe9b [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 Rogersfa824272013-11-05 16:12:57 -080042
43extern void SetQuickAllocEntryPointsInstrumented(bool instrumented);
44
Ian Rogers62d6c772013-02-27 08:32:07 -080045namespace instrumentation {
jeffhao725a9572012-11-13 18:20:12 -080046
Ian Rogers816432e2013-09-06 15:47:45 -070047// Do we want to deoptimize for method entry and exit listeners or just try to intercept
48// invocations? Deoptimization forces all code to run in the interpreter and considerably hurts the
49// application's performance.
Ian Rogers7b6da362013-09-11 09:29:40 -070050static constexpr bool kDeoptimizeForAccurateMethodEntryExitListeners = false;
Ian Rogers816432e2013-09-06 15:47:45 -070051
Ian Rogers62d6c772013-02-27 08:32:07 -080052static bool InstallStubsClassVisitor(mirror::Class* klass, void* arg)
jeffhao725a9572012-11-13 18:20:12 -080053 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -080054 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
55 return instrumentation->InstallStubsForClass(klass);
56}
57
58bool Instrumentation::InstallStubsForClass(mirror::Class* klass) {
59 bool uninstall = !entry_exit_stubs_installed_ && !interpreter_stubs_installed_;
60 ClassLinker* class_linker = NULL;
61 if (uninstall) {
62 class_linker = Runtime::Current()->GetClassLinker();
63 }
64 bool is_initialized = klass->IsInitialized();
jeffhao725a9572012-11-13 18:20:12 -080065 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -070066 mirror::ArtMethod* method = klass->GetDirectMethod(i);
Ian Rogers62d6c772013-02-27 08:32:07 -080067 if (!method->IsAbstract()) {
68 const void* new_code;
69 if (uninstall) {
Jeff Hao65d15d92013-07-16 16:39:33 -070070 if (forced_interpret_only_ && !method->IsNative() && !method->IsProxyMethod()) {
Ian Rogers848871b2013-08-05 10:56:33 -070071 new_code = GetCompiledCodeToInterpreterBridge();
Jeff Hao65d15d92013-07-16 16:39:33 -070072 } else if (is_initialized || !method->IsStatic() || method->IsConstructor()) {
Ian Rogers62d6c772013-02-27 08:32:07 -080073 new_code = class_linker->GetOatCodeFor(method);
74 } else {
Jeff Hao0aba0ba2013-06-03 14:49:28 -070075 new_code = GetResolutionTrampoline(class_linker);
Ian Rogers62d6c772013-02-27 08:32:07 -080076 }
77 } else { // !uninstall
78 if (!interpreter_stubs_installed_ || method->IsNative()) {
Ian Rogers848871b2013-08-05 10:56:33 -070079 new_code = GetQuickInstrumentationEntryPoint();
Ian Rogers62d6c772013-02-27 08:32:07 -080080 } else {
Ian Rogers848871b2013-08-05 10:56:33 -070081 new_code = GetCompiledCodeToInterpreterBridge();
Ian Rogers62d6c772013-02-27 08:32:07 -080082 }
83 }
Jeff Haoaa4a7932013-05-13 11:28:27 -070084 method->SetEntryPointFromCompiledCode(new_code);
jeffhao725a9572012-11-13 18:20:12 -080085 }
86 }
jeffhao725a9572012-11-13 18:20:12 -080087 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -070088 mirror::ArtMethod* method = klass->GetVirtualMethod(i);
Ian Rogers62d6c772013-02-27 08:32:07 -080089 if (!method->IsAbstract()) {
90 const void* new_code;
91 if (uninstall) {
Jeff Hao65d15d92013-07-16 16:39:33 -070092 if (forced_interpret_only_ && !method->IsNative() && !method->IsProxyMethod()) {
Ian Rogers848871b2013-08-05 10:56:33 -070093 new_code = GetCompiledCodeToInterpreterBridge();
Jeff Hao65d15d92013-07-16 16:39:33 -070094 } else {
95 new_code = class_linker->GetOatCodeFor(method);
96 }
Ian Rogers62d6c772013-02-27 08:32:07 -080097 } else { // !uninstall
98 if (!interpreter_stubs_installed_ || method->IsNative()) {
Ian Rogers848871b2013-08-05 10:56:33 -070099 new_code = GetQuickInstrumentationEntryPoint();
Ian Rogers62d6c772013-02-27 08:32:07 -0800100 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700101 new_code = GetCompiledCodeToInterpreterBridge();
Ian Rogers62d6c772013-02-27 08:32:07 -0800102 }
103 }
Jeff Haoaa4a7932013-05-13 11:28:27 -0700104 method->SetEntryPointFromCompiledCode(new_code);
jeffhao725a9572012-11-13 18:20:12 -0800105 }
106 }
107 return true;
108}
109
Ian Rogers62d6c772013-02-27 08:32:07 -0800110// Places the instrumentation exit pc as the return PC for every quick frame. This also allows
111// deoptimization of quick frames to interpreter frames.
112static void InstrumentationInstallStack(Thread* thread, void* arg)
Ian Rogers306057f2012-11-26 12:45:53 -0800113 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
114 struct InstallStackVisitor : public StackVisitor {
Ian Rogers62d6c772013-02-27 08:32:07 -0800115 InstallStackVisitor(Thread* thread, Context* context, uintptr_t instrumentation_exit_pc)
116 : StackVisitor(thread, context), instrumentation_stack_(thread->GetInstrumentationStack()),
117 instrumentation_exit_pc_(instrumentation_exit_pc), last_return_pc_(0) {}
jeffhao725a9572012-11-13 18:20:12 -0800118
Ian Rogers306057f2012-11-26 12:45:53 -0800119 virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700120 mirror::ArtMethod* m = GetMethod();
Ian Rogers306057f2012-11-26 12:45:53 -0800121 if (GetCurrentQuickFrame() == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800122 if (kVerboseInstrumentation) {
123 LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId()
124 << " Method=" << PrettyMethod(m);
125 }
Ian Rogers306057f2012-11-26 12:45:53 -0800126 return true; // Ignore shadow frames.
127 }
Ian Rogers306057f2012-11-26 12:45:53 -0800128 if (m == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800129 if (kVerboseInstrumentation) {
130 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
131 }
132 last_return_pc_ = 0;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700133 return true; // Ignore upcalls.
Ian Rogers306057f2012-11-26 12:45:53 -0800134 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800135 if (m->IsRuntimeMethod()) {
136 if (kVerboseInstrumentation) {
137 LOG(INFO) << " Skipping runtime method. Frame " << GetFrameId();
138 }
139 last_return_pc_ = GetReturnPc();
Ian Rogers306057f2012-11-26 12:45:53 -0800140 return true; // Ignore unresolved methods since they will be instrumented after resolution.
141 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800142 if (kVerboseInstrumentation) {
143 LOG(INFO) << " Installing exit stub in " << DescribeLocation();
144 }
145 uintptr_t return_pc = GetReturnPc();
146 CHECK_NE(return_pc, instrumentation_exit_pc_);
147 CHECK_NE(return_pc, 0U);
Jeff Hao9a916d32013-06-27 18:45:37 -0700148 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, return_pc, GetFrameId(),
149 false);
Ian Rogers62d6c772013-02-27 08:32:07 -0800150 if (kVerboseInstrumentation) {
151 LOG(INFO) << "Pushing frame " << instrumentation_frame.Dump();
152 }
153 instrumentation_stack_->push_back(instrumentation_frame);
154 dex_pcs_.push_back(m->ToDexPc(last_return_pc_));
Ian Rogers306057f2012-11-26 12:45:53 -0800155 SetReturnPc(instrumentation_exit_pc_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800156 last_return_pc_ = return_pc;
Ian Rogers306057f2012-11-26 12:45:53 -0800157 return true; // Continue.
158 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800159 std::deque<InstrumentationStackFrame>* const instrumentation_stack_;
160 std::vector<uint32_t> dex_pcs_;
Ian Rogers306057f2012-11-26 12:45:53 -0800161 const uintptr_t instrumentation_exit_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800162 uintptr_t last_return_pc_;
Ian Rogers306057f2012-11-26 12:45:53 -0800163 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800164 if (kVerboseInstrumentation) {
165 std::string thread_name;
166 thread->GetThreadName(thread_name);
167 LOG(INFO) << "Installing exit stubs in " << thread_name;
Ian Rogers306057f2012-11-26 12:45:53 -0800168 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800169 UniquePtr<Context> context(Context::Create());
Ian Rogers848871b2013-08-05 10:56:33 -0700170 uintptr_t instrumentation_exit_pc = GetQuickInstrumentationExitPc();
Ian Rogers62d6c772013-02-27 08:32:07 -0800171 InstallStackVisitor visitor(thread, context.get(), instrumentation_exit_pc);
172 visitor.WalkStack(true);
173
174 // Create method enter events for all methods current on the thread's stack.
175 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
176 typedef std::deque<InstrumentationStackFrame>::const_reverse_iterator It;
177 for (It it = thread->GetInstrumentationStack()->rbegin(),
178 end = thread->GetInstrumentationStack()->rend(); it != end; ++it) {
179 mirror::Object* this_object = (*it).this_object_;
Brian Carlstromea46f952013-07-30 01:26:50 -0700180 mirror::ArtMethod* method = (*it).method_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800181 uint32_t dex_pc = visitor.dex_pcs_.back();
182 visitor.dex_pcs_.pop_back();
183 instrumentation->MethodEnterEvent(thread, this_object, method, dex_pc);
184 }
185 thread->VerifyStack();
Ian Rogers306057f2012-11-26 12:45:53 -0800186}
187
Ian Rogers62d6c772013-02-27 08:32:07 -0800188// Removes the instrumentation exit pc as the return PC for every quick frame.
189static void InstrumentationRestoreStack(Thread* thread, void* arg)
Ian Rogers306057f2012-11-26 12:45:53 -0800190 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
191 struct RestoreStackVisitor : public StackVisitor {
Ian Rogers62d6c772013-02-27 08:32:07 -0800192 RestoreStackVisitor(Thread* thread, uintptr_t instrumentation_exit_pc,
193 Instrumentation* instrumentation)
194 : StackVisitor(thread, NULL), thread_(thread),
195 instrumentation_exit_pc_(instrumentation_exit_pc),
196 instrumentation_(instrumentation),
197 instrumentation_stack_(thread->GetInstrumentationStack()),
198 frames_removed_(0) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800199
200 virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800201 if (instrumentation_stack_->size() == 0) {
jeffhao725a9572012-11-13 18:20:12 -0800202 return false; // Stop.
203 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700204 mirror::ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800205 if (GetCurrentQuickFrame() == NULL) {
206 if (kVerboseInstrumentation) {
207 LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId() << " Method=" << PrettyMethod(m);
208 }
209 return true; // Ignore shadow frames.
210 }
Ian Rogers306057f2012-11-26 12:45:53 -0800211 if (m == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800212 if (kVerboseInstrumentation) {
213 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
214 }
Ian Rogers306057f2012-11-26 12:45:53 -0800215 return true; // Ignore upcalls.
216 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800217 bool removed_stub = false;
218 // TODO: make this search more efficient?
Mathieu Chartier02e25112013-08-14 16:14:24 -0700219 for (InstrumentationStackFrame instrumentation_frame : *instrumentation_stack_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800220 if (instrumentation_frame.frame_id_ == GetFrameId()) {
221 if (kVerboseInstrumentation) {
222 LOG(INFO) << " Removing exit stub in " << DescribeLocation();
223 }
Jeff Hao9a916d32013-06-27 18:45:37 -0700224 if (instrumentation_frame.interpreter_entry_) {
225 CHECK(m == Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
226 } else {
227 CHECK(m == instrumentation_frame.method_) << PrettyMethod(m);
228 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800229 SetReturnPc(instrumentation_frame.return_pc_);
230 // Create the method exit events. As the methods didn't really exit the result is 0.
231 instrumentation_->MethodExitEvent(thread_, instrumentation_frame.this_object_, m,
232 GetDexPc(), JValue());
233 frames_removed_++;
234 removed_stub = true;
235 break;
236 }
237 }
238 if (!removed_stub) {
239 if (kVerboseInstrumentation) {
240 LOG(INFO) << " No exit stub in " << DescribeLocation();
Ian Rogers306057f2012-11-26 12:45:53 -0800241 }
jeffhao725a9572012-11-13 18:20:12 -0800242 }
243 return true; // Continue.
244 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800245 Thread* const thread_;
Ian Rogers306057f2012-11-26 12:45:53 -0800246 const uintptr_t instrumentation_exit_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800247 Instrumentation* const instrumentation_;
248 std::deque<instrumentation::InstrumentationStackFrame>* const instrumentation_stack_;
249 size_t frames_removed_;
jeffhao725a9572012-11-13 18:20:12 -0800250 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800251 if (kVerboseInstrumentation) {
252 std::string thread_name;
253 thread->GetThreadName(thread_name);
254 LOG(INFO) << "Removing exit stubs in " << thread_name;
255 }
256 std::deque<instrumentation::InstrumentationStackFrame>* stack = thread->GetInstrumentationStack();
257 if (stack->size() > 0) {
258 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers848871b2013-08-05 10:56:33 -0700259 uintptr_t instrumentation_exit_pc = GetQuickInstrumentationExitPc();
Ian Rogers62d6c772013-02-27 08:32:07 -0800260 RestoreStackVisitor visitor(thread, instrumentation_exit_pc, instrumentation);
261 visitor.WalkStack(true);
262 CHECK_EQ(visitor.frames_removed_, stack->size());
263 while (stack->size() > 0) {
264 stack->pop_front();
265 }
jeffhao725a9572012-11-13 18:20:12 -0800266 }
267}
268
Ian Rogers62d6c772013-02-27 08:32:07 -0800269void Instrumentation::AddListener(InstrumentationListener* listener, uint32_t events) {
270 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
271 bool require_entry_exit_stubs = false;
272 bool require_interpreter = false;
273 if ((events & kMethodEntered) != 0) {
274 method_entry_listeners_.push_back(listener);
Ian Rogers816432e2013-09-06 15:47:45 -0700275 require_interpreter = kDeoptimizeForAccurateMethodEntryExitListeners;
276 require_entry_exit_stubs = !kDeoptimizeForAccurateMethodEntryExitListeners;
Ian Rogers62d6c772013-02-27 08:32:07 -0800277 have_method_entry_listeners_ = true;
278 }
279 if ((events & kMethodExited) != 0) {
280 method_exit_listeners_.push_back(listener);
Ian Rogers816432e2013-09-06 15:47:45 -0700281 require_interpreter = kDeoptimizeForAccurateMethodEntryExitListeners;
282 require_entry_exit_stubs = !kDeoptimizeForAccurateMethodEntryExitListeners;
Ian Rogers62d6c772013-02-27 08:32:07 -0800283 have_method_exit_listeners_ = true;
284 }
285 if ((events & kMethodUnwind) != 0) {
286 method_unwind_listeners_.push_back(listener);
287 have_method_unwind_listeners_ = true;
288 }
289 if ((events & kDexPcMoved) != 0) {
290 dex_pc_listeners_.push_back(listener);
291 require_interpreter = true;
292 have_dex_pc_listeners_ = true;
293 }
Jeff Hao14dd5a82013-04-11 10:23:36 -0700294 if ((events & kExceptionCaught) != 0) {
295 exception_caught_listeners_.push_back(listener);
296 have_exception_caught_listeners_ = true;
297 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800298 ConfigureStubs(require_entry_exit_stubs, require_interpreter);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200299 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800300}
301
Ian Rogers62d6c772013-02-27 08:32:07 -0800302void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) {
303 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
304 bool require_entry_exit_stubs = false;
305 bool require_interpreter = false;
306
307 if ((events & kMethodEntered) != 0) {
308 bool contains = std::find(method_entry_listeners_.begin(), method_entry_listeners_.end(),
309 listener) != method_entry_listeners_.end();
310 if (contains) {
311 method_entry_listeners_.remove(listener);
312 }
313 have_method_entry_listeners_ = method_entry_listeners_.size() > 0;
Ian Rogers816432e2013-09-06 15:47:45 -0700314 require_entry_exit_stubs |= have_method_entry_listeners_ &&
315 !kDeoptimizeForAccurateMethodEntryExitListeners;
316 require_interpreter = have_method_entry_listeners_ &&
317 kDeoptimizeForAccurateMethodEntryExitListeners;
Ian Rogers62d6c772013-02-27 08:32:07 -0800318 }
319 if ((events & kMethodExited) != 0) {
320 bool contains = std::find(method_exit_listeners_.begin(), method_exit_listeners_.end(),
321 listener) != method_exit_listeners_.end();
322 if (contains) {
323 method_exit_listeners_.remove(listener);
324 }
325 have_method_exit_listeners_ = method_exit_listeners_.size() > 0;
Ian Rogers816432e2013-09-06 15:47:45 -0700326 require_entry_exit_stubs |= have_method_exit_listeners_ &&
327 !kDeoptimizeForAccurateMethodEntryExitListeners;
328 require_interpreter = have_method_exit_listeners_ &&
329 kDeoptimizeForAccurateMethodEntryExitListeners;
Ian Rogers62d6c772013-02-27 08:32:07 -0800330 }
331 if ((events & kMethodUnwind) != 0) {
332 method_unwind_listeners_.remove(listener);
333 }
334 if ((events & kDexPcMoved) != 0) {
335 bool contains = std::find(dex_pc_listeners_.begin(), dex_pc_listeners_.end(),
336 listener) != dex_pc_listeners_.end();
337 if (contains) {
338 dex_pc_listeners_.remove(listener);
339 }
340 have_dex_pc_listeners_ = dex_pc_listeners_.size() > 0;
341 require_interpreter |= have_dex_pc_listeners_;
342 }
Jeff Hao14dd5a82013-04-11 10:23:36 -0700343 if ((events & kExceptionCaught) != 0) {
344 exception_caught_listeners_.remove(listener);
345 have_exception_caught_listeners_ = exception_caught_listeners_.size() > 0;
346 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800347 ConfigureStubs(require_entry_exit_stubs, require_interpreter);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200348 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800349}
350
Ian Rogers62d6c772013-02-27 08:32:07 -0800351void Instrumentation::ConfigureStubs(bool require_entry_exit_stubs, bool require_interpreter) {
352 interpret_only_ = require_interpreter || forced_interpret_only_;
353 // Compute what level of instrumentation is required and compare to current.
354 int desired_level, current_level;
355 if (require_interpreter) {
356 desired_level = 2;
357 } else if (require_entry_exit_stubs) {
358 desired_level = 1;
359 } else {
360 desired_level = 0;
361 }
362 if (interpreter_stubs_installed_) {
363 current_level = 2;
364 } else if (entry_exit_stubs_installed_) {
365 current_level = 1;
366 } else {
367 current_level = 0;
368 }
369 if (desired_level == current_level) {
370 // We're already set.
371 return;
372 }
373 Thread* self = Thread::Current();
374 Runtime* runtime = Runtime::Current();
375 Locks::thread_list_lock_->AssertNotHeld(self);
376 if (desired_level > 0) {
377 if (require_interpreter) {
378 interpreter_stubs_installed_ = true;
379 } else {
380 CHECK(require_entry_exit_stubs);
381 entry_exit_stubs_installed_ = true;
382 }
383 runtime->GetClassLinker()->VisitClasses(InstallStubsClassVisitor, this);
384 instrumentation_stubs_installed_ = true;
385 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
386 runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this);
387 } else {
388 interpreter_stubs_installed_ = false;
389 entry_exit_stubs_installed_ = false;
390 runtime->GetClassLinker()->VisitClasses(InstallStubsClassVisitor, this);
391 instrumentation_stubs_installed_ = false;
392 MutexLock mu(self, *Locks::thread_list_lock_);
393 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
394 }
jeffhao725a9572012-11-13 18:20:12 -0800395}
396
Ian Rogersfa824272013-11-05 16:12:57 -0800397static void ResetQuickAllocEntryPointsForThread(Thread* thread, void* arg) {
398 thread->ResetQuickAllocEntryPointsForThread();
399}
400
401void Instrumentation::InstrumentQuickAllocEntryPoints() {
402 // TODO: the read of quick_alloc_entry_points_instrumentation_counter_ is racey and this code
403 // should be guarded by a lock.
404 DCHECK_GE(quick_alloc_entry_points_instrumentation_counter_, 0U);
405 bool enable_instrumentation = (quick_alloc_entry_points_instrumentation_counter_ == 0);
406 quick_alloc_entry_points_instrumentation_counter_++;
407 if (enable_instrumentation) {
408 // Instrumentation wasn't enabled so enable it.
409 SetQuickAllocEntryPointsInstrumented(true);
410 Runtime* runtime = Runtime::Current();
411 if (runtime->IsStarted()) {
412 ThreadList* tl = runtime->GetThreadList();
413 Thread* self = Thread::Current();
414 tl->SuspendAll();
415 {
416 MutexLock mu(self, *Locks::thread_list_lock_);
417 tl->ForEach(ResetQuickAllocEntryPointsForThread, NULL);
418 }
419 tl->ResumeAll();
420 }
421 }
422}
423
424void Instrumentation::UninstrumentQuickAllocEntryPoints() {
425 // TODO: the read of quick_alloc_entry_points_instrumentation_counter_ is racey and this code
426 // should be guarded by a lock.
427 DCHECK_GT(quick_alloc_entry_points_instrumentation_counter_, 0U);
428 quick_alloc_entry_points_instrumentation_counter_--;
429 bool disable_instrumentation = (quick_alloc_entry_points_instrumentation_counter_ == 0);
430 if (disable_instrumentation) {
431 SetQuickAllocEntryPointsInstrumented(false);
432 Runtime* runtime = Runtime::Current();
433 if (runtime->IsStarted()) {
434 ThreadList* tl = Runtime::Current()->GetThreadList();
435 Thread* self = Thread::Current();
436 tl->SuspendAll();
437 {
438 MutexLock mu(self, *Locks::thread_list_lock_);
439 tl->ForEach(ResetQuickAllocEntryPointsForThread, NULL);
440 }
441 tl->ResumeAll();
442 }
443 }
444}
445
Brian Carlstromea46f952013-07-30 01:26:50 -0700446void Instrumentation::UpdateMethodsCode(mirror::ArtMethod* method, const void* code) const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800447 if (LIKELY(!instrumentation_stubs_installed_)) {
Jeff Haoaa4a7932013-05-13 11:28:27 -0700448 method->SetEntryPointFromCompiledCode(code);
Jeff Hao65d15d92013-07-16 16:39:33 -0700449 } else {
450 if (!interpreter_stubs_installed_ || method->IsNative()) {
Ian Rogers848871b2013-08-05 10:56:33 -0700451 method->SetEntryPointFromCompiledCode(GetQuickInstrumentationEntryPoint());
Jeff Hao65d15d92013-07-16 16:39:33 -0700452 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700453 method->SetEntryPointFromCompiledCode(GetCompiledCodeToInterpreterBridge());
Jeff Hao65d15d92013-07-16 16:39:33 -0700454 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800455 }
jeffhao725a9572012-11-13 18:20:12 -0800456}
457
Brian Carlstromea46f952013-07-30 01:26:50 -0700458const void* Instrumentation::GetQuickCodeFor(const mirror::ArtMethod* method) const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800459 Runtime* runtime = Runtime::Current();
460 if (LIKELY(!instrumentation_stubs_installed_)) {
Jeff Haoaa4a7932013-05-13 11:28:27 -0700461 const void* code = method->GetEntryPointFromCompiledCode();
Ian Rogers62d6c772013-02-27 08:32:07 -0800462 DCHECK(code != NULL);
Ian Rogers848871b2013-08-05 10:56:33 -0700463 if (LIKELY(code != GetQuickResolutionTrampoline(runtime->GetClassLinker()) &&
464 code != GetQuickToInterpreterBridge())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800465 return code;
466 }
467 }
468 return runtime->GetClassLinker()->GetOatCodeFor(method);
jeffhao725a9572012-11-13 18:20:12 -0800469}
470
Ian Rogers62d6c772013-02-27 08:32:07 -0800471void Instrumentation::MethodEnterEventImpl(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700472 const mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800473 uint32_t dex_pc) const {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700474 auto it = method_entry_listeners_.begin();
Jeff Hao65d15d92013-07-16 16:39:33 -0700475 bool is_end = (it == method_entry_listeners_.end());
476 // Implemented this way to prevent problems caused by modification of the list while iterating.
477 while (!is_end) {
478 InstrumentationListener* cur = *it;
479 ++it;
480 is_end = (it == method_entry_listeners_.end());
481 cur->MethodEntered(thread, this_object, method, dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800482 }
483}
484
485void Instrumentation::MethodExitEventImpl(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700486 const mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800487 uint32_t dex_pc, const JValue& return_value) const {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700488 auto it = method_exit_listeners_.begin();
Jeff Hao65d15d92013-07-16 16:39:33 -0700489 bool is_end = (it == method_exit_listeners_.end());
490 // Implemented this way to prevent problems caused by modification of the list while iterating.
491 while (!is_end) {
492 InstrumentationListener* cur = *it;
493 ++it;
494 is_end = (it == method_exit_listeners_.end());
495 cur->MethodExited(thread, this_object, method, dex_pc, return_value);
Ian Rogers62d6c772013-02-27 08:32:07 -0800496 }
497}
498
499void Instrumentation::MethodUnwindEvent(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700500 const mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800501 uint32_t dex_pc) const {
502 if (have_method_unwind_listeners_) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700503 for (InstrumentationListener* listener : method_unwind_listeners_) {
504 listener->MethodUnwind(thread, method, dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800505 }
506 }
507}
508
509void Instrumentation::DexPcMovedEventImpl(Thread* thread, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700510 const mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800511 uint32_t dex_pc) const {
512 // TODO: STL copy-on-write collection? The copy below is due to the debug listener having an
513 // action where it can remove itself as a listener and break the iterator. The copy only works
514 // around the problem and in general we may have to move to something like reference counting to
515 // ensure listeners are deleted correctly.
516 std::list<InstrumentationListener*> copy(dex_pc_listeners_);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700517 for (InstrumentationListener* listener : copy) {
518 listener->DexPcMoved(thread, this_object, method, dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800519 }
520}
521
522void Instrumentation::ExceptionCaughtEvent(Thread* thread, const ThrowLocation& throw_location,
Brian Carlstromea46f952013-07-30 01:26:50 -0700523 mirror::ArtMethod* catch_method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800524 uint32_t catch_dex_pc,
Sebastien Hertz947ff082013-09-17 14:10:13 +0200525 mirror::Throwable* exception_object) const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800526 if (have_exception_caught_listeners_) {
Jeff Haoc0bd4da2013-04-11 15:52:28 -0700527 DCHECK_EQ(thread->GetException(NULL), exception_object);
528 thread->ClearException();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700529 for (InstrumentationListener* listener : exception_caught_listeners_) {
530 listener->ExceptionCaught(thread, throw_location, catch_method, catch_dex_pc, exception_object);
Ian Rogers62d6c772013-02-27 08:32:07 -0800531 }
Jeff Haoc0bd4da2013-04-11 15:52:28 -0700532 thread->SetException(throw_location, exception_object);
Ian Rogers62d6c772013-02-27 08:32:07 -0800533 }
534}
535
536static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame,
537 int delta)
538 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
539 size_t frame_id = StackVisitor::ComputeNumFrames(self) + delta;
540 if (frame_id != instrumentation_frame.frame_id_) {
541 LOG(ERROR) << "Expected frame_id=" << frame_id << " but found "
542 << instrumentation_frame.frame_id_;
543 StackVisitor::DescribeStack(self);
544 CHECK_EQ(frame_id, instrumentation_frame.frame_id_);
545 }
546}
547
548void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700549 mirror::ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -0700550 uintptr_t lr, bool interpreter_entry) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800551 // We have a callee-save frame meaning this value is guaranteed to never be 0.
552 size_t frame_id = StackVisitor::ComputeNumFrames(self);
553 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
554 if (kVerboseInstrumentation) {
Brian Carlstrom2d888622013-07-18 17:02:00 -0700555 LOG(INFO) << "Entering " << PrettyMethod(method) << " from PC " << reinterpret_cast<void*>(lr);
Ian Rogers62d6c772013-02-27 08:32:07 -0800556 }
557 instrumentation::InstrumentationStackFrame instrumentation_frame(this_object, method, lr,
Jeff Hao9a916d32013-06-27 18:45:37 -0700558 frame_id, interpreter_entry);
Ian Rogers62d6c772013-02-27 08:32:07 -0800559 stack->push_front(instrumentation_frame);
560
561 MethodEnterEvent(self, this_object, method, 0);
562}
563
564uint64_t Instrumentation::PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc,
565 uint64_t gpr_result, uint64_t fpr_result) {
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 stack->pop_front();
571
572 // Set return PC and check the sanity of the stack.
573 *return_pc = instrumentation_frame.return_pc_;
574 CheckStackDepth(self, instrumentation_frame, 0);
575
Brian Carlstromea46f952013-07-30 01:26:50 -0700576 mirror::ArtMethod* method = instrumentation_frame.method_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800577 char return_shorty = MethodHelper(method).GetShorty()[0];
578 JValue return_value;
579 if (return_shorty == 'V') {
580 return_value.SetJ(0);
581 } else if (return_shorty == 'F' || return_shorty == 'D') {
582 return_value.SetJ(fpr_result);
583 } else {
584 return_value.SetJ(gpr_result);
585 }
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 mirror::Object* this_object = instrumentation_frame.this_object_;
590 MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value);
jeffhao725a9572012-11-13 18:20:12 -0800591
Ian Rogers62d6c772013-02-27 08:32:07 -0800592 bool deoptimize = false;
593 if (interpreter_stubs_installed_) {
594 // Deoptimize unless we're returning to an upcall.
595 NthCallerVisitor visitor(self, 1, true);
596 visitor.WalkStack(true);
597 deoptimize = visitor.caller != NULL;
598 if (deoptimize && kVerboseInstrumentation) {
599 LOG(INFO) << "Deoptimizing into " << PrettyMethod(visitor.caller);
600 }
601 }
602 if (deoptimize) {
603 if (kVerboseInstrumentation) {
604 LOG(INFO) << "Deoptimizing from " << PrettyMethod(method)
605 << " result is " << std::hex << return_value.GetJ();
606 }
607 self->SetDeoptimizationReturnValue(return_value);
Ian Rogers848871b2013-08-05 10:56:33 -0700608 return static_cast<uint64_t>(GetQuickDeoptimizationEntryPoint()) |
Ian Rogers62d6c772013-02-27 08:32:07 -0800609 (static_cast<uint64_t>(*return_pc) << 32);
610 } else {
611 if (kVerboseInstrumentation) {
Brian Carlstrom2d888622013-07-18 17:02:00 -0700612 LOG(INFO) << "Returning from " << PrettyMethod(method)
613 << " to PC " << reinterpret_cast<void*>(*return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800614 }
615 return *return_pc;
616 }
jeffhao725a9572012-11-13 18:20:12 -0800617}
618
Ian Rogers62d6c772013-02-27 08:32:07 -0800619void Instrumentation::PopMethodForUnwind(Thread* self, bool is_deoptimization) const {
620 // Do the pop.
621 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
622 CHECK_GT(stack->size(), 0U);
623 InstrumentationStackFrame instrumentation_frame = stack->front();
624 // TODO: bring back CheckStackDepth(self, instrumentation_frame, 2);
625 stack->pop_front();
626
Brian Carlstromea46f952013-07-30 01:26:50 -0700627 mirror::ArtMethod* method = instrumentation_frame.method_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800628 if (is_deoptimization) {
629 if (kVerboseInstrumentation) {
630 LOG(INFO) << "Popping for deoptimization " << PrettyMethod(method);
631 }
632 } else {
633 if (kVerboseInstrumentation) {
634 LOG(INFO) << "Popping for unwind " << PrettyMethod(method);
635 }
636
637 // Notify listeners of method unwind.
638 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
639 // return_pc.
640 uint32_t dex_pc = DexFile::kDexNoIndex;
641 MethodUnwindEvent(self, instrumentation_frame.this_object_, method, dex_pc);
642 }
643}
644
645std::string InstrumentationStackFrame::Dump() const {
646 std::ostringstream os;
647 os << "Frame " << frame_id_ << " " << PrettyMethod(method_) << ":"
648 << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_);
649 return os.str();
650}
651
652} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -0800653} // namespace art