blob: 03fd964ffebfd82d433146b509dcf7573fee3d25 [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
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010088 bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier28357fa2016-10-18 16:27:40 -070089 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),
Alex Light3e36a9c2018-06-19 09:45:05 -0700165 deoptimized_methods_lock_("deoptimized methods lock", kGenericBottomLock),
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 Light3e36a9c2018-06-19 09:45:05 -0700228 new_quick_code = GetCodeForInvoke(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100229 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700230 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100231 }
232 } else { // !uninstall
Sebastien Hertzbae182c2013-12-17 10:42:03 +0100233 if ((interpreter_stubs_installed_ || forced_interpret_only_ || IsDeoptimized(method)) &&
234 !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800235 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100236 } else {
237 // Do not overwrite resolution trampoline. When the trampoline initializes the method's
238 // class, all its static methods code will be set to the instrumentation entry point.
239 // For more details, see ClassLinker::FixupStaticTrampolines.
240 if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Alex Light2d441b12018-06-08 15:33:21 -0700241 if (entry_exit_stubs_installed_) {
242 // This needs to be checked first since the instrumentation entrypoint will be able to
243 // find the actual JIT compiled code that corresponds to this method.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800244 new_quick_code = GetQuickInstrumentationEntryPoint();
Alex Light2d441b12018-06-08 15:33:21 -0700245 } else if (NeedDebugVersionFor(method)) {
246 // It would be great to search the JIT for its implementation here but we cannot due to
247 // the locks we hold. Instead just set to the interpreter bridge and that code will search
248 // the JIT when it gets called and replace the entrypoint then.
249 new_quick_code = GetQuickToInterpreterBridge();
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000250 } else {
Alex Lightfc49fec2018-01-16 22:28:36 +0000251 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100252 }
253 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700254 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100255 }
256 }
257 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800258 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100259}
260
Ian Rogers62d6c772013-02-27 08:32:07 -0800261// Places the instrumentation exit pc as the return PC for every quick frame. This also allows
262// deoptimization of quick frames to interpreter frames.
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100263// Since we may already have done this previously, we need to push new instrumentation frame before
264// existing instrumentation frames.
Ian Rogers62d6c772013-02-27 08:32:07 -0800265static void InstrumentationInstallStack(Thread* thread, void* arg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700266 REQUIRES_SHARED(Locks::mutator_lock_) {
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100267 struct InstallStackVisitor final : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800268 InstallStackVisitor(Thread* thread_in, Context* context, uintptr_t instrumentation_exit_pc)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100269 : StackVisitor(thread_in, context, kInstrumentationStackWalk),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800270 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100271 instrumentation_exit_pc_(instrumentation_exit_pc),
Alex Lighte9278662018-03-08 16:55:58 -0800272 reached_existing_instrumentation_frames_(false), instrumentation_stack_depth_(0),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100273 last_return_pc_(0) {
274 }
jeffhao725a9572012-11-13 18:20:12 -0800275
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100276 bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700277 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700278 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800279 if (kVerboseInstrumentation) {
280 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
281 }
282 last_return_pc_ = 0;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700283 return true; // Ignore upcalls.
Ian Rogers306057f2012-11-26 12:45:53 -0800284 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700285 if (GetCurrentQuickFrame() == nullptr) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800286 bool interpreter_frame = true;
Sebastien Hertz320deb22014-06-11 19:45:05 +0200287 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, 0, GetFrameId(),
288 interpreter_frame);
Jeff Haoa15a81b2014-05-27 18:25:47 -0700289 if (kVerboseInstrumentation) {
290 LOG(INFO) << "Pushing shadow frame " << instrumentation_frame.Dump();
291 }
292 shadow_stack_.push_back(instrumentation_frame);
293 return true; // Continue.
294 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800295 uintptr_t return_pc = GetReturnPc();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200296 if (kVerboseInstrumentation) {
297 LOG(INFO) << " Installing exit stub in " << DescribeLocation();
298 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100299 if (return_pc == instrumentation_exit_pc_) {
Mingyao Yang2ee17902017-08-30 11:37:08 -0700300 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
301
302 if (m->IsRuntimeMethod()) {
303 const InstrumentationStackFrame& frame =
Vladimir Marko35d5b8a2018-07-03 09:18:32 +0100304 (*instrumentation_stack_)[instrumentation_stack_depth_];
Mingyao Yang2ee17902017-08-30 11:37:08 -0700305 if (frame.interpreter_entry_) {
306 // This instrumentation frame is for an interpreter bridge and is
307 // pushed when executing the instrumented interpreter bridge. So method
308 // enter event must have been reported. However we need to push a DEX pc
309 // into the dex_pcs_ list to match size of instrumentation stack.
Andreas Gampee2abbc62017-09-15 11:59:26 -0700310 uint32_t dex_pc = dex::kDexNoIndex;
Mingyao Yang2ee17902017-08-30 11:37:08 -0700311 dex_pcs_.push_back(dex_pc);
312 last_return_pc_ = frame.return_pc_;
313 ++instrumentation_stack_depth_;
314 return true;
315 }
316 }
317
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100318 // We've reached a frame which has already been installed with instrumentation exit stub.
Alex Light74c91c92018-03-08 14:01:44 -0800319 // We should have already installed instrumentation or be interpreter on previous frames.
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100320 reached_existing_instrumentation_frames_ = true;
321
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200322 const InstrumentationStackFrame& frame =
Vladimir Marko35d5b8a2018-07-03 09:18:32 +0100323 (*instrumentation_stack_)[instrumentation_stack_depth_];
David Sehr709b0702016-10-13 09:12:37 -0700324 CHECK_EQ(m, frame.method_) << "Expected " << ArtMethod::PrettyMethod(m)
325 << ", Found " << ArtMethod::PrettyMethod(frame.method_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100326 return_pc = frame.return_pc_;
327 if (kVerboseInstrumentation) {
328 LOG(INFO) << "Ignoring already instrumented " << frame.Dump();
329 }
330 } else {
331 CHECK_NE(return_pc, 0U);
Alex Light74c91c92018-03-08 14:01:44 -0800332 if (UNLIKELY(reached_existing_instrumentation_frames_ && !m->IsRuntimeMethod())) {
333 // We already saw an existing instrumentation frame so this should be a runtime-method
334 // inserted by the interpreter or runtime.
Alex Lighte9278662018-03-08 16:55:58 -0800335 std::string thread_name;
336 GetThread()->GetThreadName(thread_name);
337 uint32_t dex_pc = dex::kDexNoIndex;
338 if (last_return_pc_ != 0 &&
339 GetCurrentOatQuickMethodHeader() != nullptr) {
340 dex_pc = GetCurrentOatQuickMethodHeader()->ToDexPc(m, last_return_pc_);
341 }
Alex Light74c91c92018-03-08 14:01:44 -0800342 LOG(FATAL) << "While walking " << thread_name << " found unexpected non-runtime method"
343 << " without instrumentation exit return or interpreter frame."
Alex Lighte9278662018-03-08 16:55:58 -0800344 << " method is " << GetMethod()->PrettyMethod()
345 << " return_pc is " << std::hex << return_pc
346 << " dex pc: " << dex_pc;
347 UNREACHABLE();
348 }
Mingyao Yang2ee17902017-08-30 11:37:08 -0700349 InstrumentationStackFrame instrumentation_frame(
350 m->IsRuntimeMethod() ? nullptr : GetThisObject(),
351 m,
352 return_pc,
353 GetFrameId(), // A runtime method still gets a frame id.
354 false);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100355 if (kVerboseInstrumentation) {
356 LOG(INFO) << "Pushing frame " << instrumentation_frame.Dump();
357 }
358
Sebastien Hertz320deb22014-06-11 19:45:05 +0200359 // Insert frame at the right position so we do not corrupt the instrumentation stack.
360 // Instrumentation stack frames are in descending frame id order.
361 auto it = instrumentation_stack_->begin();
362 for (auto end = instrumentation_stack_->end(); it != end; ++it) {
363 const InstrumentationStackFrame& current = *it;
364 if (instrumentation_frame.frame_id_ >= current.frame_id_) {
365 break;
366 }
367 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100368 instrumentation_stack_->insert(it, instrumentation_frame);
369 SetReturnPc(instrumentation_exit_pc_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800370 }
Andreas Gampee2abbc62017-09-15 11:59:26 -0700371 uint32_t dex_pc = dex::kDexNoIndex;
Mingyao Yang2ee17902017-08-30 11:37:08 -0700372 if (last_return_pc_ != 0 &&
373 GetCurrentOatQuickMethodHeader() != nullptr) {
374 dex_pc = GetCurrentOatQuickMethodHeader()->ToDexPc(m, last_return_pc_);
375 }
376 dex_pcs_.push_back(dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800377 last_return_pc_ = return_pc;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100378 ++instrumentation_stack_depth_;
Ian Rogers306057f2012-11-26 12:45:53 -0800379 return true; // Continue.
380 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800381 std::deque<InstrumentationStackFrame>* const instrumentation_stack_;
Jeff Haoa15a81b2014-05-27 18:25:47 -0700382 std::vector<InstrumentationStackFrame> shadow_stack_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800383 std::vector<uint32_t> dex_pcs_;
Ian Rogers306057f2012-11-26 12:45:53 -0800384 const uintptr_t instrumentation_exit_pc_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100385 bool reached_existing_instrumentation_frames_;
386 size_t instrumentation_stack_depth_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800387 uintptr_t last_return_pc_;
Ian Rogers306057f2012-11-26 12:45:53 -0800388 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800389 if (kVerboseInstrumentation) {
390 std::string thread_name;
391 thread->GetThreadName(thread_name);
392 LOG(INFO) << "Installing exit stubs in " << thread_name;
Ian Rogers306057f2012-11-26 12:45:53 -0800393 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100394
395 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers700a4022014-05-19 16:49:03 -0700396 std::unique_ptr<Context> context(Context::Create());
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700397 uintptr_t instrumentation_exit_pc = reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100398 InstallStackVisitor visitor(thread, context.get(), instrumentation_exit_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800399 visitor.WalkStack(true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100400 CHECK_EQ(visitor.dex_pcs_.size(), thread->GetInstrumentationStack()->size());
Ian Rogers62d6c772013-02-27 08:32:07 -0800401
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100402 if (instrumentation->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100403 // Create method enter events for all methods currently on the thread's stack. We only do this
404 // if no debugger is attached to prevent from posting events twice.
Jeff Haoa15a81b2014-05-27 18:25:47 -0700405 auto ssi = visitor.shadow_stack_.rbegin();
406 for (auto isi = thread->GetInstrumentationStack()->rbegin(),
407 end = thread->GetInstrumentationStack()->rend(); isi != end; ++isi) {
408 while (ssi != visitor.shadow_stack_.rend() && (*ssi).frame_id_ < (*isi).frame_id_) {
409 instrumentation->MethodEnterEvent(thread, (*ssi).this_object_, (*ssi).method_, 0);
410 ++ssi;
411 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100412 uint32_t dex_pc = visitor.dex_pcs_.back();
413 visitor.dex_pcs_.pop_back();
Alex Lightdc5423f2018-06-08 10:43:38 -0700414 if (!isi->interpreter_entry_ && !isi->method_->IsRuntimeMethod()) {
Sebastien Hertz320deb22014-06-11 19:45:05 +0200415 instrumentation->MethodEnterEvent(thread, (*isi).this_object_, (*isi).method_, dex_pc);
416 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100417 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800418 }
419 thread->VerifyStack();
Ian Rogers306057f2012-11-26 12:45:53 -0800420}
421
Mingyao Yang99170c62015-07-06 11:10:37 -0700422void Instrumentation::InstrumentThreadStack(Thread* thread) {
423 instrumentation_stubs_installed_ = true;
424 InstrumentationInstallStack(thread, this);
425}
426
Ian Rogers62d6c772013-02-27 08:32:07 -0800427// Removes the instrumentation exit pc as the return PC for every quick frame.
428static void InstrumentationRestoreStack(Thread* thread, void* arg)
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000429 REQUIRES(Locks::mutator_lock_) {
430 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
431
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100432 struct RestoreStackVisitor final : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800433 RestoreStackVisitor(Thread* thread_in, uintptr_t instrumentation_exit_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800434 Instrumentation* instrumentation)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100435 : StackVisitor(thread_in, nullptr, kInstrumentationStackWalk),
436 thread_(thread_in),
Ian Rogers62d6c772013-02-27 08:32:07 -0800437 instrumentation_exit_pc_(instrumentation_exit_pc),
438 instrumentation_(instrumentation),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800439 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Ian Rogers62d6c772013-02-27 08:32:07 -0800440 frames_removed_(0) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800441
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100442 bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800443 if (instrumentation_stack_->size() == 0) {
jeffhao725a9572012-11-13 18:20:12 -0800444 return false; // Stop.
445 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700446 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700447 if (GetCurrentQuickFrame() == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800448 if (kVerboseInstrumentation) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200449 LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId()
David Sehr709b0702016-10-13 09:12:37 -0700450 << " Method=" << ArtMethod::PrettyMethod(m);
Ian Rogers62d6c772013-02-27 08:32:07 -0800451 }
452 return true; // Ignore shadow frames.
453 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700454 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800455 if (kVerboseInstrumentation) {
456 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
457 }
Ian Rogers306057f2012-11-26 12:45:53 -0800458 return true; // Ignore upcalls.
459 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800460 bool removed_stub = false;
461 // TODO: make this search more efficient?
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100462 const size_t frameId = GetFrameId();
463 for (const InstrumentationStackFrame& instrumentation_frame : *instrumentation_stack_) {
464 if (instrumentation_frame.frame_id_ == frameId) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800465 if (kVerboseInstrumentation) {
466 LOG(INFO) << " Removing exit stub in " << DescribeLocation();
467 }
Jeff Hao9a916d32013-06-27 18:45:37 -0700468 if (instrumentation_frame.interpreter_entry_) {
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700469 CHECK(m == Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Jeff Hao9a916d32013-06-27 18:45:37 -0700470 } else {
David Sehr709b0702016-10-13 09:12:37 -0700471 CHECK(m == instrumentation_frame.method_) << ArtMethod::PrettyMethod(m);
Jeff Hao9a916d32013-06-27 18:45:37 -0700472 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800473 SetReturnPc(instrumentation_frame.return_pc_);
Mingyao Yang2ee17902017-08-30 11:37:08 -0700474 if (instrumentation_->ShouldNotifyMethodEnterExitEvents() &&
475 !m->IsRuntimeMethod()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100476 // Create the method exit events. As the methods didn't really exit the result is 0.
477 // We only do this if no debugger is attached to prevent from posting events twice.
478 instrumentation_->MethodExitEvent(thread_, instrumentation_frame.this_object_, m,
479 GetDexPc(), JValue());
480 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800481 frames_removed_++;
482 removed_stub = true;
483 break;
484 }
485 }
486 if (!removed_stub) {
487 if (kVerboseInstrumentation) {
488 LOG(INFO) << " No exit stub in " << DescribeLocation();
Ian Rogers306057f2012-11-26 12:45:53 -0800489 }
jeffhao725a9572012-11-13 18:20:12 -0800490 }
491 return true; // Continue.
492 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800493 Thread* const thread_;
Ian Rogers306057f2012-11-26 12:45:53 -0800494 const uintptr_t instrumentation_exit_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800495 Instrumentation* const instrumentation_;
496 std::deque<instrumentation::InstrumentationStackFrame>* const instrumentation_stack_;
497 size_t frames_removed_;
jeffhao725a9572012-11-13 18:20:12 -0800498 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800499 if (kVerboseInstrumentation) {
500 std::string thread_name;
501 thread->GetThreadName(thread_name);
502 LOG(INFO) << "Removing exit stubs in " << thread_name;
503 }
504 std::deque<instrumentation::InstrumentationStackFrame>* stack = thread->GetInstrumentationStack();
505 if (stack->size() > 0) {
506 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700507 uintptr_t instrumentation_exit_pc =
508 reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Ian Rogers62d6c772013-02-27 08:32:07 -0800509 RestoreStackVisitor visitor(thread, instrumentation_exit_pc, instrumentation);
510 visitor.WalkStack(true);
511 CHECK_EQ(visitor.frames_removed_, stack->size());
512 while (stack->size() > 0) {
513 stack->pop_front();
514 }
jeffhao725a9572012-11-13 18:20:12 -0800515 }
516}
517
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200518static bool HasEvent(Instrumentation::InstrumentationEvent expected, uint32_t events) {
519 return (events & expected) != 0;
520}
521
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000522static void PotentiallyAddListenerTo(Instrumentation::InstrumentationEvent event,
523 uint32_t events,
524 std::list<InstrumentationListener*>& list,
525 InstrumentationListener* listener,
526 bool* has_listener)
527 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) {
528 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
529 if (!HasEvent(event, events)) {
530 return;
531 }
532 // If there is a free slot in the list, we insert the listener in that slot.
533 // Otherwise we add it to the end of the list.
534 auto it = std::find(list.begin(), list.end(), nullptr);
535 if (it != list.end()) {
536 *it = listener;
537 } else {
538 list.push_back(listener);
539 }
540 *has_listener = true;
541}
542
Ian Rogers62d6c772013-02-27 08:32:07 -0800543void Instrumentation::AddListener(InstrumentationListener* listener, uint32_t events) {
544 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000545 PotentiallyAddListenerTo(kMethodEntered,
546 events,
547 method_entry_listeners_,
548 listener,
549 &have_method_entry_listeners_);
550 PotentiallyAddListenerTo(kMethodExited,
551 events,
552 method_exit_listeners_,
553 listener,
554 &have_method_exit_listeners_);
555 PotentiallyAddListenerTo(kMethodUnwind,
556 events,
557 method_unwind_listeners_,
558 listener,
559 &have_method_unwind_listeners_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000560 PotentiallyAddListenerTo(kBranch,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000561 events,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000562 branch_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000563 listener,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000564 &have_branch_listeners_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000565 PotentiallyAddListenerTo(kInvokeVirtualOrInterface,
566 events,
567 invoke_virtual_or_interface_listeners_,
568 listener,
569 &have_invoke_virtual_or_interface_listeners_);
570 PotentiallyAddListenerTo(kDexPcMoved,
571 events,
572 dex_pc_listeners_,
573 listener,
574 &have_dex_pc_listeners_);
575 PotentiallyAddListenerTo(kFieldRead,
576 events,
577 field_read_listeners_,
578 listener,
579 &have_field_read_listeners_);
580 PotentiallyAddListenerTo(kFieldWritten,
581 events,
582 field_write_listeners_,
583 listener,
584 &have_field_write_listeners_);
Alex Light6e1607e2017-08-23 10:06:18 -0700585 PotentiallyAddListenerTo(kExceptionThrown,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000586 events,
Alex Light6e1607e2017-08-23 10:06:18 -0700587 exception_thrown_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000588 listener,
Alex Light6e1607e2017-08-23 10:06:18 -0700589 &have_exception_thrown_listeners_);
Alex Lighte814f9d2017-07-31 16:14:39 -0700590 PotentiallyAddListenerTo(kWatchedFramePop,
591 events,
592 watched_frame_pop_listeners_,
593 listener,
594 &have_watched_frame_pop_listeners_);
Alex Light9fb1ab12017-09-05 09:32:49 -0700595 PotentiallyAddListenerTo(kExceptionHandled,
596 events,
597 exception_handled_listeners_,
598 listener,
599 &have_exception_handled_listeners_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200600 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800601}
602
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000603static void PotentiallyRemoveListenerFrom(Instrumentation::InstrumentationEvent event,
604 uint32_t events,
605 std::list<InstrumentationListener*>& list,
606 InstrumentationListener* listener,
607 bool* has_listener)
608 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) {
609 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
610 if (!HasEvent(event, events)) {
611 return;
612 }
613 auto it = std::find(list.begin(), list.end(), listener);
614 if (it != list.end()) {
615 // Just update the entry, do not remove from the list. Removing entries in the list
616 // is unsafe when mutators are iterating over it.
617 *it = nullptr;
618 }
619
620 // Check if the list contains any non-null listener, and update 'has_listener'.
621 for (InstrumentationListener* l : list) {
622 if (l != nullptr) {
623 *has_listener = true;
624 return;
625 }
626 }
627 *has_listener = false;
628}
629
Ian Rogers62d6c772013-02-27 08:32:07 -0800630void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) {
631 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000632 PotentiallyRemoveListenerFrom(kMethodEntered,
633 events,
634 method_entry_listeners_,
635 listener,
636 &have_method_entry_listeners_);
637 PotentiallyRemoveListenerFrom(kMethodExited,
638 events,
639 method_exit_listeners_,
640 listener,
641 &have_method_exit_listeners_);
642 PotentiallyRemoveListenerFrom(kMethodUnwind,
643 events,
644 method_unwind_listeners_,
645 listener,
646 &have_method_unwind_listeners_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000647 PotentiallyRemoveListenerFrom(kBranch,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000648 events,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000649 branch_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000650 listener,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000651 &have_branch_listeners_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000652 PotentiallyRemoveListenerFrom(kInvokeVirtualOrInterface,
653 events,
654 invoke_virtual_or_interface_listeners_,
655 listener,
656 &have_invoke_virtual_or_interface_listeners_);
657 PotentiallyRemoveListenerFrom(kDexPcMoved,
658 events,
659 dex_pc_listeners_,
660 listener,
661 &have_dex_pc_listeners_);
662 PotentiallyRemoveListenerFrom(kFieldRead,
663 events,
664 field_read_listeners_,
665 listener,
666 &have_field_read_listeners_);
667 PotentiallyRemoveListenerFrom(kFieldWritten,
668 events,
669 field_write_listeners_,
670 listener,
671 &have_field_write_listeners_);
Alex Light6e1607e2017-08-23 10:06:18 -0700672 PotentiallyRemoveListenerFrom(kExceptionThrown,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000673 events,
Alex Light6e1607e2017-08-23 10:06:18 -0700674 exception_thrown_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000675 listener,
Alex Light6e1607e2017-08-23 10:06:18 -0700676 &have_exception_thrown_listeners_);
Alex Lighte814f9d2017-07-31 16:14:39 -0700677 PotentiallyRemoveListenerFrom(kWatchedFramePop,
678 events,
679 watched_frame_pop_listeners_,
680 listener,
681 &have_watched_frame_pop_listeners_);
Alex Light9fb1ab12017-09-05 09:32:49 -0700682 PotentiallyRemoveListenerFrom(kExceptionHandled,
683 events,
684 exception_handled_listeners_,
685 listener,
686 &have_exception_handled_listeners_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200687 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800688}
689
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200690Instrumentation::InstrumentationLevel Instrumentation::GetCurrentInstrumentationLevel() const {
Alex Light4ba388a2017-01-27 10:26:49 -0800691 if (interpreter_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200692 return InstrumentationLevel::kInstrumentWithInterpreter;
Ian Rogers62d6c772013-02-27 08:32:07 -0800693 } else if (entry_exit_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200694 return InstrumentationLevel::kInstrumentWithInstrumentationStubs;
Ian Rogers62d6c772013-02-27 08:32:07 -0800695 } else {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200696 return InstrumentationLevel::kInstrumentNothing;
Ian Rogers62d6c772013-02-27 08:32:07 -0800697 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200698}
699
Alex Lightdba61482016-12-21 08:20:29 -0800700bool Instrumentation::RequiresInstrumentationInstallation(InstrumentationLevel new_level) const {
Alex Light4ba388a2017-01-27 10:26:49 -0800701 // We need to reinstall instrumentation if we go to a different level.
702 return GetCurrentInstrumentationLevel() != new_level;
Alex Lightdba61482016-12-21 08:20:29 -0800703}
704
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200705void Instrumentation::ConfigureStubs(const char* key, InstrumentationLevel desired_level) {
706 // Store the instrumentation level for this key or remove it.
707 if (desired_level == InstrumentationLevel::kInstrumentNothing) {
708 // The client no longer needs instrumentation.
709 requested_instrumentation_levels_.erase(key);
710 } else {
711 // The client needs instrumentation.
712 requested_instrumentation_levels_.Overwrite(key, desired_level);
713 }
714
715 // Look for the highest required instrumentation level.
716 InstrumentationLevel requested_level = InstrumentationLevel::kInstrumentNothing;
717 for (const auto& v : requested_instrumentation_levels_) {
718 requested_level = std::max(requested_level, v.second);
719 }
720
721 interpret_only_ = (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) ||
722 forced_interpret_only_;
723
Alex Lightdba61482016-12-21 08:20:29 -0800724 if (!RequiresInstrumentationInstallation(requested_level)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800725 // We're already set.
726 return;
727 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100728 Thread* const self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -0800729 Runtime* runtime = Runtime::Current();
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100730 Locks::mutator_lock_->AssertExclusiveHeld(self);
Ian Rogers62d6c772013-02-27 08:32:07 -0800731 Locks::thread_list_lock_->AssertNotHeld(self);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200732 if (requested_level > InstrumentationLevel::kInstrumentNothing) {
Alex Light4ba388a2017-01-27 10:26:49 -0800733 if (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800734 interpreter_stubs_installed_ = true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800735 entry_exit_stubs_installed_ = true;
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200736 } else {
737 CHECK_EQ(requested_level, InstrumentationLevel::kInstrumentWithInstrumentationStubs);
738 entry_exit_stubs_installed_ = true;
739 interpreter_stubs_installed_ = false;
Ian Rogers62d6c772013-02-27 08:32:07 -0800740 }
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700741 InstallStubsClassVisitor visitor(this);
742 runtime->GetClassLinker()->VisitClasses(&visitor);
Ian Rogers62d6c772013-02-27 08:32:07 -0800743 instrumentation_stubs_installed_ = true;
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100744 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800745 runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this);
746 } else {
747 interpreter_stubs_installed_ = false;
748 entry_exit_stubs_installed_ = false;
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700749 InstallStubsClassVisitor visitor(this);
750 runtime->GetClassLinker()->VisitClasses(&visitor);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100751 // Restore stack only if there is no method currently deoptimized.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700752 bool empty;
753 {
754 ReaderMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700755 empty = IsDeoptimizedMethodsEmpty(); // Avoid lock violation.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700756 }
757 if (empty) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100758 MutexLock mu(self, *Locks::thread_list_lock_);
759 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000760 // Only do this after restoring, as walking the stack when restoring will see
761 // the instrumentation exit pc.
762 instrumentation_stubs_installed_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100763 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800764 }
jeffhao725a9572012-11-13 18:20:12 -0800765}
766
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200767static void ResetQuickAllocEntryPointsForThread(Thread* thread, void* arg ATTRIBUTE_UNUSED) {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800768 thread->ResetQuickAllocEntryPointsForThread(kUseReadBarrier && thread->GetIsGcMarking());
Ian Rogersfa824272013-11-05 16:12:57 -0800769}
770
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700771void Instrumentation::SetEntrypointsInstrumented(bool instrumented) {
772 Thread* self = Thread::Current();
Mathieu Chartier661974a2014-01-09 11:23:53 -0800773 Runtime* runtime = Runtime::Current();
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700774 Locks::mutator_lock_->AssertNotHeld(self);
775 Locks::instrument_entrypoints_lock_->AssertHeld(self);
776 if (runtime->IsStarted()) {
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700777 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700778 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
Mathieu Chartier661974a2014-01-09 11:23:53 -0800779 SetQuickAllocEntryPointsInstrumented(instrumented);
780 ResetQuickAllocEntryPoints();
Mathieu Chartier50e93312016-03-16 11:25:29 -0700781 alloc_entrypoints_instrumented_ = instrumented;
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700782 } else {
783 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
784 SetQuickAllocEntryPointsInstrumented(instrumented);
Andreas Gampe157c77e2016-10-17 17:44:41 -0700785
786 // Note: ResetQuickAllocEntryPoints only works when the runtime is started. Manually run the
787 // update for just this thread.
Andreas Gampe162ae502016-10-18 10:03:42 -0700788 // Note: self may be null. One of those paths is setting instrumentation in the Heap
789 // constructor for gcstress mode.
790 if (self != nullptr) {
791 ResetQuickAllocEntryPointsForThread(self, nullptr);
792 }
Andreas Gampe157c77e2016-10-17 17:44:41 -0700793
Mathieu Chartier50e93312016-03-16 11:25:29 -0700794 alloc_entrypoints_instrumented_ = instrumented;
Mathieu Chartier661974a2014-01-09 11:23:53 -0800795 }
796}
797
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700798void Instrumentation::InstrumentQuickAllocEntryPoints() {
799 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
800 InstrumentQuickAllocEntryPointsLocked();
Ian Rogersfa824272013-11-05 16:12:57 -0800801}
802
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700803void Instrumentation::UninstrumentQuickAllocEntryPoints() {
804 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
805 UninstrumentQuickAllocEntryPointsLocked();
806}
807
808void Instrumentation::InstrumentQuickAllocEntryPointsLocked() {
809 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
810 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
811 SetEntrypointsInstrumented(true);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800812 }
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700813 ++quick_alloc_entry_points_instrumentation_counter_;
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700814}
815
816void Instrumentation::UninstrumentQuickAllocEntryPointsLocked() {
817 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
818 CHECK_GT(quick_alloc_entry_points_instrumentation_counter_, 0U);
819 --quick_alloc_entry_points_instrumentation_counter_;
820 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
821 SetEntrypointsInstrumented(false);
822 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800823}
824
825void Instrumentation::ResetQuickAllocEntryPoints() {
826 Runtime* runtime = Runtime::Current();
827 if (runtime->IsStarted()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800828 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700829 runtime->GetThreadList()->ForEach(ResetQuickAllocEntryPointsForThread, nullptr);
Ian Rogersfa824272013-11-05 16:12:57 -0800830 }
831}
832
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700833void Instrumentation::UpdateMethodsCodeImpl(ArtMethod* method, const void* quick_code) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800834 const void* new_quick_code;
Ian Rogers62d6c772013-02-27 08:32:07 -0800835 if (LIKELY(!instrumentation_stubs_installed_)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800836 new_quick_code = quick_code;
Jeff Hao65d15d92013-07-16 16:39:33 -0700837 } else {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100838 if ((interpreter_stubs_installed_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800839 new_quick_code = GetQuickToInterpreterBridge();
Jeff Hao65d15d92013-07-16 16:39:33 -0700840 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700841 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700842 if (class_linker->IsQuickResolutionStub(quick_code) ||
843 class_linker->IsQuickToInterpreterBridge(quick_code)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700844 new_quick_code = quick_code;
Alex Light6cae5ea2018-06-07 17:07:02 -0700845 } else if (entry_exit_stubs_installed_ &&
846 // We need to make sure not to replace anything that InstallStubsForMethod
847 // wouldn't. Specifically we cannot stub out Proxy.<init> since subtypes copy the
848 // implementation directly and this will confuse the instrumentation trampolines.
849 // TODO We should remove the need for this since it makes it impossible to profile
850 // Proxy.<init> correctly in all cases.
851 method != jni::DecodeArtMethod(WellKnownClasses::java_lang_reflect_Proxy_init)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700852 new_quick_code = GetQuickInstrumentationEntryPoint();
Alex Light2d441b12018-06-08 15:33:21 -0700853 if (!method->IsNative() && Runtime::Current()->GetJit() != nullptr) {
854 // Native methods use trampoline entrypoints during interpreter tracing.
855 DCHECK(!Runtime::Current()->GetJit()->GetCodeCache()->GetGarbageCollectCode());
856 ProfilingInfo* profiling_info = method->GetProfilingInfo(kRuntimePointerSize);
857 // Tracing will look at the saved entry point in the profiling info to know the actual
858 // entrypoint, so we store it here.
859 if (profiling_info != nullptr) {
860 profiling_info->SetSavedEntryPoint(quick_code);
861 }
862 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700863 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700864 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700865 }
Jeff Hao65d15d92013-07-16 16:39:33 -0700866 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800867 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800868 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100869}
870
Nicolas Geoffraya6e0e7d2018-01-26 13:16:50 +0000871void Instrumentation::UpdateNativeMethodsCodeToJitCode(ArtMethod* method, const void* quick_code) {
872 // We don't do any read barrier on `method`'s declaring class in this code, as the JIT might
873 // enter here on a soon-to-be deleted ArtMethod. Updating the entrypoint is OK though, as
874 // the ArtMethod is still in memory.
875 const void* new_quick_code = quick_code;
876 if (UNLIKELY(instrumentation_stubs_installed_) && entry_exit_stubs_installed_) {
877 new_quick_code = GetQuickInstrumentationEntryPoint();
878 }
879 UpdateEntrypoints(method, new_quick_code);
880}
881
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700882void Instrumentation::UpdateMethodsCode(ArtMethod* method, const void* quick_code) {
883 DCHECK(method->GetDeclaringClass()->IsResolved());
884 UpdateMethodsCodeImpl(method, quick_code);
885}
886
Alex Light0a5ec3d2017-07-25 16:50:26 -0700887void Instrumentation::UpdateMethodsCodeToInterpreterEntryPoint(ArtMethod* method) {
888 UpdateMethodsCodeImpl(method, GetQuickToInterpreterBridge());
889}
890
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000891void Instrumentation::UpdateMethodsCodeForJavaDebuggable(ArtMethod* method,
892 const void* quick_code) {
893 // When the runtime is set to Java debuggable, we may update the entry points of
894 // all methods of a class to the interpreter bridge. A method's declaring class
895 // might not be in resolved state yet in that case, so we bypass the DCHECK in
896 // UpdateMethodsCode.
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700897 UpdateMethodsCodeImpl(method, quick_code);
898}
899
Mathieu Chartiere401d142015-04-22 13:56:20 -0700900bool Instrumentation::AddDeoptimizedMethod(ArtMethod* method) {
901 if (IsDeoptimizedMethod(method)) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700902 // Already in the map. Return.
903 return false;
904 }
905 // Not found. Add it.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700906 deoptimized_methods_.insert(method);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700907 return true;
908}
909
Mathieu Chartiere401d142015-04-22 13:56:20 -0700910bool Instrumentation::IsDeoptimizedMethod(ArtMethod* method) {
911 return deoptimized_methods_.find(method) != deoptimized_methods_.end();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700912}
913
Mathieu Chartiere401d142015-04-22 13:56:20 -0700914ArtMethod* Instrumentation::BeginDeoptimizedMethod() {
915 if (deoptimized_methods_.empty()) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700916 // Empty.
917 return nullptr;
918 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700919 return *deoptimized_methods_.begin();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700920}
921
Mathieu Chartiere401d142015-04-22 13:56:20 -0700922bool Instrumentation::RemoveDeoptimizedMethod(ArtMethod* method) {
923 auto it = deoptimized_methods_.find(method);
924 if (it == deoptimized_methods_.end()) {
925 return false;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700926 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700927 deoptimized_methods_.erase(it);
928 return true;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700929}
930
931bool Instrumentation::IsDeoptimizedMethodsEmpty() const {
932 return deoptimized_methods_.empty();
933}
934
Mathieu Chartiere401d142015-04-22 13:56:20 -0700935void Instrumentation::Deoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100936 CHECK(!method->IsNative());
937 CHECK(!method->IsProxyMethod());
Alex Light9139e002015-10-09 15:59:48 -0700938 CHECK(method->IsInvokable());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100939
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700940 Thread* self = Thread::Current();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700941 {
942 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700943 bool has_not_been_deoptimized = AddDeoptimizedMethod(method);
David Sehr709b0702016-10-13 09:12:37 -0700944 CHECK(has_not_been_deoptimized) << "Method " << ArtMethod::PrettyMethod(method)
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200945 << " is already deoptimized";
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700946 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100947 if (!interpreter_stubs_installed_) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800948 UpdateEntrypoints(method, GetQuickInstrumentationEntryPoint());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100949
950 // Install instrumentation exit stub and instrumentation frames. We may already have installed
951 // these previously so it will only cover the newly created frames.
952 instrumentation_stubs_installed_ = true;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700953 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100954 Runtime::Current()->GetThreadList()->ForEach(InstrumentationInstallStack, this);
955 }
956}
957
Mathieu Chartiere401d142015-04-22 13:56:20 -0700958void Instrumentation::Undeoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100959 CHECK(!method->IsNative());
960 CHECK(!method->IsProxyMethod());
Alex Light9139e002015-10-09 15:59:48 -0700961 CHECK(method->IsInvokable());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100962
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700963 Thread* self = Thread::Current();
964 bool empty;
965 {
966 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700967 bool found_and_erased = RemoveDeoptimizedMethod(method);
David Sehr709b0702016-10-13 09:12:37 -0700968 CHECK(found_and_erased) << "Method " << ArtMethod::PrettyMethod(method)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700969 << " is not deoptimized";
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700970 empty = IsDeoptimizedMethodsEmpty();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700971 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100972
973 // Restore code and possibly stack only if we did not deoptimize everything.
974 if (!interpreter_stubs_installed_) {
975 // Restore its code or resolution trampoline.
976 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800977 if (method->IsStatic() && !method->IsConstructor() &&
978 !method->GetDeclaringClass()->IsInitialized()) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800979 UpdateEntrypoints(method, GetQuickResolutionStub());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100980 } else {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000981 const void* quick_code = NeedDebugVersionFor(method)
982 ? GetQuickToInterpreterBridge()
Alex Lightfc49fec2018-01-16 22:28:36 +0000983 : class_linker->GetQuickOatCodeFor(method);
Elliott Hughes956af0f2014-12-11 14:34:28 -0800984 UpdateEntrypoints(method, quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100985 }
986
987 // If there is no deoptimized method left, we can restore the stack of each thread.
Alex Lightf244a572018-06-08 13:56:51 -0700988 if (empty && !entry_exit_stubs_installed_) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700989 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100990 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
991 instrumentation_stubs_installed_ = false;
992 }
993 }
994}
995
Mathieu Chartiere401d142015-04-22 13:56:20 -0700996bool Instrumentation::IsDeoptimized(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100997 DCHECK(method != nullptr);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700998 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700999 return IsDeoptimizedMethod(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001000}
1001
1002void Instrumentation::EnableDeoptimization() {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -07001003 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -07001004 CHECK(IsDeoptimizedMethodsEmpty());
Sebastien Hertz11d40c22014-02-19 18:00:17 +01001005 CHECK_EQ(deoptimization_enabled_, false);
1006 deoptimization_enabled_ = true;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001007}
1008
Sebastien Hertz0462c4c2015-04-01 16:34:17 +02001009void Instrumentation::DisableDeoptimization(const char* key) {
Sebastien Hertz11d40c22014-02-19 18:00:17 +01001010 CHECK_EQ(deoptimization_enabled_, true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001011 // If we deoptimized everything, undo it.
Alex Lightdba61482016-12-21 08:20:29 -08001012 InstrumentationLevel level = GetCurrentInstrumentationLevel();
1013 if (level == InstrumentationLevel::kInstrumentWithInterpreter) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +02001014 UndeoptimizeEverything(key);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001015 }
1016 // Undeoptimized selected methods.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -07001017 while (true) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001018 ArtMethod* method;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -07001019 {
1020 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -07001021 if (IsDeoptimizedMethodsEmpty()) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -07001022 break;
1023 }
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -07001024 method = BeginDeoptimizedMethod();
1025 CHECK(method != nullptr);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -07001026 }
1027 Undeoptimize(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001028 }
Sebastien Hertz11d40c22014-02-19 18:00:17 +01001029 deoptimization_enabled_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001030}
1031
Sebastien Hertz11d40c22014-02-19 18:00:17 +01001032// Indicates if instrumentation should notify method enter/exit events to the listeners.
1033bool Instrumentation::ShouldNotifyMethodEnterExitEvents() const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +02001034 if (!HasMethodEntryListeners() && !HasMethodExitListeners()) {
1035 return false;
1036 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +01001037 return !deoptimization_enabled_ && !interpreter_stubs_installed_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001038}
1039
Sebastien Hertz0462c4c2015-04-01 16:34:17 +02001040void Instrumentation::DeoptimizeEverything(const char* key) {
1041 CHECK(deoptimization_enabled_);
1042 ConfigureStubs(key, InstrumentationLevel::kInstrumentWithInterpreter);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001043}
1044
Sebastien Hertz0462c4c2015-04-01 16:34:17 +02001045void Instrumentation::UndeoptimizeEverything(const char* key) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001046 CHECK(interpreter_stubs_installed_);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +02001047 CHECK(deoptimization_enabled_);
1048 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001049}
1050
Sebastien Hertz0462c4c2015-04-01 16:34:17 +02001051void Instrumentation::EnableMethodTracing(const char* key, bool needs_interpreter) {
1052 InstrumentationLevel level;
1053 if (needs_interpreter) {
1054 level = InstrumentationLevel::kInstrumentWithInterpreter;
1055 } else {
1056 level = InstrumentationLevel::kInstrumentWithInstrumentationStubs;
Alex Light2d441b12018-06-08 15:33:21 -07001057 if (Runtime::Current()->GetJit() != nullptr) {
1058 // TODO b/110263880 It would be better if we didn't need to do this.
1059 // Since we need to hold the method entrypoint across a suspend to ensure instrumentation
1060 // hooks are called correctly we have to disable jit-gc to ensure that the entrypoint doesn't
1061 // go away. Furthermore we need to leave this off permanently since one could get the same
1062 // effect by causing this to be toggled on and off.
1063 Runtime::Current()->GetJit()->GetCodeCache()->SetGarbageCollectCode(false);
1064 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +02001065 }
1066 ConfigureStubs(key, level);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001067}
1068
Sebastien Hertz0462c4c2015-04-01 16:34:17 +02001069void Instrumentation::DisableMethodTracing(const char* key) {
1070 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
jeffhao725a9572012-11-13 18:20:12 -08001071}
1072
Alex Light2d441b12018-06-08 15:33:21 -07001073const void* Instrumentation::GetCodeForInvoke(ArtMethod* method) const {
1074 // This is called by instrumentation entry only and that should never be getting proxy methods.
1075 DCHECK(!method->IsProxyMethod()) << method->PrettyMethod();
1076 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1077 if (LIKELY(!instrumentation_stubs_installed_ && !interpreter_stubs_installed_)) {
1078 // In general we just return whatever the method thinks its entrypoint is here. The only
1079 // exception is if it still has the instrumentation entrypoint. That means we are racing another
1080 // thread getting rid of instrumentation which is unexpected but possible. In that case we want
1081 // to wait and try to get it from the oat file or jit.
1082 const void* code = method->GetEntryPointFromQuickCompiledCodePtrSize(kRuntimePointerSize);
1083 DCHECK(code != nullptr);
1084 if (code != GetQuickInstrumentationEntryPoint()) {
1085 return code;
1086 } else if (method->IsNative()) {
1087 return class_linker->GetQuickOatCodeFor(method);
1088 }
1089 // We don't know what it is. Fallthough to try to find the code from the JIT or Oat file.
1090 } else if (method->IsNative()) {
1091 // TODO We could have JIT compiled native entrypoints. It might be worth it to find these.
1092 return class_linker->GetQuickOatCodeFor(method);
1093 } else if (UNLIKELY(interpreter_stubs_installed_)) {
1094 return GetQuickToInterpreterBridge();
1095 }
1096 // Since the method cannot be native due to ifs above we can always fall back to interpreter
1097 // bridge.
1098 const void* result = GetQuickToInterpreterBridge();
1099 if (!NeedDebugVersionFor(method)) {
1100 // If we don't need a debug version we should see what the oat file/class linker has to say.
1101 result = class_linker->GetQuickOatCodeFor(method);
1102 }
1103 // If both those fail try the jit.
1104 if (result == GetQuickToInterpreterBridge()) {
1105 jit::Jit* jit = Runtime::Current()->GetJit();
1106 if (jit != nullptr) {
1107 const void* res = jit->GetCodeCache()->FindCompiledCodeForInstrumentation(method);
1108 if (res != nullptr) {
1109 result = res;
1110 }
1111 }
1112 }
1113 return result;
1114}
1115
Andreas Gampe542451c2016-07-26 09:02:02 -07001116const void* Instrumentation::GetQuickCodeFor(ArtMethod* method, PointerSize pointer_size) const {
Vladimir Marko97d7e1c2016-10-04 14:44:28 +01001117 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers62d6c772013-02-27 08:32:07 -08001118 if (LIKELY(!instrumentation_stubs_installed_)) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001119 const void* code = method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Vladimir Marko8a630572014-04-09 18:45:35 +01001120 DCHECK(code != nullptr);
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001121 if (LIKELY(!class_linker->IsQuickResolutionStub(code) &&
1122 !class_linker->IsQuickToInterpreterBridge(code)) &&
1123 !class_linker->IsQuickResolutionStub(code) &&
1124 !class_linker->IsQuickToInterpreterBridge(code)) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001125 return code;
1126 }
1127 }
Alex Lightfc49fec2018-01-16 22:28:36 +00001128 return class_linker->GetQuickOatCodeFor(method);
jeffhao725a9572012-11-13 18:20:12 -08001129}
1130
Alex Lightd7661582017-05-01 13:48:16 -07001131void Instrumentation::MethodEnterEventImpl(Thread* thread,
1132 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001133 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -08001134 uint32_t dex_pc) const {
Mingyao Yang2ee17902017-08-30 11:37:08 -07001135 DCHECK(!method->IsRuntimeMethod());
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001136 if (HasMethodEntryListeners()) {
Alex Lightd7661582017-05-01 13:48:16 -07001137 Thread* self = Thread::Current();
1138 StackHandleScope<1> hs(self);
1139 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001140 for (InstrumentationListener* listener : method_entry_listeners_) {
1141 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001142 listener->MethodEntered(thread, thiz, method, dex_pc);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001143 }
1144 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001145 }
1146}
1147
Alex Lightd7661582017-05-01 13:48:16 -07001148void Instrumentation::MethodExitEventImpl(Thread* thread,
1149 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001150 ArtMethod* method,
Alex Lightd7661582017-05-01 13:48:16 -07001151 uint32_t dex_pc,
1152 const JValue& return_value) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001153 if (HasMethodExitListeners()) {
Alex Lightd7661582017-05-01 13:48:16 -07001154 Thread* self = Thread::Current();
1155 StackHandleScope<2> hs(self);
1156 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
1157 if (method->GetInterfaceMethodIfProxy(kRuntimePointerSize)
1158 ->GetReturnTypePrimitive() != Primitive::kPrimNot) {
1159 for (InstrumentationListener* listener : method_exit_listeners_) {
1160 if (listener != nullptr) {
1161 listener->MethodExited(thread, thiz, method, dex_pc, return_value);
1162 }
1163 }
1164 } else {
1165 Handle<mirror::Object> ret(hs.NewHandle(return_value.GetL()));
1166 for (InstrumentationListener* listener : method_exit_listeners_) {
1167 if (listener != nullptr) {
1168 listener->MethodExited(thread, thiz, method, dex_pc, ret);
1169 }
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001170 }
1171 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001172 }
1173}
1174
Alex Lightd7661582017-05-01 13:48:16 -07001175void Instrumentation::MethodUnwindEvent(Thread* thread,
1176 mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001177 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -08001178 uint32_t dex_pc) const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +02001179 if (HasMethodUnwindListeners()) {
Alex Lightd7661582017-05-01 13:48:16 -07001180 Thread* self = Thread::Current();
1181 StackHandleScope<1> hs(self);
1182 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Mathieu Chartier02e25112013-08-14 16:14:24 -07001183 for (InstrumentationListener* listener : method_unwind_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001184 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001185 listener->MethodUnwind(thread, thiz, method, dex_pc);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001186 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001187 }
1188 }
1189}
1190
Alex Lightd7661582017-05-01 13:48:16 -07001191void Instrumentation::DexPcMovedEventImpl(Thread* thread,
1192 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001193 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -08001194 uint32_t dex_pc) const {
Alex Lightd7661582017-05-01 13:48:16 -07001195 Thread* self = Thread::Current();
1196 StackHandleScope<1> hs(self);
1197 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001198 for (InstrumentationListener* listener : dex_pc_listeners_) {
1199 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001200 listener->DexPcMoved(thread, thiz, method, dex_pc);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001201 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001202 }
1203}
1204
Nicolas Geoffray81f0f952016-01-20 16:25:19 +00001205void Instrumentation::BranchImpl(Thread* thread,
1206 ArtMethod* method,
1207 uint32_t dex_pc,
1208 int32_t offset) const {
1209 for (InstrumentationListener* listener : branch_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001210 if (listener != nullptr) {
Nicolas Geoffray81f0f952016-01-20 16:25:19 +00001211 listener->Branch(thread, method, dex_pc, offset);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001212 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001213 }
1214}
1215
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001216void Instrumentation::InvokeVirtualOrInterfaceImpl(Thread* thread,
Alex Lightd7661582017-05-01 13:48:16 -07001217 ObjPtr<mirror::Object> this_object,
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001218 ArtMethod* caller,
1219 uint32_t dex_pc,
1220 ArtMethod* callee) const {
Alex Lightd7661582017-05-01 13:48:16 -07001221 Thread* self = Thread::Current();
1222 StackHandleScope<1> hs(self);
1223 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001224 for (InstrumentationListener* listener : invoke_virtual_or_interface_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001225 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001226 listener->InvokeVirtualOrInterface(thread, thiz, caller, dex_pc, callee);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001227 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001228 }
1229}
1230
Alex Lighte814f9d2017-07-31 16:14:39 -07001231void Instrumentation::WatchedFramePopImpl(Thread* thread, const ShadowFrame& frame) const {
1232 for (InstrumentationListener* listener : watched_frame_pop_listeners_) {
1233 if (listener != nullptr) {
1234 listener->WatchedFramePop(thread, frame);
1235 }
1236 }
1237}
1238
Alex Lightd7661582017-05-01 13:48:16 -07001239void Instrumentation::FieldReadEventImpl(Thread* thread,
1240 ObjPtr<mirror::Object> this_object,
1241 ArtMethod* method,
1242 uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -07001243 ArtField* field) const {
Alex Lightd7661582017-05-01 13:48:16 -07001244 Thread* self = Thread::Current();
1245 StackHandleScope<1> hs(self);
1246 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001247 for (InstrumentationListener* listener : field_read_listeners_) {
1248 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001249 listener->FieldRead(thread, thiz, method, dex_pc, field);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001250 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001251 }
1252}
1253
Alex Lightd7661582017-05-01 13:48:16 -07001254void Instrumentation::FieldWriteEventImpl(Thread* thread,
1255 ObjPtr<mirror::Object> this_object,
1256 ArtMethod* method,
1257 uint32_t dex_pc,
1258 ArtField* field,
1259 const JValue& field_value) const {
1260 Thread* self = Thread::Current();
1261 StackHandleScope<2> hs(self);
1262 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
1263 if (field->IsPrimitiveType()) {
1264 for (InstrumentationListener* listener : field_write_listeners_) {
1265 if (listener != nullptr) {
1266 listener->FieldWritten(thread, thiz, method, dex_pc, field, field_value);
1267 }
1268 }
1269 } else {
1270 Handle<mirror::Object> val(hs.NewHandle(field_value.GetL()));
1271 for (InstrumentationListener* listener : field_write_listeners_) {
1272 if (listener != nullptr) {
1273 listener->FieldWritten(thread, thiz, method, dex_pc, field, val);
1274 }
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001275 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001276 }
1277}
1278
Alex Light6e1607e2017-08-23 10:06:18 -07001279void Instrumentation::ExceptionThrownEvent(Thread* thread,
Sebastien Hertz947ff082013-09-17 14:10:13 +02001280 mirror::Throwable* exception_object) const {
Hiroshi Yamauchi3481f7a2017-02-10 12:07:36 -08001281 Thread* self = Thread::Current();
1282 StackHandleScope<1> hs(self);
1283 Handle<mirror::Throwable> h_exception(hs.NewHandle(exception_object));
Alex Light6e1607e2017-08-23 10:06:18 -07001284 if (HasExceptionThrownListeners()) {
Hiroshi Yamauchi3481f7a2017-02-10 12:07:36 -08001285 DCHECK_EQ(thread->GetException(), h_exception.Get());
Jeff Haoc0bd4da2013-04-11 15:52:28 -07001286 thread->ClearException();
Alex Light6e1607e2017-08-23 10:06:18 -07001287 for (InstrumentationListener* listener : exception_thrown_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001288 if (listener != nullptr) {
Alex Light6e1607e2017-08-23 10:06:18 -07001289 listener->ExceptionThrown(thread, h_exception);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001290 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001291 }
Alex Light9fb1ab12017-09-05 09:32:49 -07001292 // See b/65049545 for discussion about this behavior.
1293 thread->AssertNoPendingException();
Hiroshi Yamauchi3481f7a2017-02-10 12:07:36 -08001294 thread->SetException(h_exception.Get());
Ian Rogers62d6c772013-02-27 08:32:07 -08001295 }
1296}
1297
Alex Light9fb1ab12017-09-05 09:32:49 -07001298void Instrumentation::ExceptionHandledEvent(Thread* thread,
1299 mirror::Throwable* exception_object) const {
1300 Thread* self = Thread::Current();
1301 StackHandleScope<1> hs(self);
1302 Handle<mirror::Throwable> h_exception(hs.NewHandle(exception_object));
1303 if (HasExceptionHandledListeners()) {
1304 // We should have cleared the exception so that callers can detect a new one.
1305 DCHECK(thread->GetException() == nullptr);
1306 for (InstrumentationListener* listener : exception_handled_listeners_) {
1307 if (listener != nullptr) {
1308 listener->ExceptionHandled(thread, h_exception);
1309 }
1310 }
1311 }
1312}
1313
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +00001314// Computes a frame ID by ignoring inlined frames.
1315size_t Instrumentation::ComputeFrameId(Thread* self,
1316 size_t frame_depth,
1317 size_t inlined_frames_before_frame) {
1318 CHECK_GE(frame_depth, inlined_frames_before_frame);
1319 size_t no_inline_depth = frame_depth - inlined_frames_before_frame;
1320 return StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) - no_inline_depth;
1321}
1322
Ian Rogers62d6c772013-02-27 08:32:07 -08001323static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame,
1324 int delta)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001325 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +01001326 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) + delta;
Ian Rogers62d6c772013-02-27 08:32:07 -08001327 if (frame_id != instrumentation_frame.frame_id_) {
1328 LOG(ERROR) << "Expected frame_id=" << frame_id << " but found "
1329 << instrumentation_frame.frame_id_;
1330 StackVisitor::DescribeStack(self);
1331 CHECK_EQ(frame_id, instrumentation_frame.frame_id_);
1332 }
1333}
1334
1335void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001336 ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -07001337 uintptr_t lr, bool interpreter_entry) {
Alex Lightb7edcda2017-04-27 13:20:31 -07001338 DCHECK(!self->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -08001339 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1340 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001341 LOG(INFO) << "Entering " << ArtMethod::PrettyMethod(method) << " from PC "
1342 << reinterpret_cast<void*>(lr);
Ian Rogers62d6c772013-02-27 08:32:07 -08001343 }
Alex Lightb7edcda2017-04-27 13:20:31 -07001344
1345 // We send the enter event before pushing the instrumentation frame to make cleanup easier. If the
1346 // event causes an exception we can simply send the unwind event and return.
1347 StackHandleScope<1> hs(self);
1348 Handle<mirror::Object> h_this(hs.NewHandle(this_object));
1349 if (!interpreter_entry) {
1350 MethodEnterEvent(self, h_this.Get(), method, 0);
1351 if (self->IsExceptionPending()) {
1352 MethodUnwindEvent(self, h_this.Get(), method, 0);
1353 return;
1354 }
1355 }
1356
1357 // We have a callee-save frame meaning this value is guaranteed to never be 0.
1358 DCHECK(!self->IsExceptionPending());
1359 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk);
1360
1361 instrumentation::InstrumentationStackFrame instrumentation_frame(h_this.Get(), method, lr,
Jeff Hao9a916d32013-06-27 18:45:37 -07001362 frame_id, interpreter_entry);
Ian Rogers62d6c772013-02-27 08:32:07 -08001363 stack->push_front(instrumentation_frame);
Ian Rogers62d6c772013-02-27 08:32:07 -08001364}
1365
Mingyao Yang2ee17902017-08-30 11:37:08 -07001366DeoptimizationMethodType Instrumentation::GetDeoptimizationMethodType(ArtMethod* method) {
1367 if (method->IsRuntimeMethod()) {
1368 // Certain methods have strict requirement on whether the dex instruction
1369 // should be re-executed upon deoptimization.
1370 if (method == Runtime::Current()->GetCalleeSaveMethod(
1371 CalleeSaveType::kSaveEverythingForClinit)) {
1372 return DeoptimizationMethodType::kKeepDexPc;
1373 }
1374 if (method == Runtime::Current()->GetCalleeSaveMethod(
1375 CalleeSaveType::kSaveEverythingForSuspendCheck)) {
1376 return DeoptimizationMethodType::kKeepDexPc;
1377 }
1378 }
1379 return DeoptimizationMethodType::kDefault;
1380}
1381
1382// Try to get the shorty of a runtime method if it's an invocation stub.
1383struct RuntimeMethodShortyVisitor : public StackVisitor {
1384 explicit RuntimeMethodShortyVisitor(Thread* thread)
1385 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
1386 shorty('V') {}
1387
Andreas Gampefa6a1b02018-09-07 08:11:55 -07001388 bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
Mingyao Yang2ee17902017-08-30 11:37:08 -07001389 ArtMethod* m = GetMethod();
1390 if (m != nullptr && !m->IsRuntimeMethod()) {
1391 // The first Java method.
1392 if (m->IsNative()) {
1393 // Use JNI method's shorty for the jni stub.
1394 shorty = m->GetShorty()[0];
1395 return false;
1396 }
1397 if (m->IsProxyMethod()) {
1398 // Proxy method just invokes its proxied method via
1399 // art_quick_proxy_invoke_handler.
1400 shorty = m->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty()[0];
1401 return false;
1402 }
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001403 const Instruction& instr = m->DexInstructions().InstructionAt(GetDexPc());
1404 if (instr.IsInvoke()) {
Mingyao Yang2ee17902017-08-30 11:37:08 -07001405 const DexFile* dex_file = m->GetDexFile();
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001406 if (interpreter::IsStringInit(dex_file, instr.VRegB())) {
Mingyao Yang2ee17902017-08-30 11:37:08 -07001407 // Invoking string init constructor is turned into invoking
1408 // StringFactory.newStringFromChars() which returns a string.
1409 shorty = 'L';
1410 return false;
1411 }
1412 // A regular invoke, use callee's shorty.
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001413 uint32_t method_idx = instr.VRegB();
Mingyao Yang2ee17902017-08-30 11:37:08 -07001414 shorty = dex_file->GetMethodShorty(method_idx)[0];
1415 }
1416 // Stop stack walking since we've seen a Java frame.
1417 return false;
1418 }
1419 return true;
1420 }
1421
1422 char shorty;
1423};
1424
Alex Lightb7edcda2017-04-27 13:20:31 -07001425TwoWordReturn Instrumentation::PopInstrumentationStackFrame(Thread* self,
1426 uintptr_t* return_pc,
1427 uint64_t* gpr_result,
1428 uint64_t* fpr_result) {
1429 DCHECK(gpr_result != nullptr);
1430 DCHECK(fpr_result != nullptr);
Ian Rogers62d6c772013-02-27 08:32:07 -08001431 // Do the pop.
1432 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1433 CHECK_GT(stack->size(), 0U);
1434 InstrumentationStackFrame instrumentation_frame = stack->front();
1435 stack->pop_front();
1436
1437 // Set return PC and check the sanity of the stack.
1438 *return_pc = instrumentation_frame.return_pc_;
1439 CheckStackDepth(self, instrumentation_frame, 0);
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001440 self->VerifyStack();
Ian Rogers62d6c772013-02-27 08:32:07 -08001441
Mathieu Chartiere401d142015-04-22 13:56:20 -07001442 ArtMethod* method = instrumentation_frame.method_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001443 uint32_t length;
Andreas Gampe542451c2016-07-26 09:02:02 -07001444 const PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mingyao Yang2ee17902017-08-30 11:37:08 -07001445 char return_shorty;
1446
1447 // Runtime method does not call into MethodExitEvent() so there should not be
1448 // suspension point below.
1449 ScopedAssertNoThreadSuspension ants(__FUNCTION__, method->IsRuntimeMethod());
1450 if (method->IsRuntimeMethod()) {
1451 if (method != Runtime::Current()->GetCalleeSaveMethod(
1452 CalleeSaveType::kSaveEverythingForClinit)) {
1453 // If the caller is at an invocation point and the runtime method is not
1454 // for clinit, we need to pass return results to the caller.
1455 // We need the correct shorty to decide whether we need to pass the return
1456 // result for deoptimization below.
1457 RuntimeMethodShortyVisitor visitor(self);
1458 visitor.WalkStack();
1459 return_shorty = visitor.shorty;
1460 } else {
1461 // Some runtime methods such as allocations, unresolved field getters, etc.
1462 // have return value. We don't need to set return_value since MethodExitEvent()
1463 // below isn't called for runtime methods. Deoptimization doesn't need the
1464 // value either since the dex instruction will be re-executed by the
1465 // interpreter, except these two cases:
1466 // (1) For an invoke, which is handled above to get the correct shorty.
1467 // (2) For MONITOR_ENTER/EXIT, which cannot be re-executed since it's not
1468 // idempotent. However there is no return value for it anyway.
1469 return_shorty = 'V';
1470 }
1471 } else {
1472 return_shorty = method->GetInterfaceMethodIfProxy(pointer_size)->GetShorty(&length)[0];
1473 }
1474
Alex Lightb7edcda2017-04-27 13:20:31 -07001475 bool is_ref = return_shorty == '[' || return_shorty == 'L';
1476 StackHandleScope<1> hs(self);
1477 MutableHandle<mirror::Object> res(hs.NewHandle<mirror::Object>(nullptr));
Ian Rogers62d6c772013-02-27 08:32:07 -08001478 JValue return_value;
1479 if (return_shorty == 'V') {
1480 return_value.SetJ(0);
1481 } else if (return_shorty == 'F' || return_shorty == 'D') {
Alex Lightb7edcda2017-04-27 13:20:31 -07001482 return_value.SetJ(*fpr_result);
Ian Rogers62d6c772013-02-27 08:32:07 -08001483 } else {
Alex Lightb7edcda2017-04-27 13:20:31 -07001484 return_value.SetJ(*gpr_result);
1485 }
1486 if (is_ref) {
1487 // Take a handle to the return value so we won't lose it if we suspend.
1488 res.Assign(return_value.GetL());
Ian Rogers62d6c772013-02-27 08:32:07 -08001489 }
1490 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1491 // return_pc.
Andreas Gampee2abbc62017-09-15 11:59:26 -07001492 uint32_t dex_pc = dex::kDexNoIndex;
Ian Rogers62d6c772013-02-27 08:32:07 -08001493 mirror::Object* this_object = instrumentation_frame.this_object_;
Mingyao Yang2ee17902017-08-30 11:37:08 -07001494 if (!method->IsRuntimeMethod() && !instrumentation_frame.interpreter_entry_) {
Sebastien Hertz320deb22014-06-11 19:45:05 +02001495 MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value);
1496 }
jeffhao725a9572012-11-13 18:20:12 -08001497
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001498 // Deoptimize if the caller needs to continue execution in the interpreter. Do nothing if we get
1499 // back to an upcall.
1500 NthCallerVisitor visitor(self, 1, true);
1501 visitor.WalkStack(true);
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001502 bool deoptimize = (visitor.caller != nullptr) &&
Daniel Mihalyieb076692014-08-22 17:33:31 +02001503 (interpreter_stubs_installed_ || IsDeoptimized(visitor.caller) ||
1504 Dbg::IsForcedInterpreterNeededForUpcall(self, visitor.caller));
Alex Lightb7edcda2017-04-27 13:20:31 -07001505 if (is_ref) {
1506 // Restore the return value if it's a reference since it might have moved.
1507 *reinterpret_cast<mirror::Object**>(gpr_result) = res.Get();
1508 }
Nicolas Geoffraya0619e22016-12-20 13:57:43 +00001509 if (deoptimize && Runtime::Current()->IsAsyncDeoptimizeable(*return_pc)) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001510 if (kVerboseInstrumentation) {
Andreas Gampe46ee31b2016-12-14 10:11:49 -08001511 LOG(INFO) << "Deoptimizing "
1512 << visitor.caller->PrettyMethod()
1513 << " by returning from "
1514 << method->PrettyMethod()
1515 << " with result "
1516 << std::hex << return_value.GetJ() << std::dec
1517 << " in "
1518 << *self;
Ian Rogers62d6c772013-02-27 08:32:07 -08001519 }
Mingyao Yang2ee17902017-08-30 11:37:08 -07001520 DeoptimizationMethodType deopt_method_type = GetDeoptimizationMethodType(method);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001521 self->PushDeoptimizationContext(return_value,
Mingyao Yang2ee17902017-08-30 11:37:08 -07001522 return_shorty == 'L' || return_shorty == '[',
1523 nullptr /* no pending exception */,
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001524 false /* from_code */,
Mingyao Yang2ee17902017-08-30 11:37:08 -07001525 deopt_method_type);
Andreas Gamped58342c2014-06-05 14:18:08 -07001526 return GetTwoWordSuccessValue(*return_pc,
1527 reinterpret_cast<uintptr_t>(GetQuickDeoptimizationEntryPoint()));
Ian Rogers62d6c772013-02-27 08:32:07 -08001528 } else {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +00001529 if (deoptimize && !Runtime::Current()->IsAsyncDeoptimizeable(*return_pc)) {
Alex Lightd8eb6732018-01-29 15:16:02 -08001530 VLOG(deopt) << "Got a deoptimization request on un-deoptimizable " << method->PrettyMethod()
1531 << " at PC " << reinterpret_cast<void*>(*return_pc);
Nicolas Geoffraya0619e22016-12-20 13:57:43 +00001532 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001533 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001534 LOG(INFO) << "Returning from " << method->PrettyMethod()
Brian Carlstrom2d888622013-07-18 17:02:00 -07001535 << " to PC " << reinterpret_cast<void*>(*return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001536 }
Andreas Gamped58342c2014-06-05 14:18:08 -07001537 return GetTwoWordSuccessValue(0, *return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001538 }
jeffhao725a9572012-11-13 18:20:12 -08001539}
1540
Alex Light2c8206f2018-06-08 14:51:09 -07001541uintptr_t Instrumentation::PopFramesForDeoptimization(Thread* self, size_t nframes) const {
Ian Rogers62d6c772013-02-27 08:32:07 -08001542 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
Alex Light2c8206f2018-06-08 14:51:09 -07001543 CHECK_GE(stack->size(), nframes);
1544 if (nframes == 0) {
1545 return 0u;
1546 }
1547 // Only need to send instrumentation events if it's not for deopt (do give the log messages if we
1548 // have verbose-instrumentation anyway though).
1549 if (kVerboseInstrumentation) {
1550 for (size_t i = 0; i < nframes; i++) {
1551 LOG(INFO) << "Popping for deoptimization " << stack->at(i).method_->PrettyMethod();
Mingyao Yang2ee17902017-08-30 11:37:08 -07001552 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001553 }
Alex Light2c8206f2018-06-08 14:51:09 -07001554 // Now that we've sent all the instrumentation events we can actually modify the
1555 // instrumentation-stack. We cannot do this earlier since MethodUnwindEvent can re-enter java and
1556 // do other things that require the instrumentation stack to be in a consistent state with the
1557 // actual stack.
1558 for (size_t i = 0; i < nframes - 1; i++) {
1559 stack->pop_front();
1560 }
1561 uintptr_t return_pc = stack->front().return_pc_;
Alex Lightb7edcda2017-04-27 13:20:31 -07001562 stack->pop_front();
Alex Light2c8206f2018-06-08 14:51:09 -07001563 return return_pc;
Ian Rogers62d6c772013-02-27 08:32:07 -08001564}
1565
1566std::string InstrumentationStackFrame::Dump() const {
1567 std::ostringstream os;
David Sehr709b0702016-10-13 09:12:37 -07001568 os << "Frame " << frame_id_ << " " << ArtMethod::PrettyMethod(method_) << ":"
Ian Rogers62d6c772013-02-27 08:32:07 -08001569 << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_);
1570 return os.str();
1571}
1572
1573} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -08001574} // namespace art