blob: 89a63ac2d93e1d594ee20c590dfac7b43de41083 [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"
Mathieu Chartierd8891782014-03-02 13:28:37 -080026#include "entrypoints/quick/quick_alloc_entrypoints.h"
Sebastien Hertz138dbfc2013-12-04 18:15:25 +010027#include "interpreter/interpreter.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070028#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "mirror/class-inl.h"
30#include "mirror/dex_cache.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031#include "mirror/object_array-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070032#include "mirror/object-inl.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080033#include "nth_caller_visitor.h"
Ian Rogersc928de92013-02-27 14:30:44 -080034#if !defined(ART_USE_PORTABLE_COMPILER)
Ian Rogers166db042013-07-26 12:05:57 -070035#include "entrypoints/quick/quick_entrypoints.h"
jeffhao725a9572012-11-13 18:20:12 -080036#endif
37#include "object_utils.h"
38#include "os.h"
39#include "scoped_thread_state_change.h"
40#include "thread.h"
41#include "thread_list.h"
jeffhao725a9572012-11-13 18:20:12 -080042
43namespace art {
Ian Rogersfa824272013-11-05 16:12:57 -080044
Ian Rogers62d6c772013-02-27 08:32:07 -080045namespace instrumentation {
jeffhao725a9572012-11-13 18:20:12 -080046
Sebastien Hertz5bfd5c92013-11-15 11:36:07 +010047const bool kVerboseInstrumentation = false;
48
Ian Rogers816432e2013-09-06 15:47:45 -070049// Do we want to deoptimize for method entry and exit listeners or just try to intercept
50// invocations? Deoptimization forces all code to run in the interpreter and considerably hurts the
51// application's performance.
Ian Rogers7b6da362013-09-11 09:29:40 -070052static constexpr bool kDeoptimizeForAccurateMethodEntryExitListeners = false;
Ian Rogers816432e2013-09-06 15:47:45 -070053
Ian Rogers62d6c772013-02-27 08:32:07 -080054static bool InstallStubsClassVisitor(mirror::Class* klass, void* arg)
jeffhao725a9572012-11-13 18:20:12 -080055 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -080056 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
57 return instrumentation->InstallStubsForClass(klass);
58}
59
60bool Instrumentation::InstallStubsForClass(mirror::Class* klass) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +010061 for (size_t i = 0, e = klass->NumDirectMethods(); i < e; i++) {
62 InstallStubsForMethod(klass->GetDirectMethod(i));
jeffhao725a9572012-11-13 18:20:12 -080063 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +010064 for (size_t i = 0, e = klass->NumVirtualMethods(); i < e; i++) {
65 InstallStubsForMethod(klass->GetVirtualMethod(i));
jeffhao725a9572012-11-13 18:20:12 -080066 }
67 return true;
68}
69
Ian Rogersef7d42f2014-01-06 12:55:46 -080070static void UpdateEntrypoints(mirror::ArtMethod* method, const void* quick_code,
71 const void* portable_code, bool have_portable_code)
72 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
73 method->SetEntryPointFromPortableCompiledCode(portable_code);
74 method->SetEntryPointFromQuickCompiledCode(quick_code);
75 bool portable_enabled = method->IsPortableCompiled();
76 if (have_portable_code && !portable_enabled) {
77 method->SetIsPortableCompiled();
78 } else if (portable_enabled) {
79 method->ClearIsPortableCompiled();
80 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +010081 if (!method->IsResolutionMethod()) {
Hiroshi Yamauchi563b47c2014-02-28 17:18:37 -080082 if (quick_code == GetQuickToInterpreterBridge() ||
83 (quick_code == GetQuickResolutionTrampoline(Runtime::Current()->GetClassLinker()) &&
84 Runtime::Current()->GetInstrumentation()->IsForcedInterpretOnly()
85 && !method->IsNative() && !method->IsProxyMethod())) {
86 if (kIsDebugBuild) {
87 if (quick_code == GetQuickToInterpreterBridge()) {
88 DCHECK(portable_code == GetPortableToInterpreterBridge());
89 } else if (quick_code == GetQuickResolutionTrampoline(Runtime::Current()->GetClassLinker())) {
90 DCHECK(portable_code == GetPortableResolutionTrampoline(Runtime::Current()->GetClassLinker()));
91 }
92 }
Ian Rogersef7d42f2014-01-06 12:55:46 -080093 DCHECK(!method->IsNative()) << PrettyMethod(method);
Hiroshi Yamauchi563b47c2014-02-28 17:18:37 -080094 DCHECK(!method->IsProxyMethod()) << PrettyMethod(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +010095 method->SetEntryPointFromInterpreter(art::interpreter::artInterpreterToInterpreterBridge);
96 } else {
97 method->SetEntryPointFromInterpreter(art::artInterpreterToCompiledCodeBridge);
98 }
99 }
100}
101
102void Instrumentation::InstallStubsForMethod(mirror::ArtMethod* method) {
103 if (method->IsAbstract() || method->IsProxyMethod()) {
104 // Do not change stubs for these methods.
105 return;
106 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800107 const void* new_portable_code;
108 const void* new_quick_code;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100109 bool uninstall = !entry_exit_stubs_installed_ && !interpreter_stubs_installed_;
110 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
111 bool is_class_initialized = method->GetDeclaringClass()->IsInitialized();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800112 bool have_portable_code = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100113 if (uninstall) {
114 if ((forced_interpret_only_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800115 new_portable_code = GetPortableToInterpreterBridge();
116 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100117 } else if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800118 new_portable_code = class_linker->GetPortableOatCodeFor(method, &have_portable_code);
119 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100120 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800121 new_portable_code = GetPortableResolutionTrampoline(class_linker);
122 new_quick_code = GetQuickResolutionTrampoline(class_linker);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100123 }
124 } else { // !uninstall
125 if ((interpreter_stubs_installed_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800126 new_portable_code = GetPortableToInterpreterBridge();
127 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100128 } else {
129 // Do not overwrite resolution trampoline. When the trampoline initializes the method's
130 // class, all its static methods code will be set to the instrumentation entry point.
131 // For more details, see ClassLinker::FixupStaticTrampolines.
132 if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
133 // Do not overwrite interpreter to prevent from posting method entry/exit events twice.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800134 new_portable_code = class_linker->GetPortableOatCodeFor(method, &have_portable_code);
135 new_quick_code = class_linker->GetQuickOatCodeFor(method);
136 if (entry_exit_stubs_installed_ && new_quick_code != GetQuickToInterpreterBridge()) {
137 DCHECK(new_portable_code != GetPortableToInterpreterBridge());
138 new_portable_code = GetPortableToInterpreterBridge();
139 new_quick_code = GetQuickInstrumentationEntryPoint();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100140 }
141 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800142 new_portable_code = GetPortableResolutionTrampoline(class_linker);
143 new_quick_code = GetQuickResolutionTrampoline(class_linker);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100144 }
145 }
146 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800147 UpdateEntrypoints(method, new_quick_code, new_portable_code, have_portable_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100148}
149
Ian Rogers62d6c772013-02-27 08:32:07 -0800150// Places the instrumentation exit pc as the return PC for every quick frame. This also allows
151// deoptimization of quick frames to interpreter frames.
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100152// Since we may already have done this previously, we need to push new instrumentation frame before
153// existing instrumentation frames.
Ian Rogers62d6c772013-02-27 08:32:07 -0800154static void InstrumentationInstallStack(Thread* thread, void* arg)
Ian Rogers306057f2012-11-26 12:45:53 -0800155 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
156 struct InstallStackVisitor : public StackVisitor {
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100157 InstallStackVisitor(Thread* thread, Context* context, uintptr_t instrumentation_exit_pc)
Ian Rogers62d6c772013-02-27 08:32:07 -0800158 : StackVisitor(thread, context), instrumentation_stack_(thread->GetInstrumentationStack()),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100159 existing_instrumentation_frames_count_(instrumentation_stack_->size()),
160 instrumentation_exit_pc_(instrumentation_exit_pc),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100161 reached_existing_instrumentation_frames_(false), instrumentation_stack_depth_(0),
162 last_return_pc_(0) {
163 }
jeffhao725a9572012-11-13 18:20:12 -0800164
Ian Rogers306057f2012-11-26 12:45:53 -0800165 virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700166 mirror::ArtMethod* m = GetMethod();
Ian Rogers306057f2012-11-26 12:45:53 -0800167 if (GetCurrentQuickFrame() == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800168 if (kVerboseInstrumentation) {
169 LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId()
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100170 << " Method=" << PrettyMethod(m);
Ian Rogers62d6c772013-02-27 08:32:07 -0800171 }
Ian Rogers306057f2012-11-26 12:45:53 -0800172 return true; // Ignore shadow frames.
173 }
Ian Rogers306057f2012-11-26 12:45:53 -0800174 if (m == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800175 if (kVerboseInstrumentation) {
176 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
177 }
178 last_return_pc_ = 0;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700179 return true; // Ignore upcalls.
Ian Rogers306057f2012-11-26 12:45:53 -0800180 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800181 if (m->IsRuntimeMethod()) {
182 if (kVerboseInstrumentation) {
183 LOG(INFO) << " Skipping runtime method. Frame " << GetFrameId();
184 }
185 last_return_pc_ = GetReturnPc();
Ian Rogers306057f2012-11-26 12:45:53 -0800186 return true; // Ignore unresolved methods since they will be instrumented after resolution.
187 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800188 if (kVerboseInstrumentation) {
189 LOG(INFO) << " Installing exit stub in " << DescribeLocation();
190 }
191 uintptr_t return_pc = GetReturnPc();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100192 if (return_pc == instrumentation_exit_pc_) {
193 // We've reached a frame which has already been installed with instrumentation exit stub.
194 // We should have already installed instrumentation on previous frames.
195 reached_existing_instrumentation_frames_ = true;
196
197 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
198 const InstrumentationStackFrame& frame = instrumentation_stack_->at(instrumentation_stack_depth_);
199 CHECK_EQ(m, frame.method_) << "Expected " << PrettyMethod(m)
200 << ", Found " << PrettyMethod(frame.method_);
201 return_pc = frame.return_pc_;
202 if (kVerboseInstrumentation) {
203 LOG(INFO) << "Ignoring already instrumented " << frame.Dump();
204 }
205 } else {
206 CHECK_NE(return_pc, 0U);
207 CHECK(!reached_existing_instrumentation_frames_);
208 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, return_pc, GetFrameId(),
209 false);
210 if (kVerboseInstrumentation) {
211 LOG(INFO) << "Pushing frame " << instrumentation_frame.Dump();
212 }
213
214 // Insert frame before old ones so we do not corrupt the instrumentation stack.
215 auto it = instrumentation_stack_->end() - existing_instrumentation_frames_count_;
216 instrumentation_stack_->insert(it, instrumentation_frame);
217 SetReturnPc(instrumentation_exit_pc_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800218 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800219 dex_pcs_.push_back(m->ToDexPc(last_return_pc_));
Ian Rogers62d6c772013-02-27 08:32:07 -0800220 last_return_pc_ = return_pc;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100221 ++instrumentation_stack_depth_;
Ian Rogers306057f2012-11-26 12:45:53 -0800222 return true; // Continue.
223 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800224 std::deque<InstrumentationStackFrame>* const instrumentation_stack_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100225 const size_t existing_instrumentation_frames_count_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800226 std::vector<uint32_t> dex_pcs_;
Ian Rogers306057f2012-11-26 12:45:53 -0800227 const uintptr_t instrumentation_exit_pc_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100228 bool reached_existing_instrumentation_frames_;
229 size_t instrumentation_stack_depth_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800230 uintptr_t last_return_pc_;
Ian Rogers306057f2012-11-26 12:45:53 -0800231 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800232 if (kVerboseInstrumentation) {
233 std::string thread_name;
234 thread->GetThreadName(thread_name);
235 LOG(INFO) << "Installing exit stubs in " << thread_name;
Ian Rogers306057f2012-11-26 12:45:53 -0800236 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100237
238 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800239 UniquePtr<Context> context(Context::Create());
Ian Rogers848871b2013-08-05 10:56:33 -0700240 uintptr_t instrumentation_exit_pc = GetQuickInstrumentationExitPc();
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100241 InstallStackVisitor visitor(thread, context.get(), instrumentation_exit_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800242 visitor.WalkStack(true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100243 CHECK_EQ(visitor.dex_pcs_.size(), thread->GetInstrumentationStack()->size());
Ian Rogers62d6c772013-02-27 08:32:07 -0800244
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100245 if (!instrumentation->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100246 // Create method enter events for all methods currently on the thread's stack. We only do this
247 // if no debugger is attached to prevent from posting events twice.
248 typedef std::deque<InstrumentationStackFrame>::const_reverse_iterator It;
249 for (It it = thread->GetInstrumentationStack()->rbegin(),
250 end = thread->GetInstrumentationStack()->rend(); it != end; ++it) {
251 mirror::Object* this_object = (*it).this_object_;
252 mirror::ArtMethod* method = (*it).method_;
253 uint32_t dex_pc = visitor.dex_pcs_.back();
254 visitor.dex_pcs_.pop_back();
255 instrumentation->MethodEnterEvent(thread, this_object, method, dex_pc);
256 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800257 }
258 thread->VerifyStack();
Ian Rogers306057f2012-11-26 12:45:53 -0800259}
260
Ian Rogers62d6c772013-02-27 08:32:07 -0800261// Removes the instrumentation exit pc as the return PC for every quick frame.
262static void InstrumentationRestoreStack(Thread* thread, void* arg)
Ian Rogers306057f2012-11-26 12:45:53 -0800263 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
264 struct RestoreStackVisitor : public StackVisitor {
Ian Rogers62d6c772013-02-27 08:32:07 -0800265 RestoreStackVisitor(Thread* thread, uintptr_t instrumentation_exit_pc,
266 Instrumentation* instrumentation)
267 : StackVisitor(thread, NULL), thread_(thread),
268 instrumentation_exit_pc_(instrumentation_exit_pc),
269 instrumentation_(instrumentation),
270 instrumentation_stack_(thread->GetInstrumentationStack()),
271 frames_removed_(0) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800272
273 virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800274 if (instrumentation_stack_->size() == 0) {
jeffhao725a9572012-11-13 18:20:12 -0800275 return false; // Stop.
276 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700277 mirror::ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800278 if (GetCurrentQuickFrame() == NULL) {
279 if (kVerboseInstrumentation) {
280 LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId() << " Method=" << PrettyMethod(m);
281 }
282 return true; // Ignore shadow frames.
283 }
Ian Rogers306057f2012-11-26 12:45:53 -0800284 if (m == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800285 if (kVerboseInstrumentation) {
286 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
287 }
Ian Rogers306057f2012-11-26 12:45:53 -0800288 return true; // Ignore upcalls.
289 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800290 bool removed_stub = false;
291 // TODO: make this search more efficient?
Mathieu Chartier02e25112013-08-14 16:14:24 -0700292 for (InstrumentationStackFrame instrumentation_frame : *instrumentation_stack_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800293 if (instrumentation_frame.frame_id_ == GetFrameId()) {
294 if (kVerboseInstrumentation) {
295 LOG(INFO) << " Removing exit stub in " << DescribeLocation();
296 }
Jeff Hao9a916d32013-06-27 18:45:37 -0700297 if (instrumentation_frame.interpreter_entry_) {
298 CHECK(m == Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
299 } else {
300 CHECK(m == instrumentation_frame.method_) << PrettyMethod(m);
301 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800302 SetReturnPc(instrumentation_frame.return_pc_);
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100303 if (!instrumentation_->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100304 // Create the method exit events. As the methods didn't really exit the result is 0.
305 // We only do this if no debugger is attached to prevent from posting events twice.
306 instrumentation_->MethodExitEvent(thread_, instrumentation_frame.this_object_, m,
307 GetDexPc(), JValue());
308 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800309 frames_removed_++;
310 removed_stub = true;
311 break;
312 }
313 }
314 if (!removed_stub) {
315 if (kVerboseInstrumentation) {
316 LOG(INFO) << " No exit stub in " << DescribeLocation();
Ian Rogers306057f2012-11-26 12:45:53 -0800317 }
jeffhao725a9572012-11-13 18:20:12 -0800318 }
319 return true; // Continue.
320 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800321 Thread* const thread_;
Ian Rogers306057f2012-11-26 12:45:53 -0800322 const uintptr_t instrumentation_exit_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800323 Instrumentation* const instrumentation_;
324 std::deque<instrumentation::InstrumentationStackFrame>* const instrumentation_stack_;
325 size_t frames_removed_;
jeffhao725a9572012-11-13 18:20:12 -0800326 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800327 if (kVerboseInstrumentation) {
328 std::string thread_name;
329 thread->GetThreadName(thread_name);
330 LOG(INFO) << "Removing exit stubs in " << thread_name;
331 }
332 std::deque<instrumentation::InstrumentationStackFrame>* stack = thread->GetInstrumentationStack();
333 if (stack->size() > 0) {
334 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers848871b2013-08-05 10:56:33 -0700335 uintptr_t instrumentation_exit_pc = GetQuickInstrumentationExitPc();
Ian Rogers62d6c772013-02-27 08:32:07 -0800336 RestoreStackVisitor visitor(thread, instrumentation_exit_pc, instrumentation);
337 visitor.WalkStack(true);
338 CHECK_EQ(visitor.frames_removed_, stack->size());
339 while (stack->size() > 0) {
340 stack->pop_front();
341 }
jeffhao725a9572012-11-13 18:20:12 -0800342 }
343}
344
Ian Rogers62d6c772013-02-27 08:32:07 -0800345void Instrumentation::AddListener(InstrumentationListener* listener, uint32_t events) {
346 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Ian Rogers62d6c772013-02-27 08:32:07 -0800347 if ((events & kMethodEntered) != 0) {
348 method_entry_listeners_.push_back(listener);
Ian Rogers62d6c772013-02-27 08:32:07 -0800349 have_method_entry_listeners_ = true;
350 }
351 if ((events & kMethodExited) != 0) {
352 method_exit_listeners_.push_back(listener);
Ian Rogers62d6c772013-02-27 08:32:07 -0800353 have_method_exit_listeners_ = true;
354 }
355 if ((events & kMethodUnwind) != 0) {
356 method_unwind_listeners_.push_back(listener);
357 have_method_unwind_listeners_ = true;
358 }
359 if ((events & kDexPcMoved) != 0) {
360 dex_pc_listeners_.push_back(listener);
Ian Rogers62d6c772013-02-27 08:32:07 -0800361 have_dex_pc_listeners_ = true;
362 }
Jeff Hao14dd5a82013-04-11 10:23:36 -0700363 if ((events & kExceptionCaught) != 0) {
364 exception_caught_listeners_.push_back(listener);
365 have_exception_caught_listeners_ = true;
366 }
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200367 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800368}
369
Ian Rogers62d6c772013-02-27 08:32:07 -0800370void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) {
371 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Ian Rogers62d6c772013-02-27 08:32:07 -0800372
373 if ((events & kMethodEntered) != 0) {
374 bool contains = std::find(method_entry_listeners_.begin(), method_entry_listeners_.end(),
375 listener) != method_entry_listeners_.end();
376 if (contains) {
377 method_entry_listeners_.remove(listener);
378 }
379 have_method_entry_listeners_ = method_entry_listeners_.size() > 0;
Ian Rogers62d6c772013-02-27 08:32:07 -0800380 }
381 if ((events & kMethodExited) != 0) {
382 bool contains = std::find(method_exit_listeners_.begin(), method_exit_listeners_.end(),
383 listener) != method_exit_listeners_.end();
384 if (contains) {
385 method_exit_listeners_.remove(listener);
386 }
387 have_method_exit_listeners_ = method_exit_listeners_.size() > 0;
Ian Rogers62d6c772013-02-27 08:32:07 -0800388 }
389 if ((events & kMethodUnwind) != 0) {
390 method_unwind_listeners_.remove(listener);
391 }
392 if ((events & kDexPcMoved) != 0) {
393 bool contains = std::find(dex_pc_listeners_.begin(), dex_pc_listeners_.end(),
394 listener) != dex_pc_listeners_.end();
395 if (contains) {
396 dex_pc_listeners_.remove(listener);
397 }
398 have_dex_pc_listeners_ = dex_pc_listeners_.size() > 0;
Ian Rogers62d6c772013-02-27 08:32:07 -0800399 }
Jeff Hao14dd5a82013-04-11 10:23:36 -0700400 if ((events & kExceptionCaught) != 0) {
401 exception_caught_listeners_.remove(listener);
402 have_exception_caught_listeners_ = exception_caught_listeners_.size() > 0;
403 }
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200404 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800405}
406
Ian Rogers62d6c772013-02-27 08:32:07 -0800407void Instrumentation::ConfigureStubs(bool require_entry_exit_stubs, bool require_interpreter) {
408 interpret_only_ = require_interpreter || forced_interpret_only_;
409 // Compute what level of instrumentation is required and compare to current.
410 int desired_level, current_level;
411 if (require_interpreter) {
412 desired_level = 2;
413 } else if (require_entry_exit_stubs) {
414 desired_level = 1;
415 } else {
416 desired_level = 0;
417 }
418 if (interpreter_stubs_installed_) {
419 current_level = 2;
420 } else if (entry_exit_stubs_installed_) {
421 current_level = 1;
422 } else {
423 current_level = 0;
424 }
425 if (desired_level == current_level) {
426 // We're already set.
427 return;
428 }
429 Thread* self = Thread::Current();
430 Runtime* runtime = Runtime::Current();
431 Locks::thread_list_lock_->AssertNotHeld(self);
432 if (desired_level > 0) {
433 if (require_interpreter) {
434 interpreter_stubs_installed_ = true;
435 } else {
436 CHECK(require_entry_exit_stubs);
437 entry_exit_stubs_installed_ = true;
438 }
439 runtime->GetClassLinker()->VisitClasses(InstallStubsClassVisitor, this);
440 instrumentation_stubs_installed_ = true;
441 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
442 runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this);
443 } else {
444 interpreter_stubs_installed_ = false;
445 entry_exit_stubs_installed_ = false;
446 runtime->GetClassLinker()->VisitClasses(InstallStubsClassVisitor, this);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100447 // Restore stack only if there is no method currently deoptimized.
448 if (deoptimized_methods_.empty()) {
449 instrumentation_stubs_installed_ = false;
450 MutexLock mu(self, *Locks::thread_list_lock_);
451 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
452 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800453 }
jeffhao725a9572012-11-13 18:20:12 -0800454}
455
Ian Rogersfa824272013-11-05 16:12:57 -0800456static void ResetQuickAllocEntryPointsForThread(Thread* thread, void* arg) {
457 thread->ResetQuickAllocEntryPointsForThread();
458}
459
Mathieu Chartier661974a2014-01-09 11:23:53 -0800460void Instrumentation::SetEntrypointsInstrumented(bool instrumented) {
461 Runtime* runtime = Runtime::Current();
462 ThreadList* tl = runtime->GetThreadList();
463 if (runtime->IsStarted()) {
464 tl->SuspendAll();
465 }
466 {
467 MutexLock mu(Thread::Current(), *Locks::runtime_shutdown_lock_);
468 SetQuickAllocEntryPointsInstrumented(instrumented);
469 ResetQuickAllocEntryPoints();
470 }
471 if (runtime->IsStarted()) {
472 tl->ResumeAll();
473 }
474}
475
Ian Rogersfa824272013-11-05 16:12:57 -0800476void Instrumentation::InstrumentQuickAllocEntryPoints() {
477 // TODO: the read of quick_alloc_entry_points_instrumentation_counter_ is racey and this code
478 // should be guarded by a lock.
Ian Rogersb122a4b2013-11-19 18:00:50 -0800479 DCHECK_GE(quick_alloc_entry_points_instrumentation_counter_.Load(), 0);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800480 const bool enable_instrumentation =
Ian Rogersb122a4b2013-11-19 18:00:50 -0800481 quick_alloc_entry_points_instrumentation_counter_.FetchAndAdd(1) == 0;
Ian Rogersfa824272013-11-05 16:12:57 -0800482 if (enable_instrumentation) {
Mathieu Chartier661974a2014-01-09 11:23:53 -0800483 SetEntrypointsInstrumented(true);
Ian Rogersfa824272013-11-05 16:12:57 -0800484 }
485}
486
487void Instrumentation::UninstrumentQuickAllocEntryPoints() {
488 // TODO: the read of quick_alloc_entry_points_instrumentation_counter_ is racey and this code
489 // should be guarded by a lock.
Ian Rogersb122a4b2013-11-19 18:00:50 -0800490 DCHECK_GT(quick_alloc_entry_points_instrumentation_counter_.Load(), 0);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800491 const bool disable_instrumentation =
Ian Rogersb122a4b2013-11-19 18:00:50 -0800492 quick_alloc_entry_points_instrumentation_counter_.FetchAndSub(1) == 1;
Ian Rogersfa824272013-11-05 16:12:57 -0800493 if (disable_instrumentation) {
Mathieu Chartier661974a2014-01-09 11:23:53 -0800494 SetEntrypointsInstrumented(false);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800495 }
496}
497
498void Instrumentation::ResetQuickAllocEntryPoints() {
499 Runtime* runtime = Runtime::Current();
500 if (runtime->IsStarted()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800501 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
502 runtime->GetThreadList()->ForEach(ResetQuickAllocEntryPointsForThread, NULL);
Ian Rogersfa824272013-11-05 16:12:57 -0800503 }
504}
505
Ian Rogersef7d42f2014-01-06 12:55:46 -0800506void Instrumentation::UpdateMethodsCode(mirror::ArtMethod* method, const void* quick_code,
507 const void* portable_code, bool have_portable_code) const {
508 const void* new_portable_code;
509 const void* new_quick_code;
510 bool new_have_portable_code;
Ian Rogers62d6c772013-02-27 08:32:07 -0800511 if (LIKELY(!instrumentation_stubs_installed_)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800512 new_portable_code = portable_code;
513 new_quick_code = quick_code;
514 new_have_portable_code = have_portable_code;
Jeff Hao65d15d92013-07-16 16:39:33 -0700515 } else {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100516 if ((interpreter_stubs_installed_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800517 new_portable_code = GetPortableToInterpreterBridge();
518 new_quick_code = GetQuickToInterpreterBridge();
519 new_have_portable_code = false;
520 } else if (quick_code == GetQuickResolutionTrampoline(Runtime::Current()->GetClassLinker()) ||
521 quick_code == GetQuickToInterpreterBridge()) {
522 DCHECK((portable_code == GetPortableResolutionTrampoline(Runtime::Current()->GetClassLinker())) ||
523 (portable_code == GetPortableToInterpreterBridge()));
524 new_portable_code = portable_code;
525 new_quick_code = quick_code;
526 new_have_portable_code = have_portable_code;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100527 } else if (entry_exit_stubs_installed_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800528 new_quick_code = GetQuickInstrumentationEntryPoint();
529 new_portable_code = GetPortableToInterpreterBridge();
530 new_have_portable_code = false;
Jeff Hao65d15d92013-07-16 16:39:33 -0700531 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800532 new_portable_code = portable_code;
533 new_quick_code = quick_code;
534 new_have_portable_code = have_portable_code;
Jeff Hao65d15d92013-07-16 16:39:33 -0700535 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800536 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800537 UpdateEntrypoints(method, new_quick_code, new_portable_code, new_have_portable_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100538}
539
540void Instrumentation::Deoptimize(mirror::ArtMethod* method) {
541 CHECK(!method->IsNative());
542 CHECK(!method->IsProxyMethod());
543 CHECK(!method->IsAbstract());
544
545 std::pair<std::set<mirror::ArtMethod*>::iterator, bool> pair = deoptimized_methods_.insert(method);
546 bool already_deoptimized = !pair.second;
547 CHECK(!already_deoptimized) << "Method " << PrettyMethod(method) << " is already deoptimized";
548
549 if (!interpreter_stubs_installed_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800550 UpdateEntrypoints(method, GetQuickToInterpreterBridge(), GetPortableToInterpreterBridge(),
551 false);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100552
553 // Install instrumentation exit stub and instrumentation frames. We may already have installed
554 // these previously so it will only cover the newly created frames.
555 instrumentation_stubs_installed_ = true;
556 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
557 Runtime::Current()->GetThreadList()->ForEach(InstrumentationInstallStack, this);
558 }
559}
560
561void Instrumentation::Undeoptimize(mirror::ArtMethod* method) {
562 CHECK(!method->IsNative());
563 CHECK(!method->IsProxyMethod());
564 CHECK(!method->IsAbstract());
565
566 auto it = deoptimized_methods_.find(method);
567 CHECK(it != deoptimized_methods_.end()) << "Method " << PrettyMethod(method) << " is not deoptimized";
568 deoptimized_methods_.erase(it);
569
570 // Restore code and possibly stack only if we did not deoptimize everything.
571 if (!interpreter_stubs_installed_) {
572 // Restore its code or resolution trampoline.
573 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800574 if (method->IsStatic() && !method->IsConstructor() &&
575 !method->GetDeclaringClass()->IsInitialized()) {
576 UpdateEntrypoints(method, GetQuickResolutionTrampoline(class_linker),
577 GetPortableResolutionTrampoline(class_linker), false);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100578 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800579 bool have_portable_code = false;
580 const void* quick_code = class_linker->GetQuickOatCodeFor(method);
581 const void* portable_code = class_linker->GetPortableOatCodeFor(method, &have_portable_code);
582 UpdateEntrypoints(method, quick_code, portable_code, have_portable_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100583 }
584
585 // If there is no deoptimized method left, we can restore the stack of each thread.
586 if (deoptimized_methods_.empty()) {
587 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
588 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
589 instrumentation_stubs_installed_ = false;
590 }
591 }
592}
593
594bool Instrumentation::IsDeoptimized(mirror::ArtMethod* method) const {
595 DCHECK(method != nullptr);
596 return deoptimized_methods_.count(method);
597}
598
599void Instrumentation::EnableDeoptimization() {
600 CHECK(deoptimized_methods_.empty());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100601 CHECK_EQ(deoptimization_enabled_, false);
602 deoptimization_enabled_ = true;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100603}
604
605void Instrumentation::DisableDeoptimization() {
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100606 CHECK_EQ(deoptimization_enabled_, true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100607 // If we deoptimized everything, undo it.
608 if (interpreter_stubs_installed_) {
609 UndeoptimizeEverything();
610 }
611 // Undeoptimized selected methods.
612 while (!deoptimized_methods_.empty()) {
613 auto it_begin = deoptimized_methods_.begin();
614 Undeoptimize(*it_begin);
615 }
616 CHECK(deoptimized_methods_.empty());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100617 deoptimization_enabled_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100618}
619
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100620// Indicates if instrumentation should notify method enter/exit events to the listeners.
621bool Instrumentation::ShouldNotifyMethodEnterExitEvents() const {
622 return deoptimization_enabled_ || interpreter_stubs_installed_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100623}
624
625void Instrumentation::DeoptimizeEverything() {
626 CHECK(!interpreter_stubs_installed_);
627 ConfigureStubs(false, true);
628}
629
630void Instrumentation::UndeoptimizeEverything() {
631 CHECK(interpreter_stubs_installed_);
632 ConfigureStubs(false, false);
633}
634
635void Instrumentation::EnableMethodTracing() {
636 bool require_interpreter = kDeoptimizeForAccurateMethodEntryExitListeners;
637 ConfigureStubs(!require_interpreter, require_interpreter);
638}
639
640void Instrumentation::DisableMethodTracing() {
641 ConfigureStubs(false, false);
jeffhao725a9572012-11-13 18:20:12 -0800642}
643
Ian Rogersef7d42f2014-01-06 12:55:46 -0800644const void* Instrumentation::GetQuickCodeFor(mirror::ArtMethod* method) const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800645 Runtime* runtime = Runtime::Current();
646 if (LIKELY(!instrumentation_stubs_installed_)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800647 const void* code = method->GetEntryPointFromQuickCompiledCode();
Ian Rogers62d6c772013-02-27 08:32:07 -0800648 DCHECK(code != NULL);
Ian Rogers848871b2013-08-05 10:56:33 -0700649 if (LIKELY(code != GetQuickResolutionTrampoline(runtime->GetClassLinker()) &&
650 code != GetQuickToInterpreterBridge())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800651 return code;
652 }
653 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800654 return runtime->GetClassLinker()->GetQuickOatCodeFor(method);
jeffhao725a9572012-11-13 18:20:12 -0800655}
656
Ian Rogers62d6c772013-02-27 08:32:07 -0800657void Instrumentation::MethodEnterEventImpl(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800658 mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800659 uint32_t dex_pc) const {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700660 auto it = method_entry_listeners_.begin();
Jeff Hao65d15d92013-07-16 16:39:33 -0700661 bool is_end = (it == method_entry_listeners_.end());
662 // Implemented this way to prevent problems caused by modification of the list while iterating.
663 while (!is_end) {
664 InstrumentationListener* cur = *it;
665 ++it;
666 is_end = (it == method_entry_listeners_.end());
667 cur->MethodEntered(thread, this_object, method, dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800668 }
669}
670
671void Instrumentation::MethodExitEventImpl(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800672 mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800673 uint32_t dex_pc, const JValue& return_value) const {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700674 auto it = method_exit_listeners_.begin();
Jeff Hao65d15d92013-07-16 16:39:33 -0700675 bool is_end = (it == method_exit_listeners_.end());
676 // Implemented this way to prevent problems caused by modification of the list while iterating.
677 while (!is_end) {
678 InstrumentationListener* cur = *it;
679 ++it;
680 is_end = (it == method_exit_listeners_.end());
681 cur->MethodExited(thread, this_object, method, dex_pc, return_value);
Ian Rogers62d6c772013-02-27 08:32:07 -0800682 }
683}
684
685void Instrumentation::MethodUnwindEvent(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800686 mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800687 uint32_t dex_pc) const {
688 if (have_method_unwind_listeners_) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700689 for (InstrumentationListener* listener : method_unwind_listeners_) {
Sebastien Hertz51db44a2013-11-19 10:00:29 +0100690 listener->MethodUnwind(thread, this_object, method, dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800691 }
692 }
693}
694
695void Instrumentation::DexPcMovedEventImpl(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800696 mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800697 uint32_t dex_pc) const {
698 // TODO: STL copy-on-write collection? The copy below is due to the debug listener having an
699 // action where it can remove itself as a listener and break the iterator. The copy only works
700 // around the problem and in general we may have to move to something like reference counting to
701 // ensure listeners are deleted correctly.
702 std::list<InstrumentationListener*> copy(dex_pc_listeners_);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700703 for (InstrumentationListener* listener : copy) {
704 listener->DexPcMoved(thread, this_object, method, dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800705 }
706}
707
708void Instrumentation::ExceptionCaughtEvent(Thread* thread, const ThrowLocation& throw_location,
Brian Carlstromea46f952013-07-30 01:26:50 -0700709 mirror::ArtMethod* catch_method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800710 uint32_t catch_dex_pc,
Sebastien Hertz947ff082013-09-17 14:10:13 +0200711 mirror::Throwable* exception_object) const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800712 if (have_exception_caught_listeners_) {
Jeff Haoc0bd4da2013-04-11 15:52:28 -0700713 DCHECK_EQ(thread->GetException(NULL), exception_object);
714 thread->ClearException();
Mathieu Chartier02e25112013-08-14 16:14:24 -0700715 for (InstrumentationListener* listener : exception_caught_listeners_) {
716 listener->ExceptionCaught(thread, throw_location, catch_method, catch_dex_pc, exception_object);
Ian Rogers62d6c772013-02-27 08:32:07 -0800717 }
Jeff Haoc0bd4da2013-04-11 15:52:28 -0700718 thread->SetException(throw_location, exception_object);
Ian Rogers62d6c772013-02-27 08:32:07 -0800719 }
720}
721
722static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame,
723 int delta)
724 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
725 size_t frame_id = StackVisitor::ComputeNumFrames(self) + delta;
726 if (frame_id != instrumentation_frame.frame_id_) {
727 LOG(ERROR) << "Expected frame_id=" << frame_id << " but found "
728 << instrumentation_frame.frame_id_;
729 StackVisitor::DescribeStack(self);
730 CHECK_EQ(frame_id, instrumentation_frame.frame_id_);
731 }
732}
733
734void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700735 mirror::ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -0700736 uintptr_t lr, bool interpreter_entry) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800737 // We have a callee-save frame meaning this value is guaranteed to never be 0.
738 size_t frame_id = StackVisitor::ComputeNumFrames(self);
739 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
740 if (kVerboseInstrumentation) {
Brian Carlstrom2d888622013-07-18 17:02:00 -0700741 LOG(INFO) << "Entering " << PrettyMethod(method) << " from PC " << reinterpret_cast<void*>(lr);
Ian Rogers62d6c772013-02-27 08:32:07 -0800742 }
743 instrumentation::InstrumentationStackFrame instrumentation_frame(this_object, method, lr,
Jeff Hao9a916d32013-06-27 18:45:37 -0700744 frame_id, interpreter_entry);
Ian Rogers62d6c772013-02-27 08:32:07 -0800745 stack->push_front(instrumentation_frame);
746
747 MethodEnterEvent(self, this_object, method, 0);
748}
749
750uint64_t Instrumentation::PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc,
751 uint64_t gpr_result, uint64_t fpr_result) {
752 // Do the pop.
753 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
754 CHECK_GT(stack->size(), 0U);
755 InstrumentationStackFrame instrumentation_frame = stack->front();
756 stack->pop_front();
757
758 // Set return PC and check the sanity of the stack.
759 *return_pc = instrumentation_frame.return_pc_;
760 CheckStackDepth(self, instrumentation_frame, 0);
761
Brian Carlstromea46f952013-07-30 01:26:50 -0700762 mirror::ArtMethod* method = instrumentation_frame.method_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800763 char return_shorty = MethodHelper(method).GetShorty()[0];
764 JValue return_value;
765 if (return_shorty == 'V') {
766 return_value.SetJ(0);
767 } else if (return_shorty == 'F' || return_shorty == 'D') {
768 return_value.SetJ(fpr_result);
769 } else {
770 return_value.SetJ(gpr_result);
771 }
772 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
773 // return_pc.
774 uint32_t dex_pc = DexFile::kDexNoIndex;
775 mirror::Object* this_object = instrumentation_frame.this_object_;
776 MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value);
jeffhao725a9572012-11-13 18:20:12 -0800777
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100778 // Deoptimize if the caller needs to continue execution in the interpreter. Do nothing if we get
779 // back to an upcall.
780 NthCallerVisitor visitor(self, 1, true);
781 visitor.WalkStack(true);
782 bool deoptimize = (visitor.caller != NULL) &&
783 (interpreter_stubs_installed_ || IsDeoptimized(visitor.caller));
784 if (deoptimize && kVerboseInstrumentation) {
785 LOG(INFO) << "Deoptimizing into " << PrettyMethod(visitor.caller);
Ian Rogers62d6c772013-02-27 08:32:07 -0800786 }
787 if (deoptimize) {
788 if (kVerboseInstrumentation) {
789 LOG(INFO) << "Deoptimizing from " << PrettyMethod(method)
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100790 << " result is " << std::hex << return_value.GetJ();
Ian Rogers62d6c772013-02-27 08:32:07 -0800791 }
792 self->SetDeoptimizationReturnValue(return_value);
Ian Rogers848871b2013-08-05 10:56:33 -0700793 return static_cast<uint64_t>(GetQuickDeoptimizationEntryPoint()) |
Ian Rogers62d6c772013-02-27 08:32:07 -0800794 (static_cast<uint64_t>(*return_pc) << 32);
795 } else {
796 if (kVerboseInstrumentation) {
Brian Carlstrom2d888622013-07-18 17:02:00 -0700797 LOG(INFO) << "Returning from " << PrettyMethod(method)
798 << " to PC " << reinterpret_cast<void*>(*return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800799 }
800 return *return_pc;
801 }
jeffhao725a9572012-11-13 18:20:12 -0800802}
803
Ian Rogers62d6c772013-02-27 08:32:07 -0800804void Instrumentation::PopMethodForUnwind(Thread* self, bool is_deoptimization) const {
805 // Do the pop.
806 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
807 CHECK_GT(stack->size(), 0U);
808 InstrumentationStackFrame instrumentation_frame = stack->front();
809 // TODO: bring back CheckStackDepth(self, instrumentation_frame, 2);
810 stack->pop_front();
811
Brian Carlstromea46f952013-07-30 01:26:50 -0700812 mirror::ArtMethod* method = instrumentation_frame.method_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800813 if (is_deoptimization) {
814 if (kVerboseInstrumentation) {
815 LOG(INFO) << "Popping for deoptimization " << PrettyMethod(method);
816 }
817 } else {
818 if (kVerboseInstrumentation) {
819 LOG(INFO) << "Popping for unwind " << PrettyMethod(method);
820 }
821
822 // Notify listeners of method unwind.
823 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
824 // return_pc.
825 uint32_t dex_pc = DexFile::kDexNoIndex;
826 MethodUnwindEvent(self, instrumentation_frame.this_object_, method, dex_pc);
827 }
828}
829
830std::string InstrumentationStackFrame::Dump() const {
831 std::ostringstream os;
832 os << "Frame " << frame_id_ << " " << PrettyMethod(method_) << ":"
833 << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_);
834 return os.str();
835}
836
837} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -0800838} // namespace art