blob: 1ddca8785fee8a901bd68920379d21d4260f48be [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
Ian Rogers62d6c772013-02-27 08:32:07 -080097
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070098Instrumentation::Instrumentation()
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +000099 : instrumentation_stubs_installed_(false),
100 entry_exit_stubs_installed_(false),
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700101 interpreter_stubs_installed_(false),
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000102 interpret_only_(false),
103 forced_interpret_only_(false),
104 have_method_entry_listeners_(false),
105 have_method_exit_listeners_(false),
106 have_method_unwind_listeners_(false),
107 have_dex_pc_listeners_(false),
108 have_field_read_listeners_(false),
109 have_field_write_listeners_(false),
Alex Light6e1607e2017-08-23 10:06:18 -0700110 have_exception_thrown_listeners_(false),
Alex Lighte814f9d2017-07-31 16:14:39 -0700111 have_watched_frame_pop_listeners_(false),
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000112 have_branch_listeners_(false),
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000113 have_invoke_virtual_or_interface_listeners_(false),
Alex Light9fb1ab12017-09-05 09:32:49 -0700114 have_exception_handled_listeners_(false),
Mathieu Chartierb8aa1e42016-04-05 14:36:57 -0700115 deoptimized_methods_lock_("deoptimized methods lock", kDeoptimizedMethodsLock),
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700116 deoptimization_enabled_(false),
117 interpreter_handler_table_(kMainHandlerTable),
Mathieu Chartier50e93312016-03-16 11:25:29 -0700118 quick_alloc_entry_points_instrumentation_counter_(0),
119 alloc_entrypoints_instrumented_(false) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700120}
121
Sebastien Hertza10aa372015-01-21 17:30:58 +0100122void Instrumentation::InstallStubsForClass(mirror::Class* klass) {
Vladimir Marko72ab6842017-01-20 19:32:50 +0000123 if (!klass->IsResolved()) {
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100124 // We need the class to be resolved to install/uninstall stubs. Otherwise its methods
125 // could not be initialized or linked with regards to class inheritance.
Vladimir Marko72ab6842017-01-20 19:32:50 +0000126 } else if (klass->IsErroneousResolved()) {
127 // We can't execute code in a erroneous class: do nothing.
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100128 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700129 for (ArtMethod& method : klass->GetMethods(kRuntimePointerSize)) {
Alex Light51a64d52015-12-17 13:55:59 -0800130 InstallStubsForMethod(&method);
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100131 }
jeffhao725a9572012-11-13 18:20:12 -0800132 }
jeffhao725a9572012-11-13 18:20:12 -0800133}
134
Mathieu Chartiere401d142015-04-22 13:56:20 -0700135static void UpdateEntrypoints(ArtMethod* method, const void* quick_code)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700136 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800137 method->SetEntryPointFromQuickCompiledCode(quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100138}
139
Alex Light0fa17862017-10-24 13:43:05 -0700140bool Instrumentation::NeedDebugVersionFor(ArtMethod* method) const
141 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2858632018-04-02 11:28:50 -0700142 art::Runtime* runtime = Runtime::Current();
143 // If anything says we need the debug version or we are debuggable we will need the debug version
144 // of the method.
145 return (runtime->GetRuntimeCallbacks()->MethodNeedsDebugVersion(method) ||
146 runtime->IsJavaDebuggable()) &&
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800147 !method->IsNative() &&
Alex Lightf2858632018-04-02 11:28:50 -0700148 !method->IsProxyMethod();
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800149}
150
Mathieu Chartiere401d142015-04-22 13:56:20 -0700151void Instrumentation::InstallStubsForMethod(ArtMethod* method) {
Alex Light9139e002015-10-09 15:59:48 -0700152 if (!method->IsInvokable() || method->IsProxyMethod()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100153 // Do not change stubs for these methods.
154 return;
155 }
Jeff Hao56802772014-08-19 10:17:36 -0700156 // Don't stub Proxy.<init>. Note that the Proxy class itself is not a proxy class.
Alex Light6cae5ea2018-06-07 17:07:02 -0700157 // TODO We should remove the need for this since it means we cannot always correctly detect calls
158 // to Proxy.<init>
159 // Annoyingly this can be called before we have actually initialized WellKnownClasses so therefore
160 // we also need to check this based on the declaring-class descriptor. The check is valid because
161 // Proxy only has a single constructor.
162 ArtMethod* well_known_proxy_init = jni::DecodeArtMethod(
163 WellKnownClasses::java_lang_reflect_Proxy_init);
164 if ((LIKELY(well_known_proxy_init != nullptr) && UNLIKELY(method == well_known_proxy_init)) ||
165 UNLIKELY(method->IsConstructor() &&
166 method->GetDeclaringClass()->DescriptorEquals("Ljava/lang/reflect/Proxy;"))) {
Jeff Haodb8a6642014-08-14 17:18:52 -0700167 return;
168 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800169 const void* new_quick_code;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100170 bool uninstall = !entry_exit_stubs_installed_ && !interpreter_stubs_installed_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800171 Runtime* const runtime = Runtime::Current();
172 ClassLinker* const class_linker = runtime->GetClassLinker();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100173 bool is_class_initialized = method->GetDeclaringClass()->IsInitialized();
174 if (uninstall) {
175 if ((forced_interpret_only_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800176 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100177 } else if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Alex Light2d441b12018-06-08 15:33:21 -0700178 // It would be great to search the JIT for its implementation here but we cannot due to the
179 // locks we hold. Instead just set to the interpreter bridge and that code will search the JIT
180 // when it gets called and replace the entrypoint then.
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000181 if (NeedDebugVersionFor(method)) {
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800182 new_quick_code = GetQuickToInterpreterBridge();
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000183 } else {
Alex Lightfc49fec2018-01-16 22:28:36 +0000184 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800185 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100186 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700187 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100188 }
189 } else { // !uninstall
Sebastien Hertzbae182c2013-12-17 10:42:03 +0100190 if ((interpreter_stubs_installed_ || forced_interpret_only_ || IsDeoptimized(method)) &&
191 !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800192 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100193 } else {
194 // Do not overwrite resolution trampoline. When the trampoline initializes the method's
195 // class, all its static methods code will be set to the instrumentation entry point.
196 // For more details, see ClassLinker::FixupStaticTrampolines.
197 if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Alex Light2d441b12018-06-08 15:33:21 -0700198 if (entry_exit_stubs_installed_) {
199 // This needs to be checked first since the instrumentation entrypoint will be able to
200 // find the actual JIT compiled code that corresponds to this method.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800201 new_quick_code = GetQuickInstrumentationEntryPoint();
Alex Light2d441b12018-06-08 15:33:21 -0700202 } else if (NeedDebugVersionFor(method)) {
203 // It would be great to search the JIT for its implementation here but we cannot due to
204 // the locks we hold. Instead just set to the interpreter bridge and that code will search
205 // the JIT when it gets called and replace the entrypoint then.
206 new_quick_code = GetQuickToInterpreterBridge();
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000207 } else {
Alex Lightfc49fec2018-01-16 22:28:36 +0000208 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100209 }
210 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700211 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100212 }
213 }
214 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800215 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100216}
217
Ian Rogers62d6c772013-02-27 08:32:07 -0800218// Places the instrumentation exit pc as the return PC for every quick frame. This also allows
219// deoptimization of quick frames to interpreter frames.
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100220// Since we may already have done this previously, we need to push new instrumentation frame before
221// existing instrumentation frames.
Ian Rogers62d6c772013-02-27 08:32:07 -0800222static void InstrumentationInstallStack(Thread* thread, void* arg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700223 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200224 struct InstallStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800225 InstallStackVisitor(Thread* thread_in, Context* context, uintptr_t instrumentation_exit_pc)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100226 : StackVisitor(thread_in, context, kInstrumentationStackWalk),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800227 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100228 instrumentation_exit_pc_(instrumentation_exit_pc),
Alex Lighte9278662018-03-08 16:55:58 -0800229 reached_existing_instrumentation_frames_(false), instrumentation_stack_depth_(0),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100230 last_return_pc_(0) {
231 }
jeffhao725a9572012-11-13 18:20:12 -0800232
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700233 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700234 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700235 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800236 if (kVerboseInstrumentation) {
237 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
238 }
239 last_return_pc_ = 0;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700240 return true; // Ignore upcalls.
Ian Rogers306057f2012-11-26 12:45:53 -0800241 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700242 if (GetCurrentQuickFrame() == nullptr) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800243 bool interpreter_frame = true;
Sebastien Hertz320deb22014-06-11 19:45:05 +0200244 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, 0, GetFrameId(),
245 interpreter_frame);
Jeff Haoa15a81b2014-05-27 18:25:47 -0700246 if (kVerboseInstrumentation) {
247 LOG(INFO) << "Pushing shadow frame " << instrumentation_frame.Dump();
248 }
249 shadow_stack_.push_back(instrumentation_frame);
250 return true; // Continue.
251 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800252 uintptr_t return_pc = GetReturnPc();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200253 if (kVerboseInstrumentation) {
254 LOG(INFO) << " Installing exit stub in " << DescribeLocation();
255 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100256 if (return_pc == instrumentation_exit_pc_) {
Mingyao Yang2ee17902017-08-30 11:37:08 -0700257 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
258
259 if (m->IsRuntimeMethod()) {
260 const InstrumentationStackFrame& frame =
Vladimir Marko35d5b8a2018-07-03 09:18:32 +0100261 (*instrumentation_stack_)[instrumentation_stack_depth_];
Mingyao Yang2ee17902017-08-30 11:37:08 -0700262 if (frame.interpreter_entry_) {
263 // This instrumentation frame is for an interpreter bridge and is
264 // pushed when executing the instrumented interpreter bridge. So method
265 // enter event must have been reported. However we need to push a DEX pc
266 // into the dex_pcs_ list to match size of instrumentation stack.
Andreas Gampee2abbc62017-09-15 11:59:26 -0700267 uint32_t dex_pc = dex::kDexNoIndex;
Mingyao Yang2ee17902017-08-30 11:37:08 -0700268 dex_pcs_.push_back(dex_pc);
269 last_return_pc_ = frame.return_pc_;
270 ++instrumentation_stack_depth_;
271 return true;
272 }
273 }
274
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100275 // We've reached a frame which has already been installed with instrumentation exit stub.
Alex Light74c91c92018-03-08 14:01:44 -0800276 // We should have already installed instrumentation or be interpreter on previous frames.
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100277 reached_existing_instrumentation_frames_ = true;
278
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200279 const InstrumentationStackFrame& frame =
Vladimir Marko35d5b8a2018-07-03 09:18:32 +0100280 (*instrumentation_stack_)[instrumentation_stack_depth_];
David Sehr709b0702016-10-13 09:12:37 -0700281 CHECK_EQ(m, frame.method_) << "Expected " << ArtMethod::PrettyMethod(m)
282 << ", Found " << ArtMethod::PrettyMethod(frame.method_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100283 return_pc = frame.return_pc_;
284 if (kVerboseInstrumentation) {
285 LOG(INFO) << "Ignoring already instrumented " << frame.Dump();
286 }
287 } else {
288 CHECK_NE(return_pc, 0U);
Alex Light74c91c92018-03-08 14:01:44 -0800289 if (UNLIKELY(reached_existing_instrumentation_frames_ && !m->IsRuntimeMethod())) {
290 // We already saw an existing instrumentation frame so this should be a runtime-method
291 // inserted by the interpreter or runtime.
Alex Lighte9278662018-03-08 16:55:58 -0800292 std::string thread_name;
293 GetThread()->GetThreadName(thread_name);
294 uint32_t dex_pc = dex::kDexNoIndex;
295 if (last_return_pc_ != 0 &&
296 GetCurrentOatQuickMethodHeader() != nullptr) {
297 dex_pc = GetCurrentOatQuickMethodHeader()->ToDexPc(m, last_return_pc_);
298 }
Alex Light74c91c92018-03-08 14:01:44 -0800299 LOG(FATAL) << "While walking " << thread_name << " found unexpected non-runtime method"
300 << " without instrumentation exit return or interpreter frame."
Alex Lighte9278662018-03-08 16:55:58 -0800301 << " method is " << GetMethod()->PrettyMethod()
302 << " return_pc is " << std::hex << return_pc
303 << " dex pc: " << dex_pc;
304 UNREACHABLE();
305 }
Mingyao Yang2ee17902017-08-30 11:37:08 -0700306 InstrumentationStackFrame instrumentation_frame(
307 m->IsRuntimeMethod() ? nullptr : GetThisObject(),
308 m,
309 return_pc,
310 GetFrameId(), // A runtime method still gets a frame id.
311 false);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100312 if (kVerboseInstrumentation) {
313 LOG(INFO) << "Pushing frame " << instrumentation_frame.Dump();
314 }
315
Sebastien Hertz320deb22014-06-11 19:45:05 +0200316 // Insert frame at the right position so we do not corrupt the instrumentation stack.
317 // Instrumentation stack frames are in descending frame id order.
318 auto it = instrumentation_stack_->begin();
319 for (auto end = instrumentation_stack_->end(); it != end; ++it) {
320 const InstrumentationStackFrame& current = *it;
321 if (instrumentation_frame.frame_id_ >= current.frame_id_) {
322 break;
323 }
324 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100325 instrumentation_stack_->insert(it, instrumentation_frame);
326 SetReturnPc(instrumentation_exit_pc_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800327 }
Andreas Gampee2abbc62017-09-15 11:59:26 -0700328 uint32_t dex_pc = dex::kDexNoIndex;
Mingyao Yang2ee17902017-08-30 11:37:08 -0700329 if (last_return_pc_ != 0 &&
330 GetCurrentOatQuickMethodHeader() != nullptr) {
331 dex_pc = GetCurrentOatQuickMethodHeader()->ToDexPc(m, last_return_pc_);
332 }
333 dex_pcs_.push_back(dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800334 last_return_pc_ = return_pc;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100335 ++instrumentation_stack_depth_;
Ian Rogers306057f2012-11-26 12:45:53 -0800336 return true; // Continue.
337 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800338 std::deque<InstrumentationStackFrame>* const instrumentation_stack_;
Jeff Haoa15a81b2014-05-27 18:25:47 -0700339 std::vector<InstrumentationStackFrame> shadow_stack_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800340 std::vector<uint32_t> dex_pcs_;
Ian Rogers306057f2012-11-26 12:45:53 -0800341 const uintptr_t instrumentation_exit_pc_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100342 bool reached_existing_instrumentation_frames_;
343 size_t instrumentation_stack_depth_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800344 uintptr_t last_return_pc_;
Ian Rogers306057f2012-11-26 12:45:53 -0800345 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800346 if (kVerboseInstrumentation) {
347 std::string thread_name;
348 thread->GetThreadName(thread_name);
349 LOG(INFO) << "Installing exit stubs in " << thread_name;
Ian Rogers306057f2012-11-26 12:45:53 -0800350 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100351
352 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers700a4022014-05-19 16:49:03 -0700353 std::unique_ptr<Context> context(Context::Create());
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700354 uintptr_t instrumentation_exit_pc = reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100355 InstallStackVisitor visitor(thread, context.get(), instrumentation_exit_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800356 visitor.WalkStack(true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100357 CHECK_EQ(visitor.dex_pcs_.size(), thread->GetInstrumentationStack()->size());
Ian Rogers62d6c772013-02-27 08:32:07 -0800358
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100359 if (instrumentation->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100360 // Create method enter events for all methods currently on the thread's stack. We only do this
361 // if no debugger is attached to prevent from posting events twice.
Jeff Haoa15a81b2014-05-27 18:25:47 -0700362 auto ssi = visitor.shadow_stack_.rbegin();
363 for (auto isi = thread->GetInstrumentationStack()->rbegin(),
364 end = thread->GetInstrumentationStack()->rend(); isi != end; ++isi) {
365 while (ssi != visitor.shadow_stack_.rend() && (*ssi).frame_id_ < (*isi).frame_id_) {
366 instrumentation->MethodEnterEvent(thread, (*ssi).this_object_, (*ssi).method_, 0);
367 ++ssi;
368 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100369 uint32_t dex_pc = visitor.dex_pcs_.back();
370 visitor.dex_pcs_.pop_back();
Alex Lightdc5423f2018-06-08 10:43:38 -0700371 if (!isi->interpreter_entry_ && !isi->method_->IsRuntimeMethod()) {
Sebastien Hertz320deb22014-06-11 19:45:05 +0200372 instrumentation->MethodEnterEvent(thread, (*isi).this_object_, (*isi).method_, dex_pc);
373 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100374 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800375 }
376 thread->VerifyStack();
Ian Rogers306057f2012-11-26 12:45:53 -0800377}
378
Mingyao Yang99170c62015-07-06 11:10:37 -0700379void Instrumentation::InstrumentThreadStack(Thread* thread) {
380 instrumentation_stubs_installed_ = true;
381 InstrumentationInstallStack(thread, this);
382}
383
Ian Rogers62d6c772013-02-27 08:32:07 -0800384// Removes the instrumentation exit pc as the return PC for every quick frame.
385static void InstrumentationRestoreStack(Thread* thread, void* arg)
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000386 REQUIRES(Locks::mutator_lock_) {
387 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
388
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200389 struct RestoreStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800390 RestoreStackVisitor(Thread* thread_in, uintptr_t instrumentation_exit_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800391 Instrumentation* instrumentation)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100392 : StackVisitor(thread_in, nullptr, kInstrumentationStackWalk),
393 thread_(thread_in),
Ian Rogers62d6c772013-02-27 08:32:07 -0800394 instrumentation_exit_pc_(instrumentation_exit_pc),
395 instrumentation_(instrumentation),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800396 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Ian Rogers62d6c772013-02-27 08:32:07 -0800397 frames_removed_(0) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800398
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700399 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800400 if (instrumentation_stack_->size() == 0) {
jeffhao725a9572012-11-13 18:20:12 -0800401 return false; // Stop.
402 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700403 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700404 if (GetCurrentQuickFrame() == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800405 if (kVerboseInstrumentation) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200406 LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId()
David Sehr709b0702016-10-13 09:12:37 -0700407 << " Method=" << ArtMethod::PrettyMethod(m);
Ian Rogers62d6c772013-02-27 08:32:07 -0800408 }
409 return true; // Ignore shadow frames.
410 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700411 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800412 if (kVerboseInstrumentation) {
413 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
414 }
Ian Rogers306057f2012-11-26 12:45:53 -0800415 return true; // Ignore upcalls.
416 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800417 bool removed_stub = false;
418 // TODO: make this search more efficient?
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100419 const size_t frameId = GetFrameId();
420 for (const InstrumentationStackFrame& instrumentation_frame : *instrumentation_stack_) {
421 if (instrumentation_frame.frame_id_ == frameId) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800422 if (kVerboseInstrumentation) {
423 LOG(INFO) << " Removing exit stub in " << DescribeLocation();
424 }
Jeff Hao9a916d32013-06-27 18:45:37 -0700425 if (instrumentation_frame.interpreter_entry_) {
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700426 CHECK(m == Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Jeff Hao9a916d32013-06-27 18:45:37 -0700427 } else {
David Sehr709b0702016-10-13 09:12:37 -0700428 CHECK(m == instrumentation_frame.method_) << ArtMethod::PrettyMethod(m);
Jeff Hao9a916d32013-06-27 18:45:37 -0700429 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800430 SetReturnPc(instrumentation_frame.return_pc_);
Mingyao Yang2ee17902017-08-30 11:37:08 -0700431 if (instrumentation_->ShouldNotifyMethodEnterExitEvents() &&
432 !m->IsRuntimeMethod()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100433 // Create the method exit events. As the methods didn't really exit the result is 0.
434 // We only do this if no debugger is attached to prevent from posting events twice.
435 instrumentation_->MethodExitEvent(thread_, instrumentation_frame.this_object_, m,
436 GetDexPc(), JValue());
437 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800438 frames_removed_++;
439 removed_stub = true;
440 break;
441 }
442 }
443 if (!removed_stub) {
444 if (kVerboseInstrumentation) {
445 LOG(INFO) << " No exit stub in " << DescribeLocation();
Ian Rogers306057f2012-11-26 12:45:53 -0800446 }
jeffhao725a9572012-11-13 18:20:12 -0800447 }
448 return true; // Continue.
449 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800450 Thread* const thread_;
Ian Rogers306057f2012-11-26 12:45:53 -0800451 const uintptr_t instrumentation_exit_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800452 Instrumentation* const instrumentation_;
453 std::deque<instrumentation::InstrumentationStackFrame>* const instrumentation_stack_;
454 size_t frames_removed_;
jeffhao725a9572012-11-13 18:20:12 -0800455 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800456 if (kVerboseInstrumentation) {
457 std::string thread_name;
458 thread->GetThreadName(thread_name);
459 LOG(INFO) << "Removing exit stubs in " << thread_name;
460 }
461 std::deque<instrumentation::InstrumentationStackFrame>* stack = thread->GetInstrumentationStack();
462 if (stack->size() > 0) {
463 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700464 uintptr_t instrumentation_exit_pc =
465 reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Ian Rogers62d6c772013-02-27 08:32:07 -0800466 RestoreStackVisitor visitor(thread, instrumentation_exit_pc, instrumentation);
467 visitor.WalkStack(true);
468 CHECK_EQ(visitor.frames_removed_, stack->size());
469 while (stack->size() > 0) {
470 stack->pop_front();
471 }
jeffhao725a9572012-11-13 18:20:12 -0800472 }
473}
474
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200475static bool HasEvent(Instrumentation::InstrumentationEvent expected, uint32_t events) {
476 return (events & expected) != 0;
477}
478
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000479static void PotentiallyAddListenerTo(Instrumentation::InstrumentationEvent event,
480 uint32_t events,
481 std::list<InstrumentationListener*>& list,
482 InstrumentationListener* listener,
483 bool* has_listener)
484 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) {
485 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
486 if (!HasEvent(event, events)) {
487 return;
488 }
489 // If there is a free slot in the list, we insert the listener in that slot.
490 // Otherwise we add it to the end of the list.
491 auto it = std::find(list.begin(), list.end(), nullptr);
492 if (it != list.end()) {
493 *it = listener;
494 } else {
495 list.push_back(listener);
496 }
497 *has_listener = true;
498}
499
Ian Rogers62d6c772013-02-27 08:32:07 -0800500void Instrumentation::AddListener(InstrumentationListener* listener, uint32_t events) {
501 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000502 PotentiallyAddListenerTo(kMethodEntered,
503 events,
504 method_entry_listeners_,
505 listener,
506 &have_method_entry_listeners_);
507 PotentiallyAddListenerTo(kMethodExited,
508 events,
509 method_exit_listeners_,
510 listener,
511 &have_method_exit_listeners_);
512 PotentiallyAddListenerTo(kMethodUnwind,
513 events,
514 method_unwind_listeners_,
515 listener,
516 &have_method_unwind_listeners_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000517 PotentiallyAddListenerTo(kBranch,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000518 events,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000519 branch_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000520 listener,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000521 &have_branch_listeners_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000522 PotentiallyAddListenerTo(kInvokeVirtualOrInterface,
523 events,
524 invoke_virtual_or_interface_listeners_,
525 listener,
526 &have_invoke_virtual_or_interface_listeners_);
527 PotentiallyAddListenerTo(kDexPcMoved,
528 events,
529 dex_pc_listeners_,
530 listener,
531 &have_dex_pc_listeners_);
532 PotentiallyAddListenerTo(kFieldRead,
533 events,
534 field_read_listeners_,
535 listener,
536 &have_field_read_listeners_);
537 PotentiallyAddListenerTo(kFieldWritten,
538 events,
539 field_write_listeners_,
540 listener,
541 &have_field_write_listeners_);
Alex Light6e1607e2017-08-23 10:06:18 -0700542 PotentiallyAddListenerTo(kExceptionThrown,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000543 events,
Alex Light6e1607e2017-08-23 10:06:18 -0700544 exception_thrown_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000545 listener,
Alex Light6e1607e2017-08-23 10:06:18 -0700546 &have_exception_thrown_listeners_);
Alex Lighte814f9d2017-07-31 16:14:39 -0700547 PotentiallyAddListenerTo(kWatchedFramePop,
548 events,
549 watched_frame_pop_listeners_,
550 listener,
551 &have_watched_frame_pop_listeners_);
Alex Light9fb1ab12017-09-05 09:32:49 -0700552 PotentiallyAddListenerTo(kExceptionHandled,
553 events,
554 exception_handled_listeners_,
555 listener,
556 &have_exception_handled_listeners_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200557 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800558}
559
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000560static void PotentiallyRemoveListenerFrom(Instrumentation::InstrumentationEvent event,
561 uint32_t events,
562 std::list<InstrumentationListener*>& list,
563 InstrumentationListener* listener,
564 bool* has_listener)
565 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) {
566 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
567 if (!HasEvent(event, events)) {
568 return;
569 }
570 auto it = std::find(list.begin(), list.end(), listener);
571 if (it != list.end()) {
572 // Just update the entry, do not remove from the list. Removing entries in the list
573 // is unsafe when mutators are iterating over it.
574 *it = nullptr;
575 }
576
577 // Check if the list contains any non-null listener, and update 'has_listener'.
578 for (InstrumentationListener* l : list) {
579 if (l != nullptr) {
580 *has_listener = true;
581 return;
582 }
583 }
584 *has_listener = false;
585}
586
Ian Rogers62d6c772013-02-27 08:32:07 -0800587void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) {
588 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000589 PotentiallyRemoveListenerFrom(kMethodEntered,
590 events,
591 method_entry_listeners_,
592 listener,
593 &have_method_entry_listeners_);
594 PotentiallyRemoveListenerFrom(kMethodExited,
595 events,
596 method_exit_listeners_,
597 listener,
598 &have_method_exit_listeners_);
599 PotentiallyRemoveListenerFrom(kMethodUnwind,
600 events,
601 method_unwind_listeners_,
602 listener,
603 &have_method_unwind_listeners_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000604 PotentiallyRemoveListenerFrom(kBranch,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000605 events,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000606 branch_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000607 listener,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000608 &have_branch_listeners_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000609 PotentiallyRemoveListenerFrom(kInvokeVirtualOrInterface,
610 events,
611 invoke_virtual_or_interface_listeners_,
612 listener,
613 &have_invoke_virtual_or_interface_listeners_);
614 PotentiallyRemoveListenerFrom(kDexPcMoved,
615 events,
616 dex_pc_listeners_,
617 listener,
618 &have_dex_pc_listeners_);
619 PotentiallyRemoveListenerFrom(kFieldRead,
620 events,
621 field_read_listeners_,
622 listener,
623 &have_field_read_listeners_);
624 PotentiallyRemoveListenerFrom(kFieldWritten,
625 events,
626 field_write_listeners_,
627 listener,
628 &have_field_write_listeners_);
Alex Light6e1607e2017-08-23 10:06:18 -0700629 PotentiallyRemoveListenerFrom(kExceptionThrown,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000630 events,
Alex Light6e1607e2017-08-23 10:06:18 -0700631 exception_thrown_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000632 listener,
Alex Light6e1607e2017-08-23 10:06:18 -0700633 &have_exception_thrown_listeners_);
Alex Lighte814f9d2017-07-31 16:14:39 -0700634 PotentiallyRemoveListenerFrom(kWatchedFramePop,
635 events,
636 watched_frame_pop_listeners_,
637 listener,
638 &have_watched_frame_pop_listeners_);
Alex Light9fb1ab12017-09-05 09:32:49 -0700639 PotentiallyRemoveListenerFrom(kExceptionHandled,
640 events,
641 exception_handled_listeners_,
642 listener,
643 &have_exception_handled_listeners_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200644 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800645}
646
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200647Instrumentation::InstrumentationLevel Instrumentation::GetCurrentInstrumentationLevel() const {
Alex Light4ba388a2017-01-27 10:26:49 -0800648 if (interpreter_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200649 return InstrumentationLevel::kInstrumentWithInterpreter;
Ian Rogers62d6c772013-02-27 08:32:07 -0800650 } else if (entry_exit_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200651 return InstrumentationLevel::kInstrumentWithInstrumentationStubs;
Ian Rogers62d6c772013-02-27 08:32:07 -0800652 } else {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200653 return InstrumentationLevel::kInstrumentNothing;
Ian Rogers62d6c772013-02-27 08:32:07 -0800654 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200655}
656
Alex Lightdba61482016-12-21 08:20:29 -0800657bool Instrumentation::RequiresInstrumentationInstallation(InstrumentationLevel new_level) const {
Alex Light4ba388a2017-01-27 10:26:49 -0800658 // We need to reinstall instrumentation if we go to a different level.
659 return GetCurrentInstrumentationLevel() != new_level;
Alex Lightdba61482016-12-21 08:20:29 -0800660}
661
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200662void Instrumentation::ConfigureStubs(const char* key, InstrumentationLevel desired_level) {
663 // Store the instrumentation level for this key or remove it.
664 if (desired_level == InstrumentationLevel::kInstrumentNothing) {
665 // The client no longer needs instrumentation.
666 requested_instrumentation_levels_.erase(key);
667 } else {
668 // The client needs instrumentation.
669 requested_instrumentation_levels_.Overwrite(key, desired_level);
670 }
671
672 // Look for the highest required instrumentation level.
673 InstrumentationLevel requested_level = InstrumentationLevel::kInstrumentNothing;
674 for (const auto& v : requested_instrumentation_levels_) {
675 requested_level = std::max(requested_level, v.second);
676 }
677
678 interpret_only_ = (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) ||
679 forced_interpret_only_;
680
Alex Lightdba61482016-12-21 08:20:29 -0800681 if (!RequiresInstrumentationInstallation(requested_level)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800682 // We're already set.
683 return;
684 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100685 Thread* const self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -0800686 Runtime* runtime = Runtime::Current();
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100687 Locks::mutator_lock_->AssertExclusiveHeld(self);
Ian Rogers62d6c772013-02-27 08:32:07 -0800688 Locks::thread_list_lock_->AssertNotHeld(self);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200689 if (requested_level > InstrumentationLevel::kInstrumentNothing) {
Alex Light4ba388a2017-01-27 10:26:49 -0800690 if (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800691 interpreter_stubs_installed_ = true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800692 entry_exit_stubs_installed_ = true;
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200693 } else {
694 CHECK_EQ(requested_level, InstrumentationLevel::kInstrumentWithInstrumentationStubs);
695 entry_exit_stubs_installed_ = true;
696 interpreter_stubs_installed_ = false;
Ian Rogers62d6c772013-02-27 08:32:07 -0800697 }
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700698 InstallStubsClassVisitor visitor(this);
699 runtime->GetClassLinker()->VisitClasses(&visitor);
Ian Rogers62d6c772013-02-27 08:32:07 -0800700 instrumentation_stubs_installed_ = true;
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100701 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800702 runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this);
703 } else {
704 interpreter_stubs_installed_ = false;
705 entry_exit_stubs_installed_ = false;
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700706 InstallStubsClassVisitor visitor(this);
707 runtime->GetClassLinker()->VisitClasses(&visitor);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100708 // Restore stack only if there is no method currently deoptimized.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700709 bool empty;
710 {
711 ReaderMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700712 empty = IsDeoptimizedMethodsEmpty(); // Avoid lock violation.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700713 }
714 if (empty) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100715 MutexLock mu(self, *Locks::thread_list_lock_);
716 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000717 // Only do this after restoring, as walking the stack when restoring will see
718 // the instrumentation exit pc.
719 instrumentation_stubs_installed_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100720 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800721 }
jeffhao725a9572012-11-13 18:20:12 -0800722}
723
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200724static void ResetQuickAllocEntryPointsForThread(Thread* thread, void* arg ATTRIBUTE_UNUSED) {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800725 thread->ResetQuickAllocEntryPointsForThread(kUseReadBarrier && thread->GetIsGcMarking());
Ian Rogersfa824272013-11-05 16:12:57 -0800726}
727
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700728void Instrumentation::SetEntrypointsInstrumented(bool instrumented) {
729 Thread* self = Thread::Current();
Mathieu Chartier661974a2014-01-09 11:23:53 -0800730 Runtime* runtime = Runtime::Current();
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700731 Locks::mutator_lock_->AssertNotHeld(self);
732 Locks::instrument_entrypoints_lock_->AssertHeld(self);
733 if (runtime->IsStarted()) {
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700734 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700735 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
Mathieu Chartier661974a2014-01-09 11:23:53 -0800736 SetQuickAllocEntryPointsInstrumented(instrumented);
737 ResetQuickAllocEntryPoints();
Mathieu Chartier50e93312016-03-16 11:25:29 -0700738 alloc_entrypoints_instrumented_ = instrumented;
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700739 } else {
740 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
741 SetQuickAllocEntryPointsInstrumented(instrumented);
Andreas Gampe157c77e2016-10-17 17:44:41 -0700742
743 // Note: ResetQuickAllocEntryPoints only works when the runtime is started. Manually run the
744 // update for just this thread.
Andreas Gampe162ae502016-10-18 10:03:42 -0700745 // Note: self may be null. One of those paths is setting instrumentation in the Heap
746 // constructor for gcstress mode.
747 if (self != nullptr) {
748 ResetQuickAllocEntryPointsForThread(self, nullptr);
749 }
Andreas Gampe157c77e2016-10-17 17:44:41 -0700750
Mathieu Chartier50e93312016-03-16 11:25:29 -0700751 alloc_entrypoints_instrumented_ = instrumented;
Mathieu Chartier661974a2014-01-09 11:23:53 -0800752 }
753}
754
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700755void Instrumentation::InstrumentQuickAllocEntryPoints() {
756 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
757 InstrumentQuickAllocEntryPointsLocked();
Ian Rogersfa824272013-11-05 16:12:57 -0800758}
759
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700760void Instrumentation::UninstrumentQuickAllocEntryPoints() {
761 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
762 UninstrumentQuickAllocEntryPointsLocked();
763}
764
765void Instrumentation::InstrumentQuickAllocEntryPointsLocked() {
766 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
767 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
768 SetEntrypointsInstrumented(true);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800769 }
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700770 ++quick_alloc_entry_points_instrumentation_counter_;
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700771}
772
773void Instrumentation::UninstrumentQuickAllocEntryPointsLocked() {
774 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
775 CHECK_GT(quick_alloc_entry_points_instrumentation_counter_, 0U);
776 --quick_alloc_entry_points_instrumentation_counter_;
777 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
778 SetEntrypointsInstrumented(false);
779 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800780}
781
782void Instrumentation::ResetQuickAllocEntryPoints() {
783 Runtime* runtime = Runtime::Current();
784 if (runtime->IsStarted()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800785 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700786 runtime->GetThreadList()->ForEach(ResetQuickAllocEntryPointsForThread, nullptr);
Ian Rogersfa824272013-11-05 16:12:57 -0800787 }
788}
789
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700790void Instrumentation::UpdateMethodsCodeImpl(ArtMethod* method, const void* quick_code) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800791 const void* new_quick_code;
Ian Rogers62d6c772013-02-27 08:32:07 -0800792 if (LIKELY(!instrumentation_stubs_installed_)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800793 new_quick_code = quick_code;
Jeff Hao65d15d92013-07-16 16:39:33 -0700794 } else {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100795 if ((interpreter_stubs_installed_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800796 new_quick_code = GetQuickToInterpreterBridge();
Jeff Hao65d15d92013-07-16 16:39:33 -0700797 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700798 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700799 if (class_linker->IsQuickResolutionStub(quick_code) ||
800 class_linker->IsQuickToInterpreterBridge(quick_code)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700801 new_quick_code = quick_code;
Alex Light6cae5ea2018-06-07 17:07:02 -0700802 } else if (entry_exit_stubs_installed_ &&
803 // We need to make sure not to replace anything that InstallStubsForMethod
804 // wouldn't. Specifically we cannot stub out Proxy.<init> since subtypes copy the
805 // implementation directly and this will confuse the instrumentation trampolines.
806 // TODO We should remove the need for this since it makes it impossible to profile
807 // Proxy.<init> correctly in all cases.
808 method != jni::DecodeArtMethod(WellKnownClasses::java_lang_reflect_Proxy_init)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700809 new_quick_code = GetQuickInstrumentationEntryPoint();
Alex Light2d441b12018-06-08 15:33:21 -0700810 if (!method->IsNative() && Runtime::Current()->GetJit() != nullptr) {
811 // Native methods use trampoline entrypoints during interpreter tracing.
812 DCHECK(!Runtime::Current()->GetJit()->GetCodeCache()->GetGarbageCollectCode());
813 ProfilingInfo* profiling_info = method->GetProfilingInfo(kRuntimePointerSize);
814 // Tracing will look at the saved entry point in the profiling info to know the actual
815 // entrypoint, so we store it here.
816 if (profiling_info != nullptr) {
817 profiling_info->SetSavedEntryPoint(quick_code);
818 }
819 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700820 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700821 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700822 }
Jeff Hao65d15d92013-07-16 16:39:33 -0700823 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800824 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800825 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100826}
827
Nicolas Geoffraya6e0e7d2018-01-26 13:16:50 +0000828void Instrumentation::UpdateNativeMethodsCodeToJitCode(ArtMethod* method, const void* quick_code) {
829 // We don't do any read barrier on `method`'s declaring class in this code, as the JIT might
830 // enter here on a soon-to-be deleted ArtMethod. Updating the entrypoint is OK though, as
831 // the ArtMethod is still in memory.
832 const void* new_quick_code = quick_code;
833 if (UNLIKELY(instrumentation_stubs_installed_) && entry_exit_stubs_installed_) {
834 new_quick_code = GetQuickInstrumentationEntryPoint();
835 }
836 UpdateEntrypoints(method, new_quick_code);
837}
838
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700839void Instrumentation::UpdateMethodsCode(ArtMethod* method, const void* quick_code) {
840 DCHECK(method->GetDeclaringClass()->IsResolved());
841 UpdateMethodsCodeImpl(method, quick_code);
842}
843
Alex Light0a5ec3d2017-07-25 16:50:26 -0700844void Instrumentation::UpdateMethodsCodeToInterpreterEntryPoint(ArtMethod* method) {
845 UpdateMethodsCodeImpl(method, GetQuickToInterpreterBridge());
846}
847
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000848void Instrumentation::UpdateMethodsCodeForJavaDebuggable(ArtMethod* method,
849 const void* quick_code) {
850 // When the runtime is set to Java debuggable, we may update the entry points of
851 // all methods of a class to the interpreter bridge. A method's declaring class
852 // might not be in resolved state yet in that case, so we bypass the DCHECK in
853 // UpdateMethodsCode.
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700854 UpdateMethodsCodeImpl(method, quick_code);
855}
856
Mathieu Chartiere401d142015-04-22 13:56:20 -0700857bool Instrumentation::AddDeoptimizedMethod(ArtMethod* method) {
858 if (IsDeoptimizedMethod(method)) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700859 // Already in the map. Return.
860 return false;
861 }
862 // Not found. Add it.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700863 deoptimized_methods_.insert(method);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700864 return true;
865}
866
Mathieu Chartiere401d142015-04-22 13:56:20 -0700867bool Instrumentation::IsDeoptimizedMethod(ArtMethod* method) {
868 return deoptimized_methods_.find(method) != deoptimized_methods_.end();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700869}
870
Mathieu Chartiere401d142015-04-22 13:56:20 -0700871ArtMethod* Instrumentation::BeginDeoptimizedMethod() {
872 if (deoptimized_methods_.empty()) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700873 // Empty.
874 return nullptr;
875 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700876 return *deoptimized_methods_.begin();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700877}
878
Mathieu Chartiere401d142015-04-22 13:56:20 -0700879bool Instrumentation::RemoveDeoptimizedMethod(ArtMethod* method) {
880 auto it = deoptimized_methods_.find(method);
881 if (it == deoptimized_methods_.end()) {
882 return false;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700883 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700884 deoptimized_methods_.erase(it);
885 return true;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700886}
887
888bool Instrumentation::IsDeoptimizedMethodsEmpty() const {
889 return deoptimized_methods_.empty();
890}
891
Mathieu Chartiere401d142015-04-22 13:56:20 -0700892void Instrumentation::Deoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100893 CHECK(!method->IsNative());
894 CHECK(!method->IsProxyMethod());
Alex Light9139e002015-10-09 15:59:48 -0700895 CHECK(method->IsInvokable());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100896
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700897 Thread* self = Thread::Current();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700898 {
899 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700900 bool has_not_been_deoptimized = AddDeoptimizedMethod(method);
David Sehr709b0702016-10-13 09:12:37 -0700901 CHECK(has_not_been_deoptimized) << "Method " << ArtMethod::PrettyMethod(method)
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200902 << " is already deoptimized";
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700903 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100904 if (!interpreter_stubs_installed_) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800905 UpdateEntrypoints(method, GetQuickInstrumentationEntryPoint());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100906
907 // Install instrumentation exit stub and instrumentation frames. We may already have installed
908 // these previously so it will only cover the newly created frames.
909 instrumentation_stubs_installed_ = true;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700910 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100911 Runtime::Current()->GetThreadList()->ForEach(InstrumentationInstallStack, this);
912 }
913}
914
Mathieu Chartiere401d142015-04-22 13:56:20 -0700915void Instrumentation::Undeoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100916 CHECK(!method->IsNative());
917 CHECK(!method->IsProxyMethod());
Alex Light9139e002015-10-09 15:59:48 -0700918 CHECK(method->IsInvokable());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100919
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700920 Thread* self = Thread::Current();
921 bool empty;
922 {
923 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700924 bool found_and_erased = RemoveDeoptimizedMethod(method);
David Sehr709b0702016-10-13 09:12:37 -0700925 CHECK(found_and_erased) << "Method " << ArtMethod::PrettyMethod(method)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700926 << " is not deoptimized";
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700927 empty = IsDeoptimizedMethodsEmpty();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700928 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100929
930 // Restore code and possibly stack only if we did not deoptimize everything.
931 if (!interpreter_stubs_installed_) {
932 // Restore its code or resolution trampoline.
933 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800934 if (method->IsStatic() && !method->IsConstructor() &&
935 !method->GetDeclaringClass()->IsInitialized()) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800936 UpdateEntrypoints(method, GetQuickResolutionStub());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100937 } else {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000938 const void* quick_code = NeedDebugVersionFor(method)
939 ? GetQuickToInterpreterBridge()
Alex Lightfc49fec2018-01-16 22:28:36 +0000940 : class_linker->GetQuickOatCodeFor(method);
Elliott Hughes956af0f2014-12-11 14:34:28 -0800941 UpdateEntrypoints(method, quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100942 }
943
944 // If there is no deoptimized method left, we can restore the stack of each thread.
Alex Lightf244a572018-06-08 13:56:51 -0700945 if (empty && !entry_exit_stubs_installed_) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700946 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100947 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
948 instrumentation_stubs_installed_ = false;
949 }
950 }
951}
952
Mathieu Chartiere401d142015-04-22 13:56:20 -0700953bool Instrumentation::IsDeoptimized(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100954 DCHECK(method != nullptr);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700955 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700956 return IsDeoptimizedMethod(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100957}
958
959void Instrumentation::EnableDeoptimization() {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700960 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700961 CHECK(IsDeoptimizedMethodsEmpty());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100962 CHECK_EQ(deoptimization_enabled_, false);
963 deoptimization_enabled_ = true;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100964}
965
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200966void Instrumentation::DisableDeoptimization(const char* key) {
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100967 CHECK_EQ(deoptimization_enabled_, true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100968 // If we deoptimized everything, undo it.
Alex Lightdba61482016-12-21 08:20:29 -0800969 InstrumentationLevel level = GetCurrentInstrumentationLevel();
970 if (level == InstrumentationLevel::kInstrumentWithInterpreter) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200971 UndeoptimizeEverything(key);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100972 }
973 // Undeoptimized selected methods.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700974 while (true) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700975 ArtMethod* method;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700976 {
977 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700978 if (IsDeoptimizedMethodsEmpty()) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700979 break;
980 }
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700981 method = BeginDeoptimizedMethod();
982 CHECK(method != nullptr);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700983 }
984 Undeoptimize(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100985 }
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100986 deoptimization_enabled_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100987}
988
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100989// Indicates if instrumentation should notify method enter/exit events to the listeners.
990bool Instrumentation::ShouldNotifyMethodEnterExitEvents() const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200991 if (!HasMethodEntryListeners() && !HasMethodExitListeners()) {
992 return false;
993 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100994 return !deoptimization_enabled_ && !interpreter_stubs_installed_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100995}
996
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200997void Instrumentation::DeoptimizeEverything(const char* key) {
998 CHECK(deoptimization_enabled_);
999 ConfigureStubs(key, InstrumentationLevel::kInstrumentWithInterpreter);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001000}
1001
Sebastien Hertz0462c4c2015-04-01 16:34:17 +02001002void Instrumentation::UndeoptimizeEverything(const char* key) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001003 CHECK(interpreter_stubs_installed_);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +02001004 CHECK(deoptimization_enabled_);
1005 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001006}
1007
Sebastien Hertz0462c4c2015-04-01 16:34:17 +02001008void Instrumentation::EnableMethodTracing(const char* key, bool needs_interpreter) {
1009 InstrumentationLevel level;
1010 if (needs_interpreter) {
1011 level = InstrumentationLevel::kInstrumentWithInterpreter;
1012 } else {
1013 level = InstrumentationLevel::kInstrumentWithInstrumentationStubs;
Alex Light2d441b12018-06-08 15:33:21 -07001014 if (Runtime::Current()->GetJit() != nullptr) {
1015 // TODO b/110263880 It would be better if we didn't need to do this.
1016 // Since we need to hold the method entrypoint across a suspend to ensure instrumentation
1017 // hooks are called correctly we have to disable jit-gc to ensure that the entrypoint doesn't
1018 // go away. Furthermore we need to leave this off permanently since one could get the same
1019 // effect by causing this to be toggled on and off.
1020 Runtime::Current()->GetJit()->GetCodeCache()->SetGarbageCollectCode(false);
1021 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +02001022 }
1023 ConfigureStubs(key, level);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001024}
1025
Sebastien Hertz0462c4c2015-04-01 16:34:17 +02001026void Instrumentation::DisableMethodTracing(const char* key) {
1027 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
jeffhao725a9572012-11-13 18:20:12 -08001028}
1029
Alex Light2d441b12018-06-08 15:33:21 -07001030const void* Instrumentation::GetCodeForInvoke(ArtMethod* method) const {
1031 // This is called by instrumentation entry only and that should never be getting proxy methods.
1032 DCHECK(!method->IsProxyMethod()) << method->PrettyMethod();
1033 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1034 if (LIKELY(!instrumentation_stubs_installed_ && !interpreter_stubs_installed_)) {
1035 // In general we just return whatever the method thinks its entrypoint is here. The only
1036 // exception is if it still has the instrumentation entrypoint. That means we are racing another
1037 // thread getting rid of instrumentation which is unexpected but possible. In that case we want
1038 // to wait and try to get it from the oat file or jit.
1039 const void* code = method->GetEntryPointFromQuickCompiledCodePtrSize(kRuntimePointerSize);
1040 DCHECK(code != nullptr);
1041 if (code != GetQuickInstrumentationEntryPoint()) {
1042 return code;
1043 } else if (method->IsNative()) {
1044 return class_linker->GetQuickOatCodeFor(method);
1045 }
1046 // We don't know what it is. Fallthough to try to find the code from the JIT or Oat file.
1047 } else if (method->IsNative()) {
1048 // TODO We could have JIT compiled native entrypoints. It might be worth it to find these.
1049 return class_linker->GetQuickOatCodeFor(method);
1050 } else if (UNLIKELY(interpreter_stubs_installed_)) {
1051 return GetQuickToInterpreterBridge();
1052 }
1053 // Since the method cannot be native due to ifs above we can always fall back to interpreter
1054 // bridge.
1055 const void* result = GetQuickToInterpreterBridge();
1056 if (!NeedDebugVersionFor(method)) {
1057 // If we don't need a debug version we should see what the oat file/class linker has to say.
1058 result = class_linker->GetQuickOatCodeFor(method);
1059 }
1060 // If both those fail try the jit.
1061 if (result == GetQuickToInterpreterBridge()) {
1062 jit::Jit* jit = Runtime::Current()->GetJit();
1063 if (jit != nullptr) {
1064 const void* res = jit->GetCodeCache()->FindCompiledCodeForInstrumentation(method);
1065 if (res != nullptr) {
1066 result = res;
1067 }
1068 }
1069 }
1070 return result;
1071}
1072
Andreas Gampe542451c2016-07-26 09:02:02 -07001073const void* Instrumentation::GetQuickCodeFor(ArtMethod* method, PointerSize pointer_size) const {
Vladimir Marko97d7e1c2016-10-04 14:44:28 +01001074 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers62d6c772013-02-27 08:32:07 -08001075 if (LIKELY(!instrumentation_stubs_installed_)) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001076 const void* code = method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Vladimir Marko8a630572014-04-09 18:45:35 +01001077 DCHECK(code != nullptr);
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001078 if (LIKELY(!class_linker->IsQuickResolutionStub(code) &&
1079 !class_linker->IsQuickToInterpreterBridge(code)) &&
1080 !class_linker->IsQuickResolutionStub(code) &&
1081 !class_linker->IsQuickToInterpreterBridge(code)) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001082 return code;
1083 }
1084 }
Alex Lightfc49fec2018-01-16 22:28:36 +00001085 return class_linker->GetQuickOatCodeFor(method);
jeffhao725a9572012-11-13 18:20:12 -08001086}
1087
Alex Lightd7661582017-05-01 13:48:16 -07001088void Instrumentation::MethodEnterEventImpl(Thread* thread,
1089 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001090 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -08001091 uint32_t dex_pc) const {
Mingyao Yang2ee17902017-08-30 11:37:08 -07001092 DCHECK(!method->IsRuntimeMethod());
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001093 if (HasMethodEntryListeners()) {
Alex Lightd7661582017-05-01 13:48:16 -07001094 Thread* self = Thread::Current();
1095 StackHandleScope<1> hs(self);
1096 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001097 for (InstrumentationListener* listener : method_entry_listeners_) {
1098 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001099 listener->MethodEntered(thread, thiz, method, dex_pc);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001100 }
1101 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001102 }
1103}
1104
Alex Lightd7661582017-05-01 13:48:16 -07001105void Instrumentation::MethodExitEventImpl(Thread* thread,
1106 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001107 ArtMethod* method,
Alex Lightd7661582017-05-01 13:48:16 -07001108 uint32_t dex_pc,
1109 const JValue& return_value) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001110 if (HasMethodExitListeners()) {
Alex Lightd7661582017-05-01 13:48:16 -07001111 Thread* self = Thread::Current();
1112 StackHandleScope<2> hs(self);
1113 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
1114 if (method->GetInterfaceMethodIfProxy(kRuntimePointerSize)
1115 ->GetReturnTypePrimitive() != Primitive::kPrimNot) {
1116 for (InstrumentationListener* listener : method_exit_listeners_) {
1117 if (listener != nullptr) {
1118 listener->MethodExited(thread, thiz, method, dex_pc, return_value);
1119 }
1120 }
1121 } else {
1122 Handle<mirror::Object> ret(hs.NewHandle(return_value.GetL()));
1123 for (InstrumentationListener* listener : method_exit_listeners_) {
1124 if (listener != nullptr) {
1125 listener->MethodExited(thread, thiz, method, dex_pc, ret);
1126 }
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001127 }
1128 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001129 }
1130}
1131
Alex Lightd7661582017-05-01 13:48:16 -07001132void Instrumentation::MethodUnwindEvent(Thread* thread,
1133 mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001134 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -08001135 uint32_t dex_pc) const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +02001136 if (HasMethodUnwindListeners()) {
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));
Mathieu Chartier02e25112013-08-14 16:14:24 -07001140 for (InstrumentationListener* listener : method_unwind_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001141 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001142 listener->MethodUnwind(thread, thiz, method, dex_pc);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001143 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001144 }
1145 }
1146}
1147
Alex Lightd7661582017-05-01 13:48:16 -07001148void Instrumentation::DexPcMovedEventImpl(Thread* thread,
1149 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001150 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -08001151 uint32_t dex_pc) const {
Alex Lightd7661582017-05-01 13:48:16 -07001152 Thread* self = Thread::Current();
1153 StackHandleScope<1> hs(self);
1154 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001155 for (InstrumentationListener* listener : dex_pc_listeners_) {
1156 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001157 listener->DexPcMoved(thread, thiz, method, dex_pc);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001158 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001159 }
1160}
1161
Nicolas Geoffray81f0f952016-01-20 16:25:19 +00001162void Instrumentation::BranchImpl(Thread* thread,
1163 ArtMethod* method,
1164 uint32_t dex_pc,
1165 int32_t offset) const {
1166 for (InstrumentationListener* listener : branch_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001167 if (listener != nullptr) {
Nicolas Geoffray81f0f952016-01-20 16:25:19 +00001168 listener->Branch(thread, method, dex_pc, offset);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001169 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001170 }
1171}
1172
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001173void Instrumentation::InvokeVirtualOrInterfaceImpl(Thread* thread,
Alex Lightd7661582017-05-01 13:48:16 -07001174 ObjPtr<mirror::Object> this_object,
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001175 ArtMethod* caller,
1176 uint32_t dex_pc,
1177 ArtMethod* callee) const {
Alex Lightd7661582017-05-01 13:48:16 -07001178 Thread* self = Thread::Current();
1179 StackHandleScope<1> hs(self);
1180 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001181 for (InstrumentationListener* listener : invoke_virtual_or_interface_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001182 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001183 listener->InvokeVirtualOrInterface(thread, thiz, caller, dex_pc, callee);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001184 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001185 }
1186}
1187
Alex Lighte814f9d2017-07-31 16:14:39 -07001188void Instrumentation::WatchedFramePopImpl(Thread* thread, const ShadowFrame& frame) const {
1189 for (InstrumentationListener* listener : watched_frame_pop_listeners_) {
1190 if (listener != nullptr) {
1191 listener->WatchedFramePop(thread, frame);
1192 }
1193 }
1194}
1195
Alex Lightd7661582017-05-01 13:48:16 -07001196void Instrumentation::FieldReadEventImpl(Thread* thread,
1197 ObjPtr<mirror::Object> this_object,
1198 ArtMethod* method,
1199 uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -07001200 ArtField* field) const {
Alex Lightd7661582017-05-01 13:48:16 -07001201 Thread* self = Thread::Current();
1202 StackHandleScope<1> hs(self);
1203 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001204 for (InstrumentationListener* listener : field_read_listeners_) {
1205 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001206 listener->FieldRead(thread, thiz, method, dex_pc, field);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001207 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001208 }
1209}
1210
Alex Lightd7661582017-05-01 13:48:16 -07001211void Instrumentation::FieldWriteEventImpl(Thread* thread,
1212 ObjPtr<mirror::Object> this_object,
1213 ArtMethod* method,
1214 uint32_t dex_pc,
1215 ArtField* field,
1216 const JValue& field_value) const {
1217 Thread* self = Thread::Current();
1218 StackHandleScope<2> hs(self);
1219 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
1220 if (field->IsPrimitiveType()) {
1221 for (InstrumentationListener* listener : field_write_listeners_) {
1222 if (listener != nullptr) {
1223 listener->FieldWritten(thread, thiz, method, dex_pc, field, field_value);
1224 }
1225 }
1226 } else {
1227 Handle<mirror::Object> val(hs.NewHandle(field_value.GetL()));
1228 for (InstrumentationListener* listener : field_write_listeners_) {
1229 if (listener != nullptr) {
1230 listener->FieldWritten(thread, thiz, method, dex_pc, field, val);
1231 }
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001232 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001233 }
1234}
1235
Alex Light6e1607e2017-08-23 10:06:18 -07001236void Instrumentation::ExceptionThrownEvent(Thread* thread,
Sebastien Hertz947ff082013-09-17 14:10:13 +02001237 mirror::Throwable* exception_object) const {
Hiroshi Yamauchi3481f7a2017-02-10 12:07:36 -08001238 Thread* self = Thread::Current();
1239 StackHandleScope<1> hs(self);
1240 Handle<mirror::Throwable> h_exception(hs.NewHandle(exception_object));
Alex Light6e1607e2017-08-23 10:06:18 -07001241 if (HasExceptionThrownListeners()) {
Hiroshi Yamauchi3481f7a2017-02-10 12:07:36 -08001242 DCHECK_EQ(thread->GetException(), h_exception.Get());
Jeff Haoc0bd4da2013-04-11 15:52:28 -07001243 thread->ClearException();
Alex Light6e1607e2017-08-23 10:06:18 -07001244 for (InstrumentationListener* listener : exception_thrown_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001245 if (listener != nullptr) {
Alex Light6e1607e2017-08-23 10:06:18 -07001246 listener->ExceptionThrown(thread, h_exception);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001247 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001248 }
Alex Light9fb1ab12017-09-05 09:32:49 -07001249 // See b/65049545 for discussion about this behavior.
1250 thread->AssertNoPendingException();
Hiroshi Yamauchi3481f7a2017-02-10 12:07:36 -08001251 thread->SetException(h_exception.Get());
Ian Rogers62d6c772013-02-27 08:32:07 -08001252 }
1253}
1254
Alex Light9fb1ab12017-09-05 09:32:49 -07001255void Instrumentation::ExceptionHandledEvent(Thread* thread,
1256 mirror::Throwable* exception_object) const {
1257 Thread* self = Thread::Current();
1258 StackHandleScope<1> hs(self);
1259 Handle<mirror::Throwable> h_exception(hs.NewHandle(exception_object));
1260 if (HasExceptionHandledListeners()) {
1261 // We should have cleared the exception so that callers can detect a new one.
1262 DCHECK(thread->GetException() == nullptr);
1263 for (InstrumentationListener* listener : exception_handled_listeners_) {
1264 if (listener != nullptr) {
1265 listener->ExceptionHandled(thread, h_exception);
1266 }
1267 }
1268 }
1269}
1270
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +00001271// Computes a frame ID by ignoring inlined frames.
1272size_t Instrumentation::ComputeFrameId(Thread* self,
1273 size_t frame_depth,
1274 size_t inlined_frames_before_frame) {
1275 CHECK_GE(frame_depth, inlined_frames_before_frame);
1276 size_t no_inline_depth = frame_depth - inlined_frames_before_frame;
1277 return StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) - no_inline_depth;
1278}
1279
Ian Rogers62d6c772013-02-27 08:32:07 -08001280static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame,
1281 int delta)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001282 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +01001283 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) + delta;
Ian Rogers62d6c772013-02-27 08:32:07 -08001284 if (frame_id != instrumentation_frame.frame_id_) {
1285 LOG(ERROR) << "Expected frame_id=" << frame_id << " but found "
1286 << instrumentation_frame.frame_id_;
1287 StackVisitor::DescribeStack(self);
1288 CHECK_EQ(frame_id, instrumentation_frame.frame_id_);
1289 }
1290}
1291
1292void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001293 ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -07001294 uintptr_t lr, bool interpreter_entry) {
Alex Lightb7edcda2017-04-27 13:20:31 -07001295 DCHECK(!self->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -08001296 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1297 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001298 LOG(INFO) << "Entering " << ArtMethod::PrettyMethod(method) << " from PC "
1299 << reinterpret_cast<void*>(lr);
Ian Rogers62d6c772013-02-27 08:32:07 -08001300 }
Alex Lightb7edcda2017-04-27 13:20:31 -07001301
1302 // We send the enter event before pushing the instrumentation frame to make cleanup easier. If the
1303 // event causes an exception we can simply send the unwind event and return.
1304 StackHandleScope<1> hs(self);
1305 Handle<mirror::Object> h_this(hs.NewHandle(this_object));
1306 if (!interpreter_entry) {
1307 MethodEnterEvent(self, h_this.Get(), method, 0);
1308 if (self->IsExceptionPending()) {
1309 MethodUnwindEvent(self, h_this.Get(), method, 0);
1310 return;
1311 }
1312 }
1313
1314 // We have a callee-save frame meaning this value is guaranteed to never be 0.
1315 DCHECK(!self->IsExceptionPending());
1316 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk);
1317
1318 instrumentation::InstrumentationStackFrame instrumentation_frame(h_this.Get(), method, lr,
Jeff Hao9a916d32013-06-27 18:45:37 -07001319 frame_id, interpreter_entry);
Ian Rogers62d6c772013-02-27 08:32:07 -08001320 stack->push_front(instrumentation_frame);
Ian Rogers62d6c772013-02-27 08:32:07 -08001321}
1322
Mingyao Yang2ee17902017-08-30 11:37:08 -07001323DeoptimizationMethodType Instrumentation::GetDeoptimizationMethodType(ArtMethod* method) {
1324 if (method->IsRuntimeMethod()) {
1325 // Certain methods have strict requirement on whether the dex instruction
1326 // should be re-executed upon deoptimization.
1327 if (method == Runtime::Current()->GetCalleeSaveMethod(
1328 CalleeSaveType::kSaveEverythingForClinit)) {
1329 return DeoptimizationMethodType::kKeepDexPc;
1330 }
1331 if (method == Runtime::Current()->GetCalleeSaveMethod(
1332 CalleeSaveType::kSaveEverythingForSuspendCheck)) {
1333 return DeoptimizationMethodType::kKeepDexPc;
1334 }
1335 }
1336 return DeoptimizationMethodType::kDefault;
1337}
1338
1339// Try to get the shorty of a runtime method if it's an invocation stub.
1340struct RuntimeMethodShortyVisitor : public StackVisitor {
1341 explicit RuntimeMethodShortyVisitor(Thread* thread)
1342 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
1343 shorty('V') {}
1344
1345 bool VisitFrame() REQUIRES_SHARED(Locks::mutator_lock_) {
1346 ArtMethod* m = GetMethod();
1347 if (m != nullptr && !m->IsRuntimeMethod()) {
1348 // The first Java method.
1349 if (m->IsNative()) {
1350 // Use JNI method's shorty for the jni stub.
1351 shorty = m->GetShorty()[0];
1352 return false;
1353 }
1354 if (m->IsProxyMethod()) {
1355 // Proxy method just invokes its proxied method via
1356 // art_quick_proxy_invoke_handler.
1357 shorty = m->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty()[0];
1358 return false;
1359 }
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001360 const Instruction& instr = m->DexInstructions().InstructionAt(GetDexPc());
1361 if (instr.IsInvoke()) {
Mingyao Yang2ee17902017-08-30 11:37:08 -07001362 const DexFile* dex_file = m->GetDexFile();
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001363 if (interpreter::IsStringInit(dex_file, instr.VRegB())) {
Mingyao Yang2ee17902017-08-30 11:37:08 -07001364 // Invoking string init constructor is turned into invoking
1365 // StringFactory.newStringFromChars() which returns a string.
1366 shorty = 'L';
1367 return false;
1368 }
1369 // A regular invoke, use callee's shorty.
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001370 uint32_t method_idx = instr.VRegB();
Mingyao Yang2ee17902017-08-30 11:37:08 -07001371 shorty = dex_file->GetMethodShorty(method_idx)[0];
1372 }
1373 // Stop stack walking since we've seen a Java frame.
1374 return false;
1375 }
1376 return true;
1377 }
1378
1379 char shorty;
1380};
1381
Alex Lightb7edcda2017-04-27 13:20:31 -07001382TwoWordReturn Instrumentation::PopInstrumentationStackFrame(Thread* self,
1383 uintptr_t* return_pc,
1384 uint64_t* gpr_result,
1385 uint64_t* fpr_result) {
1386 DCHECK(gpr_result != nullptr);
1387 DCHECK(fpr_result != nullptr);
Ian Rogers62d6c772013-02-27 08:32:07 -08001388 // Do the pop.
1389 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1390 CHECK_GT(stack->size(), 0U);
1391 InstrumentationStackFrame instrumentation_frame = stack->front();
1392 stack->pop_front();
1393
1394 // Set return PC and check the sanity of the stack.
1395 *return_pc = instrumentation_frame.return_pc_;
1396 CheckStackDepth(self, instrumentation_frame, 0);
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001397 self->VerifyStack();
Ian Rogers62d6c772013-02-27 08:32:07 -08001398
Mathieu Chartiere401d142015-04-22 13:56:20 -07001399 ArtMethod* method = instrumentation_frame.method_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001400 uint32_t length;
Andreas Gampe542451c2016-07-26 09:02:02 -07001401 const PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mingyao Yang2ee17902017-08-30 11:37:08 -07001402 char return_shorty;
1403
1404 // Runtime method does not call into MethodExitEvent() so there should not be
1405 // suspension point below.
1406 ScopedAssertNoThreadSuspension ants(__FUNCTION__, method->IsRuntimeMethod());
1407 if (method->IsRuntimeMethod()) {
1408 if (method != Runtime::Current()->GetCalleeSaveMethod(
1409 CalleeSaveType::kSaveEverythingForClinit)) {
1410 // If the caller is at an invocation point and the runtime method is not
1411 // for clinit, we need to pass return results to the caller.
1412 // We need the correct shorty to decide whether we need to pass the return
1413 // result for deoptimization below.
1414 RuntimeMethodShortyVisitor visitor(self);
1415 visitor.WalkStack();
1416 return_shorty = visitor.shorty;
1417 } else {
1418 // Some runtime methods such as allocations, unresolved field getters, etc.
1419 // have return value. We don't need to set return_value since MethodExitEvent()
1420 // below isn't called for runtime methods. Deoptimization doesn't need the
1421 // value either since the dex instruction will be re-executed by the
1422 // interpreter, except these two cases:
1423 // (1) For an invoke, which is handled above to get the correct shorty.
1424 // (2) For MONITOR_ENTER/EXIT, which cannot be re-executed since it's not
1425 // idempotent. However there is no return value for it anyway.
1426 return_shorty = 'V';
1427 }
1428 } else {
1429 return_shorty = method->GetInterfaceMethodIfProxy(pointer_size)->GetShorty(&length)[0];
1430 }
1431
Alex Lightb7edcda2017-04-27 13:20:31 -07001432 bool is_ref = return_shorty == '[' || return_shorty == 'L';
1433 StackHandleScope<1> hs(self);
1434 MutableHandle<mirror::Object> res(hs.NewHandle<mirror::Object>(nullptr));
Ian Rogers62d6c772013-02-27 08:32:07 -08001435 JValue return_value;
1436 if (return_shorty == 'V') {
1437 return_value.SetJ(0);
1438 } else if (return_shorty == 'F' || return_shorty == 'D') {
Alex Lightb7edcda2017-04-27 13:20:31 -07001439 return_value.SetJ(*fpr_result);
Ian Rogers62d6c772013-02-27 08:32:07 -08001440 } else {
Alex Lightb7edcda2017-04-27 13:20:31 -07001441 return_value.SetJ(*gpr_result);
1442 }
1443 if (is_ref) {
1444 // Take a handle to the return value so we won't lose it if we suspend.
1445 res.Assign(return_value.GetL());
Ian Rogers62d6c772013-02-27 08:32:07 -08001446 }
1447 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1448 // return_pc.
Andreas Gampee2abbc62017-09-15 11:59:26 -07001449 uint32_t dex_pc = dex::kDexNoIndex;
Ian Rogers62d6c772013-02-27 08:32:07 -08001450 mirror::Object* this_object = instrumentation_frame.this_object_;
Mingyao Yang2ee17902017-08-30 11:37:08 -07001451 if (!method->IsRuntimeMethod() && !instrumentation_frame.interpreter_entry_) {
Sebastien Hertz320deb22014-06-11 19:45:05 +02001452 MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value);
1453 }
jeffhao725a9572012-11-13 18:20:12 -08001454
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001455 // Deoptimize if the caller needs to continue execution in the interpreter. Do nothing if we get
1456 // back to an upcall.
1457 NthCallerVisitor visitor(self, 1, true);
1458 visitor.WalkStack(true);
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001459 bool deoptimize = (visitor.caller != nullptr) &&
Daniel Mihalyieb076692014-08-22 17:33:31 +02001460 (interpreter_stubs_installed_ || IsDeoptimized(visitor.caller) ||
1461 Dbg::IsForcedInterpreterNeededForUpcall(self, visitor.caller));
Alex Lightb7edcda2017-04-27 13:20:31 -07001462 if (is_ref) {
1463 // Restore the return value if it's a reference since it might have moved.
1464 *reinterpret_cast<mirror::Object**>(gpr_result) = res.Get();
1465 }
Nicolas Geoffraya0619e22016-12-20 13:57:43 +00001466 if (deoptimize && Runtime::Current()->IsAsyncDeoptimizeable(*return_pc)) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001467 if (kVerboseInstrumentation) {
Andreas Gampe46ee31b2016-12-14 10:11:49 -08001468 LOG(INFO) << "Deoptimizing "
1469 << visitor.caller->PrettyMethod()
1470 << " by returning from "
1471 << method->PrettyMethod()
1472 << " with result "
1473 << std::hex << return_value.GetJ() << std::dec
1474 << " in "
1475 << *self;
Ian Rogers62d6c772013-02-27 08:32:07 -08001476 }
Mingyao Yang2ee17902017-08-30 11:37:08 -07001477 DeoptimizationMethodType deopt_method_type = GetDeoptimizationMethodType(method);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001478 self->PushDeoptimizationContext(return_value,
Mingyao Yang2ee17902017-08-30 11:37:08 -07001479 return_shorty == 'L' || return_shorty == '[',
1480 nullptr /* no pending exception */,
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001481 false /* from_code */,
Mingyao Yang2ee17902017-08-30 11:37:08 -07001482 deopt_method_type);
Andreas Gamped58342c2014-06-05 14:18:08 -07001483 return GetTwoWordSuccessValue(*return_pc,
1484 reinterpret_cast<uintptr_t>(GetQuickDeoptimizationEntryPoint()));
Ian Rogers62d6c772013-02-27 08:32:07 -08001485 } else {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +00001486 if (deoptimize && !Runtime::Current()->IsAsyncDeoptimizeable(*return_pc)) {
Alex Lightd8eb6732018-01-29 15:16:02 -08001487 VLOG(deopt) << "Got a deoptimization request on un-deoptimizable " << method->PrettyMethod()
1488 << " at PC " << reinterpret_cast<void*>(*return_pc);
Nicolas Geoffraya0619e22016-12-20 13:57:43 +00001489 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001490 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001491 LOG(INFO) << "Returning from " << method->PrettyMethod()
Brian Carlstrom2d888622013-07-18 17:02:00 -07001492 << " to PC " << reinterpret_cast<void*>(*return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001493 }
Andreas Gamped58342c2014-06-05 14:18:08 -07001494 return GetTwoWordSuccessValue(0, *return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001495 }
jeffhao725a9572012-11-13 18:20:12 -08001496}
1497
Mingyao Yangf711f2c2016-05-23 12:29:39 -07001498uintptr_t Instrumentation::PopMethodForUnwind(Thread* self, bool is_deoptimization) const {
Ian Rogers62d6c772013-02-27 08:32:07 -08001499 // Do the pop.
1500 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1501 CHECK_GT(stack->size(), 0U);
Alex Lightb7edcda2017-04-27 13:20:31 -07001502 size_t idx = stack->size();
Ian Rogers62d6c772013-02-27 08:32:07 -08001503 InstrumentationStackFrame instrumentation_frame = stack->front();
Ian Rogers62d6c772013-02-27 08:32:07 -08001504
Mathieu Chartiere401d142015-04-22 13:56:20 -07001505 ArtMethod* method = instrumentation_frame.method_;
Ian Rogers62d6c772013-02-27 08:32:07 -08001506 if (is_deoptimization) {
1507 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001508 LOG(INFO) << "Popping for deoptimization " << ArtMethod::PrettyMethod(method);
Ian Rogers62d6c772013-02-27 08:32:07 -08001509 }
1510 } else {
1511 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001512 LOG(INFO) << "Popping for unwind " << ArtMethod::PrettyMethod(method);
Ian Rogers62d6c772013-02-27 08:32:07 -08001513 }
1514
1515 // Notify listeners of method unwind.
1516 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1517 // return_pc.
Andreas Gampee2abbc62017-09-15 11:59:26 -07001518 uint32_t dex_pc = dex::kDexNoIndex;
Mingyao Yang2ee17902017-08-30 11:37:08 -07001519 if (!method->IsRuntimeMethod()) {
1520 MethodUnwindEvent(self, instrumentation_frame.this_object_, method, dex_pc);
1521 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001522 }
Alex Lightb7edcda2017-04-27 13:20:31 -07001523 // TODO: bring back CheckStackDepth(self, instrumentation_frame, 2);
1524 CHECK_EQ(stack->size(), idx);
1525 DCHECK(instrumentation_frame.method_ == stack->front().method_);
1526 stack->pop_front();
Mingyao Yangf711f2c2016-05-23 12:29:39 -07001527 return instrumentation_frame.return_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -08001528}
1529
1530std::string InstrumentationStackFrame::Dump() const {
1531 std::ostringstream os;
David Sehr709b0702016-10-13 09:12:37 -07001532 os << "Frame " << frame_id_ << " " << ArtMethod::PrettyMethod(method_) << ":"
Ian Rogers62d6c772013-02-27 08:32:07 -08001533 << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_);
1534 return os.str();
1535}
1536
1537} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -08001538} // namespace art