blob: 982af29bf01851c3e266848fc88cff33940ed49c [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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "mirror/class-inl.h"
27#include "mirror/dex_cache.h"
28#include "mirror/abstract_method-inl.h"
29#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)
jeffhao725a9572012-11-13 18:20:12 -080033#include "oat/runtime/oat_support_entrypoints.h"
34#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 Rogers62d6c772013-02-27 08:32:07 -080044static bool InstallStubsClassVisitor(mirror::Class* klass, void* arg)
jeffhao725a9572012-11-13 18:20:12 -080045 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -080046 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
47 return instrumentation->InstallStubsForClass(klass);
48}
49
50bool Instrumentation::InstallStubsForClass(mirror::Class* klass) {
51 bool uninstall = !entry_exit_stubs_installed_ && !interpreter_stubs_installed_;
52 ClassLinker* class_linker = NULL;
53 if (uninstall) {
54 class_linker = Runtime::Current()->GetClassLinker();
55 }
56 bool is_initialized = klass->IsInitialized();
jeffhao725a9572012-11-13 18:20:12 -080057 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080058 mirror::AbstractMethod* method = klass->GetDirectMethod(i);
Ian Rogers62d6c772013-02-27 08:32:07 -080059 if (!method->IsAbstract()) {
60 const void* new_code;
61 if (uninstall) {
62 if (is_initialized || !method->IsStatic() || method->IsConstructor()) {
63 new_code = class_linker->GetOatCodeFor(method);
64 } else {
65 new_code = Runtime::Current()->GetResolutionStubArray(Runtime::kStaticMethod)->GetData();
66 }
67 } else { // !uninstall
68 if (!interpreter_stubs_installed_ || method->IsNative()) {
69 new_code = GetInstrumentationEntryPoint();
70 } else {
71 new_code = GetInterpreterEntryPoint();
72 }
73 }
74 method->SetCode(new_code);
jeffhao725a9572012-11-13 18:20:12 -080075 }
76 }
jeffhao725a9572012-11-13 18:20:12 -080077 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080078 mirror::AbstractMethod* method = klass->GetVirtualMethod(i);
Ian Rogers62d6c772013-02-27 08:32:07 -080079 if (!method->IsAbstract()) {
80 const void* new_code;
81 if (uninstall) {
82 new_code = class_linker->GetOatCodeFor(method);
83 } else { // !uninstall
84 if (!interpreter_stubs_installed_ || method->IsNative()) {
85 new_code = GetInstrumentationEntryPoint();
86 } else {
87 new_code = GetInterpreterEntryPoint();
88 }
89 }
90 method->SetCode(new_code);
jeffhao725a9572012-11-13 18:20:12 -080091 }
92 }
93 return true;
94}
95
Ian Rogers62d6c772013-02-27 08:32:07 -080096// Places the instrumentation exit pc as the return PC for every quick frame. This also allows
97// deoptimization of quick frames to interpreter frames.
98static void InstrumentationInstallStack(Thread* thread, void* arg)
Ian Rogers306057f2012-11-26 12:45:53 -080099 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
100 struct InstallStackVisitor : public StackVisitor {
Ian Rogers62d6c772013-02-27 08:32:07 -0800101 InstallStackVisitor(Thread* thread, Context* context, uintptr_t instrumentation_exit_pc)
102 : StackVisitor(thread, context), instrumentation_stack_(thread->GetInstrumentationStack()),
103 instrumentation_exit_pc_(instrumentation_exit_pc), last_return_pc_(0) {}
jeffhao725a9572012-11-13 18:20:12 -0800104
Ian Rogers306057f2012-11-26 12:45:53 -0800105 virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800106 mirror::AbstractMethod* m = GetMethod();
Ian Rogers306057f2012-11-26 12:45:53 -0800107 if (GetCurrentQuickFrame() == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800108 if (kVerboseInstrumentation) {
109 LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId()
110 << " Method=" << PrettyMethod(m);
111 }
Ian Rogers306057f2012-11-26 12:45:53 -0800112 return true; // Ignore shadow frames.
113 }
Ian Rogers306057f2012-11-26 12:45:53 -0800114 if (m == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800115 if (kVerboseInstrumentation) {
116 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
117 }
118 last_return_pc_ = 0;
Ian Rogers306057f2012-11-26 12:45:53 -0800119 return true; // Ignore upcalls.
120 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800121 if (m->IsRuntimeMethod()) {
122 if (kVerboseInstrumentation) {
123 LOG(INFO) << " Skipping runtime method. Frame " << GetFrameId();
124 }
125 last_return_pc_ = GetReturnPc();
Ian Rogers306057f2012-11-26 12:45:53 -0800126 return true; // Ignore unresolved methods since they will be instrumented after resolution.
127 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800128 if (kVerboseInstrumentation) {
129 LOG(INFO) << " Installing exit stub in " << DescribeLocation();
130 }
131 uintptr_t return_pc = GetReturnPc();
132 CHECK_NE(return_pc, instrumentation_exit_pc_);
133 CHECK_NE(return_pc, 0U);
134 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, return_pc, GetFrameId());
135 if (kVerboseInstrumentation) {
136 LOG(INFO) << "Pushing frame " << instrumentation_frame.Dump();
137 }
138 instrumentation_stack_->push_back(instrumentation_frame);
139 dex_pcs_.push_back(m->ToDexPc(last_return_pc_));
Ian Rogers306057f2012-11-26 12:45:53 -0800140 SetReturnPc(instrumentation_exit_pc_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800141 last_return_pc_ = return_pc;
Ian Rogers306057f2012-11-26 12:45:53 -0800142 return true; // Continue.
143 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800144 std::deque<InstrumentationStackFrame>* const instrumentation_stack_;
145 std::vector<uint32_t> dex_pcs_;
Ian Rogers306057f2012-11-26 12:45:53 -0800146 const uintptr_t instrumentation_exit_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800147 uintptr_t last_return_pc_;
Ian Rogers306057f2012-11-26 12:45:53 -0800148 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800149 if (kVerboseInstrumentation) {
150 std::string thread_name;
151 thread->GetThreadName(thread_name);
152 LOG(INFO) << "Installing exit stubs in " << thread_name;
Ian Rogers306057f2012-11-26 12:45:53 -0800153 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800154 UniquePtr<Context> context(Context::Create());
155 uintptr_t instrumentation_exit_pc = GetInstrumentationExitPc();
156 InstallStackVisitor visitor(thread, context.get(), instrumentation_exit_pc);
157 visitor.WalkStack(true);
158
159 // Create method enter events for all methods current on the thread's stack.
160 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
161 typedef std::deque<InstrumentationStackFrame>::const_reverse_iterator It;
162 for (It it = thread->GetInstrumentationStack()->rbegin(),
163 end = thread->GetInstrumentationStack()->rend(); it != end; ++it) {
164 mirror::Object* this_object = (*it).this_object_;
165 mirror::AbstractMethod* method = (*it).method_;
166 uint32_t dex_pc = visitor.dex_pcs_.back();
167 visitor.dex_pcs_.pop_back();
168 instrumentation->MethodEnterEvent(thread, this_object, method, dex_pc);
169 }
170 thread->VerifyStack();
Ian Rogers306057f2012-11-26 12:45:53 -0800171}
172
Ian Rogers62d6c772013-02-27 08:32:07 -0800173// Removes the instrumentation exit pc as the return PC for every quick frame.
174static void InstrumentationRestoreStack(Thread* thread, void* arg)
Ian Rogers306057f2012-11-26 12:45:53 -0800175 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
176 struct RestoreStackVisitor : public StackVisitor {
Ian Rogers62d6c772013-02-27 08:32:07 -0800177 RestoreStackVisitor(Thread* thread, uintptr_t instrumentation_exit_pc,
178 Instrumentation* instrumentation)
179 : StackVisitor(thread, NULL), thread_(thread),
180 instrumentation_exit_pc_(instrumentation_exit_pc),
181 instrumentation_(instrumentation),
182 instrumentation_stack_(thread->GetInstrumentationStack()),
183 frames_removed_(0) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800184
185 virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800186 if (instrumentation_stack_->size() == 0) {
jeffhao725a9572012-11-13 18:20:12 -0800187 return false; // Stop.
188 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800189 mirror::AbstractMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800190 if (GetCurrentQuickFrame() == NULL) {
191 if (kVerboseInstrumentation) {
192 LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId() << " Method=" << PrettyMethod(m);
193 }
194 return true; // Ignore shadow frames.
195 }
Ian Rogers306057f2012-11-26 12:45:53 -0800196 if (m == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800197 if (kVerboseInstrumentation) {
198 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
199 }
Ian Rogers306057f2012-11-26 12:45:53 -0800200 return true; // Ignore upcalls.
201 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800202 typedef std::deque<instrumentation::InstrumentationStackFrame>::const_iterator It; // TODO: C++0x auto
203 bool removed_stub = false;
204 // TODO: make this search more efficient?
205 for (It it = instrumentation_stack_->begin(), end = instrumentation_stack_->end(); it != end;
206 ++it) {
207 InstrumentationStackFrame instrumentation_frame = *it;
208 if (instrumentation_frame.frame_id_ == GetFrameId()) {
209 if (kVerboseInstrumentation) {
210 LOG(INFO) << " Removing exit stub in " << DescribeLocation();
211 }
212 CHECK(m == instrumentation_frame.method_) << PrettyMethod(m);
213 SetReturnPc(instrumentation_frame.return_pc_);
214 // Create the method exit events. As the methods didn't really exit the result is 0.
215 instrumentation_->MethodExitEvent(thread_, instrumentation_frame.this_object_, m,
216 GetDexPc(), JValue());
217 frames_removed_++;
218 removed_stub = true;
219 break;
220 }
221 }
222 if (!removed_stub) {
223 if (kVerboseInstrumentation) {
224 LOG(INFO) << " No exit stub in " << DescribeLocation();
225 DescribeStack(thread_);
Ian Rogers306057f2012-11-26 12:45:53 -0800226 }
jeffhao725a9572012-11-13 18:20:12 -0800227 }
228 return true; // Continue.
229 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800230 Thread* const thread_;
Ian Rogers306057f2012-11-26 12:45:53 -0800231 const uintptr_t instrumentation_exit_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800232 Instrumentation* const instrumentation_;
233 std::deque<instrumentation::InstrumentationStackFrame>* const instrumentation_stack_;
234 size_t frames_removed_;
jeffhao725a9572012-11-13 18:20:12 -0800235 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800236 if (kVerboseInstrumentation) {
237 std::string thread_name;
238 thread->GetThreadName(thread_name);
239 LOG(INFO) << "Removing exit stubs in " << thread_name;
240 }
241 std::deque<instrumentation::InstrumentationStackFrame>* stack = thread->GetInstrumentationStack();
242 if (stack->size() > 0) {
243 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
244 uintptr_t instrumentation_exit_pc = GetInstrumentationExitPc();
245 RestoreStackVisitor visitor(thread, instrumentation_exit_pc, instrumentation);
246 visitor.WalkStack(true);
247 CHECK_EQ(visitor.frames_removed_, stack->size());
248 while (stack->size() > 0) {
249 stack->pop_front();
250 }
jeffhao725a9572012-11-13 18:20:12 -0800251 }
252}
253
Ian Rogers62d6c772013-02-27 08:32:07 -0800254void Instrumentation::AddListener(InstrumentationListener* listener, uint32_t events) {
255 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
256 bool require_entry_exit_stubs = false;
257 bool require_interpreter = false;
258 if ((events & kMethodEntered) != 0) {
259 method_entry_listeners_.push_back(listener);
260 require_entry_exit_stubs = true;
261 have_method_entry_listeners_ = true;
262 }
263 if ((events & kMethodExited) != 0) {
264 method_exit_listeners_.push_back(listener);
265 require_entry_exit_stubs = true;
266 have_method_exit_listeners_ = true;
267 }
268 if ((events & kMethodUnwind) != 0) {
269 method_unwind_listeners_.push_back(listener);
270 have_method_unwind_listeners_ = true;
271 }
272 if ((events & kDexPcMoved) != 0) {
273 dex_pc_listeners_.push_back(listener);
274 require_interpreter = true;
275 have_dex_pc_listeners_ = true;
276 }
Jeff Hao14dd5a82013-04-11 10:23:36 -0700277 if ((events & kExceptionCaught) != 0) {
278 exception_caught_listeners_.push_back(listener);
279 have_exception_caught_listeners_ = true;
280 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800281 ConfigureStubs(require_entry_exit_stubs, require_interpreter);
jeffhao725a9572012-11-13 18:20:12 -0800282}
283
Ian Rogers62d6c772013-02-27 08:32:07 -0800284void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) {
285 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
286 bool require_entry_exit_stubs = false;
287 bool require_interpreter = false;
288
289 if ((events & kMethodEntered) != 0) {
290 bool contains = std::find(method_entry_listeners_.begin(), method_entry_listeners_.end(),
291 listener) != method_entry_listeners_.end();
292 if (contains) {
293 method_entry_listeners_.remove(listener);
294 }
295 have_method_entry_listeners_ = method_entry_listeners_.size() > 0;
296 require_entry_exit_stubs |= have_method_entry_listeners_;
297 }
298 if ((events & kMethodExited) != 0) {
299 bool contains = std::find(method_exit_listeners_.begin(), method_exit_listeners_.end(),
300 listener) != method_exit_listeners_.end();
301 if (contains) {
302 method_exit_listeners_.remove(listener);
303 }
304 have_method_exit_listeners_ = method_exit_listeners_.size() > 0;
305 require_entry_exit_stubs |= have_method_exit_listeners_;
306 }
307 if ((events & kMethodUnwind) != 0) {
308 method_unwind_listeners_.remove(listener);
309 }
310 if ((events & kDexPcMoved) != 0) {
311 bool contains = std::find(dex_pc_listeners_.begin(), dex_pc_listeners_.end(),
312 listener) != dex_pc_listeners_.end();
313 if (contains) {
314 dex_pc_listeners_.remove(listener);
315 }
316 have_dex_pc_listeners_ = dex_pc_listeners_.size() > 0;
317 require_interpreter |= have_dex_pc_listeners_;
318 }
Jeff Hao14dd5a82013-04-11 10:23:36 -0700319 if ((events & kExceptionCaught) != 0) {
320 exception_caught_listeners_.remove(listener);
321 have_exception_caught_listeners_ = exception_caught_listeners_.size() > 0;
322 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800323 ConfigureStubs(require_entry_exit_stubs, require_interpreter);
jeffhao725a9572012-11-13 18:20:12 -0800324}
325
Ian Rogers62d6c772013-02-27 08:32:07 -0800326void Instrumentation::ConfigureStubs(bool require_entry_exit_stubs, bool require_interpreter) {
327 interpret_only_ = require_interpreter || forced_interpret_only_;
328 // Compute what level of instrumentation is required and compare to current.
329 int desired_level, current_level;
330 if (require_interpreter) {
331 desired_level = 2;
332 } else if (require_entry_exit_stubs) {
333 desired_level = 1;
334 } else {
335 desired_level = 0;
336 }
337 if (interpreter_stubs_installed_) {
338 current_level = 2;
339 } else if (entry_exit_stubs_installed_) {
340 current_level = 1;
341 } else {
342 current_level = 0;
343 }
344 if (desired_level == current_level) {
345 // We're already set.
346 return;
347 }
348 Thread* self = Thread::Current();
349 Runtime* runtime = Runtime::Current();
350 Locks::thread_list_lock_->AssertNotHeld(self);
351 if (desired_level > 0) {
352 if (require_interpreter) {
353 interpreter_stubs_installed_ = true;
354 } else {
355 CHECK(require_entry_exit_stubs);
356 entry_exit_stubs_installed_ = true;
357 }
358 runtime->GetClassLinker()->VisitClasses(InstallStubsClassVisitor, this);
359 instrumentation_stubs_installed_ = true;
360 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
361 runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this);
362 } else {
363 interpreter_stubs_installed_ = false;
364 entry_exit_stubs_installed_ = false;
365 runtime->GetClassLinker()->VisitClasses(InstallStubsClassVisitor, this);
366 instrumentation_stubs_installed_ = false;
367 MutexLock mu(self, *Locks::thread_list_lock_);
368 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
369 }
jeffhao725a9572012-11-13 18:20:12 -0800370}
371
Ian Rogers62d6c772013-02-27 08:32:07 -0800372void Instrumentation::UpdateMethodsCode(mirror::AbstractMethod* method, const void* code) const {
373 if (LIKELY(!instrumentation_stubs_installed_)) {
374 method->SetCode(code);
375 }
jeffhao725a9572012-11-13 18:20:12 -0800376}
377
Ian Rogers62d6c772013-02-27 08:32:07 -0800378const void* Instrumentation::GetQuickCodeFor(const mirror::AbstractMethod* method) const {
379 Runtime* runtime = Runtime::Current();
380 if (LIKELY(!instrumentation_stubs_installed_)) {
381 const void* code = method->GetCode();
382 DCHECK(code != NULL);
383 if (LIKELY(code != runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData())) {
384 return code;
385 }
386 }
387 return runtime->GetClassLinker()->GetOatCodeFor(method);
jeffhao725a9572012-11-13 18:20:12 -0800388}
389
Ian Rogers62d6c772013-02-27 08:32:07 -0800390void Instrumentation::MethodEnterEventImpl(Thread* thread, mirror::Object* this_object,
391 const mirror::AbstractMethod* method,
392 uint32_t dex_pc) const {
393 typedef std::list<InstrumentationListener*>::const_iterator It; // TODO: C++0x auto
394 for (It it = method_entry_listeners_.begin(), end = method_entry_listeners_.end(); it != end;
395 ++it) {
396 (*it)->MethodEntered(thread, this_object, method, dex_pc);
397 }
398}
399
400void Instrumentation::MethodExitEventImpl(Thread* thread, mirror::Object* this_object,
401 const mirror::AbstractMethod* method,
402 uint32_t dex_pc, const JValue& return_value) const {
403 typedef std::list<InstrumentationListener*>::const_iterator It; // TODO: C++0x auto
404 for (It it = method_exit_listeners_.begin(), end = method_exit_listeners_.end(); it != end;
405 ++it) {
406 (*it)->MethodExited(thread, this_object, method, dex_pc, return_value);
407 }
408}
409
410void Instrumentation::MethodUnwindEvent(Thread* thread, mirror::Object* this_object,
411 const mirror::AbstractMethod* method,
412 uint32_t dex_pc) const {
413 if (have_method_unwind_listeners_) {
414 typedef std::list<InstrumentationListener*>::const_iterator It; // TODO: C++0x auto
415 for (It it = method_unwind_listeners_.begin(), end = method_unwind_listeners_.end(); it != end;
416 ++it) {
417 (*it)->MethodUnwind(thread, method, dex_pc);
418 }
419 }
420}
421
422void Instrumentation::DexPcMovedEventImpl(Thread* thread, mirror::Object* this_object,
423 const mirror::AbstractMethod* method,
424 uint32_t dex_pc) const {
425 // TODO: STL copy-on-write collection? The copy below is due to the debug listener having an
426 // action where it can remove itself as a listener and break the iterator. The copy only works
427 // around the problem and in general we may have to move to something like reference counting to
428 // ensure listeners are deleted correctly.
429 std::list<InstrumentationListener*> copy(dex_pc_listeners_);
430 typedef std::list<InstrumentationListener*>::const_iterator It; // TODO: C++0x auto
431 for (It it = copy.begin(), end = copy.end(); it != end; ++it) {
432 (*it)->DexPcMoved(thread, this_object, method, dex_pc);
433 }
434}
435
436void Instrumentation::ExceptionCaughtEvent(Thread* thread, const ThrowLocation& throw_location,
437 mirror::AbstractMethod* catch_method,
438 uint32_t catch_dex_pc,
439 mirror::Throwable* exception_object) {
440 if (have_exception_caught_listeners_) {
441 typedef std::list<InstrumentationListener*>::const_iterator It; // TODO: C++0x auto
442 for (It it = exception_caught_listeners_.begin(), end = exception_caught_listeners_.end();
443 it != end; ++it) {
444 (*it)->ExceptionCaught(thread, throw_location, catch_method, catch_dex_pc, exception_object);
445 }
446 }
447}
448
449static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame,
450 int delta)
451 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
452 size_t frame_id = StackVisitor::ComputeNumFrames(self) + delta;
453 if (frame_id != instrumentation_frame.frame_id_) {
454 LOG(ERROR) << "Expected frame_id=" << frame_id << " but found "
455 << instrumentation_frame.frame_id_;
456 StackVisitor::DescribeStack(self);
457 CHECK_EQ(frame_id, instrumentation_frame.frame_id_);
458 }
459}
460
461void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
462 mirror::AbstractMethod* method,
463 uintptr_t lr) {
464 // We have a callee-save frame meaning this value is guaranteed to never be 0.
465 size_t frame_id = StackVisitor::ComputeNumFrames(self);
466 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
467 if (kVerboseInstrumentation) {
468 LOG(INFO) << "Entering " << PrettyMethod(method) << " from PC " << (void*)lr;
469 }
470 instrumentation::InstrumentationStackFrame instrumentation_frame(this_object, method, lr,
471 frame_id);
472 stack->push_front(instrumentation_frame);
473
474 MethodEnterEvent(self, this_object, method, 0);
475}
476
477uint64_t Instrumentation::PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc,
478 uint64_t gpr_result, uint64_t fpr_result) {
479 // Do the pop.
480 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
481 CHECK_GT(stack->size(), 0U);
482 InstrumentationStackFrame instrumentation_frame = stack->front();
483 stack->pop_front();
484
485 // Set return PC and check the sanity of the stack.
486 *return_pc = instrumentation_frame.return_pc_;
487 CheckStackDepth(self, instrumentation_frame, 0);
488
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800489 mirror::AbstractMethod* method = instrumentation_frame.method_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800490 char return_shorty = MethodHelper(method).GetShorty()[0];
491 JValue return_value;
492 if (return_shorty == 'V') {
493 return_value.SetJ(0);
494 } else if (return_shorty == 'F' || return_shorty == 'D') {
495 return_value.SetJ(fpr_result);
496 } else {
497 return_value.SetJ(gpr_result);
498 }
499 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
500 // return_pc.
501 uint32_t dex_pc = DexFile::kDexNoIndex;
502 mirror::Object* this_object = instrumentation_frame.this_object_;
503 MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value);
jeffhao725a9572012-11-13 18:20:12 -0800504
Ian Rogers62d6c772013-02-27 08:32:07 -0800505 bool deoptimize = false;
506 if (interpreter_stubs_installed_) {
507 // Deoptimize unless we're returning to an upcall.
508 NthCallerVisitor visitor(self, 1, true);
509 visitor.WalkStack(true);
510 deoptimize = visitor.caller != NULL;
511 if (deoptimize && kVerboseInstrumentation) {
512 LOG(INFO) << "Deoptimizing into " << PrettyMethod(visitor.caller);
513 }
514 }
515 if (deoptimize) {
516 if (kVerboseInstrumentation) {
517 LOG(INFO) << "Deoptimizing from " << PrettyMethod(method)
518 << " result is " << std::hex << return_value.GetJ();
519 }
520 self->SetDeoptimizationReturnValue(return_value);
521 return static_cast<uint64_t>(GetDeoptimizationEntryPoint()) |
522 (static_cast<uint64_t>(*return_pc) << 32);
523 } else {
524 if (kVerboseInstrumentation) {
525 LOG(INFO) << "Returning from " << PrettyMethod(method) << " to PC " << (void*)(*return_pc);
526 }
527 return *return_pc;
528 }
jeffhao725a9572012-11-13 18:20:12 -0800529}
530
Ian Rogers62d6c772013-02-27 08:32:07 -0800531void Instrumentation::PopMethodForUnwind(Thread* self, bool is_deoptimization) const {
532 // Do the pop.
533 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
534 CHECK_GT(stack->size(), 0U);
535 InstrumentationStackFrame instrumentation_frame = stack->front();
536 // TODO: bring back CheckStackDepth(self, instrumentation_frame, 2);
537 stack->pop_front();
538
539 mirror::AbstractMethod* method = instrumentation_frame.method_;
540 if (is_deoptimization) {
541 if (kVerboseInstrumentation) {
542 LOG(INFO) << "Popping for deoptimization " << PrettyMethod(method);
543 }
544 } else {
545 if (kVerboseInstrumentation) {
546 LOG(INFO) << "Popping for unwind " << PrettyMethod(method);
547 }
548
549 // Notify listeners of method unwind.
550 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
551 // return_pc.
552 uint32_t dex_pc = DexFile::kDexNoIndex;
553 MethodUnwindEvent(self, instrumentation_frame.this_object_, method, dex_pc);
554 }
555}
556
557std::string InstrumentationStackFrame::Dump() const {
558 std::ostringstream os;
559 os << "Frame " << frame_id_ << " " << PrettyMethod(method_) << ":"
560 << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_);
561 return os.str();
562}
563
564} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -0800565} // namespace art