blob: 160e8c36a36d85100362ac62f3e7994b16bbdd9b [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 Rogersc7dd2952014-10-21 23:31:19 -070021#include <sstream>
22
Ian Rogerse63db272014-07-15 15:36:11 -070023#include "arch/context.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080024#include "atomic.h"
Elliott Hughes76160052012-12-12 16:31:20 -080025#include "base/unix_file/fd_file.h"
jeffhao725a9572012-11-13 18:20:12 -080026#include "class_linker.h"
27#include "debugger.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080028#include "dex_file-inl.h"
Ian Rogersc7dd2952014-10-21 23:31:19 -070029#include "entrypoints/quick/quick_entrypoints.h"
Mathieu Chartierd8891782014-03-02 13:28:37 -080030#include "entrypoints/quick/quick_alloc_entrypoints.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070031#include "entrypoints/runtime_asm_entrypoints.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070032#include "gc_root-inl.h"
Sebastien Hertz138dbfc2013-12-04 18:15:25 +010033#include "interpreter/interpreter.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070034#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035#include "mirror/class-inl.h"
36#include "mirror/dex_cache.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "mirror/object_array-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070038#include "mirror/object-inl.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080039#include "nth_caller_visitor.h"
jeffhao725a9572012-11-13 18:20:12 -080040#include "os.h"
41#include "scoped_thread_state_change.h"
42#include "thread.h"
43#include "thread_list.h"
jeffhao725a9572012-11-13 18:20:12 -080044
45namespace art {
Ian Rogersfa824272013-11-05 16:12:57 -080046
Ian Rogers62d6c772013-02-27 08:32:07 -080047namespace instrumentation {
jeffhao725a9572012-11-13 18:20:12 -080048
Sebastien Hertz5bfd5c92013-11-15 11:36:07 +010049const bool kVerboseInstrumentation = false;
50
Ian Rogers816432e2013-09-06 15:47:45 -070051// Do we want to deoptimize for method entry and exit listeners or just try to intercept
52// invocations? Deoptimization forces all code to run in the interpreter and considerably hurts the
53// application's performance.
Jeff Haobc678bb2014-08-11 18:00:29 -070054static constexpr bool kDeoptimizeForAccurateMethodEntryExitListeners = true;
Ian Rogers816432e2013-09-06 15:47:45 -070055
Ian Rogers62d6c772013-02-27 08:32:07 -080056static bool InstallStubsClassVisitor(mirror::Class* klass, void* arg)
jeffhao725a9572012-11-13 18:20:12 -080057 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -080058 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
59 return instrumentation->InstallStubsForClass(klass);
60}
61
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070062Instrumentation::Instrumentation()
63 : instrumentation_stubs_installed_(false), entry_exit_stubs_installed_(false),
64 interpreter_stubs_installed_(false),
65 interpret_only_(false), forced_interpret_only_(false),
66 have_method_entry_listeners_(false), have_method_exit_listeners_(false),
67 have_method_unwind_listeners_(false), have_dex_pc_listeners_(false),
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +020068 have_field_read_listeners_(false), have_field_write_listeners_(false),
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070069 have_exception_caught_listeners_(false),
70 deoptimized_methods_lock_("deoptimized methods lock"),
71 deoptimization_enabled_(false),
72 interpreter_handler_table_(kMainHandlerTable),
73 quick_alloc_entry_points_instrumentation_counter_(0) {
74}
75
Ian Rogers62d6c772013-02-27 08:32:07 -080076bool Instrumentation::InstallStubsForClass(mirror::Class* klass) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +010077 for (size_t i = 0, e = klass->NumDirectMethods(); i < e; i++) {
78 InstallStubsForMethod(klass->GetDirectMethod(i));
jeffhao725a9572012-11-13 18:20:12 -080079 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +010080 for (size_t i = 0, e = klass->NumVirtualMethods(); i < e; i++) {
81 InstallStubsForMethod(klass->GetVirtualMethod(i));
jeffhao725a9572012-11-13 18:20:12 -080082 }
83 return true;
84}
85
Ian Rogersef7d42f2014-01-06 12:55:46 -080086static void UpdateEntrypoints(mirror::ArtMethod* method, const void* quick_code,
87 const void* portable_code, bool have_portable_code)
88 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
89 method->SetEntryPointFromPortableCompiledCode(portable_code);
90 method->SetEntryPointFromQuickCompiledCode(quick_code);
91 bool portable_enabled = method->IsPortableCompiled();
92 if (have_portable_code && !portable_enabled) {
93 method->SetIsPortableCompiled();
94 } else if (portable_enabled) {
95 method->ClearIsPortableCompiled();
96 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +010097 if (!method->IsResolutionMethod()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -070098 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers6f3dbba2014-10-14 17:41:57 -070099 if (class_linker->IsQuickToInterpreterBridge(quick_code) ||
100 (class_linker->IsQuickResolutionStub(quick_code) &&
101 Runtime::Current()->GetInstrumentation()->IsForcedInterpretOnly() &&
102 !method->IsNative() && !method->IsProxyMethod())) {
Hiroshi Yamauchi563b47c2014-02-28 17:18:37 -0800103 if (kIsDebugBuild) {
104 if (quick_code == GetQuickToInterpreterBridge()) {
105 DCHECK(portable_code == GetPortableToInterpreterBridge());
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700106 } else if (class_linker->IsQuickResolutionStub(quick_code)) {
107 DCHECK(class_linker->IsPortableResolutionStub(portable_code));
Hiroshi Yamauchi563b47c2014-02-28 17:18:37 -0800108 }
109 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800110 DCHECK(!method->IsNative()) << PrettyMethod(method);
Hiroshi Yamauchi563b47c2014-02-28 17:18:37 -0800111 DCHECK(!method->IsProxyMethod()) << PrettyMethod(method);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700112 method->SetEntryPointFromInterpreter(art::artInterpreterToInterpreterBridge);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100113 } else {
114 method->SetEntryPointFromInterpreter(art::artInterpreterToCompiledCodeBridge);
115 }
116 }
117}
118
119void Instrumentation::InstallStubsForMethod(mirror::ArtMethod* method) {
120 if (method->IsAbstract() || method->IsProxyMethod()) {
121 // Do not change stubs for these methods.
122 return;
123 }
Jeff Hao56802772014-08-19 10:17:36 -0700124 // Don't stub Proxy.<init>. Note that the Proxy class itself is not a proxy class.
125 if (method->IsConstructor() &&
126 method->GetDeclaringClass()->DescriptorEquals("Ljava/lang/reflect/Proxy;")) {
Jeff Haodb8a6642014-08-14 17:18:52 -0700127 return;
128 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800129 const void* new_portable_code;
130 const void* new_quick_code;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100131 bool uninstall = !entry_exit_stubs_installed_ && !interpreter_stubs_installed_;
132 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
133 bool is_class_initialized = method->GetDeclaringClass()->IsInitialized();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800134 bool have_portable_code = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100135 if (uninstall) {
136 if ((forced_interpret_only_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800137 new_portable_code = GetPortableToInterpreterBridge();
138 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100139 } else if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800140 new_portable_code = class_linker->GetPortableOatCodeFor(method, &have_portable_code);
141 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100142 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700143 new_portable_code = GetPortableResolutionStub();
144 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100145 }
146 } else { // !uninstall
Sebastien Hertzbae182c2013-12-17 10:42:03 +0100147 if ((interpreter_stubs_installed_ || forced_interpret_only_ || IsDeoptimized(method)) &&
148 !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800149 new_portable_code = GetPortableToInterpreterBridge();
150 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100151 } else {
152 // Do not overwrite resolution trampoline. When the trampoline initializes the method's
153 // class, all its static methods code will be set to the instrumentation entry point.
154 // For more details, see ClassLinker::FixupStaticTrampolines.
155 if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Sebastien Hertz320deb22014-06-11 19:45:05 +0200156 if (entry_exit_stubs_installed_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800157 new_portable_code = GetPortableToInterpreterBridge();
158 new_quick_code = GetQuickInstrumentationEntryPoint();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200159 } else {
160 new_portable_code = class_linker->GetPortableOatCodeFor(method, &have_portable_code);
161 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700162 DCHECK(!class_linker->IsQuickToInterpreterBridge(new_quick_code));
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100163 }
164 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700165 new_portable_code = GetPortableResolutionStub();
166 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100167 }
168 }
169 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800170 UpdateEntrypoints(method, new_quick_code, new_portable_code, have_portable_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100171}
172
Ian Rogers62d6c772013-02-27 08:32:07 -0800173// Places the instrumentation exit pc as the return PC for every quick frame. This also allows
174// deoptimization of quick frames to interpreter frames.
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100175// Since we may already have done this previously, we need to push new instrumentation frame before
176// existing instrumentation frames.
Ian Rogers62d6c772013-02-27 08:32:07 -0800177static void InstrumentationInstallStack(Thread* thread, void* arg)
Ian Rogers306057f2012-11-26 12:45:53 -0800178 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
179 struct InstallStackVisitor : public StackVisitor {
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100180 InstallStackVisitor(Thread* thread, Context* context, uintptr_t instrumentation_exit_pc)
Ian Rogers62d6c772013-02-27 08:32:07 -0800181 : StackVisitor(thread, context), instrumentation_stack_(thread->GetInstrumentationStack()),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100182 instrumentation_exit_pc_(instrumentation_exit_pc),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100183 reached_existing_instrumentation_frames_(false), instrumentation_stack_depth_(0),
184 last_return_pc_(0) {
185 }
jeffhao725a9572012-11-13 18:20:12 -0800186
Ian Rogers306057f2012-11-26 12:45:53 -0800187 virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700188 mirror::ArtMethod* m = GetMethod();
Ian Rogers306057f2012-11-26 12:45:53 -0800189 if (m == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800190 if (kVerboseInstrumentation) {
191 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
192 }
193 last_return_pc_ = 0;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700194 return true; // Ignore upcalls.
Ian Rogers306057f2012-11-26 12:45:53 -0800195 }
Jeff Haoa15a81b2014-05-27 18:25:47 -0700196 if (GetCurrentQuickFrame() == NULL) {
Sebastien Hertz320deb22014-06-11 19:45:05 +0200197 bool interpreter_frame = !m->IsPortableCompiled();
198 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, 0, GetFrameId(),
199 interpreter_frame);
Jeff Haoa15a81b2014-05-27 18:25:47 -0700200 if (kVerboseInstrumentation) {
201 LOG(INFO) << "Pushing shadow frame " << instrumentation_frame.Dump();
202 }
203 shadow_stack_.push_back(instrumentation_frame);
204 return true; // Continue.
205 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800206 uintptr_t return_pc = GetReturnPc();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200207 if (m->IsRuntimeMethod()) {
208 if (return_pc == instrumentation_exit_pc_) {
209 if (kVerboseInstrumentation) {
210 LOG(INFO) << " Handling quick to interpreter transition. Frame " << GetFrameId();
211 }
212 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200213 const InstrumentationStackFrame& frame =
214 instrumentation_stack_->at(instrumentation_stack_depth_);
Sebastien Hertz320deb22014-06-11 19:45:05 +0200215 CHECK(frame.interpreter_entry_);
216 // This is an interpreter frame so method enter event must have been reported. However we
217 // need to push a DEX pc into the dex_pcs_ list to match size of instrumentation stack.
218 // Since we won't report method entry here, we can safely push any DEX pc.
219 dex_pcs_.push_back(0);
220 last_return_pc_ = frame.return_pc_;
221 ++instrumentation_stack_depth_;
222 return true;
223 } else {
224 if (kVerboseInstrumentation) {
225 LOG(INFO) << " Skipping runtime method. Frame " << GetFrameId();
226 }
227 last_return_pc_ = GetReturnPc();
228 return true; // Ignore unresolved methods since they will be instrumented after resolution.
229 }
230 }
231 if (kVerboseInstrumentation) {
232 LOG(INFO) << " Installing exit stub in " << DescribeLocation();
233 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100234 if (return_pc == instrumentation_exit_pc_) {
235 // We've reached a frame which has already been installed with instrumentation exit stub.
236 // We should have already installed instrumentation on previous frames.
237 reached_existing_instrumentation_frames_ = true;
238
239 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200240 const InstrumentationStackFrame& frame =
241 instrumentation_stack_->at(instrumentation_stack_depth_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100242 CHECK_EQ(m, frame.method_) << "Expected " << PrettyMethod(m)
243 << ", Found " << PrettyMethod(frame.method_);
244 return_pc = frame.return_pc_;
245 if (kVerboseInstrumentation) {
246 LOG(INFO) << "Ignoring already instrumented " << frame.Dump();
247 }
248 } else {
249 CHECK_NE(return_pc, 0U);
250 CHECK(!reached_existing_instrumentation_frames_);
251 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, return_pc, GetFrameId(),
252 false);
253 if (kVerboseInstrumentation) {
254 LOG(INFO) << "Pushing frame " << instrumentation_frame.Dump();
255 }
256
Sebastien Hertz320deb22014-06-11 19:45:05 +0200257 // Insert frame at the right position so we do not corrupt the instrumentation stack.
258 // Instrumentation stack frames are in descending frame id order.
259 auto it = instrumentation_stack_->begin();
260 for (auto end = instrumentation_stack_->end(); it != end; ++it) {
261 const InstrumentationStackFrame& current = *it;
262 if (instrumentation_frame.frame_id_ >= current.frame_id_) {
263 break;
264 }
265 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100266 instrumentation_stack_->insert(it, instrumentation_frame);
267 SetReturnPc(instrumentation_exit_pc_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800268 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800269 dex_pcs_.push_back(m->ToDexPc(last_return_pc_));
Ian Rogers62d6c772013-02-27 08:32:07 -0800270 last_return_pc_ = return_pc;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100271 ++instrumentation_stack_depth_;
Ian Rogers306057f2012-11-26 12:45:53 -0800272 return true; // Continue.
273 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800274 std::deque<InstrumentationStackFrame>* const instrumentation_stack_;
Jeff Haoa15a81b2014-05-27 18:25:47 -0700275 std::vector<InstrumentationStackFrame> shadow_stack_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800276 std::vector<uint32_t> dex_pcs_;
Ian Rogers306057f2012-11-26 12:45:53 -0800277 const uintptr_t instrumentation_exit_pc_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100278 bool reached_existing_instrumentation_frames_;
279 size_t instrumentation_stack_depth_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800280 uintptr_t last_return_pc_;
Ian Rogers306057f2012-11-26 12:45:53 -0800281 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800282 if (kVerboseInstrumentation) {
283 std::string thread_name;
284 thread->GetThreadName(thread_name);
285 LOG(INFO) << "Installing exit stubs in " << thread_name;
Ian Rogers306057f2012-11-26 12:45:53 -0800286 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100287
288 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers700a4022014-05-19 16:49:03 -0700289 std::unique_ptr<Context> context(Context::Create());
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700290 uintptr_t instrumentation_exit_pc = reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100291 InstallStackVisitor visitor(thread, context.get(), instrumentation_exit_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800292 visitor.WalkStack(true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100293 CHECK_EQ(visitor.dex_pcs_.size(), thread->GetInstrumentationStack()->size());
Ian Rogers62d6c772013-02-27 08:32:07 -0800294
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100295 if (instrumentation->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100296 // Create method enter events for all methods currently on the thread's stack. We only do this
297 // if no debugger is attached to prevent from posting events twice.
Jeff Haoa15a81b2014-05-27 18:25:47 -0700298 auto ssi = visitor.shadow_stack_.rbegin();
299 for (auto isi = thread->GetInstrumentationStack()->rbegin(),
300 end = thread->GetInstrumentationStack()->rend(); isi != end; ++isi) {
301 while (ssi != visitor.shadow_stack_.rend() && (*ssi).frame_id_ < (*isi).frame_id_) {
302 instrumentation->MethodEnterEvent(thread, (*ssi).this_object_, (*ssi).method_, 0);
303 ++ssi;
304 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100305 uint32_t dex_pc = visitor.dex_pcs_.back();
306 visitor.dex_pcs_.pop_back();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200307 if (!isi->interpreter_entry_) {
308 instrumentation->MethodEnterEvent(thread, (*isi).this_object_, (*isi).method_, dex_pc);
309 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100310 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800311 }
312 thread->VerifyStack();
Ian Rogers306057f2012-11-26 12:45:53 -0800313}
314
Ian Rogers62d6c772013-02-27 08:32:07 -0800315// Removes the instrumentation exit pc as the return PC for every quick frame.
316static void InstrumentationRestoreStack(Thread* thread, void* arg)
Ian Rogers306057f2012-11-26 12:45:53 -0800317 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
318 struct RestoreStackVisitor : public StackVisitor {
Ian Rogers62d6c772013-02-27 08:32:07 -0800319 RestoreStackVisitor(Thread* thread, uintptr_t instrumentation_exit_pc,
320 Instrumentation* instrumentation)
321 : StackVisitor(thread, NULL), thread_(thread),
322 instrumentation_exit_pc_(instrumentation_exit_pc),
323 instrumentation_(instrumentation),
324 instrumentation_stack_(thread->GetInstrumentationStack()),
325 frames_removed_(0) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800326
327 virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800328 if (instrumentation_stack_->size() == 0) {
jeffhao725a9572012-11-13 18:20:12 -0800329 return false; // Stop.
330 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700331 mirror::ArtMethod* m = GetMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800332 if (GetCurrentQuickFrame() == NULL) {
333 if (kVerboseInstrumentation) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200334 LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId()
335 << " Method=" << PrettyMethod(m);
Ian Rogers62d6c772013-02-27 08:32:07 -0800336 }
337 return true; // Ignore shadow frames.
338 }
Ian Rogers306057f2012-11-26 12:45:53 -0800339 if (m == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800340 if (kVerboseInstrumentation) {
341 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
342 }
Ian Rogers306057f2012-11-26 12:45:53 -0800343 return true; // Ignore upcalls.
344 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800345 bool removed_stub = false;
346 // TODO: make this search more efficient?
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100347 const size_t frameId = GetFrameId();
348 for (const InstrumentationStackFrame& instrumentation_frame : *instrumentation_stack_) {
349 if (instrumentation_frame.frame_id_ == frameId) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800350 if (kVerboseInstrumentation) {
351 LOG(INFO) << " Removing exit stub in " << DescribeLocation();
352 }
Jeff Hao9a916d32013-06-27 18:45:37 -0700353 if (instrumentation_frame.interpreter_entry_) {
354 CHECK(m == Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
355 } else {
356 CHECK(m == instrumentation_frame.method_) << PrettyMethod(m);
357 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800358 SetReturnPc(instrumentation_frame.return_pc_);
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100359 if (instrumentation_->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100360 // Create the method exit events. As the methods didn't really exit the result is 0.
361 // We only do this if no debugger is attached to prevent from posting events twice.
362 instrumentation_->MethodExitEvent(thread_, instrumentation_frame.this_object_, m,
363 GetDexPc(), JValue());
364 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800365 frames_removed_++;
366 removed_stub = true;
367 break;
368 }
369 }
370 if (!removed_stub) {
371 if (kVerboseInstrumentation) {
372 LOG(INFO) << " No exit stub in " << DescribeLocation();
Ian Rogers306057f2012-11-26 12:45:53 -0800373 }
jeffhao725a9572012-11-13 18:20:12 -0800374 }
375 return true; // Continue.
376 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800377 Thread* const thread_;
Ian Rogers306057f2012-11-26 12:45:53 -0800378 const uintptr_t instrumentation_exit_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800379 Instrumentation* const instrumentation_;
380 std::deque<instrumentation::InstrumentationStackFrame>* const instrumentation_stack_;
381 size_t frames_removed_;
jeffhao725a9572012-11-13 18:20:12 -0800382 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800383 if (kVerboseInstrumentation) {
384 std::string thread_name;
385 thread->GetThreadName(thread_name);
386 LOG(INFO) << "Removing exit stubs in " << thread_name;
387 }
388 std::deque<instrumentation::InstrumentationStackFrame>* stack = thread->GetInstrumentationStack();
389 if (stack->size() > 0) {
390 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700391 uintptr_t instrumentation_exit_pc =
392 reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Ian Rogers62d6c772013-02-27 08:32:07 -0800393 RestoreStackVisitor visitor(thread, instrumentation_exit_pc, instrumentation);
394 visitor.WalkStack(true);
395 CHECK_EQ(visitor.frames_removed_, stack->size());
396 while (stack->size() > 0) {
397 stack->pop_front();
398 }
jeffhao725a9572012-11-13 18:20:12 -0800399 }
400}
401
Ian Rogers62d6c772013-02-27 08:32:07 -0800402void Instrumentation::AddListener(InstrumentationListener* listener, uint32_t events) {
403 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Ian Rogers62d6c772013-02-27 08:32:07 -0800404 if ((events & kMethodEntered) != 0) {
405 method_entry_listeners_.push_back(listener);
Ian Rogers62d6c772013-02-27 08:32:07 -0800406 have_method_entry_listeners_ = true;
407 }
408 if ((events & kMethodExited) != 0) {
409 method_exit_listeners_.push_back(listener);
Ian Rogers62d6c772013-02-27 08:32:07 -0800410 have_method_exit_listeners_ = true;
411 }
412 if ((events & kMethodUnwind) != 0) {
413 method_unwind_listeners_.push_back(listener);
414 have_method_unwind_listeners_ = true;
415 }
416 if ((events & kDexPcMoved) != 0) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200417 std::list<InstrumentationListener*>* modified;
418 if (have_dex_pc_listeners_) {
419 modified = new std::list<InstrumentationListener*>(*dex_pc_listeners_.get());
420 } else {
421 modified = new std::list<InstrumentationListener*>();
422 }
423 modified->push_back(listener);
424 dex_pc_listeners_.reset(modified);
Ian Rogers62d6c772013-02-27 08:32:07 -0800425 have_dex_pc_listeners_ = true;
426 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200427 if ((events & kFieldRead) != 0) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200428 std::list<InstrumentationListener*>* modified;
429 if (have_field_read_listeners_) {
430 modified = new std::list<InstrumentationListener*>(*field_read_listeners_.get());
431 } else {
432 modified = new std::list<InstrumentationListener*>();
433 }
434 modified->push_back(listener);
435 field_read_listeners_.reset(modified);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200436 have_field_read_listeners_ = true;
437 }
438 if ((events & kFieldWritten) != 0) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200439 std::list<InstrumentationListener*>* modified;
440 if (have_field_write_listeners_) {
441 modified = new std::list<InstrumentationListener*>(*field_write_listeners_.get());
442 } else {
443 modified = new std::list<InstrumentationListener*>();
444 }
445 modified->push_back(listener);
446 field_write_listeners_.reset(modified);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200447 have_field_write_listeners_ = true;
448 }
Jeff Hao14dd5a82013-04-11 10:23:36 -0700449 if ((events & kExceptionCaught) != 0) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200450 std::list<InstrumentationListener*>* modified;
451 if (have_exception_caught_listeners_) {
452 modified = new std::list<InstrumentationListener*>(*exception_caught_listeners_.get());
453 } else {
454 modified = new std::list<InstrumentationListener*>();
455 }
456 modified->push_back(listener);
457 exception_caught_listeners_.reset(modified);
Jeff Hao14dd5a82013-04-11 10:23:36 -0700458 have_exception_caught_listeners_ = true;
459 }
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200460 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800461}
462
Ian Rogers62d6c772013-02-27 08:32:07 -0800463void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) {
464 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Ian Rogers62d6c772013-02-27 08:32:07 -0800465
466 if ((events & kMethodEntered) != 0) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200467 if (have_method_entry_listeners_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800468 method_entry_listeners_.remove(listener);
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200469 have_method_entry_listeners_ = !method_entry_listeners_.empty();
Ian Rogers62d6c772013-02-27 08:32:07 -0800470 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800471 }
472 if ((events & kMethodExited) != 0) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200473 if (have_method_exit_listeners_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800474 method_exit_listeners_.remove(listener);
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200475 have_method_exit_listeners_ = !method_exit_listeners_.empty();
Ian Rogers62d6c772013-02-27 08:32:07 -0800476 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800477 }
478 if ((events & kMethodUnwind) != 0) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200479 if (have_method_unwind_listeners_) {
480 method_unwind_listeners_.remove(listener);
481 have_method_unwind_listeners_ = !method_unwind_listeners_.empty();
482 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800483 }
484 if ((events & kDexPcMoved) != 0) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200485 if (have_dex_pc_listeners_) {
486 std::list<InstrumentationListener*>* modified =
487 new std::list<InstrumentationListener*>(*dex_pc_listeners_.get());
488 modified->remove(listener);
489 have_dex_pc_listeners_ = !modified->empty();
490 if (have_dex_pc_listeners_) {
491 dex_pc_listeners_.reset(modified);
492 } else {
493 dex_pc_listeners_.reset();
494 delete modified;
495 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800496 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800497 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200498 if ((events & kFieldRead) != 0) {
Daniel Mihalyi66445212014-08-21 15:57:25 +0200499 if (have_field_read_listeners_) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200500 std::list<InstrumentationListener*>* modified =
501 new std::list<InstrumentationListener*>(*field_read_listeners_.get());
502 modified->remove(listener);
503 have_field_read_listeners_ = !modified->empty();
504 if (have_field_read_listeners_) {
505 field_read_listeners_.reset(modified);
506 } else {
507 field_read_listeners_.reset();
508 delete modified;
509 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200510 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200511 }
512 if ((events & kFieldWritten) != 0) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200513 if (have_field_write_listeners_) {
514 std::list<InstrumentationListener*>* modified =
515 new std::list<InstrumentationListener*>(*field_write_listeners_.get());
516 modified->remove(listener);
517 have_field_write_listeners_ = !modified->empty();
518 if (have_field_write_listeners_) {
519 field_write_listeners_.reset(modified);
520 } else {
521 field_write_listeners_.reset();
522 delete modified;
523 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200524 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200525 }
Jeff Hao14dd5a82013-04-11 10:23:36 -0700526 if ((events & kExceptionCaught) != 0) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200527 if (have_exception_caught_listeners_) {
528 std::list<InstrumentationListener*>* modified =
529 new std::list<InstrumentationListener*>(*exception_caught_listeners_.get());
530 modified->remove(listener);
531 have_exception_caught_listeners_ = !modified->empty();
532 if (have_exception_caught_listeners_) {
533 exception_caught_listeners_.reset(modified);
534 } else {
535 exception_caught_listeners_.reset();
536 delete modified;
537 }
538 }
Jeff Hao14dd5a82013-04-11 10:23:36 -0700539 }
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200540 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800541}
542
Ian Rogers62d6c772013-02-27 08:32:07 -0800543void Instrumentation::ConfigureStubs(bool require_entry_exit_stubs, bool require_interpreter) {
544 interpret_only_ = require_interpreter || forced_interpret_only_;
545 // Compute what level of instrumentation is required and compare to current.
546 int desired_level, current_level;
547 if (require_interpreter) {
548 desired_level = 2;
549 } else if (require_entry_exit_stubs) {
550 desired_level = 1;
551 } else {
552 desired_level = 0;
553 }
554 if (interpreter_stubs_installed_) {
555 current_level = 2;
556 } else if (entry_exit_stubs_installed_) {
557 current_level = 1;
558 } else {
559 current_level = 0;
560 }
561 if (desired_level == current_level) {
562 // We're already set.
563 return;
564 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100565 Thread* const self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -0800566 Runtime* runtime = Runtime::Current();
567 Locks::thread_list_lock_->AssertNotHeld(self);
568 if (desired_level > 0) {
569 if (require_interpreter) {
570 interpreter_stubs_installed_ = true;
571 } else {
572 CHECK(require_entry_exit_stubs);
573 entry_exit_stubs_installed_ = true;
574 }
575 runtime->GetClassLinker()->VisitClasses(InstallStubsClassVisitor, this);
576 instrumentation_stubs_installed_ = true;
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100577 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800578 runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this);
579 } else {
580 interpreter_stubs_installed_ = false;
581 entry_exit_stubs_installed_ = false;
582 runtime->GetClassLinker()->VisitClasses(InstallStubsClassVisitor, this);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100583 // Restore stack only if there is no method currently deoptimized.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700584 bool empty;
585 {
586 ReaderMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700587 empty = IsDeoptimizedMethodsEmpty(); // Avoid lock violation.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700588 }
589 if (empty) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100590 instrumentation_stubs_installed_ = false;
591 MutexLock mu(self, *Locks::thread_list_lock_);
592 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
593 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800594 }
jeffhao725a9572012-11-13 18:20:12 -0800595}
596
Ian Rogersfa824272013-11-05 16:12:57 -0800597static void ResetQuickAllocEntryPointsForThread(Thread* thread, void* arg) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700598 UNUSED(arg);
Ian Rogersfa824272013-11-05 16:12:57 -0800599 thread->ResetQuickAllocEntryPointsForThread();
600}
601
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700602void Instrumentation::SetEntrypointsInstrumented(bool instrumented) {
603 Thread* self = Thread::Current();
Mathieu Chartier661974a2014-01-09 11:23:53 -0800604 Runtime* runtime = Runtime::Current();
605 ThreadList* tl = runtime->GetThreadList();
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700606 Locks::mutator_lock_->AssertNotHeld(self);
607 Locks::instrument_entrypoints_lock_->AssertHeld(self);
608 if (runtime->IsStarted()) {
Mathieu Chartier661974a2014-01-09 11:23:53 -0800609 tl->SuspendAll();
610 }
611 {
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700612 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
Mathieu Chartier661974a2014-01-09 11:23:53 -0800613 SetQuickAllocEntryPointsInstrumented(instrumented);
614 ResetQuickAllocEntryPoints();
615 }
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700616 if (runtime->IsStarted()) {
Mathieu Chartier661974a2014-01-09 11:23:53 -0800617 tl->ResumeAll();
618 }
619}
620
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700621void Instrumentation::InstrumentQuickAllocEntryPoints() {
622 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
623 InstrumentQuickAllocEntryPointsLocked();
Ian Rogersfa824272013-11-05 16:12:57 -0800624}
625
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700626void Instrumentation::UninstrumentQuickAllocEntryPoints() {
627 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
628 UninstrumentQuickAllocEntryPointsLocked();
629}
630
631void Instrumentation::InstrumentQuickAllocEntryPointsLocked() {
632 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
633 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
634 SetEntrypointsInstrumented(true);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800635 }
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700636 ++quick_alloc_entry_points_instrumentation_counter_;
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700637}
638
639void Instrumentation::UninstrumentQuickAllocEntryPointsLocked() {
640 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
641 CHECK_GT(quick_alloc_entry_points_instrumentation_counter_, 0U);
642 --quick_alloc_entry_points_instrumentation_counter_;
643 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
644 SetEntrypointsInstrumented(false);
645 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800646}
647
648void Instrumentation::ResetQuickAllocEntryPoints() {
649 Runtime* runtime = Runtime::Current();
650 if (runtime->IsStarted()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800651 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
652 runtime->GetThreadList()->ForEach(ResetQuickAllocEntryPointsForThread, NULL);
Ian Rogersfa824272013-11-05 16:12:57 -0800653 }
654}
655
Ian Rogersef7d42f2014-01-06 12:55:46 -0800656void Instrumentation::UpdateMethodsCode(mirror::ArtMethod* method, const void* quick_code,
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700657 const void* portable_code, bool have_portable_code) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800658 const void* new_portable_code;
659 const void* new_quick_code;
660 bool new_have_portable_code;
Ian Rogers62d6c772013-02-27 08:32:07 -0800661 if (LIKELY(!instrumentation_stubs_installed_)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800662 new_portable_code = portable_code;
663 new_quick_code = quick_code;
664 new_have_portable_code = have_portable_code;
Jeff Hao65d15d92013-07-16 16:39:33 -0700665 } else {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100666 if ((interpreter_stubs_installed_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800667 new_portable_code = GetPortableToInterpreterBridge();
668 new_quick_code = GetQuickToInterpreterBridge();
669 new_have_portable_code = false;
Jeff Hao65d15d92013-07-16 16:39:33 -0700670 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700671 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700672 if (class_linker->IsQuickResolutionStub(quick_code) ||
673 class_linker->IsQuickToInterpreterBridge(quick_code)) {
674 DCHECK(class_linker->IsPortableResolutionStub(portable_code) ||
675 class_linker->IsPortableToInterpreterBridge(portable_code));
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700676 new_portable_code = portable_code;
677 new_quick_code = quick_code;
678 new_have_portable_code = have_portable_code;
679 } else if (entry_exit_stubs_installed_) {
680 new_quick_code = GetQuickInstrumentationEntryPoint();
681 new_portable_code = GetPortableToInterpreterBridge();
682 new_have_portable_code = false;
683 } else {
684 new_portable_code = portable_code;
685 new_quick_code = quick_code;
686 new_have_portable_code = have_portable_code;
687 }
Jeff Hao65d15d92013-07-16 16:39:33 -0700688 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800689 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800690 UpdateEntrypoints(method, new_quick_code, new_portable_code, new_have_portable_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100691}
692
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700693bool Instrumentation::AddDeoptimizedMethod(mirror::ArtMethod* method) {
694 // Note that the insert() below isn't read barrier-aware. So, this
695 // FindDeoptimizedMethod() call is necessary or else we would end up
696 // storing the same method twice in the map (the from-space and the
697 // to-space ones).
698 if (FindDeoptimizedMethod(method)) {
699 // Already in the map. Return.
700 return false;
701 }
702 // Not found. Add it.
703 int32_t hash_code = method->IdentityHashCode();
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700704 deoptimized_methods_.insert(std::make_pair(hash_code, GcRoot<mirror::ArtMethod>(method)));
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700705 return true;
706}
707
708bool Instrumentation::FindDeoptimizedMethod(mirror::ArtMethod* method) {
709 int32_t hash_code = method->IdentityHashCode();
710 auto range = deoptimized_methods_.equal_range(hash_code);
711 for (auto it = range.first; it != range.second; ++it) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700712 mirror::ArtMethod* m = it->second.Read();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700713 if (m == method) {
714 // Found.
715 return true;
716 }
717 }
718 // Not found.
719 return false;
720}
721
722mirror::ArtMethod* Instrumentation::BeginDeoptimizedMethod() {
723 auto it = deoptimized_methods_.begin();
724 if (it == deoptimized_methods_.end()) {
725 // Empty.
726 return nullptr;
727 }
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700728 return it->second.Read();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700729}
730
731bool Instrumentation::RemoveDeoptimizedMethod(mirror::ArtMethod* method) {
732 int32_t hash_code = method->IdentityHashCode();
733 auto range = deoptimized_methods_.equal_range(hash_code);
734 for (auto it = range.first; it != range.second; ++it) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700735 mirror::ArtMethod* m = it->second.Read();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700736 if (m == method) {
737 // Found. Erase and return.
738 deoptimized_methods_.erase(it);
739 return true;
740 }
741 }
742 // Not found.
743 return false;
744}
745
746bool Instrumentation::IsDeoptimizedMethodsEmpty() const {
747 return deoptimized_methods_.empty();
748}
749
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100750void Instrumentation::Deoptimize(mirror::ArtMethod* method) {
751 CHECK(!method->IsNative());
752 CHECK(!method->IsProxyMethod());
753 CHECK(!method->IsAbstract());
754
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700755 Thread* self = Thread::Current();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700756 {
757 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700758 bool has_not_been_deoptimized = AddDeoptimizedMethod(method);
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200759 CHECK(has_not_been_deoptimized) << "Method " << PrettyMethod(method)
760 << " is already deoptimized";
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700761 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100762 if (!interpreter_stubs_installed_) {
Sebastien Hertz320deb22014-06-11 19:45:05 +0200763 UpdateEntrypoints(method, GetQuickInstrumentationEntryPoint(), GetPortableToInterpreterBridge(),
Ian Rogersef7d42f2014-01-06 12:55:46 -0800764 false);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100765
766 // Install instrumentation exit stub and instrumentation frames. We may already have installed
767 // these previously so it will only cover the newly created frames.
768 instrumentation_stubs_installed_ = true;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700769 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100770 Runtime::Current()->GetThreadList()->ForEach(InstrumentationInstallStack, this);
771 }
772}
773
774void Instrumentation::Undeoptimize(mirror::ArtMethod* method) {
775 CHECK(!method->IsNative());
776 CHECK(!method->IsProxyMethod());
777 CHECK(!method->IsAbstract());
778
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700779 Thread* self = Thread::Current();
780 bool empty;
781 {
782 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700783 bool found_and_erased = RemoveDeoptimizedMethod(method);
784 CHECK(found_and_erased) << "Method " << PrettyMethod(method)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700785 << " is not deoptimized";
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700786 empty = IsDeoptimizedMethodsEmpty();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700787 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100788
789 // Restore code and possibly stack only if we did not deoptimize everything.
790 if (!interpreter_stubs_installed_) {
791 // Restore its code or resolution trampoline.
792 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800793 if (method->IsStatic() && !method->IsConstructor() &&
794 !method->GetDeclaringClass()->IsInitialized()) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700795 UpdateEntrypoints(method, GetQuickResolutionStub(), GetPortableResolutionStub(), false);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100796 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800797 bool have_portable_code = false;
798 const void* quick_code = class_linker->GetQuickOatCodeFor(method);
799 const void* portable_code = class_linker->GetPortableOatCodeFor(method, &have_portable_code);
800 UpdateEntrypoints(method, quick_code, portable_code, have_portable_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100801 }
802
803 // If there is no deoptimized method left, we can restore the stack of each thread.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700804 if (empty) {
805 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100806 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
807 instrumentation_stubs_installed_ = false;
808 }
809 }
810}
811
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700812bool Instrumentation::IsDeoptimized(mirror::ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100813 DCHECK(method != nullptr);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700814 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
815 return FindDeoptimizedMethod(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100816}
817
818void Instrumentation::EnableDeoptimization() {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700819 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700820 CHECK(IsDeoptimizedMethodsEmpty());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100821 CHECK_EQ(deoptimization_enabled_, false);
822 deoptimization_enabled_ = true;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100823}
824
825void Instrumentation::DisableDeoptimization() {
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100826 CHECK_EQ(deoptimization_enabled_, true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100827 // If we deoptimized everything, undo it.
828 if (interpreter_stubs_installed_) {
829 UndeoptimizeEverything();
830 }
831 // Undeoptimized selected methods.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700832 while (true) {
833 mirror::ArtMethod* method;
834 {
835 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700836 if (IsDeoptimizedMethodsEmpty()) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700837 break;
838 }
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700839 method = BeginDeoptimizedMethod();
840 CHECK(method != nullptr);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700841 }
842 Undeoptimize(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100843 }
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100844 deoptimization_enabled_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100845}
846
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100847// Indicates if instrumentation should notify method enter/exit events to the listeners.
848bool Instrumentation::ShouldNotifyMethodEnterExitEvents() const {
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100849 return !deoptimization_enabled_ && !interpreter_stubs_installed_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100850}
851
852void Instrumentation::DeoptimizeEverything() {
853 CHECK(!interpreter_stubs_installed_);
854 ConfigureStubs(false, true);
855}
856
857void Instrumentation::UndeoptimizeEverything() {
858 CHECK(interpreter_stubs_installed_);
859 ConfigureStubs(false, false);
860}
861
862void Instrumentation::EnableMethodTracing() {
863 bool require_interpreter = kDeoptimizeForAccurateMethodEntryExitListeners;
864 ConfigureStubs(!require_interpreter, require_interpreter);
865}
866
867void Instrumentation::DisableMethodTracing() {
868 ConfigureStubs(false, false);
jeffhao725a9572012-11-13 18:20:12 -0800869}
870
Ian Rogersef7d42f2014-01-06 12:55:46 -0800871const void* Instrumentation::GetQuickCodeFor(mirror::ArtMethod* method) const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800872 Runtime* runtime = Runtime::Current();
873 if (LIKELY(!instrumentation_stubs_installed_)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800874 const void* code = method->GetEntryPointFromQuickCompiledCode();
Vladimir Marko8a630572014-04-09 18:45:35 +0100875 DCHECK(code != nullptr);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700876 ClassLinker* class_linker = runtime->GetClassLinker();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700877 if (LIKELY(!class_linker->IsQuickResolutionStub(code) &&
878 !class_linker->IsQuickToInterpreterBridge(code)) &&
879 !class_linker->IsQuickResolutionStub(code) &&
880 !class_linker->IsQuickToInterpreterBridge(code)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800881 return code;
882 }
883 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800884 return runtime->GetClassLinker()->GetQuickOatCodeFor(method);
jeffhao725a9572012-11-13 18:20:12 -0800885}
886
Ian Rogers62d6c772013-02-27 08:32:07 -0800887void Instrumentation::MethodEnterEventImpl(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800888 mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800889 uint32_t dex_pc) const {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700890 auto it = method_entry_listeners_.begin();
Jeff Hao65d15d92013-07-16 16:39:33 -0700891 bool is_end = (it == method_entry_listeners_.end());
892 // Implemented this way to prevent problems caused by modification of the list while iterating.
893 while (!is_end) {
894 InstrumentationListener* cur = *it;
895 ++it;
896 is_end = (it == method_entry_listeners_.end());
897 cur->MethodEntered(thread, this_object, method, dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800898 }
899}
900
901void Instrumentation::MethodExitEventImpl(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800902 mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800903 uint32_t dex_pc, const JValue& return_value) const {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700904 auto it = method_exit_listeners_.begin();
Jeff Hao65d15d92013-07-16 16:39:33 -0700905 bool is_end = (it == method_exit_listeners_.end());
906 // Implemented this way to prevent problems caused by modification of the list while iterating.
907 while (!is_end) {
908 InstrumentationListener* cur = *it;
909 ++it;
910 is_end = (it == method_exit_listeners_.end());
911 cur->MethodExited(thread, this_object, method, dex_pc, return_value);
Ian Rogers62d6c772013-02-27 08:32:07 -0800912 }
913}
914
915void Instrumentation::MethodUnwindEvent(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800916 mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800917 uint32_t dex_pc) const {
918 if (have_method_unwind_listeners_) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700919 for (InstrumentationListener* listener : method_unwind_listeners_) {
Sebastien Hertz51db44a2013-11-19 10:00:29 +0100920 listener->MethodUnwind(thread, this_object, method, dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800921 }
922 }
923}
924
925void Instrumentation::DexPcMovedEventImpl(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800926 mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800927 uint32_t dex_pc) const {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200928 if (HasDexPcListeners()) {
929 std::shared_ptr<std::list<InstrumentationListener*>> original(dex_pc_listeners_);
930 for (InstrumentationListener* listener : *original.get()) {
931 listener->DexPcMoved(thread, this_object, method, dex_pc);
932 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800933 }
934}
935
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200936void Instrumentation::FieldReadEventImpl(Thread* thread, mirror::Object* this_object,
937 mirror::ArtMethod* method, uint32_t dex_pc,
938 mirror::ArtField* field) const {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200939 if (HasFieldReadListeners()) {
940 std::shared_ptr<std::list<InstrumentationListener*>> original(field_read_listeners_);
941 for (InstrumentationListener* listener : *original.get()) {
942 listener->FieldRead(thread, this_object, method, dex_pc, field);
943 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200944 }
945}
946
947void Instrumentation::FieldWriteEventImpl(Thread* thread, mirror::Object* this_object,
948 mirror::ArtMethod* method, uint32_t dex_pc,
949 mirror::ArtField* field, const JValue& field_value) const {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200950 if (HasFieldWriteListeners()) {
951 std::shared_ptr<std::list<InstrumentationListener*>> original(field_write_listeners_);
952 for (InstrumentationListener* listener : *original.get()) {
953 listener->FieldWritten(thread, this_object, method, dex_pc, field, field_value);
954 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200955 }
956}
957
Ian Rogers62d6c772013-02-27 08:32:07 -0800958void Instrumentation::ExceptionCaughtEvent(Thread* thread, const ThrowLocation& throw_location,
Brian Carlstromea46f952013-07-30 01:26:50 -0700959 mirror::ArtMethod* catch_method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800960 uint32_t catch_dex_pc,
Sebastien Hertz947ff082013-09-17 14:10:13 +0200961 mirror::Throwable* exception_object) const {
Sebastien Hertz9f102032014-05-23 08:59:42 +0200962 if (HasExceptionCaughtListeners()) {
963 DCHECK_EQ(thread->GetException(nullptr), exception_object);
964 bool is_exception_reported = thread->IsExceptionReportedToInstrumentation();
Jeff Haoc0bd4da2013-04-11 15:52:28 -0700965 thread->ClearException();
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200966 std::shared_ptr<std::list<InstrumentationListener*>> original(exception_caught_listeners_);
967 for (InstrumentationListener* listener : *original.get()) {
968 listener->ExceptionCaught(thread, throw_location, catch_method, catch_dex_pc,
969 exception_object);
Ian Rogers62d6c772013-02-27 08:32:07 -0800970 }
Jeff Haoc0bd4da2013-04-11 15:52:28 -0700971 thread->SetException(throw_location, exception_object);
Sebastien Hertz9f102032014-05-23 08:59:42 +0200972 thread->SetExceptionReportedToInstrumentation(is_exception_reported);
Ian Rogers62d6c772013-02-27 08:32:07 -0800973 }
974}
975
976static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame,
977 int delta)
978 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
979 size_t frame_id = StackVisitor::ComputeNumFrames(self) + delta;
980 if (frame_id != instrumentation_frame.frame_id_) {
981 LOG(ERROR) << "Expected frame_id=" << frame_id << " but found "
982 << instrumentation_frame.frame_id_;
983 StackVisitor::DescribeStack(self);
984 CHECK_EQ(frame_id, instrumentation_frame.frame_id_);
985 }
986}
987
988void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700989 mirror::ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -0700990 uintptr_t lr, bool interpreter_entry) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800991 // We have a callee-save frame meaning this value is guaranteed to never be 0.
992 size_t frame_id = StackVisitor::ComputeNumFrames(self);
993 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
994 if (kVerboseInstrumentation) {
Brian Carlstrom2d888622013-07-18 17:02:00 -0700995 LOG(INFO) << "Entering " << PrettyMethod(method) << " from PC " << reinterpret_cast<void*>(lr);
Ian Rogers62d6c772013-02-27 08:32:07 -0800996 }
997 instrumentation::InstrumentationStackFrame instrumentation_frame(this_object, method, lr,
Jeff Hao9a916d32013-06-27 18:45:37 -0700998 frame_id, interpreter_entry);
Ian Rogers62d6c772013-02-27 08:32:07 -0800999 stack->push_front(instrumentation_frame);
1000
Sebastien Hertz320deb22014-06-11 19:45:05 +02001001 if (!interpreter_entry) {
1002 MethodEnterEvent(self, this_object, method, 0);
1003 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001004}
1005
Andreas Gamped58342c2014-06-05 14:18:08 -07001006TwoWordReturn Instrumentation::PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc,
1007 uint64_t gpr_result,
1008 uint64_t fpr_result) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001009 // Do the pop.
1010 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1011 CHECK_GT(stack->size(), 0U);
1012 InstrumentationStackFrame instrumentation_frame = stack->front();
1013 stack->pop_front();
1014
1015 // Set return PC and check the sanity of the stack.
1016 *return_pc = instrumentation_frame.return_pc_;
1017 CheckStackDepth(self, instrumentation_frame, 0);
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001018 self->VerifyStack();
Ian Rogers62d6c772013-02-27 08:32:07 -08001019
Brian Carlstromea46f952013-07-30 01:26:50 -07001020 mirror::ArtMethod* method = instrumentation_frame.method_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001021 uint32_t length;
1022 char return_shorty = method->GetShorty(&length)[0];
Ian Rogers62d6c772013-02-27 08:32:07 -08001023 JValue return_value;
1024 if (return_shorty == 'V') {
1025 return_value.SetJ(0);
1026 } else if (return_shorty == 'F' || return_shorty == 'D') {
1027 return_value.SetJ(fpr_result);
1028 } else {
1029 return_value.SetJ(gpr_result);
1030 }
1031 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1032 // return_pc.
1033 uint32_t dex_pc = DexFile::kDexNoIndex;
1034 mirror::Object* this_object = instrumentation_frame.this_object_;
Sebastien Hertz320deb22014-06-11 19:45:05 +02001035 if (!instrumentation_frame.interpreter_entry_) {
1036 MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value);
1037 }
jeffhao725a9572012-11-13 18:20:12 -08001038
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001039 // Deoptimize if the caller needs to continue execution in the interpreter. Do nothing if we get
1040 // back to an upcall.
1041 NthCallerVisitor visitor(self, 1, true);
1042 visitor.WalkStack(true);
1043 bool deoptimize = (visitor.caller != NULL) &&
1044 (interpreter_stubs_installed_ || IsDeoptimized(visitor.caller));
1045 if (deoptimize && kVerboseInstrumentation) {
1046 LOG(INFO) << "Deoptimizing into " << PrettyMethod(visitor.caller);
Ian Rogers62d6c772013-02-27 08:32:07 -08001047 }
1048 if (deoptimize) {
1049 if (kVerboseInstrumentation) {
1050 LOG(INFO) << "Deoptimizing from " << PrettyMethod(method)
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001051 << " result is " << std::hex << return_value.GetJ();
Ian Rogers62d6c772013-02-27 08:32:07 -08001052 }
1053 self->SetDeoptimizationReturnValue(return_value);
Andreas Gamped58342c2014-06-05 14:18:08 -07001054 return GetTwoWordSuccessValue(*return_pc,
1055 reinterpret_cast<uintptr_t>(GetQuickDeoptimizationEntryPoint()));
Ian Rogers62d6c772013-02-27 08:32:07 -08001056 } else {
1057 if (kVerboseInstrumentation) {
Brian Carlstrom2d888622013-07-18 17:02:00 -07001058 LOG(INFO) << "Returning from " << PrettyMethod(method)
1059 << " to PC " << reinterpret_cast<void*>(*return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001060 }
Andreas Gamped58342c2014-06-05 14:18:08 -07001061 return GetTwoWordSuccessValue(0, *return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001062 }
jeffhao725a9572012-11-13 18:20:12 -08001063}
1064
Ian Rogers62d6c772013-02-27 08:32:07 -08001065void Instrumentation::PopMethodForUnwind(Thread* self, bool is_deoptimization) const {
1066 // Do the pop.
1067 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1068 CHECK_GT(stack->size(), 0U);
1069 InstrumentationStackFrame instrumentation_frame = stack->front();
1070 // TODO: bring back CheckStackDepth(self, instrumentation_frame, 2);
1071 stack->pop_front();
1072
Brian Carlstromea46f952013-07-30 01:26:50 -07001073 mirror::ArtMethod* method = instrumentation_frame.method_;
Ian Rogers62d6c772013-02-27 08:32:07 -08001074 if (is_deoptimization) {
1075 if (kVerboseInstrumentation) {
1076 LOG(INFO) << "Popping for deoptimization " << PrettyMethod(method);
1077 }
1078 } else {
1079 if (kVerboseInstrumentation) {
1080 LOG(INFO) << "Popping for unwind " << PrettyMethod(method);
1081 }
1082
1083 // Notify listeners of method unwind.
1084 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1085 // return_pc.
1086 uint32_t dex_pc = DexFile::kDexNoIndex;
1087 MethodUnwindEvent(self, instrumentation_frame.this_object_, method, dex_pc);
1088 }
1089}
1090
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -07001091void Instrumentation::VisitRoots(RootCallback* callback, void* arg) {
1092 WriterMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -07001093 if (IsDeoptimizedMethodsEmpty()) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -07001094 return;
1095 }
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -07001096 for (auto pair : deoptimized_methods_) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -07001097 pair.second.VisitRoot(callback, arg, 0, kRootVMInternal);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -07001098 }
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -07001099}
1100
Ian Rogers62d6c772013-02-27 08:32:07 -08001101std::string InstrumentationStackFrame::Dump() const {
1102 std::ostringstream os;
1103 os << "Frame " << frame_id_ << " " << PrettyMethod(method_) << ":"
1104 << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_);
1105 return os.str();
1106}
1107
1108} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -08001109} // namespace art