blob: e30fc9a84cfbcea11bf55101b65f10ab68f08949 [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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_INSTRUMENTATION_H_
18#define ART_RUNTIME_INSTRUMENTATION_H_
jeffhao725a9572012-11-13 18:20:12 -080019
Alex Lightb7c640d2019-03-20 15:52:13 -070020#include <functional>
Ian Rogers576ca0c2014-06-06 15:58:22 -070021#include <stdint.h>
Ian Rogers576ca0c2014-06-06 15:58:22 -070022#include <list>
Andreas Gampe7e56a072018-11-29 10:40:06 -080023#include <memory>
Mathieu Chartiere401d142015-04-22 13:56:20 -070024#include <unordered_set>
Alex Lightb7c640d2019-03-20 15:52:13 -070025#include <optional>
Ian Rogers576ca0c2014-06-06 15:58:22 -070026
Ian Rogersd582fa42014-11-05 23:46:43 -080027#include "arch/instruction_set.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070028#include "base/enums.h"
Andreas Gampe7e56a072018-11-29 10:40:06 -080029#include "base/locks.h"
Elliott Hughes76160052012-12-12 16:31:20 -080030#include "base/macros.h"
David Sehr67bf42e2018-02-26 16:43:04 -080031#include "base/safe_map.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070032#include "gc_root.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033
jeffhao725a9572012-11-13 18:20:12 -080034namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035namespace mirror {
Igor Murashkin2ffb7032017-11-08 13:35:21 -080036class Class;
37class Object;
38class Throwable;
Ian Rogers62d6c772013-02-27 08:32:07 -080039} // namespace mirror
Mathieu Chartierc7853442015-03-27 14:35:38 -070040class ArtField;
Mathieu Chartiere401d142015-04-22 13:56:20 -070041class ArtMethod;
Alex Lightd7661582017-05-01 13:48:16 -070042template <typename T> class Handle;
Alex Light2c8206f2018-06-08 14:51:09 -070043template <typename T> class MutableHandle;
Ian Rogers62d6c772013-02-27 08:32:07 -080044union JValue;
Andreas Gampe7e56a072018-11-29 10:40:06 -080045class SHARED_LOCKABLE ReaderWriterMutex;
Alex Lighte814f9d2017-07-31 16:14:39 -070046class ShadowFrame;
jeffhao725a9572012-11-13 18:20:12 -080047class Thread;
Mingyao Yang2ee17902017-08-30 11:37:08 -070048enum class DeoptimizationMethodType;
jeffhao725a9572012-11-13 18:20:12 -080049
Ian Rogers62d6c772013-02-27 08:32:07 -080050namespace instrumentation {
jeffhao725a9572012-11-13 18:20:12 -080051
Sebastien Hertzee1997a2013-09-19 14:47:09 +020052// Interpreter handler tables.
53enum InterpreterHandlerTable {
54 kMainHandlerTable = 0, // Main handler table: no suspend check, no instrumentation.
55 kAlternativeHandlerTable = 1, // Alternative handler table: suspend check and/or instrumentation
56 // enabled.
57 kNumHandlerTables
58};
59
Andreas Gampe40da2862015-02-27 12:49:04 -080060// Do we want to deoptimize for method entry and exit listeners or just try to intercept
61// invocations? Deoptimization forces all code to run in the interpreter and considerably hurts the
62// application's performance.
63static constexpr bool kDeoptimizeForAccurateMethodEntryExitListeners = true;
64
Alex Lightb7c640d2019-03-20 15:52:13 -070065// an optional frame is either Some(const ShadowFrame& current_frame) or None depending on if the
66// method being exited has a shadow-frame associed with the current stack frame. In cases where
67// there is no shadow-frame associated with this stack frame this will be None.
68using OptionalFrame = std::optional<std::reference_wrapper<const ShadowFrame>>;
69
Ian Rogers62d6c772013-02-27 08:32:07 -080070// Instrumentation event listener API. Registered listeners will get the appropriate call back for
71// the events they are listening for. The call backs supply the thread, method and dex_pc the event
72// occurred upon. The thread may or may not be Thread::Current().
73struct InstrumentationListener {
74 InstrumentationListener() {}
75 virtual ~InstrumentationListener() {}
76
77 // Call-back for when a method is entered.
Alex Lightd7661582017-05-01 13:48:16 -070078 virtual void MethodEntered(Thread* thread,
79 Handle<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -070080 ArtMethod* method,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070081 uint32_t dex_pc) REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -080082
Alex Lightd7661582017-05-01 13:48:16 -070083 virtual void MethodExited(Thread* thread,
84 Handle<mirror::Object> this_object,
85 ArtMethod* method,
86 uint32_t dex_pc,
Alex Lightb7c640d2019-03-20 15:52:13 -070087 OptionalFrame frame,
88 MutableHandle<mirror::Object>& return_value)
Alex Lightd7661582017-05-01 13:48:16 -070089 REQUIRES_SHARED(Locks::mutator_lock_);
90
91 // Call-back for when a method is exited. The implementor should either handler-ize the return
92 // value (if appropriate) or use the alternate MethodExited callback instead if they need to
93 // go through a suspend point.
94 virtual void MethodExited(Thread* thread,
95 Handle<mirror::Object> this_object,
96 ArtMethod* method,
97 uint32_t dex_pc,
Alex Lightb7c640d2019-03-20 15:52:13 -070098 OptionalFrame frame,
99 JValue& return_value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700100 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -0800101
102 // Call-back for when a method is popped due to an exception throw. A method will either cause a
103 // MethodExited call-back or a MethodUnwind call-back when its activation is removed.
Alex Lightd7661582017-05-01 13:48:16 -0700104 virtual void MethodUnwind(Thread* thread,
105 Handle<mirror::Object> this_object,
106 ArtMethod* method,
107 uint32_t dex_pc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700108 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -0800109
110 // Call-back for when the dex pc moves in a method.
Alex Lightd7661582017-05-01 13:48:16 -0700111 virtual void DexPcMoved(Thread* thread,
112 Handle<mirror::Object> this_object,
113 ArtMethod* method,
114 uint32_t new_dex_pc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700115 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -0800116
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200117 // Call-back for when we read from a field.
Alex Lightd7661582017-05-01 13:48:16 -0700118 virtual void FieldRead(Thread* thread,
119 Handle<mirror::Object> this_object,
120 ArtMethod* method,
121 uint32_t dex_pc,
122 ArtField* field) = 0;
123
124 virtual void FieldWritten(Thread* thread,
125 Handle<mirror::Object> this_object,
126 ArtMethod* method,
127 uint32_t dex_pc,
128 ArtField* field,
129 Handle<mirror::Object> field_value)
130 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200131
132 // Call-back for when we write into a field.
Alex Lightd7661582017-05-01 13:48:16 -0700133 virtual void FieldWritten(Thread* thread,
134 Handle<mirror::Object> this_object,
135 ArtMethod* method,
136 uint32_t dex_pc,
137 ArtField* field,
138 const JValue& field_value)
139 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200140
Alex Light6e1607e2017-08-23 10:06:18 -0700141 // Call-back when an exception is thrown.
142 virtual void ExceptionThrown(Thread* thread,
Alex Lightd7661582017-05-01 13:48:16 -0700143 Handle<mirror::Throwable> exception_object)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700144 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800145
Alex Light9fb1ab12017-09-05 09:32:49 -0700146 // Call-back when an exception is caught/handled by java code.
147 virtual void ExceptionHandled(Thread* thread, Handle<mirror::Throwable> exception_object)
148 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
149
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000150 // Call-back for when we execute a branch.
151 virtual void Branch(Thread* thread,
152 ArtMethod* method,
153 uint32_t dex_pc,
154 int32_t dex_pc_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700155 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100156
Alex Lighte814f9d2017-07-31 16:14:39 -0700157 // Call-back when a shadow_frame with the needs_notify_pop_ boolean set is popped off the stack by
158 // either return or exceptions. Normally instrumentation listeners should ensure that there are
159 // shadow-frames by deoptimizing stacks.
160 virtual void WatchedFramePop(Thread* thread ATTRIBUTE_UNUSED,
161 const ShadowFrame& frame ATTRIBUTE_UNUSED)
Alex Light05f47742017-09-14 00:34:44 +0000162 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
jeffhao725a9572012-11-13 18:20:12 -0800163};
164
Alex Light2c8206f2018-06-08 14:51:09 -0700165class Instrumentation;
166// A helper to send instrumentation events while popping the stack in a safe way.
167class InstrumentationStackPopper {
168 public:
169 explicit InstrumentationStackPopper(Thread* self);
170 ~InstrumentationStackPopper() REQUIRES_SHARED(Locks::mutator_lock_);
171
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000172 // Increase the number of frames being popped up to `stack_pointer`. Return true if the
173 // frames were popped without any exceptions, false otherwise. The exception that caused
174 // the pop is 'exception'.
175 bool PopFramesTo(uintptr_t stack_pointer, /*in-out*/MutableHandle<mirror::Throwable>& exception)
Alex Light2c8206f2018-06-08 14:51:09 -0700176 REQUIRES_SHARED(Locks::mutator_lock_);
177
178 private:
179 Thread* self_;
180 Instrumentation* instrumentation_;
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000181 // The stack pointer limit for frames to pop.
182 uintptr_t pop_until_;
Alex Light2c8206f2018-06-08 14:51:09 -0700183};
184
Ian Rogers62d6c772013-02-27 08:32:07 -0800185// Instrumentation is a catch-all for when extra information is required from the runtime. The
186// typical use for instrumentation is for profiling and debugging. Instrumentation may add stubs
187// to method entry and exit, it may also force execution to be switched to the interpreter and
188// trigger deoptimization.
jeffhao725a9572012-11-13 18:20:12 -0800189class Instrumentation {
190 public:
Ian Rogers62d6c772013-02-27 08:32:07 -0800191 enum InstrumentationEvent {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800192 kMethodEntered = 0x1,
193 kMethodExited = 0x2,
194 kMethodUnwind = 0x4,
195 kDexPcMoved = 0x8,
196 kFieldRead = 0x10,
197 kFieldWritten = 0x20,
Alex Light6e1607e2017-08-23 10:06:18 -0700198 kExceptionThrown = 0x40,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000199 kBranch = 0x80,
Alex Lighte814f9d2017-07-31 16:14:39 -0700200 kWatchedFramePop = 0x200,
Alex Light9fb1ab12017-09-05 09:32:49 -0700201 kExceptionHandled = 0x400,
Ian Rogers62d6c772013-02-27 08:32:07 -0800202 };
jeffhao725a9572012-11-13 18:20:12 -0800203
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200204 enum class InstrumentationLevel {
205 kInstrumentNothing, // execute without instrumentation
206 kInstrumentWithInstrumentationStubs, // execute with instrumentation entry/exit stubs
207 kInstrumentWithInterpreter // execute with interpreter
208 };
209
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700210 Instrumentation();
jeffhao725a9572012-11-13 18:20:12 -0800211
Ian Rogers62d6c772013-02-27 08:32:07 -0800212 // Add a listener to be notified of the masked together sent of instrumentation events. This
213 // suspend the runtime to install stubs. You are expected to hold the mutator lock as a proxy
214 // for saying you should have suspended all threads (installing stubs while threads are running
215 // will break).
216 void AddListener(InstrumentationListener* listener, uint32_t events)
Mathieu Chartier90443472015-07-16 20:32:27 -0700217 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800218
Ian Rogers62d6c772013-02-27 08:32:07 -0800219 // Removes a listener possibly removing instrumentation stubs.
220 void RemoveListener(InstrumentationListener* listener, uint32_t events)
Mathieu Chartier90443472015-07-16 20:32:27 -0700221 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800222
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100223 // Deoptimization.
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100224 void EnableDeoptimization()
Mathieu Chartieraa516822015-10-02 15:53:37 -0700225 REQUIRES(Locks::mutator_lock_)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800226 REQUIRES(!GetDeoptimizedMethodsLock());
Mathieu Chartieraa516822015-10-02 15:53:37 -0700227 // Calls UndeoptimizeEverything which may visit class linker classes through ConfigureStubs.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200228 void DisableDeoptimization(const char* key)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700229 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800230 REQUIRES(!GetDeoptimizedMethodsLock());
Mathieu Chartieraa516822015-10-02 15:53:37 -0700231
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100232 bool AreAllMethodsDeoptimized() const {
233 return interpreter_stubs_installed_;
234 }
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700235 bool ShouldNotifyMethodEnterExitEvents() const REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100236
Alex Lightbebd7bd2017-07-25 14:05:52 -0700237 bool CanDeoptimize() {
238 return deoptimization_enabled_;
239 }
240
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100241 // Executes everything with interpreter.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200242 void DeoptimizeEverything(const char* key)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700243 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
244 REQUIRES(!Locks::thread_list_lock_,
245 !Locks::classlinker_classes_lock_,
Andreas Gampe7e56a072018-11-29 10:40:06 -0800246 !GetDeoptimizedMethodsLock());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100247
Mathieu Chartieraa516822015-10-02 15:53:37 -0700248 // Executes everything with compiled code (or interpreter if there is no code). May visit class
249 // linker classes through ConfigureStubs.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200250 void UndeoptimizeEverything(const char* key)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700251 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
252 REQUIRES(!Locks::thread_list_lock_,
253 !Locks::classlinker_classes_lock_,
Andreas Gampe7e56a072018-11-29 10:40:06 -0800254 !GetDeoptimizedMethodsLock());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100255
256 // Deoptimize a method by forcing its execution with the interpreter. Nevertheless, a static
257 // method (except a class initializer) set to the resolution trampoline will be deoptimized only
258 // once its declaring class is initialized.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700259 void Deoptimize(ArtMethod* method)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800260 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !GetDeoptimizedMethodsLock());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100261
262 // Undeoptimze the method by restoring its entrypoints. Nevertheless, a static method
263 // (except a class initializer) set to the resolution trampoline will be updated only once its
264 // declaring class is initialized.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700265 void Undeoptimize(ArtMethod* method)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800266 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !GetDeoptimizedMethodsLock());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100267
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200268 // Indicates whether the method has been deoptimized so it is executed with the interpreter.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700269 bool IsDeoptimized(ArtMethod* method)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800270 REQUIRES(!GetDeoptimizedMethodsLock()) REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100271
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200272 // Enable method tracing by installing instrumentation entry/exit stubs or interpreter.
273 void EnableMethodTracing(const char* key,
274 bool needs_interpreter = kDeoptimizeForAccurateMethodEntryExitListeners)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700275 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
276 REQUIRES(!Locks::thread_list_lock_,
277 !Locks::classlinker_classes_lock_,
Andreas Gampe7e56a072018-11-29 10:40:06 -0800278 !GetDeoptimizedMethodsLock());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100279
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200280 // Disable method tracing by uninstalling instrumentation entry/exit stubs or interpreter.
281 void DisableMethodTracing(const char* key)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700282 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
283 REQUIRES(!Locks::thread_list_lock_,
284 !Locks::classlinker_classes_lock_,
Andreas Gampe7e56a072018-11-29 10:40:06 -0800285 !GetDeoptimizedMethodsLock());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100286
Sebastien Hertzed2be172014-08-19 15:33:43 +0200287 InterpreterHandlerTable GetInterpreterHandlerTable() const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700288 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200289 return interpreter_handler_table_;
290 }
291
Mathieu Chartier90443472015-07-16 20:32:27 -0700292 void InstrumentQuickAllocEntryPoints() REQUIRES(!Locks::instrument_entrypoints_lock_);
293 void UninstrumentQuickAllocEntryPoints() REQUIRES(!Locks::instrument_entrypoints_lock_);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700294 void InstrumentQuickAllocEntryPointsLocked()
Mathieu Chartier90443472015-07-16 20:32:27 -0700295 REQUIRES(Locks::instrument_entrypoints_lock_, !Locks::thread_list_lock_,
296 !Locks::runtime_shutdown_lock_);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700297 void UninstrumentQuickAllocEntryPointsLocked()
Mathieu Chartier90443472015-07-16 20:32:27 -0700298 REQUIRES(Locks::instrument_entrypoints_lock_, !Locks::thread_list_lock_,
299 !Locks::runtime_shutdown_lock_);
300 void ResetQuickAllocEntryPoints() REQUIRES(Locks::runtime_shutdown_lock_);
Ian Rogersfa824272013-11-05 16:12:57 -0800301
Ian Rogers62d6c772013-02-27 08:32:07 -0800302 // Update the code of a method respecting any installed stubs.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700303 void UpdateMethodsCode(ArtMethod* method, const void* quick_code)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800304 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!GetDeoptimizedMethodsLock());
Ian Rogers62d6c772013-02-27 08:32:07 -0800305
Nicolas Geoffraya6e0e7d2018-01-26 13:16:50 +0000306 // Update the code of a native method to a JITed stub.
307 void UpdateNativeMethodsCodeToJitCode(ArtMethod* method, const void* quick_code)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800308 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!GetDeoptimizedMethodsLock());
Nicolas Geoffraya6e0e7d2018-01-26 13:16:50 +0000309
Alex Light0a5ec3d2017-07-25 16:50:26 -0700310 // Update the code of a method to the interpreter respecting any installed stubs from debugger.
311 void UpdateMethodsCodeToInterpreterEntryPoint(ArtMethod* method)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800312 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!GetDeoptimizedMethodsLock());
Alex Light0a5ec3d2017-07-25 16:50:26 -0700313
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700314 // Update the code of a method respecting any installed stubs from debugger.
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000315 void UpdateMethodsCodeForJavaDebuggable(ArtMethod* method, const void* quick_code)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800316 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!GetDeoptimizedMethodsLock());
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700317
Alex Light2d441b12018-06-08 15:33:21 -0700318 // Return the code that we can execute for an invoke including from the JIT.
319 const void* GetCodeForInvoke(ArtMethod* method) const
320 REQUIRES_SHARED(Locks::mutator_lock_);
321
Ian Rogers62d6c772013-02-27 08:32:07 -0800322 // Get the quick code for the given method. More efficient than asking the class linker as it
323 // will short-cut to GetCode if instrumentation and static method resolution stubs aren't
324 // installed.
Andreas Gampe542451c2016-07-26 09:02:02 -0700325 const void* GetQuickCodeFor(ArtMethod* method, PointerSize pointer_size) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700326 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800327
328 void ForceInterpretOnly() {
329 interpret_only_ = true;
330 forced_interpret_only_ = true;
331 }
332
Brian Carlstromea46f952013-07-30 01:26:50 -0700333 // Called by ArtMethod::Invoke to determine dispatch mechanism.
Ian Rogers62d6c772013-02-27 08:32:07 -0800334 bool InterpretOnly() const {
335 return interpret_only_;
336 }
337
Hiroshi Yamauchi563b47c2014-02-28 17:18:37 -0800338 bool IsForcedInterpretOnly() const {
339 return forced_interpret_only_;
340 }
341
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800342 // Code is in boot image oat file which isn't compiled as debuggable.
343 // Need debug version (interpreter or jitted) if that's the case.
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000344 bool NeedDebugVersionFor(ArtMethod* method) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700345 REQUIRES_SHARED(Locks::mutator_lock_);
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800346
Ian Rogers62d6c772013-02-27 08:32:07 -0800347 bool AreExitStubsInstalled() const {
348 return instrumentation_stubs_installed_;
349 }
350
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700351 bool HasMethodEntryListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200352 return have_method_entry_listeners_;
353 }
354
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700355 bool HasMethodExitListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200356 return have_method_exit_listeners_;
357 }
358
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700359 bool HasMethodUnwindListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200360 return have_method_unwind_listeners_;
361 }
362
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700363 bool HasDexPcListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200364 return have_dex_pc_listeners_;
365 }
366
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700367 bool HasFieldReadListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200368 return have_field_read_listeners_;
369 }
370
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700371 bool HasFieldWriteListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200372 return have_field_write_listeners_;
373 }
374
Alex Light6e1607e2017-08-23 10:06:18 -0700375 bool HasExceptionThrownListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
376 return have_exception_thrown_listeners_;
Sebastien Hertz9f102032014-05-23 08:59:42 +0200377 }
378
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700379 bool HasBranchListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000380 return have_branch_listeners_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800381 }
382
Alex Lighte814f9d2017-07-31 16:14:39 -0700383 bool HasWatchedFramePopListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
384 return have_watched_frame_pop_listeners_;
385 }
386
Alex Light9fb1ab12017-09-05 09:32:49 -0700387 bool HasExceptionHandledListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
388 return have_exception_handled_listeners_;
389 }
390
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700391 bool IsActive() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200392 return have_dex_pc_listeners_ || have_method_entry_listeners_ || have_method_exit_listeners_ ||
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200393 have_field_read_listeners_ || have_field_write_listeners_ ||
Alex Light6e1607e2017-08-23 10:06:18 -0700394 have_exception_thrown_listeners_ || have_method_unwind_listeners_ ||
David Srbecky99f97332018-10-03 15:44:24 +0100395 have_branch_listeners_ || have_watched_frame_pop_listeners_ ||
396 have_exception_handled_listeners_;
Bill Buzbeefd522f92016-02-11 22:37:42 +0000397 }
398
Ian Rogers62d6c772013-02-27 08:32:07 -0800399 // Inform listeners that a method has been entered. A dex PC is provided as we may install
400 // listeners into executing code and get method enter events for methods already on the stack.
Vladimir Marko19711d42019-04-12 14:05:34 +0100401 void MethodEnterEvent(Thread* thread,
402 ObjPtr<mirror::Object> this_object,
403 ArtMethod* method,
404 uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700405 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200406 if (UNLIKELY(HasMethodEntryListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800407 MethodEnterEventImpl(thread, this_object, method, dex_pc);
408 }
409 }
410
411 // Inform listeners that a method has been exited.
Alex Lightb7c640d2019-03-20 15:52:13 -0700412 template<typename T>
Alex Lightd7661582017-05-01 13:48:16 -0700413 void MethodExitEvent(Thread* thread,
Vladimir Marko19711d42019-04-12 14:05:34 +0100414 ObjPtr<mirror::Object> this_object,
Alex Lightd7661582017-05-01 13:48:16 -0700415 ArtMethod* method,
416 uint32_t dex_pc,
Alex Lightb7c640d2019-03-20 15:52:13 -0700417 OptionalFrame frame,
418 T& return_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700419 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200420 if (UNLIKELY(HasMethodExitListeners())) {
Alex Lightb7c640d2019-03-20 15:52:13 -0700421 MethodExitEventImpl(thread, this_object, method, dex_pc, frame, return_value);
Ian Rogers62d6c772013-02-27 08:32:07 -0800422 }
423 }
424
425 // Inform listeners that a method has been exited due to an exception.
Vladimir Marko19711d42019-04-12 14:05:34 +0100426 void MethodUnwindEvent(Thread* thread,
427 ObjPtr<mirror::Object> this_object,
428 ArtMethod* method,
429 uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700430 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800431
432 // Inform listeners that the dex pc has moved (only supported by the interpreter).
Vladimir Marko19711d42019-04-12 14:05:34 +0100433 void DexPcMovedEvent(Thread* thread,
434 ObjPtr<mirror::Object> this_object,
435 ArtMethod* method,
436 uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700437 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200438 if (UNLIKELY(HasDexPcListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800439 DexPcMovedEventImpl(thread, this_object, method, dex_pc);
440 }
441 }
442
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000443 // Inform listeners that a branch has been taken (only supported by the interpreter).
444 void Branch(Thread* thread, ArtMethod* method, uint32_t dex_pc, int32_t offset) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700445 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000446 if (UNLIKELY(HasBranchListeners())) {
447 BranchImpl(thread, method, dex_pc, offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800448 }
449 }
450
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200451 // Inform listeners that we read a field (only supported by the interpreter).
Vladimir Marko19711d42019-04-12 14:05:34 +0100452 void FieldReadEvent(Thread* thread,
453 ObjPtr<mirror::Object> this_object,
454 ArtMethod* method,
455 uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700456 ArtField* field) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700457 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200458 if (UNLIKELY(HasFieldReadListeners())) {
459 FieldReadEventImpl(thread, this_object, method, dex_pc, field);
460 }
461 }
462
463 // Inform listeners that we write a field (only supported by the interpreter).
Vladimir Marko19711d42019-04-12 14:05:34 +0100464 void FieldWriteEvent(Thread* thread,
465 ObjPtr<mirror::Object> this_object,
466 ArtMethod* method,
467 uint32_t dex_pc,
468 ArtField* field,
469 const JValue& field_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700470 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200471 if (UNLIKELY(HasFieldWriteListeners())) {
472 FieldWriteEventImpl(thread, this_object, method, dex_pc, field, field_value);
473 }
474 }
475
Alex Lighte814f9d2017-07-31 16:14:39 -0700476 // Inform listeners that a branch has been taken (only supported by the interpreter).
477 void WatchedFramePopped(Thread* thread, const ShadowFrame& frame) const
478 REQUIRES_SHARED(Locks::mutator_lock_) {
479 if (UNLIKELY(HasWatchedFramePopListeners())) {
480 WatchedFramePopImpl(thread, frame);
481 }
482 }
483
Alex Light6e1607e2017-08-23 10:06:18 -0700484 // Inform listeners that an exception was thrown.
Vladimir Marko19711d42019-04-12 14:05:34 +0100485 void ExceptionThrownEvent(Thread* thread, ObjPtr<mirror::Throwable> exception_object) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700486 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800487
Alex Light9fb1ab12017-09-05 09:32:49 -0700488 // Inform listeners that an exception has been handled. This is not sent for native code or for
489 // exceptions which reach the end of the thread's stack.
Vladimir Marko19711d42019-04-12 14:05:34 +0100490 void ExceptionHandledEvent(Thread* thread, ObjPtr<mirror::Throwable> exception_object) const
Alex Light9fb1ab12017-09-05 09:32:49 -0700491 REQUIRES_SHARED(Locks::mutator_lock_);
492
Ian Rogers62d6c772013-02-27 08:32:07 -0800493 // Called when an instrumented method is entered. The intended link register (lr) is saved so
494 // that returning causes a branch to the method exit stub. Generates method enter events.
Vladimir Marko19711d42019-04-12 14:05:34 +0100495 void PushInstrumentationStackFrame(Thread* self,
496 ObjPtr<mirror::Object> this_object,
497 ArtMethod* method,
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000498 uintptr_t stack_pointer,
Vladimir Marko19711d42019-04-12 14:05:34 +0100499 uintptr_t lr,
Jeff Hao9a916d32013-06-27 18:45:37 -0700500 bool interpreter_entry)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700501 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800502
Mingyao Yang2ee17902017-08-30 11:37:08 -0700503 DeoptimizationMethodType GetDeoptimizationMethodType(ArtMethod* method)
504 REQUIRES_SHARED(Locks::mutator_lock_);
505
Ian Rogers62d6c772013-02-27 08:32:07 -0800506 // Called when an instrumented method is exited. Removes the pushed instrumentation frame
Alex Lightb7edcda2017-04-27 13:20:31 -0700507 // returning the intended link register. Generates method exit events. The gpr_result and
508 // fpr_result pointers are pointers to the locations where the integer/pointer and floating point
509 // result values of the function are stored. Both pointers must always be valid but the values
510 // held there will only be meaningful if interpreted as the appropriate type given the function
511 // being returned from.
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000512 TwoWordReturn PopInstrumentationStackFrame(Thread* self,
513 uintptr_t* return_pc_addr,
514 uint64_t* gpr_result,
515 uint64_t* fpr_result)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800516 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!GetDeoptimizedMethodsLock());
Ian Rogers62d6c772013-02-27 08:32:07 -0800517
Alex Light2c8206f2018-06-08 14:51:09 -0700518 // Pops nframes instrumentation frames from the current thread. Returns the return pc for the last
519 // instrumentation frame that's popped.
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000520 uintptr_t PopFramesForDeoptimization(Thread* self, uintptr_t stack_pointer) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700521 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800522
523 // Call back for configure stubs.
Vladimir Marko19711d42019-04-12 14:05:34 +0100524 void InstallStubsForClass(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800525 REQUIRES(!GetDeoptimizedMethodsLock());
jeffhao725a9572012-11-13 18:20:12 -0800526
Mathieu Chartiere401d142015-04-22 13:56:20 -0700527 void InstallStubsForMethod(ArtMethod* method)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800528 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!GetDeoptimizedMethodsLock());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100529
Alex Light40607862019-05-06 18:16:24 +0000530 // Sets up instrumentation to allow single thread deoptimization using ForceInterpreterCount.
531 void EnableSingleThreadDeopt()
532 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
533 REQUIRES(!Locks::thread_list_lock_,
534 !Locks::classlinker_classes_lock_,
535 !GetDeoptimizedMethodsLock());
536
Mingyao Yang99170c62015-07-06 11:10:37 -0700537 // Install instrumentation exit stub on every method of the stack of the given thread.
538 // This is used by the debugger to cause a deoptimization of the thread's stack after updating
539 // local variable(s).
540 void InstrumentThreadStack(Thread* thread)
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000541 REQUIRES(Locks::mutator_lock_);
Mingyao Yang99170c62015-07-06 11:10:37 -0700542
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000543 // Force all currently running frames to be deoptimized back to interpreter. This should only be
544 // used in cases where basically all compiled code has been invalidated.
545 void DeoptimizeAllThreadFrames() REQUIRES(art::Locks::mutator_lock_);
546
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000547 static size_t ComputeFrameId(Thread* self,
548 size_t frame_depth,
549 size_t inlined_frames_before_frame)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700550 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000551
Mathieu Chartiereebc3af2016-02-29 18:13:38 -0800552 // Does not hold lock, used to check if someone changed from not instrumented to instrumented
553 // during a GC suspend point.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700554 bool AllocEntrypointsInstrumented() const REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier50e93312016-03-16 11:25:29 -0700555 return alloc_entrypoints_instrumented_;
Mathieu Chartiereebc3af2016-02-29 18:13:38 -0800556 }
557
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200558 InstrumentationLevel GetCurrentInstrumentationLevel() const;
559
Alex Lightdba61482016-12-21 08:20:29 -0800560 private:
561 // Returns true if moving to the given instrumentation level requires the installation of stubs.
562 // False otherwise.
563 bool RequiresInstrumentationInstallation(InstrumentationLevel new_level) const;
564
Ian Rogers62d6c772013-02-27 08:32:07 -0800565 // Does the job of installing or removing instrumentation code within methods.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200566 // In order to support multiple clients using instrumentation at the same time,
567 // the caller must pass a unique key (a string) identifying it so we remind which
568 // instrumentation level it needs. Therefore the current instrumentation level
569 // becomes the highest instrumentation level required by a client.
570 void ConfigureStubs(const char* key, InstrumentationLevel desired_instrumentation_level)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700571 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800572 REQUIRES(!GetDeoptimizedMethodsLock(),
Mathieu Chartieraa516822015-10-02 15:53:37 -0700573 !Locks::thread_list_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700574 !Locks::classlinker_classes_lock_);
Alex Light40607862019-05-06 18:16:24 +0000575 void UpdateStubs() REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
576 REQUIRES(!GetDeoptimizedMethodsLock(),
577 !Locks::thread_list_lock_,
578 !Locks::classlinker_classes_lock_);
579 void UpdateInstrumentationLevels(InstrumentationLevel level)
580 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
581 REQUIRES(!GetDeoptimizedMethodsLock(),
582 !Locks::thread_list_lock_,
583 !Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800584
Mathieu Chartier90443472015-07-16 20:32:27 -0700585 void UpdateInterpreterHandlerTable() REQUIRES(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800586 /*
587 * TUNING: Dalvik's mterp stashes the actual current handler table base in a
588 * tls field. For Arm, this enables all suspend, debug & tracing checks to be
589 * collapsed into a single conditionally-executed ldw instruction.
590 * Move to Dalvik-style handler-table management for both the goto interpreter and
591 * mterp.
592 */
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200593 interpreter_handler_table_ = IsActive() ? kAlternativeHandlerTable : kMainHandlerTable;
594 }
595
Mathieu Chartier661974a2014-01-09 11:23:53 -0800596 // No thread safety analysis to get around SetQuickAllocEntryPointsInstrumented requiring
597 // exclusive access to mutator lock which you can't get if the runtime isn't started.
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700598 void SetEntrypointsInstrumented(bool instrumented) NO_THREAD_SAFETY_ANALYSIS;
Mathieu Chartier661974a2014-01-09 11:23:53 -0800599
Alex Lightd7661582017-05-01 13:48:16 -0700600 void MethodEnterEventImpl(Thread* thread,
601 ObjPtr<mirror::Object> this_object,
602 ArtMethod* method,
603 uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700604 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lightb7c640d2019-03-20 15:52:13 -0700605 template <typename T>
Alex Lightd7661582017-05-01 13:48:16 -0700606 void MethodExitEventImpl(Thread* thread,
607 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700608 ArtMethod* method,
Alex Lightd7661582017-05-01 13:48:16 -0700609 uint32_t dex_pc,
Alex Lightb7c640d2019-03-20 15:52:13 -0700610 OptionalFrame frame,
611 T& return_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700612 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lightd7661582017-05-01 13:48:16 -0700613 void DexPcMovedEventImpl(Thread* thread,
614 ObjPtr<mirror::Object> this_object,
615 ArtMethod* method,
616 uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700617 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000618 void BranchImpl(Thread* thread, ArtMethod* method, uint32_t dex_pc, int32_t offset) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700619 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lighte814f9d2017-07-31 16:14:39 -0700620 void WatchedFramePopImpl(Thread* thread, const ShadowFrame& frame) const
621 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lightd7661582017-05-01 13:48:16 -0700622 void FieldReadEventImpl(Thread* thread,
623 ObjPtr<mirror::Object> this_object,
624 ArtMethod* method,
625 uint32_t dex_pc,
626 ArtField* field) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700627 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lightd7661582017-05-01 13:48:16 -0700628 void FieldWriteEventImpl(Thread* thread,
629 ObjPtr<mirror::Object> this_object,
630 ArtMethod* method,
631 uint32_t dex_pc,
632 ArtField* field,
633 const JValue& field_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700634 REQUIRES_SHARED(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800635
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700636 // Read barrier-aware utility functions for accessing deoptimized_methods_
Mathieu Chartiere401d142015-04-22 13:56:20 -0700637 bool AddDeoptimizedMethod(ArtMethod* method)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800638 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(GetDeoptimizedMethodsLock());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700639 bool IsDeoptimizedMethod(ArtMethod* method)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800640 REQUIRES_SHARED(Locks::mutator_lock_, GetDeoptimizedMethodsLock());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700641 bool RemoveDeoptimizedMethod(ArtMethod* method)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800642 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(GetDeoptimizedMethodsLock());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700643 ArtMethod* BeginDeoptimizedMethod()
Andreas Gampe7e56a072018-11-29 10:40:06 -0800644 REQUIRES_SHARED(Locks::mutator_lock_, GetDeoptimizedMethodsLock());
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700645 bool IsDeoptimizedMethodsEmpty() const
Andreas Gampe7e56a072018-11-29 10:40:06 -0800646 REQUIRES_SHARED(Locks::mutator_lock_, GetDeoptimizedMethodsLock());
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700647 void UpdateMethodsCodeImpl(ArtMethod* method, const void* quick_code)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800648 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!GetDeoptimizedMethodsLock());
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700649
Andreas Gampe7e56a072018-11-29 10:40:06 -0800650 ReaderWriterMutex* GetDeoptimizedMethodsLock() const {
651 return deoptimized_methods_lock_.get();
652 }
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700653
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000654 // A counter that's incremented every time a DeoptimizeAllFrames. We check each
655 // InstrumentationStackFrames creation id against this number and if they differ we deopt even if
656 // we could otherwise continue running.
657 uint64_t current_force_deopt_id_ GUARDED_BY(Locks::mutator_lock_);
658
Brian Carlstromea46f952013-07-30 01:26:50 -0700659 // Have we hijacked ArtMethod::code_ so that it calls instrumentation/interpreter code?
Ian Rogers62d6c772013-02-27 08:32:07 -0800660 bool instrumentation_stubs_installed_;
661
Brian Carlstromea46f952013-07-30 01:26:50 -0700662 // Have we hijacked ArtMethod::code_ to reference the enter/exit stubs?
Ian Rogers62d6c772013-02-27 08:32:07 -0800663 bool entry_exit_stubs_installed_;
664
Brian Carlstromea46f952013-07-30 01:26:50 -0700665 // Have we hijacked ArtMethod::code_ to reference the enter interpreter stub?
Ian Rogers62d6c772013-02-27 08:32:07 -0800666 bool interpreter_stubs_installed_;
667
668 // Do we need the fidelity of events that we only get from running within the interpreter?
669 bool interpret_only_;
670
671 // Did the runtime request we only run in the interpreter? ie -Xint mode.
672 bool forced_interpret_only_;
673
674 // Do we have any listeners for method entry events? Short-cut to avoid taking the
675 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200676 bool have_method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800677
678 // Do we have any listeners for method exit events? Short-cut to avoid taking the
679 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200680 bool have_method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800681
682 // Do we have any listeners for method unwind events? Short-cut to avoid taking the
683 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200684 bool have_method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800685
686 // Do we have any listeners for dex move events? Short-cut to avoid taking the
687 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200688 bool have_dex_pc_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800689
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200690 // Do we have any listeners for field read events? Short-cut to avoid taking the
691 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200692 bool have_field_read_listeners_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200693
694 // Do we have any listeners for field write events? Short-cut to avoid taking the
695 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200696 bool have_field_write_listeners_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200697
Alex Light6e1607e2017-08-23 10:06:18 -0700698 // Do we have any exception thrown listeners? Short-cut to avoid taking the instrumentation_lock_.
699 bool have_exception_thrown_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800700
Alex Lighte814f9d2017-07-31 16:14:39 -0700701 // Do we have any frame pop listeners? Short-cut to avoid taking the instrumentation_lock_.
702 bool have_watched_frame_pop_listeners_ GUARDED_BY(Locks::mutator_lock_);
703
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000704 // Do we have any branch listeners? Short-cut to avoid taking the instrumentation_lock_.
705 bool have_branch_listeners_ GUARDED_BY(Locks::mutator_lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800706
Alex Light9fb1ab12017-09-05 09:32:49 -0700707 // Do we have any exception handled listeners? Short-cut to avoid taking the
708 // instrumentation_lock_.
709 bool have_exception_handled_listeners_ GUARDED_BY(Locks::mutator_lock_);
710
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200711 // Contains the instrumentation level required by each client of the instrumentation identified
712 // by a string key.
713 typedef SafeMap<const char*, InstrumentationLevel> InstrumentationLevelTable;
714 InstrumentationLevelTable requested_instrumentation_levels_ GUARDED_BY(Locks::mutator_lock_);
715
Ian Rogers62d6c772013-02-27 08:32:07 -0800716 // The event listeners, written to with the mutator_lock_ exclusively held.
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000717 // Mutators must be able to iterate over these lists concurrently, that is, with listeners being
718 // added or removed while iterating. The modifying thread holds exclusive lock,
719 // so other threads cannot iterate (i.e. read the data of the list) at the same time but they
720 // do keep iterators that need to remain valid. This is the reason these listeners are std::list
721 // and not for example std::vector: the existing storage for a std::list does not move.
722 // Note that mutators cannot make a copy of these lists before iterating, as the instrumentation
723 // listeners can also be deleted concurrently.
724 // As a result, these lists are never trimmed. That's acceptable given the low number of
725 // listeners we have.
Ian Rogers62d6c772013-02-27 08:32:07 -0800726 std::list<InstrumentationListener*> method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_);
727 std::list<InstrumentationListener*> method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_);
728 std::list<InstrumentationListener*> method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000729 std::list<InstrumentationListener*> branch_listeners_ GUARDED_BY(Locks::mutator_lock_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000730 std::list<InstrumentationListener*> dex_pc_listeners_ GUARDED_BY(Locks::mutator_lock_);
731 std::list<InstrumentationListener*> field_read_listeners_ GUARDED_BY(Locks::mutator_lock_);
732 std::list<InstrumentationListener*> field_write_listeners_ GUARDED_BY(Locks::mutator_lock_);
Alex Light6e1607e2017-08-23 10:06:18 -0700733 std::list<InstrumentationListener*> exception_thrown_listeners_ GUARDED_BY(Locks::mutator_lock_);
Alex Lighte814f9d2017-07-31 16:14:39 -0700734 std::list<InstrumentationListener*> watched_frame_pop_listeners_ GUARDED_BY(Locks::mutator_lock_);
Alex Light9fb1ab12017-09-05 09:32:49 -0700735 std::list<InstrumentationListener*> exception_handled_listeners_ GUARDED_BY(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800736
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100737 // The set of methods being deoptimized (by the debugger) which must be executed with interpreter
738 // only.
Andreas Gampe7e56a072018-11-29 10:40:06 -0800739 mutable std::unique_ptr<ReaderWriterMutex> deoptimized_methods_lock_ BOTTOM_MUTEX_ACQUIRED_AFTER;
740 std::unordered_set<ArtMethod*> deoptimized_methods_ GUARDED_BY(GetDeoptimizedMethodsLock());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100741 bool deoptimization_enabled_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100742
Ian Rogersfa824272013-11-05 16:12:57 -0800743 // Current interpreter handler table. This is updated each time the thread state flags are
744 // modified.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200745 InterpreterHandlerTable interpreter_handler_table_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200746
Ian Rogersfa824272013-11-05 16:12:57 -0800747 // Greater than 0 if quick alloc entry points instrumented.
Mathieu Chartiereebc3af2016-02-29 18:13:38 -0800748 size_t quick_alloc_entry_points_instrumentation_counter_;
Mathieu Chartier50e93312016-03-16 11:25:29 -0700749
750 // alloc_entrypoints_instrumented_ is only updated with all the threads suspended, this is done
751 // to prevent races with the GC where the GC relies on thread suspension only see
752 // alloc_entrypoints_instrumented_ change during suspend points.
753 bool alloc_entrypoints_instrumented_;
754
Alex Light40607862019-05-06 18:16:24 +0000755 // If we can use instrumentation trampolines. After the first time we instrument something with
756 // the interpreter we can no longer use trampolines because it can lead to stack corruption.
757 // TODO Figure out a way to remove the need for this.
758 bool can_use_instrumentation_trampolines_;
759
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200760 friend class InstrumentationTest; // For GetCurrentInstrumentationLevel and ConfigureStubs.
Alex Light2c8206f2018-06-08 14:51:09 -0700761 friend class InstrumentationStackPopper; // For popping instrumentation frames.
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000762 friend void InstrumentationInstallStack(Thread*, void*);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200763
jeffhao725a9572012-11-13 18:20:12 -0800764 DISALLOW_COPY_AND_ASSIGN(Instrumentation);
765};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700766std::ostream& operator<<(std::ostream& os, const Instrumentation::InstrumentationEvent& rhs);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200767std::ostream& operator<<(std::ostream& os, const Instrumentation::InstrumentationLevel& rhs);
jeffhao725a9572012-11-13 18:20:12 -0800768
Ian Rogers62d6c772013-02-27 08:32:07 -0800769// An element in the instrumentation side stack maintained in art::Thread.
770struct InstrumentationStackFrame {
Mingyao Yang2ee17902017-08-30 11:37:08 -0700771 InstrumentationStackFrame(mirror::Object* this_object,
772 ArtMethod* method,
773 uintptr_t return_pc,
774 size_t frame_id,
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000775 bool interpreter_entry,
776 uint64_t force_deopt_id)
Mingyao Yang2ee17902017-08-30 11:37:08 -0700777 : this_object_(this_object),
778 method_(method),
779 return_pc_(return_pc),
780 frame_id_(frame_id),
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000781 interpreter_entry_(interpreter_entry),
782 force_deopt_id_(force_deopt_id) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800783 }
784
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700785 std::string Dump() const REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800786
787 mirror::Object* this_object_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700788 ArtMethod* method_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100789 uintptr_t return_pc_;
790 size_t frame_id_;
791 bool interpreter_entry_;
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000792 uint64_t force_deopt_id_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800793};
794
795} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -0800796} // namespace art
797
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700798#endif // ART_RUNTIME_INSTRUMENTATION_H_