blob: 59ffdc1b5f438cf48b447b4fc77adb2a1b4938ef [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 Rogersef7d42f2014-01-06 12:55:46 -080021#include "atomic.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"
Sebastien Hertz138dbfc2013-12-04 18:15:25 +010026#include "interpreter/interpreter.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070027#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "mirror/class-inl.h"
29#include "mirror/dex_cache.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030#include "mirror/object_array-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070031#include "mirror/object-inl.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080032#include "nth_caller_visitor.h"
Ian Rogersc928de92013-02-27 14:30:44 -080033#if !defined(ART_USE_PORTABLE_COMPILER)
Ian Rogers166db042013-07-26 12:05:57 -070034#include "entrypoints/quick/quick_entrypoints.h"
jeffhao725a9572012-11-13 18:20:12 -080035#endif
36#include "object_utils.h"
37#include "os.h"
38#include "scoped_thread_state_change.h"
39#include "thread.h"
40#include "thread_list.h"
jeffhao725a9572012-11-13 18:20:12 -080041
42namespace art {
Ian Rogersfa824272013-11-05 16:12:57 -080043
44extern void SetQuickAllocEntryPointsInstrumented(bool instrumented);
45
Ian Rogers62d6c772013-02-27 08:32:07 -080046namespace instrumentation {
jeffhao725a9572012-11-13 18:20:12 -080047
Sebastien Hertz5bfd5c92013-11-15 11:36:07 +010048const bool kVerboseInstrumentation = false;
49
Ian Rogers816432e2013-09-06 15:47:45 -070050// Do we want to deoptimize for method entry and exit listeners or just try to intercept
51// invocations? Deoptimization forces all code to run in the interpreter and considerably hurts the
52// application's performance.
Ian Rogers7b6da362013-09-11 09:29:40 -070053static constexpr bool kDeoptimizeForAccurateMethodEntryExitListeners = false;
Ian Rogers816432e2013-09-06 15:47:45 -070054
Ian Rogers62d6c772013-02-27 08:32:07 -080055static bool InstallStubsClassVisitor(mirror::Class* klass, void* arg)
jeffhao725a9572012-11-13 18:20:12 -080056 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -080057 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
58 return instrumentation->InstallStubsForClass(klass);
59}
60
61bool Instrumentation::InstallStubsForClass(mirror::Class* klass) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +010062 for (size_t i = 0, e = klass->NumDirectMethods(); i < e; i++) {
63 InstallStubsForMethod(klass->GetDirectMethod(i));
jeffhao725a9572012-11-13 18:20:12 -080064 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +010065 for (size_t i = 0, e = klass->NumVirtualMethods(); i < e; i++) {
66 InstallStubsForMethod(klass->GetVirtualMethod(i));
jeffhao725a9572012-11-13 18:20:12 -080067 }
68 return true;
69}
70
Ian Rogersef7d42f2014-01-06 12:55:46 -080071static void UpdateEntrypoints(mirror::ArtMethod* method, const void* quick_code,
72 const void* portable_code, bool have_portable_code)
73 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
74 method->SetEntryPointFromPortableCompiledCode(portable_code);
75 method->SetEntryPointFromQuickCompiledCode(quick_code);
76 bool portable_enabled = method->IsPortableCompiled();
77 if (have_portable_code && !portable_enabled) {
78 method->SetIsPortableCompiled();
79 } else if (portable_enabled) {
80 method->ClearIsPortableCompiled();
81 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +010082 if (!method->IsResolutionMethod()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080083 if (quick_code == GetQuickToInterpreterBridge()) {
84 DCHECK(portable_code == GetPortableToInterpreterBridge());
85 DCHECK(!method->IsNative()) << PrettyMethod(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +010086 method->SetEntryPointFromInterpreter(art::interpreter::artInterpreterToInterpreterBridge);
87 } else {
88 method->SetEntryPointFromInterpreter(art::artInterpreterToCompiledCodeBridge);
89 }
90 }
91}
92
93void Instrumentation::InstallStubsForMethod(mirror::ArtMethod* method) {
94 if (method->IsAbstract() || method->IsProxyMethod()) {
95 // Do not change stubs for these methods.
96 return;
97 }
Ian Rogersef7d42f2014-01-06 12:55:46 -080098 const void* new_portable_code;
99 const void* new_quick_code;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100100 bool uninstall = !entry_exit_stubs_installed_ && !interpreter_stubs_installed_;
101 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
102 bool is_class_initialized = method->GetDeclaringClass()->IsInitialized();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800103 bool have_portable_code = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100104 if (uninstall) {
105 if ((forced_interpret_only_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800106 new_portable_code = GetPortableToInterpreterBridge();
107 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100108 } else if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800109 new_portable_code = class_linker->GetPortableOatCodeFor(method, &have_portable_code);
110 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100111 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800112 new_portable_code = GetPortableResolutionTrampoline(class_linker);
113 new_quick_code = GetQuickResolutionTrampoline(class_linker);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100114 }
115 } else { // !uninstall
116 if ((interpreter_stubs_installed_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800117 new_portable_code = GetPortableToInterpreterBridge();
118 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100119 } else {
120 // Do not overwrite resolution trampoline. When the trampoline initializes the method's
121 // class, all its static methods code will be set to the instrumentation entry point.
122 // For more details, see ClassLinker::FixupStaticTrampolines.
123 if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
124 // Do not overwrite interpreter to prevent from posting method entry/exit events twice.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800125 new_portable_code = class_linker->GetPortableOatCodeFor(method, &have_portable_code);
126 new_quick_code = class_linker->GetQuickOatCodeFor(method);
127 if (entry_exit_stubs_installed_ && new_quick_code != GetQuickToInterpreterBridge()) {
128 DCHECK(new_portable_code != GetPortableToInterpreterBridge());
129 new_portable_code = GetPortableToInterpreterBridge();
130 new_quick_code = GetQuickInstrumentationEntryPoint();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100131 }
132 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800133 new_portable_code = GetPortableResolutionTrampoline(class_linker);
134 new_quick_code = GetQuickResolutionTrampoline(class_linker);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100135 }
136 }
137 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800138 UpdateEntrypoints(method, new_quick_code, new_portable_code, have_portable_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100139}
140
Ian Rogers62d6c772013-02-27 08:32:07 -0800141// Places the instrumentation exit pc as the return PC for every quick frame. This also allows
142// deoptimization of quick frames to interpreter frames.
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100143// Since we may already have done this previously, we need to push new instrumentation frame before
144// existing instrumentation frames.
Ian Rogers62d6c772013-02-27 08:32:07 -0800145static void InstrumentationInstallStack(Thread* thread, void* arg)
Ian Rogers306057f2012-11-26 12:45:53 -0800146 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
147 struct InstallStackVisitor : public StackVisitor {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100148 InstallStackVisitor(Thread* thread, Context* context, uintptr_t instrumentation_exit_pc,
149 bool is_deoptimization_enabled)
Ian Rogers62d6c772013-02-27 08:32:07 -0800150 : StackVisitor(thread, context), instrumentation_stack_(thread->GetInstrumentationStack()),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100151 existing_instrumentation_frames_count_(instrumentation_stack_->size()),
152 instrumentation_exit_pc_(instrumentation_exit_pc),
153 is_deoptimization_enabled_(is_deoptimization_enabled),
154 reached_existing_instrumentation_frames_(false), instrumentation_stack_depth_(0),
155 last_return_pc_(0) {
156 }
jeffhao725a9572012-11-13 18:20:12 -0800157
Ian Rogers306057f2012-11-26 12:45:53 -0800158 virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700159 mirror::ArtMethod* m = GetMethod();
Ian Rogers306057f2012-11-26 12:45:53 -0800160 if (GetCurrentQuickFrame() == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800161 if (kVerboseInstrumentation) {
162 LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId()
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100163 << " Method=" << PrettyMethod(m);
Ian Rogers62d6c772013-02-27 08:32:07 -0800164 }
Ian Rogers306057f2012-11-26 12:45:53 -0800165 return true; // Ignore shadow frames.
166 }
Ian Rogers306057f2012-11-26 12:45:53 -0800167 if (m == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800168 if (kVerboseInstrumentation) {
169 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
170 }
171 last_return_pc_ = 0;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700172 return true; // Ignore upcalls.
Ian Rogers306057f2012-11-26 12:45:53 -0800173 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800174 if (m->IsRuntimeMethod()) {
175 if (kVerboseInstrumentation) {
176 LOG(INFO) << " Skipping runtime method. Frame " << GetFrameId();
177 }
178 last_return_pc_ = GetReturnPc();
Ian Rogers306057f2012-11-26 12:45:53 -0800179 return true; // Ignore unresolved methods since they will be instrumented after resolution.
180 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800181 if (kVerboseInstrumentation) {
182 LOG(INFO) << " Installing exit stub in " << DescribeLocation();
183 }
184 uintptr_t return_pc = GetReturnPc();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100185 if (return_pc == instrumentation_exit_pc_) {
186 // We've reached a frame which has already been installed with instrumentation exit stub.
187 // We should have already installed instrumentation on previous frames.
188 reached_existing_instrumentation_frames_ = true;
189
190 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
191 const InstrumentationStackFrame& frame = instrumentation_stack_->at(instrumentation_stack_depth_);
192 CHECK_EQ(m, frame.method_) << "Expected " << PrettyMethod(m)
193 << ", Found " << PrettyMethod(frame.method_);
194 return_pc = frame.return_pc_;
195 if (kVerboseInstrumentation) {
196 LOG(INFO) << "Ignoring already instrumented " << frame.Dump();
197 }
198 } else {
199 CHECK_NE(return_pc, 0U);
200 CHECK(!reached_existing_instrumentation_frames_);
201 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, return_pc, GetFrameId(),
202 false);
203 if (kVerboseInstrumentation) {
204 LOG(INFO) << "Pushing frame " << instrumentation_frame.Dump();
205 }
206
207 // Insert frame before old ones so we do not corrupt the instrumentation stack.
208 auto it = instrumentation_stack_->end() - existing_instrumentation_frames_count_;
209 instrumentation_stack_->insert(it, instrumentation_frame);
210 SetReturnPc(instrumentation_exit_pc_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800211 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800212 dex_pcs_.push_back(m->ToDexPc(last_return_pc_));
Ian Rogers62d6c772013-02-27 08:32:07 -0800213 last_return_pc_ = return_pc;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100214 ++instrumentation_stack_depth_;
Ian Rogers306057f2012-11-26 12:45:53 -0800215 return true; // Continue.
216 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800217 std::deque<InstrumentationStackFrame>* const instrumentation_stack_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100218 const size_t existing_instrumentation_frames_count_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800219 std::vector<uint32_t> dex_pcs_;
Ian Rogers306057f2012-11-26 12:45:53 -0800220 const uintptr_t instrumentation_exit_pc_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100221 const bool is_deoptimization_enabled_;
222 bool reached_existing_instrumentation_frames_;
223 size_t instrumentation_stack_depth_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800224 uintptr_t last_return_pc_;
Ian Rogers306057f2012-11-26 12:45:53 -0800225 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800226 if (kVerboseInstrumentation) {
227 std::string thread_name;
228 thread->GetThreadName(thread_name);
229 LOG(INFO) << "Installing exit stubs in " << thread_name;
Ian Rogers306057f2012-11-26 12:45:53 -0800230 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100231
232 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800233 UniquePtr<Context> context(Context::Create());
Ian Rogers848871b2013-08-05 10:56:33 -0700234 uintptr_t instrumentation_exit_pc = GetQuickInstrumentationExitPc();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100235 InstallStackVisitor visitor(thread, context.get(), instrumentation_exit_pc,
236 instrumentation->IsDeoptimizationEnabled());
Ian Rogers62d6c772013-02-27 08:32:07 -0800237 visitor.WalkStack(true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100238 CHECK_EQ(visitor.dex_pcs_.size(), thread->GetInstrumentationStack()->size());
Ian Rogers62d6c772013-02-27 08:32:07 -0800239
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100240 if (!instrumentation->IsDeoptimizationEnabled()) {
241 // Create method enter events for all methods currently on the thread's stack. We only do this
242 // if no debugger is attached to prevent from posting events twice.
243 typedef std::deque<InstrumentationStackFrame>::const_reverse_iterator It;
244 for (It it = thread->GetInstrumentationStack()->rbegin(),
245 end = thread->GetInstrumentationStack()->rend(); it != end; ++it) {
246 mirror::Object* this_object = (*it).this_object_;
247 mirror::ArtMethod* method = (*it).method_;
248 uint32_t dex_pc = visitor.dex_pcs_.back();
249 visitor.dex_pcs_.pop_back();
250 instrumentation->MethodEnterEvent(thread, this_object, method, dex_pc);
251 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800252 }
253 thread->VerifyStack();
Ian Rogers306057f2012-11-26 12:45:53 -0800254}
255
Ian Rogers62d6c772013-02-27 08:32:07 -0800256// Removes the instrumentation exit pc as the return PC for every quick frame.
257static void InstrumentationRestoreStack(Thread* thread, void* arg)
Ian Rogers306057f2012-11-26 12:45:53 -0800258 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
259 struct RestoreStackVisitor : public StackVisitor {
Ian Rogers62d6c772013-02-27 08:32:07 -0800260 RestoreStackVisitor(Thread* thread, uintptr_t instrumentation_exit_pc,
261 Instrumentation* instrumentation)
262 : StackVisitor(thread, NULL), thread_(thread),
263 instrumentation_exit_pc_(instrumentation_exit_pc),
264 instrumentation_(instrumentation),
265 instrumentation_stack_(thread->GetInstrumentationStack()),
266 frames_removed_(0) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800267
268 virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800269 if (instrumentation_stack_->size() == 0) {
jeffhao725a9572012-11-13 18:20:12 -0800270 return false; // Stop.
271 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700272 mirror::ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800273 if (GetCurrentQuickFrame() == NULL) {
274 if (kVerboseInstrumentation) {
275 LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId() << " Method=" << PrettyMethod(m);
276 }
277 return true; // Ignore shadow frames.
278 }
Ian Rogers306057f2012-11-26 12:45:53 -0800279 if (m == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800280 if (kVerboseInstrumentation) {
281 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
282 }
Ian Rogers306057f2012-11-26 12:45:53 -0800283 return true; // Ignore upcalls.
284 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800285 bool removed_stub = false;
286 // TODO: make this search more efficient?
Mathieu Chartier02e25112013-08-14 16:14:24 -0700287 for (InstrumentationStackFrame instrumentation_frame : *instrumentation_stack_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800288 if (instrumentation_frame.frame_id_ == GetFrameId()) {
289 if (kVerboseInstrumentation) {
290 LOG(INFO) << " Removing exit stub in " << DescribeLocation();
291 }
Jeff Hao9a916d32013-06-27 18:45:37 -0700292 if (instrumentation_frame.interpreter_entry_) {
293 CHECK(m == Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
294 } else {
295 CHECK(m == instrumentation_frame.method_) << PrettyMethod(m);
296 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800297 SetReturnPc(instrumentation_frame.return_pc_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100298 if (!instrumentation_->IsDeoptimizationEnabled()) {
299 // Create the method exit events. As the methods didn't really exit the result is 0.
300 // We only do this if no debugger is attached to prevent from posting events twice.
301 instrumentation_->MethodExitEvent(thread_, instrumentation_frame.this_object_, m,
302 GetDexPc(), JValue());
303 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800304 frames_removed_++;
305 removed_stub = true;
306 break;
307 }
308 }
309 if (!removed_stub) {
310 if (kVerboseInstrumentation) {
311 LOG(INFO) << " No exit stub in " << DescribeLocation();
Ian Rogers306057f2012-11-26 12:45:53 -0800312 }
jeffhao725a9572012-11-13 18:20:12 -0800313 }
314 return true; // Continue.
315 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800316 Thread* const thread_;
Ian Rogers306057f2012-11-26 12:45:53 -0800317 const uintptr_t instrumentation_exit_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800318 Instrumentation* const instrumentation_;
319 std::deque<instrumentation::InstrumentationStackFrame>* const instrumentation_stack_;
320 size_t frames_removed_;
jeffhao725a9572012-11-13 18:20:12 -0800321 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800322 if (kVerboseInstrumentation) {
323 std::string thread_name;
324 thread->GetThreadName(thread_name);
325 LOG(INFO) << "Removing exit stubs in " << thread_name;
326 }
327 std::deque<instrumentation::InstrumentationStackFrame>* stack = thread->GetInstrumentationStack();
328 if (stack->size() > 0) {
329 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers848871b2013-08-05 10:56:33 -0700330 uintptr_t instrumentation_exit_pc = GetQuickInstrumentationExitPc();
Ian Rogers62d6c772013-02-27 08:32:07 -0800331 RestoreStackVisitor visitor(thread, instrumentation_exit_pc, instrumentation);
332 visitor.WalkStack(true);
333 CHECK_EQ(visitor.frames_removed_, stack->size());
334 while (stack->size() > 0) {
335 stack->pop_front();
336 }
jeffhao725a9572012-11-13 18:20:12 -0800337 }
338}
339
Ian Rogers62d6c772013-02-27 08:32:07 -0800340void Instrumentation::AddListener(InstrumentationListener* listener, uint32_t events) {
341 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Ian Rogers62d6c772013-02-27 08:32:07 -0800342 if ((events & kMethodEntered) != 0) {
343 method_entry_listeners_.push_back(listener);
Ian Rogers62d6c772013-02-27 08:32:07 -0800344 have_method_entry_listeners_ = true;
345 }
346 if ((events & kMethodExited) != 0) {
347 method_exit_listeners_.push_back(listener);
Ian Rogers62d6c772013-02-27 08:32:07 -0800348 have_method_exit_listeners_ = true;
349 }
350 if ((events & kMethodUnwind) != 0) {
351 method_unwind_listeners_.push_back(listener);
352 have_method_unwind_listeners_ = true;
353 }
354 if ((events & kDexPcMoved) != 0) {
355 dex_pc_listeners_.push_back(listener);
Ian Rogers62d6c772013-02-27 08:32:07 -0800356 have_dex_pc_listeners_ = true;
357 }
Jeff Hao14dd5a82013-04-11 10:23:36 -0700358 if ((events & kExceptionCaught) != 0) {
359 exception_caught_listeners_.push_back(listener);
360 have_exception_caught_listeners_ = true;
361 }
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200362 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800363}
364
Ian Rogers62d6c772013-02-27 08:32:07 -0800365void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) {
366 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Ian Rogers62d6c772013-02-27 08:32:07 -0800367
368 if ((events & kMethodEntered) != 0) {
369 bool contains = std::find(method_entry_listeners_.begin(), method_entry_listeners_.end(),
370 listener) != method_entry_listeners_.end();
371 if (contains) {
372 method_entry_listeners_.remove(listener);
373 }
374 have_method_entry_listeners_ = method_entry_listeners_.size() > 0;
Ian Rogers62d6c772013-02-27 08:32:07 -0800375 }
376 if ((events & kMethodExited) != 0) {
377 bool contains = std::find(method_exit_listeners_.begin(), method_exit_listeners_.end(),
378 listener) != method_exit_listeners_.end();
379 if (contains) {
380 method_exit_listeners_.remove(listener);
381 }
382 have_method_exit_listeners_ = method_exit_listeners_.size() > 0;
Ian Rogers62d6c772013-02-27 08:32:07 -0800383 }
384 if ((events & kMethodUnwind) != 0) {
385 method_unwind_listeners_.remove(listener);
386 }
387 if ((events & kDexPcMoved) != 0) {
388 bool contains = std::find(dex_pc_listeners_.begin(), dex_pc_listeners_.end(),
389 listener) != dex_pc_listeners_.end();
390 if (contains) {
391 dex_pc_listeners_.remove(listener);
392 }
393 have_dex_pc_listeners_ = dex_pc_listeners_.size() > 0;
Ian Rogers62d6c772013-02-27 08:32:07 -0800394 }
Jeff Hao14dd5a82013-04-11 10:23:36 -0700395 if ((events & kExceptionCaught) != 0) {
396 exception_caught_listeners_.remove(listener);
397 have_exception_caught_listeners_ = exception_caught_listeners_.size() > 0;
398 }
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200399 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800400}
401
Ian Rogers62d6c772013-02-27 08:32:07 -0800402void Instrumentation::ConfigureStubs(bool require_entry_exit_stubs, bool require_interpreter) {
403 interpret_only_ = require_interpreter || forced_interpret_only_;
404 // Compute what level of instrumentation is required and compare to current.
405 int desired_level, current_level;
406 if (require_interpreter) {
407 desired_level = 2;
408 } else if (require_entry_exit_stubs) {
409 desired_level = 1;
410 } else {
411 desired_level = 0;
412 }
413 if (interpreter_stubs_installed_) {
414 current_level = 2;
415 } else if (entry_exit_stubs_installed_) {
416 current_level = 1;
417 } else {
418 current_level = 0;
419 }
420 if (desired_level == current_level) {
421 // We're already set.
422 return;
423 }
424 Thread* self = Thread::Current();
425 Runtime* runtime = Runtime::Current();
426 Locks::thread_list_lock_->AssertNotHeld(self);
427 if (desired_level > 0) {
428 if (require_interpreter) {
429 interpreter_stubs_installed_ = true;
430 } else {
431 CHECK(require_entry_exit_stubs);
432 entry_exit_stubs_installed_ = true;
433 }
434 runtime->GetClassLinker()->VisitClasses(InstallStubsClassVisitor, this);
435 instrumentation_stubs_installed_ = true;
436 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
437 runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this);
438 } else {
439 interpreter_stubs_installed_ = false;
440 entry_exit_stubs_installed_ = false;
441 runtime->GetClassLinker()->VisitClasses(InstallStubsClassVisitor, this);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100442 // Restore stack only if there is no method currently deoptimized.
443 if (deoptimized_methods_.empty()) {
444 instrumentation_stubs_installed_ = false;
445 MutexLock mu(self, *Locks::thread_list_lock_);
446 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
447 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800448 }
jeffhao725a9572012-11-13 18:20:12 -0800449}
450
Ian Rogersfa824272013-11-05 16:12:57 -0800451static void ResetQuickAllocEntryPointsForThread(Thread* thread, void* arg) {
452 thread->ResetQuickAllocEntryPointsForThread();
453}
454
455void Instrumentation::InstrumentQuickAllocEntryPoints() {
456 // TODO: the read of quick_alloc_entry_points_instrumentation_counter_ is racey and this code
457 // should be guarded by a lock.
Ian Rogersb122a4b2013-11-19 18:00:50 -0800458 DCHECK_GE(quick_alloc_entry_points_instrumentation_counter_.Load(), 0);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800459 const bool enable_instrumentation =
Ian Rogersb122a4b2013-11-19 18:00:50 -0800460 quick_alloc_entry_points_instrumentation_counter_.FetchAndAdd(1) == 0;
Ian Rogersfa824272013-11-05 16:12:57 -0800461 if (enable_instrumentation) {
462 // Instrumentation wasn't enabled so enable it.
463 SetQuickAllocEntryPointsInstrumented(true);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800464 ThreadList* tl = Runtime::Current()->GetThreadList();
465 tl->SuspendAll();
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800466 ResetQuickAllocEntryPoints();
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800467 tl->ResumeAll();
Ian Rogersfa824272013-11-05 16:12:57 -0800468 }
469}
470
471void Instrumentation::UninstrumentQuickAllocEntryPoints() {
472 // TODO: the read of quick_alloc_entry_points_instrumentation_counter_ is racey and this code
473 // should be guarded by a lock.
Ian Rogersb122a4b2013-11-19 18:00:50 -0800474 DCHECK_GT(quick_alloc_entry_points_instrumentation_counter_.Load(), 0);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800475 const bool disable_instrumentation =
Ian Rogersb122a4b2013-11-19 18:00:50 -0800476 quick_alloc_entry_points_instrumentation_counter_.FetchAndSub(1) == 1;
Ian Rogersfa824272013-11-05 16:12:57 -0800477 if (disable_instrumentation) {
478 SetQuickAllocEntryPointsInstrumented(false);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800479 ThreadList* tl = Runtime::Current()->GetThreadList();
480 tl->SuspendAll();
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800481 ResetQuickAllocEntryPoints();
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800482 tl->ResumeAll();
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800483 }
484}
485
486void Instrumentation::ResetQuickAllocEntryPoints() {
487 Runtime* runtime = Runtime::Current();
488 if (runtime->IsStarted()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800489 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
490 runtime->GetThreadList()->ForEach(ResetQuickAllocEntryPointsForThread, NULL);
Ian Rogersfa824272013-11-05 16:12:57 -0800491 }
492}
493
Ian Rogersef7d42f2014-01-06 12:55:46 -0800494void Instrumentation::UpdateMethodsCode(mirror::ArtMethod* method, const void* quick_code,
495 const void* portable_code, bool have_portable_code) const {
496 const void* new_portable_code;
497 const void* new_quick_code;
498 bool new_have_portable_code;
Ian Rogers62d6c772013-02-27 08:32:07 -0800499 if (LIKELY(!instrumentation_stubs_installed_)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800500 new_portable_code = portable_code;
501 new_quick_code = quick_code;
502 new_have_portable_code = have_portable_code;
Jeff Hao65d15d92013-07-16 16:39:33 -0700503 } else {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100504 if ((interpreter_stubs_installed_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800505 new_portable_code = GetPortableToInterpreterBridge();
506 new_quick_code = GetQuickToInterpreterBridge();
507 new_have_portable_code = false;
508 } else if (quick_code == GetQuickResolutionTrampoline(Runtime::Current()->GetClassLinker()) ||
509 quick_code == GetQuickToInterpreterBridge()) {
510 DCHECK((portable_code == GetPortableResolutionTrampoline(Runtime::Current()->GetClassLinker())) ||
511 (portable_code == GetPortableToInterpreterBridge()));
512 new_portable_code = portable_code;
513 new_quick_code = quick_code;
514 new_have_portable_code = have_portable_code;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100515 } else if (entry_exit_stubs_installed_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800516 new_quick_code = GetQuickInstrumentationEntryPoint();
517 new_portable_code = GetPortableToInterpreterBridge();
518 new_have_portable_code = false;
Jeff Hao65d15d92013-07-16 16:39:33 -0700519 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800520 new_portable_code = portable_code;
521 new_quick_code = quick_code;
522 new_have_portable_code = have_portable_code;
Jeff Hao65d15d92013-07-16 16:39:33 -0700523 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800524 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800525 UpdateEntrypoints(method, new_quick_code, new_portable_code, new_have_portable_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100526}
527
528void Instrumentation::Deoptimize(mirror::ArtMethod* method) {
529 CHECK(!method->IsNative());
530 CHECK(!method->IsProxyMethod());
531 CHECK(!method->IsAbstract());
532
533 std::pair<std::set<mirror::ArtMethod*>::iterator, bool> pair = deoptimized_methods_.insert(method);
534 bool already_deoptimized = !pair.second;
535 CHECK(!already_deoptimized) << "Method " << PrettyMethod(method) << " is already deoptimized";
536
537 if (!interpreter_stubs_installed_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800538 UpdateEntrypoints(method, GetQuickToInterpreterBridge(), GetPortableToInterpreterBridge(),
539 false);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100540
541 // Install instrumentation exit stub and instrumentation frames. We may already have installed
542 // these previously so it will only cover the newly created frames.
543 instrumentation_stubs_installed_ = true;
544 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
545 Runtime::Current()->GetThreadList()->ForEach(InstrumentationInstallStack, this);
546 }
547}
548
549void Instrumentation::Undeoptimize(mirror::ArtMethod* method) {
550 CHECK(!method->IsNative());
551 CHECK(!method->IsProxyMethod());
552 CHECK(!method->IsAbstract());
553
554 auto it = deoptimized_methods_.find(method);
555 CHECK(it != deoptimized_methods_.end()) << "Method " << PrettyMethod(method) << " is not deoptimized";
556 deoptimized_methods_.erase(it);
557
558 // Restore code and possibly stack only if we did not deoptimize everything.
559 if (!interpreter_stubs_installed_) {
560 // Restore its code or resolution trampoline.
561 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800562 if (method->IsStatic() && !method->IsConstructor() &&
563 !method->GetDeclaringClass()->IsInitialized()) {
564 UpdateEntrypoints(method, GetQuickResolutionTrampoline(class_linker),
565 GetPortableResolutionTrampoline(class_linker), false);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100566 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800567 bool have_portable_code = false;
568 const void* quick_code = class_linker->GetQuickOatCodeFor(method);
569 const void* portable_code = class_linker->GetPortableOatCodeFor(method, &have_portable_code);
570 UpdateEntrypoints(method, quick_code, portable_code, have_portable_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100571 }
572
573 // If there is no deoptimized method left, we can restore the stack of each thread.
574 if (deoptimized_methods_.empty()) {
575 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
576 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
577 instrumentation_stubs_installed_ = false;
578 }
579 }
580}
581
582bool Instrumentation::IsDeoptimized(mirror::ArtMethod* method) const {
583 DCHECK(method != nullptr);
584 return deoptimized_methods_.count(method);
585}
586
587void Instrumentation::EnableDeoptimization() {
588 CHECK(deoptimized_methods_.empty());
589}
590
591void Instrumentation::DisableDeoptimization() {
592 // If we deoptimized everything, undo it.
593 if (interpreter_stubs_installed_) {
594 UndeoptimizeEverything();
595 }
596 // Undeoptimized selected methods.
597 while (!deoptimized_methods_.empty()) {
598 auto it_begin = deoptimized_methods_.begin();
599 Undeoptimize(*it_begin);
600 }
601 CHECK(deoptimized_methods_.empty());
602}
603
604bool Instrumentation::IsDeoptimizationEnabled() const {
605 return interpreter_stubs_installed_ || !deoptimized_methods_.empty();
606}
607
608void Instrumentation::DeoptimizeEverything() {
609 CHECK(!interpreter_stubs_installed_);
610 ConfigureStubs(false, true);
611}
612
613void Instrumentation::UndeoptimizeEverything() {
614 CHECK(interpreter_stubs_installed_);
615 ConfigureStubs(false, false);
616}
617
618void Instrumentation::EnableMethodTracing() {
619 bool require_interpreter = kDeoptimizeForAccurateMethodEntryExitListeners;
620 ConfigureStubs(!require_interpreter, require_interpreter);
621}
622
623void Instrumentation::DisableMethodTracing() {
624 ConfigureStubs(false, false);
jeffhao725a9572012-11-13 18:20:12 -0800625}
626
Ian Rogersef7d42f2014-01-06 12:55:46 -0800627const void* Instrumentation::GetQuickCodeFor(mirror::ArtMethod* method) const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800628 Runtime* runtime = Runtime::Current();
629 if (LIKELY(!instrumentation_stubs_installed_)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800630 const void* code = method->GetEntryPointFromQuickCompiledCode();
Ian Rogers62d6c772013-02-27 08:32:07 -0800631 DCHECK(code != NULL);
Ian Rogers848871b2013-08-05 10:56:33 -0700632 if (LIKELY(code != GetQuickResolutionTrampoline(runtime->GetClassLinker()) &&
633 code != GetQuickToInterpreterBridge())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800634 return code;
635 }
636 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800637 return runtime->GetClassLinker()->GetQuickOatCodeFor(method);
jeffhao725a9572012-11-13 18:20:12 -0800638}
639
Ian Rogers62d6c772013-02-27 08:32:07 -0800640void Instrumentation::MethodEnterEventImpl(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800641 mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800642 uint32_t dex_pc) const {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700643 auto it = method_entry_listeners_.begin();
Jeff Hao65d15d92013-07-16 16:39:33 -0700644 bool is_end = (it == method_entry_listeners_.end());
645 // Implemented this way to prevent problems caused by modification of the list while iterating.
646 while (!is_end) {
647 InstrumentationListener* cur = *it;
648 ++it;
649 is_end = (it == method_entry_listeners_.end());
650 cur->MethodEntered(thread, this_object, method, dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800651 }
652}
653
654void Instrumentation::MethodExitEventImpl(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800655 mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800656 uint32_t dex_pc, const JValue& return_value) const {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700657 auto it = method_exit_listeners_.begin();
Jeff Hao65d15d92013-07-16 16:39:33 -0700658 bool is_end = (it == method_exit_listeners_.end());
659 // Implemented this way to prevent problems caused by modification of the list while iterating.
660 while (!is_end) {
661 InstrumentationListener* cur = *it;
662 ++it;
663 is_end = (it == method_exit_listeners_.end());
664 cur->MethodExited(thread, this_object, method, dex_pc, return_value);
Ian Rogers62d6c772013-02-27 08:32:07 -0800665 }
666}
667
668void Instrumentation::MethodUnwindEvent(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800669 mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800670 uint32_t dex_pc) const {
671 if (have_method_unwind_listeners_) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700672 for (InstrumentationListener* listener : method_unwind_listeners_) {
Sebastien Hertz51db44a2013-11-19 10:00:29 +0100673 listener->MethodUnwind(thread, this_object, method, dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800674 }
675 }
676}
677
678void Instrumentation::DexPcMovedEventImpl(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800679 mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800680 uint32_t dex_pc) const {
681 // TODO: STL copy-on-write collection? The copy below is due to the debug listener having an
682 // action where it can remove itself as a listener and break the iterator. The copy only works
683 // around the problem and in general we may have to move to something like reference counting to
684 // ensure listeners are deleted correctly.
685 std::list<InstrumentationListener*> copy(dex_pc_listeners_);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700686 for (InstrumentationListener* listener : copy) {
687 listener->DexPcMoved(thread, this_object, method, dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800688 }
689}
690
691void Instrumentation::ExceptionCaughtEvent(Thread* thread, const ThrowLocation& throw_location,
Brian Carlstromea46f952013-07-30 01:26:50 -0700692 mirror::ArtMethod* catch_method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800693 uint32_t catch_dex_pc,
Sebastien Hertz947ff082013-09-17 14:10:13 +0200694 mirror::Throwable* exception_object) const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800695 if (have_exception_caught_listeners_) {
Jeff Haoc0bd4da2013-04-11 15:52:28 -0700696 DCHECK_EQ(thread->GetException(NULL), exception_object);
697 thread->ClearException();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700698 for (InstrumentationListener* listener : exception_caught_listeners_) {
699 listener->ExceptionCaught(thread, throw_location, catch_method, catch_dex_pc, exception_object);
Ian Rogers62d6c772013-02-27 08:32:07 -0800700 }
Jeff Haoc0bd4da2013-04-11 15:52:28 -0700701 thread->SetException(throw_location, exception_object);
Ian Rogers62d6c772013-02-27 08:32:07 -0800702 }
703}
704
705static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame,
706 int delta)
707 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
708 size_t frame_id = StackVisitor::ComputeNumFrames(self) + delta;
709 if (frame_id != instrumentation_frame.frame_id_) {
710 LOG(ERROR) << "Expected frame_id=" << frame_id << " but found "
711 << instrumentation_frame.frame_id_;
712 StackVisitor::DescribeStack(self);
713 CHECK_EQ(frame_id, instrumentation_frame.frame_id_);
714 }
715}
716
717void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700718 mirror::ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -0700719 uintptr_t lr, bool interpreter_entry) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800720 // We have a callee-save frame meaning this value is guaranteed to never be 0.
721 size_t frame_id = StackVisitor::ComputeNumFrames(self);
722 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
723 if (kVerboseInstrumentation) {
Brian Carlstrom2d888622013-07-18 17:02:00 -0700724 LOG(INFO) << "Entering " << PrettyMethod(method) << " from PC " << reinterpret_cast<void*>(lr);
Ian Rogers62d6c772013-02-27 08:32:07 -0800725 }
726 instrumentation::InstrumentationStackFrame instrumentation_frame(this_object, method, lr,
Jeff Hao9a916d32013-06-27 18:45:37 -0700727 frame_id, interpreter_entry);
Ian Rogers62d6c772013-02-27 08:32:07 -0800728 stack->push_front(instrumentation_frame);
729
730 MethodEnterEvent(self, this_object, method, 0);
731}
732
733uint64_t Instrumentation::PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc,
734 uint64_t gpr_result, uint64_t fpr_result) {
735 // Do the pop.
736 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
737 CHECK_GT(stack->size(), 0U);
738 InstrumentationStackFrame instrumentation_frame = stack->front();
739 stack->pop_front();
740
741 // Set return PC and check the sanity of the stack.
742 *return_pc = instrumentation_frame.return_pc_;
743 CheckStackDepth(self, instrumentation_frame, 0);
744
Brian Carlstromea46f952013-07-30 01:26:50 -0700745 mirror::ArtMethod* method = instrumentation_frame.method_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800746 char return_shorty = MethodHelper(method).GetShorty()[0];
747 JValue return_value;
748 if (return_shorty == 'V') {
749 return_value.SetJ(0);
750 } else if (return_shorty == 'F' || return_shorty == 'D') {
751 return_value.SetJ(fpr_result);
752 } else {
753 return_value.SetJ(gpr_result);
754 }
755 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
756 // return_pc.
757 uint32_t dex_pc = DexFile::kDexNoIndex;
758 mirror::Object* this_object = instrumentation_frame.this_object_;
759 MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value);
jeffhao725a9572012-11-13 18:20:12 -0800760
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100761 // Deoptimize if the caller needs to continue execution in the interpreter. Do nothing if we get
762 // back to an upcall.
763 NthCallerVisitor visitor(self, 1, true);
764 visitor.WalkStack(true);
765 bool deoptimize = (visitor.caller != NULL) &&
766 (interpreter_stubs_installed_ || IsDeoptimized(visitor.caller));
767 if (deoptimize && kVerboseInstrumentation) {
768 LOG(INFO) << "Deoptimizing into " << PrettyMethod(visitor.caller);
Ian Rogers62d6c772013-02-27 08:32:07 -0800769 }
770 if (deoptimize) {
771 if (kVerboseInstrumentation) {
772 LOG(INFO) << "Deoptimizing from " << PrettyMethod(method)
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100773 << " result is " << std::hex << return_value.GetJ();
Ian Rogers62d6c772013-02-27 08:32:07 -0800774 }
775 self->SetDeoptimizationReturnValue(return_value);
Ian Rogers848871b2013-08-05 10:56:33 -0700776 return static_cast<uint64_t>(GetQuickDeoptimizationEntryPoint()) |
Ian Rogers62d6c772013-02-27 08:32:07 -0800777 (static_cast<uint64_t>(*return_pc) << 32);
778 } else {
779 if (kVerboseInstrumentation) {
Brian Carlstrom2d888622013-07-18 17:02:00 -0700780 LOG(INFO) << "Returning from " << PrettyMethod(method)
781 << " to PC " << reinterpret_cast<void*>(*return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800782 }
783 return *return_pc;
784 }
jeffhao725a9572012-11-13 18:20:12 -0800785}
786
Ian Rogers62d6c772013-02-27 08:32:07 -0800787void Instrumentation::PopMethodForUnwind(Thread* self, bool is_deoptimization) const {
788 // Do the pop.
789 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
790 CHECK_GT(stack->size(), 0U);
791 InstrumentationStackFrame instrumentation_frame = stack->front();
792 // TODO: bring back CheckStackDepth(self, instrumentation_frame, 2);
793 stack->pop_front();
794
Brian Carlstromea46f952013-07-30 01:26:50 -0700795 mirror::ArtMethod* method = instrumentation_frame.method_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800796 if (is_deoptimization) {
797 if (kVerboseInstrumentation) {
798 LOG(INFO) << "Popping for deoptimization " << PrettyMethod(method);
799 }
800 } else {
801 if (kVerboseInstrumentation) {
802 LOG(INFO) << "Popping for unwind " << PrettyMethod(method);
803 }
804
805 // Notify listeners of method unwind.
806 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
807 // return_pc.
808 uint32_t dex_pc = DexFile::kDexNoIndex;
809 MethodUnwindEvent(self, instrumentation_frame.this_object_, method, dex_pc);
810 }
811}
812
813std::string InstrumentationStackFrame::Dump() const {
814 std::ostringstream os;
815 os << "Frame " << frame_id_ << " " << PrettyMethod(method_) << ":"
816 << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_);
817 return os.str();
818}
819
820} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -0800821} // namespace art