blob: d25893f4c9efdc1eb8a548a568890a53fbaf9adc [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
Ian Rogersc7dd2952014-10-21 23:31:19 -070019#include <sstream>
20
Ian Rogerse63db272014-07-15 15:36:11 -070021#include "arch/context.h"
Alex Lightd7661582017-05-01 13:48:16 -070022#include "art_field-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070023#include "art_method-inl.h"
David Sehrc431b9d2018-03-02 12:01:51 -080024#include "base/atomic.h"
Andreas Gampe8228cdf2017-05-30 15:03:54 -070025#include "base/callee_save_type.h"
jeffhao725a9572012-11-13 18:20:12 -080026#include "class_linker.h"
27#include "debugger.h"
David Sehr9e734c72018-01-04 17:56:19 -080028#include "dex/dex_file-inl.h"
29#include "dex/dex_file_types.h"
30#include "dex/dex_instruction-inl.h"
Mathieu Chartierd8891782014-03-02 13:28:37 -080031#include "entrypoints/quick/quick_alloc_entrypoints.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070032#include "entrypoints/quick/quick_entrypoints.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070033#include "entrypoints/runtime_asm_entrypoints.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070034#include "gc_root-inl.h"
Sebastien Hertz138dbfc2013-12-04 18:15:25 +010035#include "interpreter/interpreter.h"
Mingyao Yang2ee17902017-08-30 11:37:08 -070036#include "interpreter/interpreter_common.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080037#include "jit/jit.h"
38#include "jit/jit_code_cache.h"
Alex Lightd7661582017-05-01 13:48:16 -070039#include "jvalue-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040#include "mirror/class-inl.h"
41#include "mirror/dex_cache.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070042#include "mirror/object-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070043#include "mirror/object_array-inl.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080044#include "nth_caller_visitor.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010045#include "oat_quick_method_header.h"
jeffhao725a9572012-11-13 18:20:12 -080046#include "thread.h"
47#include "thread_list.h"
jeffhao725a9572012-11-13 18:20:12 -080048
49namespace art {
Ian Rogers62d6c772013-02-27 08:32:07 -080050namespace instrumentation {
jeffhao725a9572012-11-13 18:20:12 -080051
Sebastien Hertz0462c4c2015-04-01 16:34:17 +020052constexpr bool kVerboseInstrumentation = false;
Sebastien Hertz5bfd5c92013-11-15 11:36:07 +010053
Alex Lightd7661582017-05-01 13:48:16 -070054void InstrumentationListener::MethodExited(Thread* thread,
55 Handle<mirror::Object> this_object,
56 ArtMethod* method,
57 uint32_t dex_pc,
58 Handle<mirror::Object> return_value) {
59 DCHECK_EQ(method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetReturnTypePrimitive(),
60 Primitive::kPrimNot);
61 JValue v;
62 v.SetL(return_value.Get());
63 MethodExited(thread, this_object, method, dex_pc, v);
64}
65
66void InstrumentationListener::FieldWritten(Thread* thread,
67 Handle<mirror::Object> this_object,
68 ArtMethod* method,
69 uint32_t dex_pc,
70 ArtField* field,
71 Handle<mirror::Object> field_value) {
72 DCHECK(!field->IsPrimitiveType());
73 JValue v;
74 v.SetL(field_value.Get());
75 FieldWritten(thread, this_object, method, dex_pc, field, v);
76}
77
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010078// Instrumentation works on non-inlined frames by updating returned PCs
79// of compiled frames.
80static constexpr StackVisitor::StackWalkKind kInstrumentationStackWalk =
81 StackVisitor::StackWalkKind::kSkipInlinedFrames;
82
Mathieu Chartiere0671ce2015-07-28 17:23:28 -070083class InstallStubsClassVisitor : public ClassVisitor {
84 public:
85 explicit InstallStubsClassVisitor(Instrumentation* instrumentation)
86 : instrumentation_(instrumentation) {}
87
Mathieu Chartier28357fa2016-10-18 16:27:40 -070088 bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE REQUIRES(Locks::mutator_lock_) {
89 instrumentation_->InstallStubsForClass(klass.Ptr());
Mathieu Chartiere0671ce2015-07-28 17:23:28 -070090 return true; // we visit all classes.
91 }
92
93 private:
94 Instrumentation* const instrumentation_;
95};
96
Alex Light2c8206f2018-06-08 14:51:09 -070097InstrumentationStackPopper::InstrumentationStackPopper(Thread* self)
98 : self_(self),
99 instrumentation_(Runtime::Current()->GetInstrumentation()),
100 frames_to_remove_(0) {}
101
102InstrumentationStackPopper::~InstrumentationStackPopper() {
103 std::deque<instrumentation::InstrumentationStackFrame>* stack = self_->GetInstrumentationStack();
104 for (size_t i = 0; i < frames_to_remove_; i++) {
105 stack->pop_front();
106 }
107}
108
109bool InstrumentationStackPopper::PopFramesTo(uint32_t desired_pops,
110 MutableHandle<mirror::Throwable>& exception) {
111 std::deque<instrumentation::InstrumentationStackFrame>* stack = self_->GetInstrumentationStack();
112 DCHECK_LE(frames_to_remove_, desired_pops);
113 DCHECK_GE(stack->size(), desired_pops);
114 DCHECK(!self_->IsExceptionPending());
115 if (!instrumentation_->HasMethodUnwindListeners()) {
116 frames_to_remove_ = desired_pops;
117 return true;
118 }
119 if (kVerboseInstrumentation) {
120 LOG(INFO) << "Popping frames for exception " << exception->Dump();
121 }
122 // The instrumentation events expect the exception to be set.
123 self_->SetException(exception.Get());
124 bool new_exception_thrown = false;
125 for (; frames_to_remove_ < desired_pops && !new_exception_thrown; frames_to_remove_++) {
126 InstrumentationStackFrame frame = stack->at(frames_to_remove_);
127 ArtMethod* method = frame.method_;
128 // Notify listeners of method unwind.
129 // TODO: improve the dex_pc information here.
130 uint32_t dex_pc = dex::kDexNoIndex;
131 if (kVerboseInstrumentation) {
132 LOG(INFO) << "Popping for unwind " << method->PrettyMethod();
133 }
134 if (!method->IsRuntimeMethod() && !frame.interpreter_entry_) {
135 instrumentation_->MethodUnwindEvent(self_, frame.this_object_, method, dex_pc);
136 new_exception_thrown = self_->GetException() != exception.Get();
137 }
138 }
139 exception.Assign(self_->GetException());
140 self_->ClearException();
141 if (kVerboseInstrumentation && new_exception_thrown) {
142 LOG(INFO) << "Failed to pop " << (desired_pops - frames_to_remove_)
143 << " frames due to new exception";
144 }
145 return !new_exception_thrown;
146}
Ian Rogers62d6c772013-02-27 08:32:07 -0800147
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700148Instrumentation::Instrumentation()
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000149 : instrumentation_stubs_installed_(false),
150 entry_exit_stubs_installed_(false),
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700151 interpreter_stubs_installed_(false),
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000152 interpret_only_(false),
153 forced_interpret_only_(false),
154 have_method_entry_listeners_(false),
155 have_method_exit_listeners_(false),
156 have_method_unwind_listeners_(false),
157 have_dex_pc_listeners_(false),
158 have_field_read_listeners_(false),
159 have_field_write_listeners_(false),
Alex Light6e1607e2017-08-23 10:06:18 -0700160 have_exception_thrown_listeners_(false),
Alex Lighte814f9d2017-07-31 16:14:39 -0700161 have_watched_frame_pop_listeners_(false),
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000162 have_branch_listeners_(false),
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000163 have_invoke_virtual_or_interface_listeners_(false),
Alex Light9fb1ab12017-09-05 09:32:49 -0700164 have_exception_handled_listeners_(false),
Mathieu Chartierb8aa1e42016-04-05 14:36:57 -0700165 deoptimized_methods_lock_("deoptimized methods lock", kDeoptimizedMethodsLock),
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700166 deoptimization_enabled_(false),
167 interpreter_handler_table_(kMainHandlerTable),
Mathieu Chartier50e93312016-03-16 11:25:29 -0700168 quick_alloc_entry_points_instrumentation_counter_(0),
169 alloc_entrypoints_instrumented_(false) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700170}
171
Sebastien Hertza10aa372015-01-21 17:30:58 +0100172void Instrumentation::InstallStubsForClass(mirror::Class* klass) {
Vladimir Marko72ab6842017-01-20 19:32:50 +0000173 if (!klass->IsResolved()) {
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100174 // We need the class to be resolved to install/uninstall stubs. Otherwise its methods
175 // could not be initialized or linked with regards to class inheritance.
Vladimir Marko72ab6842017-01-20 19:32:50 +0000176 } else if (klass->IsErroneousResolved()) {
177 // We can't execute code in a erroneous class: do nothing.
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100178 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700179 for (ArtMethod& method : klass->GetMethods(kRuntimePointerSize)) {
Alex Light51a64d52015-12-17 13:55:59 -0800180 InstallStubsForMethod(&method);
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100181 }
jeffhao725a9572012-11-13 18:20:12 -0800182 }
jeffhao725a9572012-11-13 18:20:12 -0800183}
184
Mathieu Chartiere401d142015-04-22 13:56:20 -0700185static void UpdateEntrypoints(ArtMethod* method, const void* quick_code)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700186 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800187 method->SetEntryPointFromQuickCompiledCode(quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100188}
189
Alex Light0fa17862017-10-24 13:43:05 -0700190bool Instrumentation::NeedDebugVersionFor(ArtMethod* method) const
191 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2858632018-04-02 11:28:50 -0700192 art::Runtime* runtime = Runtime::Current();
193 // If anything says we need the debug version or we are debuggable we will need the debug version
194 // of the method.
195 return (runtime->GetRuntimeCallbacks()->MethodNeedsDebugVersion(method) ||
196 runtime->IsJavaDebuggable()) &&
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800197 !method->IsNative() &&
Alex Lightf2858632018-04-02 11:28:50 -0700198 !method->IsProxyMethod();
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800199}
200
Mathieu Chartiere401d142015-04-22 13:56:20 -0700201void Instrumentation::InstallStubsForMethod(ArtMethod* method) {
Alex Light9139e002015-10-09 15:59:48 -0700202 if (!method->IsInvokable() || method->IsProxyMethod()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100203 // Do not change stubs for these methods.
204 return;
205 }
Jeff Hao56802772014-08-19 10:17:36 -0700206 // Don't stub Proxy.<init>. Note that the Proxy class itself is not a proxy class.
Alex Light6cae5ea2018-06-07 17:07:02 -0700207 // TODO We should remove the need for this since it means we cannot always correctly detect calls
208 // to Proxy.<init>
209 // Annoyingly this can be called before we have actually initialized WellKnownClasses so therefore
210 // we also need to check this based on the declaring-class descriptor. The check is valid because
211 // Proxy only has a single constructor.
212 ArtMethod* well_known_proxy_init = jni::DecodeArtMethod(
213 WellKnownClasses::java_lang_reflect_Proxy_init);
214 if ((LIKELY(well_known_proxy_init != nullptr) && UNLIKELY(method == well_known_proxy_init)) ||
215 UNLIKELY(method->IsConstructor() &&
216 method->GetDeclaringClass()->DescriptorEquals("Ljava/lang/reflect/Proxy;"))) {
Jeff Haodb8a6642014-08-14 17:18:52 -0700217 return;
218 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800219 const void* new_quick_code;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100220 bool uninstall = !entry_exit_stubs_installed_ && !interpreter_stubs_installed_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800221 Runtime* const runtime = Runtime::Current();
222 ClassLinker* const class_linker = runtime->GetClassLinker();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100223 bool is_class_initialized = method->GetDeclaringClass()->IsInitialized();
224 if (uninstall) {
225 if ((forced_interpret_only_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800226 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100227 } else if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Alex Light2d441b12018-06-08 15:33:21 -0700228 // It would be great to search the JIT for its implementation here but we cannot due to the
229 // locks we hold. Instead just set to the interpreter bridge and that code will search the JIT
230 // when it gets called and replace the entrypoint then.
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000231 if (NeedDebugVersionFor(method)) {
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800232 new_quick_code = GetQuickToInterpreterBridge();
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000233 } else {
Alex Lightfc49fec2018-01-16 22:28:36 +0000234 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800235 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100236 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700237 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100238 }
239 } else { // !uninstall
Sebastien Hertzbae182c2013-12-17 10:42:03 +0100240 if ((interpreter_stubs_installed_ || forced_interpret_only_ || IsDeoptimized(method)) &&
241 !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800242 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100243 } else {
244 // Do not overwrite resolution trampoline. When the trampoline initializes the method's
245 // class, all its static methods code will be set to the instrumentation entry point.
246 // For more details, see ClassLinker::FixupStaticTrampolines.
247 if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Alex Light2d441b12018-06-08 15:33:21 -0700248 if (entry_exit_stubs_installed_) {
249 // This needs to be checked first since the instrumentation entrypoint will be able to
250 // find the actual JIT compiled code that corresponds to this method.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800251 new_quick_code = GetQuickInstrumentationEntryPoint();
Alex Light2d441b12018-06-08 15:33:21 -0700252 } else if (NeedDebugVersionFor(method)) {
253 // It would be great to search the JIT for its implementation here but we cannot due to
254 // the locks we hold. Instead just set to the interpreter bridge and that code will search
255 // the JIT when it gets called and replace the entrypoint then.
256 new_quick_code = GetQuickToInterpreterBridge();
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000257 } else {
Alex Lightfc49fec2018-01-16 22:28:36 +0000258 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100259 }
260 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700261 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100262 }
263 }
264 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800265 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100266}
267
Ian Rogers62d6c772013-02-27 08:32:07 -0800268// Places the instrumentation exit pc as the return PC for every quick frame. This also allows
269// deoptimization of quick frames to interpreter frames.
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100270// Since we may already have done this previously, we need to push new instrumentation frame before
271// existing instrumentation frames.
Ian Rogers62d6c772013-02-27 08:32:07 -0800272static void InstrumentationInstallStack(Thread* thread, void* arg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700273 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200274 struct InstallStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800275 InstallStackVisitor(Thread* thread_in, Context* context, uintptr_t instrumentation_exit_pc)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100276 : StackVisitor(thread_in, context, kInstrumentationStackWalk),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800277 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100278 instrumentation_exit_pc_(instrumentation_exit_pc),
Alex Lighte9278662018-03-08 16:55:58 -0800279 reached_existing_instrumentation_frames_(false), instrumentation_stack_depth_(0),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100280 last_return_pc_(0) {
281 }
jeffhao725a9572012-11-13 18:20:12 -0800282
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700283 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700284 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700285 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800286 if (kVerboseInstrumentation) {
287 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
288 }
289 last_return_pc_ = 0;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700290 return true; // Ignore upcalls.
Ian Rogers306057f2012-11-26 12:45:53 -0800291 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700292 if (GetCurrentQuickFrame() == nullptr) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800293 bool interpreter_frame = true;
Sebastien Hertz320deb22014-06-11 19:45:05 +0200294 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, 0, GetFrameId(),
295 interpreter_frame);
Jeff Haoa15a81b2014-05-27 18:25:47 -0700296 if (kVerboseInstrumentation) {
297 LOG(INFO) << "Pushing shadow frame " << instrumentation_frame.Dump();
298 }
299 shadow_stack_.push_back(instrumentation_frame);
300 return true; // Continue.
301 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800302 uintptr_t return_pc = GetReturnPc();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200303 if (kVerboseInstrumentation) {
304 LOG(INFO) << " Installing exit stub in " << DescribeLocation();
305 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100306 if (return_pc == instrumentation_exit_pc_) {
Mingyao Yang2ee17902017-08-30 11:37:08 -0700307 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
308
309 if (m->IsRuntimeMethod()) {
310 const InstrumentationStackFrame& frame =
Vladimir Marko35d5b8a2018-07-03 09:18:32 +0100311 (*instrumentation_stack_)[instrumentation_stack_depth_];
Mingyao Yang2ee17902017-08-30 11:37:08 -0700312 if (frame.interpreter_entry_) {
313 // This instrumentation frame is for an interpreter bridge and is
314 // pushed when executing the instrumented interpreter bridge. So method
315 // enter event must have been reported. However we need to push a DEX pc
316 // into the dex_pcs_ list to match size of instrumentation stack.
Andreas Gampee2abbc62017-09-15 11:59:26 -0700317 uint32_t dex_pc = dex::kDexNoIndex;
Mingyao Yang2ee17902017-08-30 11:37:08 -0700318 dex_pcs_.push_back(dex_pc);
319 last_return_pc_ = frame.return_pc_;
320 ++instrumentation_stack_depth_;
321 return true;
322 }
323 }
324
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100325 // We've reached a frame which has already been installed with instrumentation exit stub.
Alex Light74c91c92018-03-08 14:01:44 -0800326 // We should have already installed instrumentation or be interpreter on previous frames.
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100327 reached_existing_instrumentation_frames_ = true;
328
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200329 const InstrumentationStackFrame& frame =
Vladimir Marko35d5b8a2018-07-03 09:18:32 +0100330 (*instrumentation_stack_)[instrumentation_stack_depth_];
David Sehr709b0702016-10-13 09:12:37 -0700331 CHECK_EQ(m, frame.method_) << "Expected " << ArtMethod::PrettyMethod(m)
332 << ", Found " << ArtMethod::PrettyMethod(frame.method_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100333 return_pc = frame.return_pc_;
334 if (kVerboseInstrumentation) {
335 LOG(INFO) << "Ignoring already instrumented " << frame.Dump();
336 }
337 } else {
338 CHECK_NE(return_pc, 0U);
Alex Light74c91c92018-03-08 14:01:44 -0800339 if (UNLIKELY(reached_existing_instrumentation_frames_ && !m->IsRuntimeMethod())) {
340 // We already saw an existing instrumentation frame so this should be a runtime-method
341 // inserted by the interpreter or runtime.
Alex Lighte9278662018-03-08 16:55:58 -0800342 std::string thread_name;
343 GetThread()->GetThreadName(thread_name);
344 uint32_t dex_pc = dex::kDexNoIndex;
345 if (last_return_pc_ != 0 &&
346 GetCurrentOatQuickMethodHeader() != nullptr) {
347 dex_pc = GetCurrentOatQuickMethodHeader()->ToDexPc(m, last_return_pc_);
348 }
Alex Light74c91c92018-03-08 14:01:44 -0800349 LOG(FATAL) << "While walking " << thread_name << " found unexpected non-runtime method"
350 << " without instrumentation exit return or interpreter frame."
Alex Lighte9278662018-03-08 16:55:58 -0800351 << " method is " << GetMethod()->PrettyMethod()
352 << " return_pc is " << std::hex << return_pc
353 << " dex pc: " << dex_pc;
354 UNREACHABLE();
355 }
Mingyao Yang2ee17902017-08-30 11:37:08 -0700356 InstrumentationStackFrame instrumentation_frame(
357 m->IsRuntimeMethod() ? nullptr : GetThisObject(),
358 m,
359 return_pc,
360 GetFrameId(), // A runtime method still gets a frame id.
361 false);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100362 if (kVerboseInstrumentation) {
363 LOG(INFO) << "Pushing frame " << instrumentation_frame.Dump();
364 }
365
Sebastien Hertz320deb22014-06-11 19:45:05 +0200366 // Insert frame at the right position so we do not corrupt the instrumentation stack.
367 // Instrumentation stack frames are in descending frame id order.
368 auto it = instrumentation_stack_->begin();
369 for (auto end = instrumentation_stack_->end(); it != end; ++it) {
370 const InstrumentationStackFrame& current = *it;
371 if (instrumentation_frame.frame_id_ >= current.frame_id_) {
372 break;
373 }
374 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100375 instrumentation_stack_->insert(it, instrumentation_frame);
376 SetReturnPc(instrumentation_exit_pc_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800377 }
Andreas Gampee2abbc62017-09-15 11:59:26 -0700378 uint32_t dex_pc = dex::kDexNoIndex;
Mingyao Yang2ee17902017-08-30 11:37:08 -0700379 if (last_return_pc_ != 0 &&
380 GetCurrentOatQuickMethodHeader() != nullptr) {
381 dex_pc = GetCurrentOatQuickMethodHeader()->ToDexPc(m, last_return_pc_);
382 }
383 dex_pcs_.push_back(dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800384 last_return_pc_ = return_pc;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100385 ++instrumentation_stack_depth_;
Ian Rogers306057f2012-11-26 12:45:53 -0800386 return true; // Continue.
387 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800388 std::deque<InstrumentationStackFrame>* const instrumentation_stack_;
Jeff Haoa15a81b2014-05-27 18:25:47 -0700389 std::vector<InstrumentationStackFrame> shadow_stack_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800390 std::vector<uint32_t> dex_pcs_;
Ian Rogers306057f2012-11-26 12:45:53 -0800391 const uintptr_t instrumentation_exit_pc_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100392 bool reached_existing_instrumentation_frames_;
393 size_t instrumentation_stack_depth_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800394 uintptr_t last_return_pc_;
Ian Rogers306057f2012-11-26 12:45:53 -0800395 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800396 if (kVerboseInstrumentation) {
397 std::string thread_name;
398 thread->GetThreadName(thread_name);
399 LOG(INFO) << "Installing exit stubs in " << thread_name;
Ian Rogers306057f2012-11-26 12:45:53 -0800400 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100401
402 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers700a4022014-05-19 16:49:03 -0700403 std::unique_ptr<Context> context(Context::Create());
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700404 uintptr_t instrumentation_exit_pc = reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100405 InstallStackVisitor visitor(thread, context.get(), instrumentation_exit_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800406 visitor.WalkStack(true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100407 CHECK_EQ(visitor.dex_pcs_.size(), thread->GetInstrumentationStack()->size());
Ian Rogers62d6c772013-02-27 08:32:07 -0800408
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100409 if (instrumentation->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100410 // Create method enter events for all methods currently on the thread's stack. We only do this
411 // if no debugger is attached to prevent from posting events twice.
Jeff Haoa15a81b2014-05-27 18:25:47 -0700412 auto ssi = visitor.shadow_stack_.rbegin();
413 for (auto isi = thread->GetInstrumentationStack()->rbegin(),
414 end = thread->GetInstrumentationStack()->rend(); isi != end; ++isi) {
415 while (ssi != visitor.shadow_stack_.rend() && (*ssi).frame_id_ < (*isi).frame_id_) {
416 instrumentation->MethodEnterEvent(thread, (*ssi).this_object_, (*ssi).method_, 0);
417 ++ssi;
418 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100419 uint32_t dex_pc = visitor.dex_pcs_.back();
420 visitor.dex_pcs_.pop_back();
Alex Lightdc5423f2018-06-08 10:43:38 -0700421 if (!isi->interpreter_entry_ && !isi->method_->IsRuntimeMethod()) {
Sebastien Hertz320deb22014-06-11 19:45:05 +0200422 instrumentation->MethodEnterEvent(thread, (*isi).this_object_, (*isi).method_, dex_pc);
423 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100424 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800425 }
426 thread->VerifyStack();
Ian Rogers306057f2012-11-26 12:45:53 -0800427}
428
Mingyao Yang99170c62015-07-06 11:10:37 -0700429void Instrumentation::InstrumentThreadStack(Thread* thread) {
430 instrumentation_stubs_installed_ = true;
431 InstrumentationInstallStack(thread, this);
432}
433
Ian Rogers62d6c772013-02-27 08:32:07 -0800434// Removes the instrumentation exit pc as the return PC for every quick frame.
435static void InstrumentationRestoreStack(Thread* thread, void* arg)
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000436 REQUIRES(Locks::mutator_lock_) {
437 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
438
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200439 struct RestoreStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800440 RestoreStackVisitor(Thread* thread_in, uintptr_t instrumentation_exit_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800441 Instrumentation* instrumentation)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100442 : StackVisitor(thread_in, nullptr, kInstrumentationStackWalk),
443 thread_(thread_in),
Ian Rogers62d6c772013-02-27 08:32:07 -0800444 instrumentation_exit_pc_(instrumentation_exit_pc),
445 instrumentation_(instrumentation),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800446 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Ian Rogers62d6c772013-02-27 08:32:07 -0800447 frames_removed_(0) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800448
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700449 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800450 if (instrumentation_stack_->size() == 0) {
jeffhao725a9572012-11-13 18:20:12 -0800451 return false; // Stop.
452 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700453 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700454 if (GetCurrentQuickFrame() == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800455 if (kVerboseInstrumentation) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200456 LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId()
David Sehr709b0702016-10-13 09:12:37 -0700457 << " Method=" << ArtMethod::PrettyMethod(m);
Ian Rogers62d6c772013-02-27 08:32:07 -0800458 }
459 return true; // Ignore shadow frames.
460 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700461 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800462 if (kVerboseInstrumentation) {
463 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
464 }
Ian Rogers306057f2012-11-26 12:45:53 -0800465 return true; // Ignore upcalls.
466 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800467 bool removed_stub = false;
468 // TODO: make this search more efficient?
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100469 const size_t frameId = GetFrameId();
470 for (const InstrumentationStackFrame& instrumentation_frame : *instrumentation_stack_) {
471 if (instrumentation_frame.frame_id_ == frameId) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800472 if (kVerboseInstrumentation) {
473 LOG(INFO) << " Removing exit stub in " << DescribeLocation();
474 }
Jeff Hao9a916d32013-06-27 18:45:37 -0700475 if (instrumentation_frame.interpreter_entry_) {
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700476 CHECK(m == Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Jeff Hao9a916d32013-06-27 18:45:37 -0700477 } else {
David Sehr709b0702016-10-13 09:12:37 -0700478 CHECK(m == instrumentation_frame.method_) << ArtMethod::PrettyMethod(m);
Jeff Hao9a916d32013-06-27 18:45:37 -0700479 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800480 SetReturnPc(instrumentation_frame.return_pc_);
Mingyao Yang2ee17902017-08-30 11:37:08 -0700481 if (instrumentation_->ShouldNotifyMethodEnterExitEvents() &&
482 !m->IsRuntimeMethod()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100483 // Create the method exit events. As the methods didn't really exit the result is 0.
484 // We only do this if no debugger is attached to prevent from posting events twice.
485 instrumentation_->MethodExitEvent(thread_, instrumentation_frame.this_object_, m,
486 GetDexPc(), JValue());
487 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800488 frames_removed_++;
489 removed_stub = true;
490 break;
491 }
492 }
493 if (!removed_stub) {
494 if (kVerboseInstrumentation) {
495 LOG(INFO) << " No exit stub in " << DescribeLocation();
Ian Rogers306057f2012-11-26 12:45:53 -0800496 }
jeffhao725a9572012-11-13 18:20:12 -0800497 }
498 return true; // Continue.
499 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800500 Thread* const thread_;
Ian Rogers306057f2012-11-26 12:45:53 -0800501 const uintptr_t instrumentation_exit_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800502 Instrumentation* const instrumentation_;
503 std::deque<instrumentation::InstrumentationStackFrame>* const instrumentation_stack_;
504 size_t frames_removed_;
jeffhao725a9572012-11-13 18:20:12 -0800505 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800506 if (kVerboseInstrumentation) {
507 std::string thread_name;
508 thread->GetThreadName(thread_name);
509 LOG(INFO) << "Removing exit stubs in " << thread_name;
510 }
511 std::deque<instrumentation::InstrumentationStackFrame>* stack = thread->GetInstrumentationStack();
512 if (stack->size() > 0) {
513 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700514 uintptr_t instrumentation_exit_pc =
515 reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Ian Rogers62d6c772013-02-27 08:32:07 -0800516 RestoreStackVisitor visitor(thread, instrumentation_exit_pc, instrumentation);
517 visitor.WalkStack(true);
518 CHECK_EQ(visitor.frames_removed_, stack->size());
519 while (stack->size() > 0) {
520 stack->pop_front();
521 }
jeffhao725a9572012-11-13 18:20:12 -0800522 }
523}
524
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200525static bool HasEvent(Instrumentation::InstrumentationEvent expected, uint32_t events) {
526 return (events & expected) != 0;
527}
528
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000529static void PotentiallyAddListenerTo(Instrumentation::InstrumentationEvent event,
530 uint32_t events,
531 std::list<InstrumentationListener*>& list,
532 InstrumentationListener* listener,
533 bool* has_listener)
534 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) {
535 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
536 if (!HasEvent(event, events)) {
537 return;
538 }
539 // If there is a free slot in the list, we insert the listener in that slot.
540 // Otherwise we add it to the end of the list.
541 auto it = std::find(list.begin(), list.end(), nullptr);
542 if (it != list.end()) {
543 *it = listener;
544 } else {
545 list.push_back(listener);
546 }
547 *has_listener = true;
548}
549
Ian Rogers62d6c772013-02-27 08:32:07 -0800550void Instrumentation::AddListener(InstrumentationListener* listener, uint32_t events) {
551 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000552 PotentiallyAddListenerTo(kMethodEntered,
553 events,
554 method_entry_listeners_,
555 listener,
556 &have_method_entry_listeners_);
557 PotentiallyAddListenerTo(kMethodExited,
558 events,
559 method_exit_listeners_,
560 listener,
561 &have_method_exit_listeners_);
562 PotentiallyAddListenerTo(kMethodUnwind,
563 events,
564 method_unwind_listeners_,
565 listener,
566 &have_method_unwind_listeners_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000567 PotentiallyAddListenerTo(kBranch,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000568 events,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000569 branch_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000570 listener,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000571 &have_branch_listeners_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000572 PotentiallyAddListenerTo(kInvokeVirtualOrInterface,
573 events,
574 invoke_virtual_or_interface_listeners_,
575 listener,
576 &have_invoke_virtual_or_interface_listeners_);
577 PotentiallyAddListenerTo(kDexPcMoved,
578 events,
579 dex_pc_listeners_,
580 listener,
581 &have_dex_pc_listeners_);
582 PotentiallyAddListenerTo(kFieldRead,
583 events,
584 field_read_listeners_,
585 listener,
586 &have_field_read_listeners_);
587 PotentiallyAddListenerTo(kFieldWritten,
588 events,
589 field_write_listeners_,
590 listener,
591 &have_field_write_listeners_);
Alex Light6e1607e2017-08-23 10:06:18 -0700592 PotentiallyAddListenerTo(kExceptionThrown,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000593 events,
Alex Light6e1607e2017-08-23 10:06:18 -0700594 exception_thrown_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000595 listener,
Alex Light6e1607e2017-08-23 10:06:18 -0700596 &have_exception_thrown_listeners_);
Alex Lighte814f9d2017-07-31 16:14:39 -0700597 PotentiallyAddListenerTo(kWatchedFramePop,
598 events,
599 watched_frame_pop_listeners_,
600 listener,
601 &have_watched_frame_pop_listeners_);
Alex Light9fb1ab12017-09-05 09:32:49 -0700602 PotentiallyAddListenerTo(kExceptionHandled,
603 events,
604 exception_handled_listeners_,
605 listener,
606 &have_exception_handled_listeners_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200607 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800608}
609
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000610static void PotentiallyRemoveListenerFrom(Instrumentation::InstrumentationEvent event,
611 uint32_t events,
612 std::list<InstrumentationListener*>& list,
613 InstrumentationListener* listener,
614 bool* has_listener)
615 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) {
616 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
617 if (!HasEvent(event, events)) {
618 return;
619 }
620 auto it = std::find(list.begin(), list.end(), listener);
621 if (it != list.end()) {
622 // Just update the entry, do not remove from the list. Removing entries in the list
623 // is unsafe when mutators are iterating over it.
624 *it = nullptr;
625 }
626
627 // Check if the list contains any non-null listener, and update 'has_listener'.
628 for (InstrumentationListener* l : list) {
629 if (l != nullptr) {
630 *has_listener = true;
631 return;
632 }
633 }
634 *has_listener = false;
635}
636
Ian Rogers62d6c772013-02-27 08:32:07 -0800637void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) {
638 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000639 PotentiallyRemoveListenerFrom(kMethodEntered,
640 events,
641 method_entry_listeners_,
642 listener,
643 &have_method_entry_listeners_);
644 PotentiallyRemoveListenerFrom(kMethodExited,
645 events,
646 method_exit_listeners_,
647 listener,
648 &have_method_exit_listeners_);
649 PotentiallyRemoveListenerFrom(kMethodUnwind,
650 events,
651 method_unwind_listeners_,
652 listener,
653 &have_method_unwind_listeners_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000654 PotentiallyRemoveListenerFrom(kBranch,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000655 events,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000656 branch_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000657 listener,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000658 &have_branch_listeners_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000659 PotentiallyRemoveListenerFrom(kInvokeVirtualOrInterface,
660 events,
661 invoke_virtual_or_interface_listeners_,
662 listener,
663 &have_invoke_virtual_or_interface_listeners_);
664 PotentiallyRemoveListenerFrom(kDexPcMoved,
665 events,
666 dex_pc_listeners_,
667 listener,
668 &have_dex_pc_listeners_);
669 PotentiallyRemoveListenerFrom(kFieldRead,
670 events,
671 field_read_listeners_,
672 listener,
673 &have_field_read_listeners_);
674 PotentiallyRemoveListenerFrom(kFieldWritten,
675 events,
676 field_write_listeners_,
677 listener,
678 &have_field_write_listeners_);
Alex Light6e1607e2017-08-23 10:06:18 -0700679 PotentiallyRemoveListenerFrom(kExceptionThrown,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000680 events,
Alex Light6e1607e2017-08-23 10:06:18 -0700681 exception_thrown_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000682 listener,
Alex Light6e1607e2017-08-23 10:06:18 -0700683 &have_exception_thrown_listeners_);
Alex Lighte814f9d2017-07-31 16:14:39 -0700684 PotentiallyRemoveListenerFrom(kWatchedFramePop,
685 events,
686 watched_frame_pop_listeners_,
687 listener,
688 &have_watched_frame_pop_listeners_);
Alex Light9fb1ab12017-09-05 09:32:49 -0700689 PotentiallyRemoveListenerFrom(kExceptionHandled,
690 events,
691 exception_handled_listeners_,
692 listener,
693 &have_exception_handled_listeners_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200694 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800695}
696
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200697Instrumentation::InstrumentationLevel Instrumentation::GetCurrentInstrumentationLevel() const {
Alex Light4ba388a2017-01-27 10:26:49 -0800698 if (interpreter_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200699 return InstrumentationLevel::kInstrumentWithInterpreter;
Ian Rogers62d6c772013-02-27 08:32:07 -0800700 } else if (entry_exit_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200701 return InstrumentationLevel::kInstrumentWithInstrumentationStubs;
Ian Rogers62d6c772013-02-27 08:32:07 -0800702 } else {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200703 return InstrumentationLevel::kInstrumentNothing;
Ian Rogers62d6c772013-02-27 08:32:07 -0800704 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200705}
706
Alex Lightdba61482016-12-21 08:20:29 -0800707bool Instrumentation::RequiresInstrumentationInstallation(InstrumentationLevel new_level) const {
Alex Light4ba388a2017-01-27 10:26:49 -0800708 // We need to reinstall instrumentation if we go to a different level.
709 return GetCurrentInstrumentationLevel() != new_level;
Alex Lightdba61482016-12-21 08:20:29 -0800710}
711
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200712void Instrumentation::ConfigureStubs(const char* key, InstrumentationLevel desired_level) {
713 // Store the instrumentation level for this key or remove it.
714 if (desired_level == InstrumentationLevel::kInstrumentNothing) {
715 // The client no longer needs instrumentation.
716 requested_instrumentation_levels_.erase(key);
717 } else {
718 // The client needs instrumentation.
719 requested_instrumentation_levels_.Overwrite(key, desired_level);
720 }
721
722 // Look for the highest required instrumentation level.
723 InstrumentationLevel requested_level = InstrumentationLevel::kInstrumentNothing;
724 for (const auto& v : requested_instrumentation_levels_) {
725 requested_level = std::max(requested_level, v.second);
726 }
727
728 interpret_only_ = (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) ||
729 forced_interpret_only_;
730
Alex Lightdba61482016-12-21 08:20:29 -0800731 if (!RequiresInstrumentationInstallation(requested_level)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800732 // We're already set.
733 return;
734 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100735 Thread* const self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -0800736 Runtime* runtime = Runtime::Current();
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100737 Locks::mutator_lock_->AssertExclusiveHeld(self);
Ian Rogers62d6c772013-02-27 08:32:07 -0800738 Locks::thread_list_lock_->AssertNotHeld(self);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200739 if (requested_level > InstrumentationLevel::kInstrumentNothing) {
Alex Light4ba388a2017-01-27 10:26:49 -0800740 if (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800741 interpreter_stubs_installed_ = true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800742 entry_exit_stubs_installed_ = true;
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200743 } else {
744 CHECK_EQ(requested_level, InstrumentationLevel::kInstrumentWithInstrumentationStubs);
745 entry_exit_stubs_installed_ = true;
746 interpreter_stubs_installed_ = false;
Ian Rogers62d6c772013-02-27 08:32:07 -0800747 }
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700748 InstallStubsClassVisitor visitor(this);
749 runtime->GetClassLinker()->VisitClasses(&visitor);
Ian Rogers62d6c772013-02-27 08:32:07 -0800750 instrumentation_stubs_installed_ = true;
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100751 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800752 runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this);
753 } else {
754 interpreter_stubs_installed_ = false;
755 entry_exit_stubs_installed_ = false;
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700756 InstallStubsClassVisitor visitor(this);
757 runtime->GetClassLinker()->VisitClasses(&visitor);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100758 // Restore stack only if there is no method currently deoptimized.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700759 bool empty;
760 {
761 ReaderMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700762 empty = IsDeoptimizedMethodsEmpty(); // Avoid lock violation.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700763 }
764 if (empty) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100765 MutexLock mu(self, *Locks::thread_list_lock_);
766 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000767 // Only do this after restoring, as walking the stack when restoring will see
768 // the instrumentation exit pc.
769 instrumentation_stubs_installed_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100770 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800771 }
jeffhao725a9572012-11-13 18:20:12 -0800772}
773
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200774static void ResetQuickAllocEntryPointsForThread(Thread* thread, void* arg ATTRIBUTE_UNUSED) {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800775 thread->ResetQuickAllocEntryPointsForThread(kUseReadBarrier && thread->GetIsGcMarking());
Ian Rogersfa824272013-11-05 16:12:57 -0800776}
777
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700778void Instrumentation::SetEntrypointsInstrumented(bool instrumented) {
779 Thread* self = Thread::Current();
Mathieu Chartier661974a2014-01-09 11:23:53 -0800780 Runtime* runtime = Runtime::Current();
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700781 Locks::mutator_lock_->AssertNotHeld(self);
782 Locks::instrument_entrypoints_lock_->AssertHeld(self);
783 if (runtime->IsStarted()) {
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700784 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700785 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
Mathieu Chartier661974a2014-01-09 11:23:53 -0800786 SetQuickAllocEntryPointsInstrumented(instrumented);
787 ResetQuickAllocEntryPoints();
Mathieu Chartier50e93312016-03-16 11:25:29 -0700788 alloc_entrypoints_instrumented_ = instrumented;
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700789 } else {
790 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
791 SetQuickAllocEntryPointsInstrumented(instrumented);
Andreas Gampe157c77e2016-10-17 17:44:41 -0700792
793 // Note: ResetQuickAllocEntryPoints only works when the runtime is started. Manually run the
794 // update for just this thread.
Andreas Gampe162ae502016-10-18 10:03:42 -0700795 // Note: self may be null. One of those paths is setting instrumentation in the Heap
796 // constructor for gcstress mode.
797 if (self != nullptr) {
798 ResetQuickAllocEntryPointsForThread(self, nullptr);
799 }
Andreas Gampe157c77e2016-10-17 17:44:41 -0700800
Mathieu Chartier50e93312016-03-16 11:25:29 -0700801 alloc_entrypoints_instrumented_ = instrumented;
Mathieu Chartier661974a2014-01-09 11:23:53 -0800802 }
803}
804
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700805void Instrumentation::InstrumentQuickAllocEntryPoints() {
806 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
807 InstrumentQuickAllocEntryPointsLocked();
Ian Rogersfa824272013-11-05 16:12:57 -0800808}
809
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700810void Instrumentation::UninstrumentQuickAllocEntryPoints() {
811 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
812 UninstrumentQuickAllocEntryPointsLocked();
813}
814
815void Instrumentation::InstrumentQuickAllocEntryPointsLocked() {
816 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
817 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
818 SetEntrypointsInstrumented(true);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800819 }
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700820 ++quick_alloc_entry_points_instrumentation_counter_;
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700821}
822
823void Instrumentation::UninstrumentQuickAllocEntryPointsLocked() {
824 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
825 CHECK_GT(quick_alloc_entry_points_instrumentation_counter_, 0U);
826 --quick_alloc_entry_points_instrumentation_counter_;
827 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
828 SetEntrypointsInstrumented(false);
829 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800830}
831
832void Instrumentation::ResetQuickAllocEntryPoints() {
833 Runtime* runtime = Runtime::Current();
834 if (runtime->IsStarted()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800835 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700836 runtime->GetThreadList()->ForEach(ResetQuickAllocEntryPointsForThread, nullptr);
Ian Rogersfa824272013-11-05 16:12:57 -0800837 }
838}
839
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700840void Instrumentation::UpdateMethodsCodeImpl(ArtMethod* method, const void* quick_code) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800841 const void* new_quick_code;
Ian Rogers62d6c772013-02-27 08:32:07 -0800842 if (LIKELY(!instrumentation_stubs_installed_)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800843 new_quick_code = quick_code;
Jeff Hao65d15d92013-07-16 16:39:33 -0700844 } else {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100845 if ((interpreter_stubs_installed_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800846 new_quick_code = GetQuickToInterpreterBridge();
Jeff Hao65d15d92013-07-16 16:39:33 -0700847 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700848 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700849 if (class_linker->IsQuickResolutionStub(quick_code) ||
850 class_linker->IsQuickToInterpreterBridge(quick_code)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700851 new_quick_code = quick_code;
Alex Light6cae5ea2018-06-07 17:07:02 -0700852 } else if (entry_exit_stubs_installed_ &&
853 // We need to make sure not to replace anything that InstallStubsForMethod
854 // wouldn't. Specifically we cannot stub out Proxy.<init> since subtypes copy the
855 // implementation directly and this will confuse the instrumentation trampolines.
856 // TODO We should remove the need for this since it makes it impossible to profile
857 // Proxy.<init> correctly in all cases.
858 method != jni::DecodeArtMethod(WellKnownClasses::java_lang_reflect_Proxy_init)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700859 new_quick_code = GetQuickInstrumentationEntryPoint();
Alex Light2d441b12018-06-08 15:33:21 -0700860 if (!method->IsNative() && Runtime::Current()->GetJit() != nullptr) {
861 // Native methods use trampoline entrypoints during interpreter tracing.
862 DCHECK(!Runtime::Current()->GetJit()->GetCodeCache()->GetGarbageCollectCode());
863 ProfilingInfo* profiling_info = method->GetProfilingInfo(kRuntimePointerSize);
864 // Tracing will look at the saved entry point in the profiling info to know the actual
865 // entrypoint, so we store it here.
866 if (profiling_info != nullptr) {
867 profiling_info->SetSavedEntryPoint(quick_code);
868 }
869 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700870 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700871 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700872 }
Jeff Hao65d15d92013-07-16 16:39:33 -0700873 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800874 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800875 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100876}
877
Nicolas Geoffraya6e0e7d2018-01-26 13:16:50 +0000878void Instrumentation::UpdateNativeMethodsCodeToJitCode(ArtMethod* method, const void* quick_code) {
879 // We don't do any read barrier on `method`'s declaring class in this code, as the JIT might
880 // enter here on a soon-to-be deleted ArtMethod. Updating the entrypoint is OK though, as
881 // the ArtMethod is still in memory.
882 const void* new_quick_code = quick_code;
883 if (UNLIKELY(instrumentation_stubs_installed_) && entry_exit_stubs_installed_) {
884 new_quick_code = GetQuickInstrumentationEntryPoint();
885 }
886 UpdateEntrypoints(method, new_quick_code);
887}
888
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700889void Instrumentation::UpdateMethodsCode(ArtMethod* method, const void* quick_code) {
890 DCHECK(method->GetDeclaringClass()->IsResolved());
891 UpdateMethodsCodeImpl(method, quick_code);
892}
893
Alex Light0a5ec3d2017-07-25 16:50:26 -0700894void Instrumentation::UpdateMethodsCodeToInterpreterEntryPoint(ArtMethod* method) {
895 UpdateMethodsCodeImpl(method, GetQuickToInterpreterBridge());
896}
897
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000898void Instrumentation::UpdateMethodsCodeForJavaDebuggable(ArtMethod* method,
899 const void* quick_code) {
900 // When the runtime is set to Java debuggable, we may update the entry points of
901 // all methods of a class to the interpreter bridge. A method's declaring class
902 // might not be in resolved state yet in that case, so we bypass the DCHECK in
903 // UpdateMethodsCode.
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700904 UpdateMethodsCodeImpl(method, quick_code);
905}
906
Mathieu Chartiere401d142015-04-22 13:56:20 -0700907bool Instrumentation::AddDeoptimizedMethod(ArtMethod* method) {
908 if (IsDeoptimizedMethod(method)) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700909 // Already in the map. Return.
910 return false;
911 }
912 // Not found. Add it.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700913 deoptimized_methods_.insert(method);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700914 return true;
915}
916
Mathieu Chartiere401d142015-04-22 13:56:20 -0700917bool Instrumentation::IsDeoptimizedMethod(ArtMethod* method) {
918 return deoptimized_methods_.find(method) != deoptimized_methods_.end();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700919}
920
Mathieu Chartiere401d142015-04-22 13:56:20 -0700921ArtMethod* Instrumentation::BeginDeoptimizedMethod() {
922 if (deoptimized_methods_.empty()) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700923 // Empty.
924 return nullptr;
925 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700926 return *deoptimized_methods_.begin();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700927}
928
Mathieu Chartiere401d142015-04-22 13:56:20 -0700929bool Instrumentation::RemoveDeoptimizedMethod(ArtMethod* method) {
930 auto it = deoptimized_methods_.find(method);
931 if (it == deoptimized_methods_.end()) {
932 return false;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700933 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700934 deoptimized_methods_.erase(it);
935 return true;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700936}
937
938bool Instrumentation::IsDeoptimizedMethodsEmpty() const {
939 return deoptimized_methods_.empty();
940}
941
Mathieu Chartiere401d142015-04-22 13:56:20 -0700942void Instrumentation::Deoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100943 CHECK(!method->IsNative());
944 CHECK(!method->IsProxyMethod());
Alex Light9139e002015-10-09 15:59:48 -0700945 CHECK(method->IsInvokable());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100946
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700947 Thread* self = Thread::Current();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700948 {
949 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700950 bool has_not_been_deoptimized = AddDeoptimizedMethod(method);
David Sehr709b0702016-10-13 09:12:37 -0700951 CHECK(has_not_been_deoptimized) << "Method " << ArtMethod::PrettyMethod(method)
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200952 << " is already deoptimized";
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700953 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100954 if (!interpreter_stubs_installed_) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800955 UpdateEntrypoints(method, GetQuickInstrumentationEntryPoint());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100956
957 // Install instrumentation exit stub and instrumentation frames. We may already have installed
958 // these previously so it will only cover the newly created frames.
959 instrumentation_stubs_installed_ = true;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700960 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100961 Runtime::Current()->GetThreadList()->ForEach(InstrumentationInstallStack, this);
962 }
963}
964
Mathieu Chartiere401d142015-04-22 13:56:20 -0700965void Instrumentation::Undeoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100966 CHECK(!method->IsNative());
967 CHECK(!method->IsProxyMethod());
Alex Light9139e002015-10-09 15:59:48 -0700968 CHECK(method->IsInvokable());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100969
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700970 Thread* self = Thread::Current();
971 bool empty;
972 {
973 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700974 bool found_and_erased = RemoveDeoptimizedMethod(method);
David Sehr709b0702016-10-13 09:12:37 -0700975 CHECK(found_and_erased) << "Method " << ArtMethod::PrettyMethod(method)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700976 << " is not deoptimized";
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700977 empty = IsDeoptimizedMethodsEmpty();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700978 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100979
980 // Restore code and possibly stack only if we did not deoptimize everything.
981 if (!interpreter_stubs_installed_) {
982 // Restore its code or resolution trampoline.
983 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800984 if (method->IsStatic() && !method->IsConstructor() &&
985 !method->GetDeclaringClass()->IsInitialized()) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800986 UpdateEntrypoints(method, GetQuickResolutionStub());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100987 } else {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000988 const void* quick_code = NeedDebugVersionFor(method)
989 ? GetQuickToInterpreterBridge()
Alex Lightfc49fec2018-01-16 22:28:36 +0000990 : class_linker->GetQuickOatCodeFor(method);
Elliott Hughes956af0f2014-12-11 14:34:28 -0800991 UpdateEntrypoints(method, quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100992 }
993
994 // If there is no deoptimized method left, we can restore the stack of each thread.
Alex Lightf244a572018-06-08 13:56:51 -0700995 if (empty && !entry_exit_stubs_installed_) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700996 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100997 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
998 instrumentation_stubs_installed_ = false;
999 }
1000 }
1001}
1002
Mathieu Chartiere401d142015-04-22 13:56:20 -07001003bool Instrumentation::IsDeoptimized(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001004 DCHECK(method != nullptr);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -07001005 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001006 return IsDeoptimizedMethod(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001007}
1008
1009void Instrumentation::EnableDeoptimization() {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -07001010 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -07001011 CHECK(IsDeoptimizedMethodsEmpty());
Sebastien Hertz11d40c22014-02-19 18:00:17 +01001012 CHECK_EQ(deoptimization_enabled_, false);
1013 deoptimization_enabled_ = true;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001014}
1015
Sebastien Hertz0462c4c2015-04-01 16:34:17 +02001016void Instrumentation::DisableDeoptimization(const char* key) {
Sebastien Hertz11d40c22014-02-19 18:00:17 +01001017 CHECK_EQ(deoptimization_enabled_, true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001018 // If we deoptimized everything, undo it.
Alex Lightdba61482016-12-21 08:20:29 -08001019 InstrumentationLevel level = GetCurrentInstrumentationLevel();
1020 if (level == InstrumentationLevel::kInstrumentWithInterpreter) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +02001021 UndeoptimizeEverything(key);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001022 }
1023 // Undeoptimized selected methods.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -07001024 while (true) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001025 ArtMethod* method;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -07001026 {
1027 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -07001028 if (IsDeoptimizedMethodsEmpty()) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -07001029 break;
1030 }
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -07001031 method = BeginDeoptimizedMethod();
1032 CHECK(method != nullptr);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -07001033 }
1034 Undeoptimize(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001035 }
Sebastien Hertz11d40c22014-02-19 18:00:17 +01001036 deoptimization_enabled_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001037}
1038
Sebastien Hertz11d40c22014-02-19 18:00:17 +01001039// Indicates if instrumentation should notify method enter/exit events to the listeners.
1040bool Instrumentation::ShouldNotifyMethodEnterExitEvents() const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +02001041 if (!HasMethodEntryListeners() && !HasMethodExitListeners()) {
1042 return false;
1043 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +01001044 return !deoptimization_enabled_ && !interpreter_stubs_installed_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001045}
1046
Sebastien Hertz0462c4c2015-04-01 16:34:17 +02001047void Instrumentation::DeoptimizeEverything(const char* key) {
1048 CHECK(deoptimization_enabled_);
1049 ConfigureStubs(key, InstrumentationLevel::kInstrumentWithInterpreter);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001050}
1051
Sebastien Hertz0462c4c2015-04-01 16:34:17 +02001052void Instrumentation::UndeoptimizeEverything(const char* key) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001053 CHECK(interpreter_stubs_installed_);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +02001054 CHECK(deoptimization_enabled_);
1055 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001056}
1057
Sebastien Hertz0462c4c2015-04-01 16:34:17 +02001058void Instrumentation::EnableMethodTracing(const char* key, bool needs_interpreter) {
1059 InstrumentationLevel level;
1060 if (needs_interpreter) {
1061 level = InstrumentationLevel::kInstrumentWithInterpreter;
1062 } else {
1063 level = InstrumentationLevel::kInstrumentWithInstrumentationStubs;
Alex Light2d441b12018-06-08 15:33:21 -07001064 if (Runtime::Current()->GetJit() != nullptr) {
1065 // TODO b/110263880 It would be better if we didn't need to do this.
1066 // Since we need to hold the method entrypoint across a suspend to ensure instrumentation
1067 // hooks are called correctly we have to disable jit-gc to ensure that the entrypoint doesn't
1068 // go away. Furthermore we need to leave this off permanently since one could get the same
1069 // effect by causing this to be toggled on and off.
1070 Runtime::Current()->GetJit()->GetCodeCache()->SetGarbageCollectCode(false);
1071 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +02001072 }
1073 ConfigureStubs(key, level);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001074}
1075
Sebastien Hertz0462c4c2015-04-01 16:34:17 +02001076void Instrumentation::DisableMethodTracing(const char* key) {
1077 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
jeffhao725a9572012-11-13 18:20:12 -08001078}
1079
Alex Light2d441b12018-06-08 15:33:21 -07001080const void* Instrumentation::GetCodeForInvoke(ArtMethod* method) const {
1081 // This is called by instrumentation entry only and that should never be getting proxy methods.
1082 DCHECK(!method->IsProxyMethod()) << method->PrettyMethod();
1083 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1084 if (LIKELY(!instrumentation_stubs_installed_ && !interpreter_stubs_installed_)) {
1085 // In general we just return whatever the method thinks its entrypoint is here. The only
1086 // exception is if it still has the instrumentation entrypoint. That means we are racing another
1087 // thread getting rid of instrumentation which is unexpected but possible. In that case we want
1088 // to wait and try to get it from the oat file or jit.
1089 const void* code = method->GetEntryPointFromQuickCompiledCodePtrSize(kRuntimePointerSize);
1090 DCHECK(code != nullptr);
1091 if (code != GetQuickInstrumentationEntryPoint()) {
1092 return code;
1093 } else if (method->IsNative()) {
1094 return class_linker->GetQuickOatCodeFor(method);
1095 }
1096 // We don't know what it is. Fallthough to try to find the code from the JIT or Oat file.
1097 } else if (method->IsNative()) {
1098 // TODO We could have JIT compiled native entrypoints. It might be worth it to find these.
1099 return class_linker->GetQuickOatCodeFor(method);
1100 } else if (UNLIKELY(interpreter_stubs_installed_)) {
1101 return GetQuickToInterpreterBridge();
1102 }
1103 // Since the method cannot be native due to ifs above we can always fall back to interpreter
1104 // bridge.
1105 const void* result = GetQuickToInterpreterBridge();
1106 if (!NeedDebugVersionFor(method)) {
1107 // If we don't need a debug version we should see what the oat file/class linker has to say.
1108 result = class_linker->GetQuickOatCodeFor(method);
1109 }
1110 // If both those fail try the jit.
1111 if (result == GetQuickToInterpreterBridge()) {
1112 jit::Jit* jit = Runtime::Current()->GetJit();
1113 if (jit != nullptr) {
1114 const void* res = jit->GetCodeCache()->FindCompiledCodeForInstrumentation(method);
1115 if (res != nullptr) {
1116 result = res;
1117 }
1118 }
1119 }
1120 return result;
1121}
1122
Andreas Gampe542451c2016-07-26 09:02:02 -07001123const void* Instrumentation::GetQuickCodeFor(ArtMethod* method, PointerSize pointer_size) const {
Vladimir Marko97d7e1c2016-10-04 14:44:28 +01001124 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers62d6c772013-02-27 08:32:07 -08001125 if (LIKELY(!instrumentation_stubs_installed_)) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001126 const void* code = method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Vladimir Marko8a630572014-04-09 18:45:35 +01001127 DCHECK(code != nullptr);
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001128 if (LIKELY(!class_linker->IsQuickResolutionStub(code) &&
1129 !class_linker->IsQuickToInterpreterBridge(code)) &&
1130 !class_linker->IsQuickResolutionStub(code) &&
1131 !class_linker->IsQuickToInterpreterBridge(code)) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001132 return code;
1133 }
1134 }
Alex Lightfc49fec2018-01-16 22:28:36 +00001135 return class_linker->GetQuickOatCodeFor(method);
jeffhao725a9572012-11-13 18:20:12 -08001136}
1137
Alex Lightd7661582017-05-01 13:48:16 -07001138void Instrumentation::MethodEnterEventImpl(Thread* thread,
1139 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001140 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -08001141 uint32_t dex_pc) const {
Mingyao Yang2ee17902017-08-30 11:37:08 -07001142 DCHECK(!method->IsRuntimeMethod());
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001143 if (HasMethodEntryListeners()) {
Alex Lightd7661582017-05-01 13:48:16 -07001144 Thread* self = Thread::Current();
1145 StackHandleScope<1> hs(self);
1146 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001147 for (InstrumentationListener* listener : method_entry_listeners_) {
1148 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001149 listener->MethodEntered(thread, thiz, method, dex_pc);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001150 }
1151 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001152 }
1153}
1154
Alex Lightd7661582017-05-01 13:48:16 -07001155void Instrumentation::MethodExitEventImpl(Thread* thread,
1156 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001157 ArtMethod* method,
Alex Lightd7661582017-05-01 13:48:16 -07001158 uint32_t dex_pc,
1159 const JValue& return_value) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001160 if (HasMethodExitListeners()) {
Alex Lightd7661582017-05-01 13:48:16 -07001161 Thread* self = Thread::Current();
1162 StackHandleScope<2> hs(self);
1163 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
1164 if (method->GetInterfaceMethodIfProxy(kRuntimePointerSize)
1165 ->GetReturnTypePrimitive() != Primitive::kPrimNot) {
1166 for (InstrumentationListener* listener : method_exit_listeners_) {
1167 if (listener != nullptr) {
1168 listener->MethodExited(thread, thiz, method, dex_pc, return_value);
1169 }
1170 }
1171 } else {
1172 Handle<mirror::Object> ret(hs.NewHandle(return_value.GetL()));
1173 for (InstrumentationListener* listener : method_exit_listeners_) {
1174 if (listener != nullptr) {
1175 listener->MethodExited(thread, thiz, method, dex_pc, ret);
1176 }
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001177 }
1178 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001179 }
1180}
1181
Alex Lightd7661582017-05-01 13:48:16 -07001182void Instrumentation::MethodUnwindEvent(Thread* thread,
1183 mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001184 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -08001185 uint32_t dex_pc) const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +02001186 if (HasMethodUnwindListeners()) {
Alex Lightd7661582017-05-01 13:48:16 -07001187 Thread* self = Thread::Current();
1188 StackHandleScope<1> hs(self);
1189 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Mathieu Chartier02e25112013-08-14 16:14:24 -07001190 for (InstrumentationListener* listener : method_unwind_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001191 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001192 listener->MethodUnwind(thread, thiz, method, dex_pc);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001193 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001194 }
1195 }
1196}
1197
Alex Lightd7661582017-05-01 13:48:16 -07001198void Instrumentation::DexPcMovedEventImpl(Thread* thread,
1199 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001200 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -08001201 uint32_t dex_pc) const {
Alex Lightd7661582017-05-01 13:48:16 -07001202 Thread* self = Thread::Current();
1203 StackHandleScope<1> hs(self);
1204 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001205 for (InstrumentationListener* listener : dex_pc_listeners_) {
1206 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001207 listener->DexPcMoved(thread, thiz, method, dex_pc);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001208 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001209 }
1210}
1211
Nicolas Geoffray81f0f952016-01-20 16:25:19 +00001212void Instrumentation::BranchImpl(Thread* thread,
1213 ArtMethod* method,
1214 uint32_t dex_pc,
1215 int32_t offset) const {
1216 for (InstrumentationListener* listener : branch_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001217 if (listener != nullptr) {
Nicolas Geoffray81f0f952016-01-20 16:25:19 +00001218 listener->Branch(thread, method, dex_pc, offset);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001219 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001220 }
1221}
1222
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001223void Instrumentation::InvokeVirtualOrInterfaceImpl(Thread* thread,
Alex Lightd7661582017-05-01 13:48:16 -07001224 ObjPtr<mirror::Object> this_object,
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001225 ArtMethod* caller,
1226 uint32_t dex_pc,
1227 ArtMethod* callee) const {
Alex Lightd7661582017-05-01 13:48:16 -07001228 Thread* self = Thread::Current();
1229 StackHandleScope<1> hs(self);
1230 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001231 for (InstrumentationListener* listener : invoke_virtual_or_interface_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001232 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001233 listener->InvokeVirtualOrInterface(thread, thiz, caller, dex_pc, callee);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001234 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001235 }
1236}
1237
Alex Lighte814f9d2017-07-31 16:14:39 -07001238void Instrumentation::WatchedFramePopImpl(Thread* thread, const ShadowFrame& frame) const {
1239 for (InstrumentationListener* listener : watched_frame_pop_listeners_) {
1240 if (listener != nullptr) {
1241 listener->WatchedFramePop(thread, frame);
1242 }
1243 }
1244}
1245
Alex Lightd7661582017-05-01 13:48:16 -07001246void Instrumentation::FieldReadEventImpl(Thread* thread,
1247 ObjPtr<mirror::Object> this_object,
1248 ArtMethod* method,
1249 uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -07001250 ArtField* field) const {
Alex Lightd7661582017-05-01 13:48:16 -07001251 Thread* self = Thread::Current();
1252 StackHandleScope<1> hs(self);
1253 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001254 for (InstrumentationListener* listener : field_read_listeners_) {
1255 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001256 listener->FieldRead(thread, thiz, method, dex_pc, field);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001257 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001258 }
1259}
1260
Alex Lightd7661582017-05-01 13:48:16 -07001261void Instrumentation::FieldWriteEventImpl(Thread* thread,
1262 ObjPtr<mirror::Object> this_object,
1263 ArtMethod* method,
1264 uint32_t dex_pc,
1265 ArtField* field,
1266 const JValue& field_value) const {
1267 Thread* self = Thread::Current();
1268 StackHandleScope<2> hs(self);
1269 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
1270 if (field->IsPrimitiveType()) {
1271 for (InstrumentationListener* listener : field_write_listeners_) {
1272 if (listener != nullptr) {
1273 listener->FieldWritten(thread, thiz, method, dex_pc, field, field_value);
1274 }
1275 }
1276 } else {
1277 Handle<mirror::Object> val(hs.NewHandle(field_value.GetL()));
1278 for (InstrumentationListener* listener : field_write_listeners_) {
1279 if (listener != nullptr) {
1280 listener->FieldWritten(thread, thiz, method, dex_pc, field, val);
1281 }
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001282 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001283 }
1284}
1285
Alex Light6e1607e2017-08-23 10:06:18 -07001286void Instrumentation::ExceptionThrownEvent(Thread* thread,
Sebastien Hertz947ff082013-09-17 14:10:13 +02001287 mirror::Throwable* exception_object) const {
Hiroshi Yamauchi3481f7a2017-02-10 12:07:36 -08001288 Thread* self = Thread::Current();
1289 StackHandleScope<1> hs(self);
1290 Handle<mirror::Throwable> h_exception(hs.NewHandle(exception_object));
Alex Light6e1607e2017-08-23 10:06:18 -07001291 if (HasExceptionThrownListeners()) {
Hiroshi Yamauchi3481f7a2017-02-10 12:07:36 -08001292 DCHECK_EQ(thread->GetException(), h_exception.Get());
Jeff Haoc0bd4da2013-04-11 15:52:28 -07001293 thread->ClearException();
Alex Light6e1607e2017-08-23 10:06:18 -07001294 for (InstrumentationListener* listener : exception_thrown_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001295 if (listener != nullptr) {
Alex Light6e1607e2017-08-23 10:06:18 -07001296 listener->ExceptionThrown(thread, h_exception);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001297 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001298 }
Alex Light9fb1ab12017-09-05 09:32:49 -07001299 // See b/65049545 for discussion about this behavior.
1300 thread->AssertNoPendingException();
Hiroshi Yamauchi3481f7a2017-02-10 12:07:36 -08001301 thread->SetException(h_exception.Get());
Ian Rogers62d6c772013-02-27 08:32:07 -08001302 }
1303}
1304
Alex Light9fb1ab12017-09-05 09:32:49 -07001305void Instrumentation::ExceptionHandledEvent(Thread* thread,
1306 mirror::Throwable* exception_object) const {
1307 Thread* self = Thread::Current();
1308 StackHandleScope<1> hs(self);
1309 Handle<mirror::Throwable> h_exception(hs.NewHandle(exception_object));
1310 if (HasExceptionHandledListeners()) {
1311 // We should have cleared the exception so that callers can detect a new one.
1312 DCHECK(thread->GetException() == nullptr);
1313 for (InstrumentationListener* listener : exception_handled_listeners_) {
1314 if (listener != nullptr) {
1315 listener->ExceptionHandled(thread, h_exception);
1316 }
1317 }
1318 }
1319}
1320
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +00001321// Computes a frame ID by ignoring inlined frames.
1322size_t Instrumentation::ComputeFrameId(Thread* self,
1323 size_t frame_depth,
1324 size_t inlined_frames_before_frame) {
1325 CHECK_GE(frame_depth, inlined_frames_before_frame);
1326 size_t no_inline_depth = frame_depth - inlined_frames_before_frame;
1327 return StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) - no_inline_depth;
1328}
1329
Ian Rogers62d6c772013-02-27 08:32:07 -08001330static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame,
1331 int delta)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001332 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +01001333 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) + delta;
Ian Rogers62d6c772013-02-27 08:32:07 -08001334 if (frame_id != instrumentation_frame.frame_id_) {
1335 LOG(ERROR) << "Expected frame_id=" << frame_id << " but found "
1336 << instrumentation_frame.frame_id_;
1337 StackVisitor::DescribeStack(self);
1338 CHECK_EQ(frame_id, instrumentation_frame.frame_id_);
1339 }
1340}
1341
1342void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001343 ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -07001344 uintptr_t lr, bool interpreter_entry) {
Alex Lightb7edcda2017-04-27 13:20:31 -07001345 DCHECK(!self->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -08001346 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1347 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001348 LOG(INFO) << "Entering " << ArtMethod::PrettyMethod(method) << " from PC "
1349 << reinterpret_cast<void*>(lr);
Ian Rogers62d6c772013-02-27 08:32:07 -08001350 }
Alex Lightb7edcda2017-04-27 13:20:31 -07001351
1352 // We send the enter event before pushing the instrumentation frame to make cleanup easier. If the
1353 // event causes an exception we can simply send the unwind event and return.
1354 StackHandleScope<1> hs(self);
1355 Handle<mirror::Object> h_this(hs.NewHandle(this_object));
1356 if (!interpreter_entry) {
1357 MethodEnterEvent(self, h_this.Get(), method, 0);
1358 if (self->IsExceptionPending()) {
1359 MethodUnwindEvent(self, h_this.Get(), method, 0);
1360 return;
1361 }
1362 }
1363
1364 // We have a callee-save frame meaning this value is guaranteed to never be 0.
1365 DCHECK(!self->IsExceptionPending());
1366 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk);
1367
1368 instrumentation::InstrumentationStackFrame instrumentation_frame(h_this.Get(), method, lr,
Jeff Hao9a916d32013-06-27 18:45:37 -07001369 frame_id, interpreter_entry);
Ian Rogers62d6c772013-02-27 08:32:07 -08001370 stack->push_front(instrumentation_frame);
Ian Rogers62d6c772013-02-27 08:32:07 -08001371}
1372
Mingyao Yang2ee17902017-08-30 11:37:08 -07001373DeoptimizationMethodType Instrumentation::GetDeoptimizationMethodType(ArtMethod* method) {
1374 if (method->IsRuntimeMethod()) {
1375 // Certain methods have strict requirement on whether the dex instruction
1376 // should be re-executed upon deoptimization.
1377 if (method == Runtime::Current()->GetCalleeSaveMethod(
1378 CalleeSaveType::kSaveEverythingForClinit)) {
1379 return DeoptimizationMethodType::kKeepDexPc;
1380 }
1381 if (method == Runtime::Current()->GetCalleeSaveMethod(
1382 CalleeSaveType::kSaveEverythingForSuspendCheck)) {
1383 return DeoptimizationMethodType::kKeepDexPc;
1384 }
1385 }
1386 return DeoptimizationMethodType::kDefault;
1387}
1388
1389// Try to get the shorty of a runtime method if it's an invocation stub.
1390struct RuntimeMethodShortyVisitor : public StackVisitor {
1391 explicit RuntimeMethodShortyVisitor(Thread* thread)
1392 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
1393 shorty('V') {}
1394
1395 bool VisitFrame() REQUIRES_SHARED(Locks::mutator_lock_) {
1396 ArtMethod* m = GetMethod();
1397 if (m != nullptr && !m->IsRuntimeMethod()) {
1398 // The first Java method.
1399 if (m->IsNative()) {
1400 // Use JNI method's shorty for the jni stub.
1401 shorty = m->GetShorty()[0];
1402 return false;
1403 }
1404 if (m->IsProxyMethod()) {
1405 // Proxy method just invokes its proxied method via
1406 // art_quick_proxy_invoke_handler.
1407 shorty = m->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty()[0];
1408 return false;
1409 }
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001410 const Instruction& instr = m->DexInstructions().InstructionAt(GetDexPc());
1411 if (instr.IsInvoke()) {
Mingyao Yang2ee17902017-08-30 11:37:08 -07001412 const DexFile* dex_file = m->GetDexFile();
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001413 if (interpreter::IsStringInit(dex_file, instr.VRegB())) {
Mingyao Yang2ee17902017-08-30 11:37:08 -07001414 // Invoking string init constructor is turned into invoking
1415 // StringFactory.newStringFromChars() which returns a string.
1416 shorty = 'L';
1417 return false;
1418 }
1419 // A regular invoke, use callee's shorty.
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001420 uint32_t method_idx = instr.VRegB();
Mingyao Yang2ee17902017-08-30 11:37:08 -07001421 shorty = dex_file->GetMethodShorty(method_idx)[0];
1422 }
1423 // Stop stack walking since we've seen a Java frame.
1424 return false;
1425 }
1426 return true;
1427 }
1428
1429 char shorty;
1430};
1431
Alex Lightb7edcda2017-04-27 13:20:31 -07001432TwoWordReturn Instrumentation::PopInstrumentationStackFrame(Thread* self,
1433 uintptr_t* return_pc,
1434 uint64_t* gpr_result,
1435 uint64_t* fpr_result) {
1436 DCHECK(gpr_result != nullptr);
1437 DCHECK(fpr_result != nullptr);
Ian Rogers62d6c772013-02-27 08:32:07 -08001438 // Do the pop.
1439 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1440 CHECK_GT(stack->size(), 0U);
1441 InstrumentationStackFrame instrumentation_frame = stack->front();
1442 stack->pop_front();
1443
1444 // Set return PC and check the sanity of the stack.
1445 *return_pc = instrumentation_frame.return_pc_;
1446 CheckStackDepth(self, instrumentation_frame, 0);
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001447 self->VerifyStack();
Ian Rogers62d6c772013-02-27 08:32:07 -08001448
Mathieu Chartiere401d142015-04-22 13:56:20 -07001449 ArtMethod* method = instrumentation_frame.method_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001450 uint32_t length;
Andreas Gampe542451c2016-07-26 09:02:02 -07001451 const PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mingyao Yang2ee17902017-08-30 11:37:08 -07001452 char return_shorty;
1453
1454 // Runtime method does not call into MethodExitEvent() so there should not be
1455 // suspension point below.
1456 ScopedAssertNoThreadSuspension ants(__FUNCTION__, method->IsRuntimeMethod());
1457 if (method->IsRuntimeMethod()) {
1458 if (method != Runtime::Current()->GetCalleeSaveMethod(
1459 CalleeSaveType::kSaveEverythingForClinit)) {
1460 // If the caller is at an invocation point and the runtime method is not
1461 // for clinit, we need to pass return results to the caller.
1462 // We need the correct shorty to decide whether we need to pass the return
1463 // result for deoptimization below.
1464 RuntimeMethodShortyVisitor visitor(self);
1465 visitor.WalkStack();
1466 return_shorty = visitor.shorty;
1467 } else {
1468 // Some runtime methods such as allocations, unresolved field getters, etc.
1469 // have return value. We don't need to set return_value since MethodExitEvent()
1470 // below isn't called for runtime methods. Deoptimization doesn't need the
1471 // value either since the dex instruction will be re-executed by the
1472 // interpreter, except these two cases:
1473 // (1) For an invoke, which is handled above to get the correct shorty.
1474 // (2) For MONITOR_ENTER/EXIT, which cannot be re-executed since it's not
1475 // idempotent. However there is no return value for it anyway.
1476 return_shorty = 'V';
1477 }
1478 } else {
1479 return_shorty = method->GetInterfaceMethodIfProxy(pointer_size)->GetShorty(&length)[0];
1480 }
1481
Alex Lightb7edcda2017-04-27 13:20:31 -07001482 bool is_ref = return_shorty == '[' || return_shorty == 'L';
1483 StackHandleScope<1> hs(self);
1484 MutableHandle<mirror::Object> res(hs.NewHandle<mirror::Object>(nullptr));
Ian Rogers62d6c772013-02-27 08:32:07 -08001485 JValue return_value;
1486 if (return_shorty == 'V') {
1487 return_value.SetJ(0);
1488 } else if (return_shorty == 'F' || return_shorty == 'D') {
Alex Lightb7edcda2017-04-27 13:20:31 -07001489 return_value.SetJ(*fpr_result);
Ian Rogers62d6c772013-02-27 08:32:07 -08001490 } else {
Alex Lightb7edcda2017-04-27 13:20:31 -07001491 return_value.SetJ(*gpr_result);
1492 }
1493 if (is_ref) {
1494 // Take a handle to the return value so we won't lose it if we suspend.
1495 res.Assign(return_value.GetL());
Ian Rogers62d6c772013-02-27 08:32:07 -08001496 }
1497 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1498 // return_pc.
Andreas Gampee2abbc62017-09-15 11:59:26 -07001499 uint32_t dex_pc = dex::kDexNoIndex;
Ian Rogers62d6c772013-02-27 08:32:07 -08001500 mirror::Object* this_object = instrumentation_frame.this_object_;
Mingyao Yang2ee17902017-08-30 11:37:08 -07001501 if (!method->IsRuntimeMethod() && !instrumentation_frame.interpreter_entry_) {
Sebastien Hertz320deb22014-06-11 19:45:05 +02001502 MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value);
1503 }
jeffhao725a9572012-11-13 18:20:12 -08001504
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001505 // Deoptimize if the caller needs to continue execution in the interpreter. Do nothing if we get
1506 // back to an upcall.
1507 NthCallerVisitor visitor(self, 1, true);
1508 visitor.WalkStack(true);
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001509 bool deoptimize = (visitor.caller != nullptr) &&
Daniel Mihalyieb076692014-08-22 17:33:31 +02001510 (interpreter_stubs_installed_ || IsDeoptimized(visitor.caller) ||
1511 Dbg::IsForcedInterpreterNeededForUpcall(self, visitor.caller));
Alex Lightb7edcda2017-04-27 13:20:31 -07001512 if (is_ref) {
1513 // Restore the return value if it's a reference since it might have moved.
1514 *reinterpret_cast<mirror::Object**>(gpr_result) = res.Get();
1515 }
Nicolas Geoffraya0619e22016-12-20 13:57:43 +00001516 if (deoptimize && Runtime::Current()->IsAsyncDeoptimizeable(*return_pc)) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001517 if (kVerboseInstrumentation) {
Andreas Gampe46ee31b2016-12-14 10:11:49 -08001518 LOG(INFO) << "Deoptimizing "
1519 << visitor.caller->PrettyMethod()
1520 << " by returning from "
1521 << method->PrettyMethod()
1522 << " with result "
1523 << std::hex << return_value.GetJ() << std::dec
1524 << " in "
1525 << *self;
Ian Rogers62d6c772013-02-27 08:32:07 -08001526 }
Mingyao Yang2ee17902017-08-30 11:37:08 -07001527 DeoptimizationMethodType deopt_method_type = GetDeoptimizationMethodType(method);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001528 self->PushDeoptimizationContext(return_value,
Mingyao Yang2ee17902017-08-30 11:37:08 -07001529 return_shorty == 'L' || return_shorty == '[',
1530 nullptr /* no pending exception */,
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001531 false /* from_code */,
Mingyao Yang2ee17902017-08-30 11:37:08 -07001532 deopt_method_type);
Andreas Gamped58342c2014-06-05 14:18:08 -07001533 return GetTwoWordSuccessValue(*return_pc,
1534 reinterpret_cast<uintptr_t>(GetQuickDeoptimizationEntryPoint()));
Ian Rogers62d6c772013-02-27 08:32:07 -08001535 } else {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +00001536 if (deoptimize && !Runtime::Current()->IsAsyncDeoptimizeable(*return_pc)) {
Alex Lightd8eb6732018-01-29 15:16:02 -08001537 VLOG(deopt) << "Got a deoptimization request on un-deoptimizable " << method->PrettyMethod()
1538 << " at PC " << reinterpret_cast<void*>(*return_pc);
Nicolas Geoffraya0619e22016-12-20 13:57:43 +00001539 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001540 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001541 LOG(INFO) << "Returning from " << method->PrettyMethod()
Brian Carlstrom2d888622013-07-18 17:02:00 -07001542 << " to PC " << reinterpret_cast<void*>(*return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001543 }
Andreas Gamped58342c2014-06-05 14:18:08 -07001544 return GetTwoWordSuccessValue(0, *return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001545 }
jeffhao725a9572012-11-13 18:20:12 -08001546}
1547
Alex Light2c8206f2018-06-08 14:51:09 -07001548uintptr_t Instrumentation::PopFramesForDeoptimization(Thread* self, size_t nframes) const {
Ian Rogers62d6c772013-02-27 08:32:07 -08001549 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
Alex Light2c8206f2018-06-08 14:51:09 -07001550 CHECK_GE(stack->size(), nframes);
1551 if (nframes == 0) {
1552 return 0u;
1553 }
1554 // Only need to send instrumentation events if it's not for deopt (do give the log messages if we
1555 // have verbose-instrumentation anyway though).
1556 if (kVerboseInstrumentation) {
1557 for (size_t i = 0; i < nframes; i++) {
1558 LOG(INFO) << "Popping for deoptimization " << stack->at(i).method_->PrettyMethod();
Mingyao Yang2ee17902017-08-30 11:37:08 -07001559 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001560 }
Alex Light2c8206f2018-06-08 14:51:09 -07001561 // Now that we've sent all the instrumentation events we can actually modify the
1562 // instrumentation-stack. We cannot do this earlier since MethodUnwindEvent can re-enter java and
1563 // do other things that require the instrumentation stack to be in a consistent state with the
1564 // actual stack.
1565 for (size_t i = 0; i < nframes - 1; i++) {
1566 stack->pop_front();
1567 }
1568 uintptr_t return_pc = stack->front().return_pc_;
Alex Lightb7edcda2017-04-27 13:20:31 -07001569 stack->pop_front();
Alex Light2c8206f2018-06-08 14:51:09 -07001570 return return_pc;
Ian Rogers62d6c772013-02-27 08:32:07 -08001571}
1572
1573std::string InstrumentationStackFrame::Dump() const {
1574 std::ostringstream os;
David Sehr709b0702016-10-13 09:12:37 -07001575 os << "Frame " << frame_id_ << " " << ArtMethod::PrettyMethod(method_) << ":"
Ian Rogers62d6c772013-02-27 08:32:07 -08001576 << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_);
1577 return os.str();
1578}
1579
1580} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -08001581} // namespace art