blob: 55e93cbc1fb2b1986d2897c6e9a3a246caceb0e1 [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 }
277 ConfigureStubs(require_entry_exit_stubs, require_interpreter);
jeffhao725a9572012-11-13 18:20:12 -0800278}
279
Ian Rogers62d6c772013-02-27 08:32:07 -0800280void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) {
281 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
282 bool require_entry_exit_stubs = false;
283 bool require_interpreter = false;
284
285 if ((events & kMethodEntered) != 0) {
286 bool contains = std::find(method_entry_listeners_.begin(), method_entry_listeners_.end(),
287 listener) != method_entry_listeners_.end();
288 if (contains) {
289 method_entry_listeners_.remove(listener);
290 }
291 have_method_entry_listeners_ = method_entry_listeners_.size() > 0;
292 require_entry_exit_stubs |= have_method_entry_listeners_;
293 }
294 if ((events & kMethodExited) != 0) {
295 bool contains = std::find(method_exit_listeners_.begin(), method_exit_listeners_.end(),
296 listener) != method_exit_listeners_.end();
297 if (contains) {
298 method_exit_listeners_.remove(listener);
299 }
300 have_method_exit_listeners_ = method_exit_listeners_.size() > 0;
301 require_entry_exit_stubs |= have_method_exit_listeners_;
302 }
303 if ((events & kMethodUnwind) != 0) {
304 method_unwind_listeners_.remove(listener);
305 }
306 if ((events & kDexPcMoved) != 0) {
307 bool contains = std::find(dex_pc_listeners_.begin(), dex_pc_listeners_.end(),
308 listener) != dex_pc_listeners_.end();
309 if (contains) {
310 dex_pc_listeners_.remove(listener);
311 }
312 have_dex_pc_listeners_ = dex_pc_listeners_.size() > 0;
313 require_interpreter |= have_dex_pc_listeners_;
314 }
315 ConfigureStubs(require_entry_exit_stubs, require_interpreter);
jeffhao725a9572012-11-13 18:20:12 -0800316}
317
Ian Rogers62d6c772013-02-27 08:32:07 -0800318void Instrumentation::ConfigureStubs(bool require_entry_exit_stubs, bool require_interpreter) {
319 interpret_only_ = require_interpreter || forced_interpret_only_;
320 // Compute what level of instrumentation is required and compare to current.
321 int desired_level, current_level;
322 if (require_interpreter) {
323 desired_level = 2;
324 } else if (require_entry_exit_stubs) {
325 desired_level = 1;
326 } else {
327 desired_level = 0;
328 }
329 if (interpreter_stubs_installed_) {
330 current_level = 2;
331 } else if (entry_exit_stubs_installed_) {
332 current_level = 1;
333 } else {
334 current_level = 0;
335 }
336 if (desired_level == current_level) {
337 // We're already set.
338 return;
339 }
340 Thread* self = Thread::Current();
341 Runtime* runtime = Runtime::Current();
342 Locks::thread_list_lock_->AssertNotHeld(self);
343 if (desired_level > 0) {
344 if (require_interpreter) {
345 interpreter_stubs_installed_ = true;
346 } else {
347 CHECK(require_entry_exit_stubs);
348 entry_exit_stubs_installed_ = true;
349 }
350 runtime->GetClassLinker()->VisitClasses(InstallStubsClassVisitor, this);
351 instrumentation_stubs_installed_ = true;
352 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
353 runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this);
354 } else {
355 interpreter_stubs_installed_ = false;
356 entry_exit_stubs_installed_ = false;
357 runtime->GetClassLinker()->VisitClasses(InstallStubsClassVisitor, this);
358 instrumentation_stubs_installed_ = false;
359 MutexLock mu(self, *Locks::thread_list_lock_);
360 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
361 }
jeffhao725a9572012-11-13 18:20:12 -0800362}
363
Ian Rogers62d6c772013-02-27 08:32:07 -0800364void Instrumentation::UpdateMethodsCode(mirror::AbstractMethod* method, const void* code) const {
365 if (LIKELY(!instrumentation_stubs_installed_)) {
366 method->SetCode(code);
367 }
jeffhao725a9572012-11-13 18:20:12 -0800368}
369
Ian Rogers62d6c772013-02-27 08:32:07 -0800370const void* Instrumentation::GetQuickCodeFor(const mirror::AbstractMethod* method) const {
371 Runtime* runtime = Runtime::Current();
372 if (LIKELY(!instrumentation_stubs_installed_)) {
373 const void* code = method->GetCode();
374 DCHECK(code != NULL);
375 if (LIKELY(code != runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData())) {
376 return code;
377 }
378 }
379 return runtime->GetClassLinker()->GetOatCodeFor(method);
jeffhao725a9572012-11-13 18:20:12 -0800380}
381
Ian Rogers62d6c772013-02-27 08:32:07 -0800382void Instrumentation::MethodEnterEventImpl(Thread* thread, mirror::Object* this_object,
383 const mirror::AbstractMethod* method,
384 uint32_t dex_pc) const {
385 typedef std::list<InstrumentationListener*>::const_iterator It; // TODO: C++0x auto
386 for (It it = method_entry_listeners_.begin(), end = method_entry_listeners_.end(); it != end;
387 ++it) {
388 (*it)->MethodEntered(thread, this_object, method, dex_pc);
389 }
390}
391
392void Instrumentation::MethodExitEventImpl(Thread* thread, mirror::Object* this_object,
393 const mirror::AbstractMethod* method,
394 uint32_t dex_pc, const JValue& return_value) const {
395 typedef std::list<InstrumentationListener*>::const_iterator It; // TODO: C++0x auto
396 for (It it = method_exit_listeners_.begin(), end = method_exit_listeners_.end(); it != end;
397 ++it) {
398 (*it)->MethodExited(thread, this_object, method, dex_pc, return_value);
399 }
400}
401
402void Instrumentation::MethodUnwindEvent(Thread* thread, mirror::Object* this_object,
403 const mirror::AbstractMethod* method,
404 uint32_t dex_pc) const {
405 if (have_method_unwind_listeners_) {
406 typedef std::list<InstrumentationListener*>::const_iterator It; // TODO: C++0x auto
407 for (It it = method_unwind_listeners_.begin(), end = method_unwind_listeners_.end(); it != end;
408 ++it) {
409 (*it)->MethodUnwind(thread, method, dex_pc);
410 }
411 }
412}
413
414void Instrumentation::DexPcMovedEventImpl(Thread* thread, mirror::Object* this_object,
415 const mirror::AbstractMethod* method,
416 uint32_t dex_pc) const {
417 // TODO: STL copy-on-write collection? The copy below is due to the debug listener having an
418 // action where it can remove itself as a listener and break the iterator. The copy only works
419 // around the problem and in general we may have to move to something like reference counting to
420 // ensure listeners are deleted correctly.
421 std::list<InstrumentationListener*> copy(dex_pc_listeners_);
422 typedef std::list<InstrumentationListener*>::const_iterator It; // TODO: C++0x auto
423 for (It it = copy.begin(), end = copy.end(); it != end; ++it) {
424 (*it)->DexPcMoved(thread, this_object, method, dex_pc);
425 }
426}
427
428void Instrumentation::ExceptionCaughtEvent(Thread* thread, const ThrowLocation& throw_location,
429 mirror::AbstractMethod* catch_method,
430 uint32_t catch_dex_pc,
431 mirror::Throwable* exception_object) {
432 if (have_exception_caught_listeners_) {
433 typedef std::list<InstrumentationListener*>::const_iterator It; // TODO: C++0x auto
434 for (It it = exception_caught_listeners_.begin(), end = exception_caught_listeners_.end();
435 it != end; ++it) {
436 (*it)->ExceptionCaught(thread, throw_location, catch_method, catch_dex_pc, exception_object);
437 }
438 }
439}
440
441static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame,
442 int delta)
443 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
444 size_t frame_id = StackVisitor::ComputeNumFrames(self) + delta;
445 if (frame_id != instrumentation_frame.frame_id_) {
446 LOG(ERROR) << "Expected frame_id=" << frame_id << " but found "
447 << instrumentation_frame.frame_id_;
448 StackVisitor::DescribeStack(self);
449 CHECK_EQ(frame_id, instrumentation_frame.frame_id_);
450 }
451}
452
453void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
454 mirror::AbstractMethod* method,
455 uintptr_t lr) {
456 // We have a callee-save frame meaning this value is guaranteed to never be 0.
457 size_t frame_id = StackVisitor::ComputeNumFrames(self);
458 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
459 if (kVerboseInstrumentation) {
460 LOG(INFO) << "Entering " << PrettyMethod(method) << " from PC " << (void*)lr;
461 }
462 instrumentation::InstrumentationStackFrame instrumentation_frame(this_object, method, lr,
463 frame_id);
464 stack->push_front(instrumentation_frame);
465
466 MethodEnterEvent(self, this_object, method, 0);
467}
468
469uint64_t Instrumentation::PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc,
470 uint64_t gpr_result, uint64_t fpr_result) {
471 // Do the pop.
472 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
473 CHECK_GT(stack->size(), 0U);
474 InstrumentationStackFrame instrumentation_frame = stack->front();
475 stack->pop_front();
476
477 // Set return PC and check the sanity of the stack.
478 *return_pc = instrumentation_frame.return_pc_;
479 CheckStackDepth(self, instrumentation_frame, 0);
480
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800481 mirror::AbstractMethod* method = instrumentation_frame.method_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800482 char return_shorty = MethodHelper(method).GetShorty()[0];
483 JValue return_value;
484 if (return_shorty == 'V') {
485 return_value.SetJ(0);
486 } else if (return_shorty == 'F' || return_shorty == 'D') {
487 return_value.SetJ(fpr_result);
488 } else {
489 return_value.SetJ(gpr_result);
490 }
491 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
492 // return_pc.
493 uint32_t dex_pc = DexFile::kDexNoIndex;
494 mirror::Object* this_object = instrumentation_frame.this_object_;
495 MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value);
jeffhao725a9572012-11-13 18:20:12 -0800496
Ian Rogers62d6c772013-02-27 08:32:07 -0800497 bool deoptimize = false;
498 if (interpreter_stubs_installed_) {
499 // Deoptimize unless we're returning to an upcall.
500 NthCallerVisitor visitor(self, 1, true);
501 visitor.WalkStack(true);
502 deoptimize = visitor.caller != NULL;
503 if (deoptimize && kVerboseInstrumentation) {
504 LOG(INFO) << "Deoptimizing into " << PrettyMethod(visitor.caller);
505 }
506 }
507 if (deoptimize) {
508 if (kVerboseInstrumentation) {
509 LOG(INFO) << "Deoptimizing from " << PrettyMethod(method)
510 << " result is " << std::hex << return_value.GetJ();
511 }
512 self->SetDeoptimizationReturnValue(return_value);
513 return static_cast<uint64_t>(GetDeoptimizationEntryPoint()) |
514 (static_cast<uint64_t>(*return_pc) << 32);
515 } else {
516 if (kVerboseInstrumentation) {
517 LOG(INFO) << "Returning from " << PrettyMethod(method) << " to PC " << (void*)(*return_pc);
518 }
519 return *return_pc;
520 }
jeffhao725a9572012-11-13 18:20:12 -0800521}
522
Ian Rogers62d6c772013-02-27 08:32:07 -0800523void Instrumentation::PopMethodForUnwind(Thread* self, bool is_deoptimization) const {
524 // Do the pop.
525 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
526 CHECK_GT(stack->size(), 0U);
527 InstrumentationStackFrame instrumentation_frame = stack->front();
528 // TODO: bring back CheckStackDepth(self, instrumentation_frame, 2);
529 stack->pop_front();
530
531 mirror::AbstractMethod* method = instrumentation_frame.method_;
532 if (is_deoptimization) {
533 if (kVerboseInstrumentation) {
534 LOG(INFO) << "Popping for deoptimization " << PrettyMethod(method);
535 }
536 } else {
537 if (kVerboseInstrumentation) {
538 LOG(INFO) << "Popping for unwind " << PrettyMethod(method);
539 }
540
541 // Notify listeners of method unwind.
542 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
543 // return_pc.
544 uint32_t dex_pc = DexFile::kDexNoIndex;
545 MethodUnwindEvent(self, instrumentation_frame.this_object_, method, dex_pc);
546 }
547}
548
549std::string InstrumentationStackFrame::Dump() const {
550 std::ostringstream os;
551 os << "Frame " << frame_id_ << " " << PrettyMethod(method_) << ":"
552 << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_);
553 return os.str();
554}
555
556} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -0800557} // namespace art