blob: a7907c88427a1167a14d0d935d52226b642197ba [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
172 // Increase the number of frames being popped to 'desired_pops' return true if the frames were
173 // popped without any exceptions, false otherwise. The exception that caused the pop is
174 // 'exception'.
175 bool PopFramesTo(uint32_t desired_pops, /*in-out*/MutableHandle<mirror::Throwable>& exception)
176 REQUIRES_SHARED(Locks::mutator_lock_);
177
178 private:
179 Thread* self_;
180 Instrumentation* instrumentation_;
181 uint32_t frames_to_remove_;
182};
183
Ian Rogers62d6c772013-02-27 08:32:07 -0800184// Instrumentation is a catch-all for when extra information is required from the runtime. The
185// typical use for instrumentation is for profiling and debugging. Instrumentation may add stubs
186// to method entry and exit, it may also force execution to be switched to the interpreter and
187// trigger deoptimization.
jeffhao725a9572012-11-13 18:20:12 -0800188class Instrumentation {
189 public:
Ian Rogers62d6c772013-02-27 08:32:07 -0800190 enum InstrumentationEvent {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800191 kMethodEntered = 0x1,
192 kMethodExited = 0x2,
193 kMethodUnwind = 0x4,
194 kDexPcMoved = 0x8,
195 kFieldRead = 0x10,
196 kFieldWritten = 0x20,
Alex Light6e1607e2017-08-23 10:06:18 -0700197 kExceptionThrown = 0x40,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000198 kBranch = 0x80,
Alex Lighte814f9d2017-07-31 16:14:39 -0700199 kWatchedFramePop = 0x200,
Alex Light9fb1ab12017-09-05 09:32:49 -0700200 kExceptionHandled = 0x400,
Ian Rogers62d6c772013-02-27 08:32:07 -0800201 };
jeffhao725a9572012-11-13 18:20:12 -0800202
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200203 enum class InstrumentationLevel {
204 kInstrumentNothing, // execute without instrumentation
205 kInstrumentWithInstrumentationStubs, // execute with instrumentation entry/exit stubs
206 kInstrumentWithInterpreter // execute with interpreter
207 };
208
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700209 Instrumentation();
jeffhao725a9572012-11-13 18:20:12 -0800210
Ian Rogers62d6c772013-02-27 08:32:07 -0800211 // Add a listener to be notified of the masked together sent of instrumentation events. This
212 // suspend the runtime to install stubs. You are expected to hold the mutator lock as a proxy
213 // for saying you should have suspended all threads (installing stubs while threads are running
214 // will break).
215 void AddListener(InstrumentationListener* listener, uint32_t events)
Mathieu Chartier90443472015-07-16 20:32:27 -0700216 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800217
Ian Rogers62d6c772013-02-27 08:32:07 -0800218 // Removes a listener possibly removing instrumentation stubs.
219 void RemoveListener(InstrumentationListener* listener, uint32_t events)
Mathieu Chartier90443472015-07-16 20:32:27 -0700220 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800221
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100222 // Deoptimization.
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100223 void EnableDeoptimization()
Mathieu Chartieraa516822015-10-02 15:53:37 -0700224 REQUIRES(Locks::mutator_lock_)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800225 REQUIRES(!GetDeoptimizedMethodsLock());
Mathieu Chartieraa516822015-10-02 15:53:37 -0700226 // Calls UndeoptimizeEverything which may visit class linker classes through ConfigureStubs.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200227 void DisableDeoptimization(const char* key)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700228 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800229 REQUIRES(!GetDeoptimizedMethodsLock());
Mathieu Chartieraa516822015-10-02 15:53:37 -0700230
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100231 bool AreAllMethodsDeoptimized() const {
232 return interpreter_stubs_installed_;
233 }
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700234 bool ShouldNotifyMethodEnterExitEvents() const REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100235
Alex Lightbebd7bd2017-07-25 14:05:52 -0700236 bool CanDeoptimize() {
237 return deoptimization_enabled_;
238 }
239
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100240 // Executes everything with interpreter.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200241 void DeoptimizeEverything(const char* key)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700242 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
243 REQUIRES(!Locks::thread_list_lock_,
244 !Locks::classlinker_classes_lock_,
Andreas Gampe7e56a072018-11-29 10:40:06 -0800245 !GetDeoptimizedMethodsLock());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100246
Mathieu Chartieraa516822015-10-02 15:53:37 -0700247 // Executes everything with compiled code (or interpreter if there is no code). May visit class
248 // linker classes through ConfigureStubs.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200249 void UndeoptimizeEverything(const char* key)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700250 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
251 REQUIRES(!Locks::thread_list_lock_,
252 !Locks::classlinker_classes_lock_,
Andreas Gampe7e56a072018-11-29 10:40:06 -0800253 !GetDeoptimizedMethodsLock());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100254
255 // Deoptimize a method by forcing its execution with the interpreter. Nevertheless, a static
256 // method (except a class initializer) set to the resolution trampoline will be deoptimized only
257 // once its declaring class is initialized.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700258 void Deoptimize(ArtMethod* method)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800259 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !GetDeoptimizedMethodsLock());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100260
261 // Undeoptimze the method by restoring its entrypoints. Nevertheless, a static method
262 // (except a class initializer) set to the resolution trampoline will be updated only once its
263 // declaring class is initialized.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700264 void Undeoptimize(ArtMethod* method)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800265 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !GetDeoptimizedMethodsLock());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100266
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200267 // Indicates whether the method has been deoptimized so it is executed with the interpreter.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700268 bool IsDeoptimized(ArtMethod* method)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800269 REQUIRES(!GetDeoptimizedMethodsLock()) REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100270
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200271 // Enable method tracing by installing instrumentation entry/exit stubs or interpreter.
272 void EnableMethodTracing(const char* key,
273 bool needs_interpreter = kDeoptimizeForAccurateMethodEntryExitListeners)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700274 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
275 REQUIRES(!Locks::thread_list_lock_,
276 !Locks::classlinker_classes_lock_,
Andreas Gampe7e56a072018-11-29 10:40:06 -0800277 !GetDeoptimizedMethodsLock());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100278
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200279 // Disable method tracing by uninstalling instrumentation entry/exit stubs or interpreter.
280 void DisableMethodTracing(const char* key)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700281 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
282 REQUIRES(!Locks::thread_list_lock_,
283 !Locks::classlinker_classes_lock_,
Andreas Gampe7e56a072018-11-29 10:40:06 -0800284 !GetDeoptimizedMethodsLock());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100285
Sebastien Hertzed2be172014-08-19 15:33:43 +0200286 InterpreterHandlerTable GetInterpreterHandlerTable() const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700287 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200288 return interpreter_handler_table_;
289 }
290
Mathieu Chartier90443472015-07-16 20:32:27 -0700291 void InstrumentQuickAllocEntryPoints() REQUIRES(!Locks::instrument_entrypoints_lock_);
292 void UninstrumentQuickAllocEntryPoints() REQUIRES(!Locks::instrument_entrypoints_lock_);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700293 void InstrumentQuickAllocEntryPointsLocked()
Mathieu Chartier90443472015-07-16 20:32:27 -0700294 REQUIRES(Locks::instrument_entrypoints_lock_, !Locks::thread_list_lock_,
295 !Locks::runtime_shutdown_lock_);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700296 void UninstrumentQuickAllocEntryPointsLocked()
Mathieu Chartier90443472015-07-16 20:32:27 -0700297 REQUIRES(Locks::instrument_entrypoints_lock_, !Locks::thread_list_lock_,
298 !Locks::runtime_shutdown_lock_);
299 void ResetQuickAllocEntryPoints() REQUIRES(Locks::runtime_shutdown_lock_);
Ian Rogersfa824272013-11-05 16:12:57 -0800300
Ian Rogers62d6c772013-02-27 08:32:07 -0800301 // Update the code of a method respecting any installed stubs.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700302 void UpdateMethodsCode(ArtMethod* method, const void* quick_code)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800303 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!GetDeoptimizedMethodsLock());
Ian Rogers62d6c772013-02-27 08:32:07 -0800304
Nicolas Geoffraya6e0e7d2018-01-26 13:16:50 +0000305 // Update the code of a native method to a JITed stub.
306 void UpdateNativeMethodsCodeToJitCode(ArtMethod* method, const void* quick_code)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800307 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!GetDeoptimizedMethodsLock());
Nicolas Geoffraya6e0e7d2018-01-26 13:16:50 +0000308
Alex Light0a5ec3d2017-07-25 16:50:26 -0700309 // Update the code of a method to the interpreter respecting any installed stubs from debugger.
310 void UpdateMethodsCodeToInterpreterEntryPoint(ArtMethod* method)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800311 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!GetDeoptimizedMethodsLock());
Alex Light0a5ec3d2017-07-25 16:50:26 -0700312
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700313 // Update the code of a method respecting any installed stubs from debugger.
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000314 void UpdateMethodsCodeForJavaDebuggable(ArtMethod* method, const void* quick_code)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800315 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!GetDeoptimizedMethodsLock());
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700316
Alex Light2d441b12018-06-08 15:33:21 -0700317 // Return the code that we can execute for an invoke including from the JIT.
318 const void* GetCodeForInvoke(ArtMethod* method) const
319 REQUIRES_SHARED(Locks::mutator_lock_);
320
Ian Rogers62d6c772013-02-27 08:32:07 -0800321 // Get the quick code for the given method. More efficient than asking the class linker as it
322 // will short-cut to GetCode if instrumentation and static method resolution stubs aren't
323 // installed.
Andreas Gampe542451c2016-07-26 09:02:02 -0700324 const void* GetQuickCodeFor(ArtMethod* method, PointerSize pointer_size) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700325 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800326
327 void ForceInterpretOnly() {
328 interpret_only_ = true;
329 forced_interpret_only_ = true;
330 }
331
Brian Carlstromea46f952013-07-30 01:26:50 -0700332 // Called by ArtMethod::Invoke to determine dispatch mechanism.
Ian Rogers62d6c772013-02-27 08:32:07 -0800333 bool InterpretOnly() const {
334 return interpret_only_;
335 }
336
Hiroshi Yamauchi563b47c2014-02-28 17:18:37 -0800337 bool IsForcedInterpretOnly() const {
338 return forced_interpret_only_;
339 }
340
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800341 // Code is in boot image oat file which isn't compiled as debuggable.
342 // Need debug version (interpreter or jitted) if that's the case.
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000343 bool NeedDebugVersionFor(ArtMethod* method) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700344 REQUIRES_SHARED(Locks::mutator_lock_);
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800345
Ian Rogers62d6c772013-02-27 08:32:07 -0800346 bool AreExitStubsInstalled() const {
347 return instrumentation_stubs_installed_;
348 }
349
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700350 bool HasMethodEntryListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200351 return have_method_entry_listeners_;
352 }
353
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700354 bool HasMethodExitListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200355 return have_method_exit_listeners_;
356 }
357
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700358 bool HasMethodUnwindListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200359 return have_method_unwind_listeners_;
360 }
361
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700362 bool HasDexPcListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200363 return have_dex_pc_listeners_;
364 }
365
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700366 bool HasFieldReadListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200367 return have_field_read_listeners_;
368 }
369
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700370 bool HasFieldWriteListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200371 return have_field_write_listeners_;
372 }
373
Alex Light6e1607e2017-08-23 10:06:18 -0700374 bool HasExceptionThrownListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
375 return have_exception_thrown_listeners_;
Sebastien Hertz9f102032014-05-23 08:59:42 +0200376 }
377
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700378 bool HasBranchListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000379 return have_branch_listeners_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800380 }
381
Alex Lighte814f9d2017-07-31 16:14:39 -0700382 bool HasWatchedFramePopListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
383 return have_watched_frame_pop_listeners_;
384 }
385
Alex Light9fb1ab12017-09-05 09:32:49 -0700386 bool HasExceptionHandledListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
387 return have_exception_handled_listeners_;
388 }
389
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700390 bool IsActive() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200391 return have_dex_pc_listeners_ || have_method_entry_listeners_ || have_method_exit_listeners_ ||
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200392 have_field_read_listeners_ || have_field_write_listeners_ ||
Alex Light6e1607e2017-08-23 10:06:18 -0700393 have_exception_thrown_listeners_ || have_method_unwind_listeners_ ||
David Srbecky99f97332018-10-03 15:44:24 +0100394 have_branch_listeners_ || have_watched_frame_pop_listeners_ ||
395 have_exception_handled_listeners_;
Bill Buzbeefd522f92016-02-11 22:37:42 +0000396 }
397
Ian Rogers62d6c772013-02-27 08:32:07 -0800398 // Inform listeners that a method has been entered. A dex PC is provided as we may install
399 // listeners into executing code and get method enter events for methods already on the stack.
Vladimir Marko19711d42019-04-12 14:05:34 +0100400 void MethodEnterEvent(Thread* thread,
401 ObjPtr<mirror::Object> this_object,
402 ArtMethod* method,
403 uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700404 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200405 if (UNLIKELY(HasMethodEntryListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800406 MethodEnterEventImpl(thread, this_object, method, dex_pc);
407 }
408 }
409
410 // Inform listeners that a method has been exited.
Alex Lightb7c640d2019-03-20 15:52:13 -0700411 template<typename T>
Alex Lightd7661582017-05-01 13:48:16 -0700412 void MethodExitEvent(Thread* thread,
Vladimir Marko19711d42019-04-12 14:05:34 +0100413 ObjPtr<mirror::Object> this_object,
Alex Lightd7661582017-05-01 13:48:16 -0700414 ArtMethod* method,
415 uint32_t dex_pc,
Alex Lightb7c640d2019-03-20 15:52:13 -0700416 OptionalFrame frame,
417 T& return_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700418 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200419 if (UNLIKELY(HasMethodExitListeners())) {
Alex Lightb7c640d2019-03-20 15:52:13 -0700420 MethodExitEventImpl(thread, this_object, method, dex_pc, frame, return_value);
Ian Rogers62d6c772013-02-27 08:32:07 -0800421 }
422 }
423
424 // Inform listeners that a method has been exited due to an exception.
Vladimir Marko19711d42019-04-12 14:05:34 +0100425 void MethodUnwindEvent(Thread* thread,
426 ObjPtr<mirror::Object> this_object,
427 ArtMethod* method,
428 uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700429 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800430
431 // Inform listeners that the dex pc has moved (only supported by the interpreter).
Vladimir Marko19711d42019-04-12 14:05:34 +0100432 void DexPcMovedEvent(Thread* thread,
433 ObjPtr<mirror::Object> this_object,
434 ArtMethod* method,
435 uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700436 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200437 if (UNLIKELY(HasDexPcListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800438 DexPcMovedEventImpl(thread, this_object, method, dex_pc);
439 }
440 }
441
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000442 // Inform listeners that a branch has been taken (only supported by the interpreter).
443 void Branch(Thread* thread, ArtMethod* method, uint32_t dex_pc, int32_t offset) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700444 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000445 if (UNLIKELY(HasBranchListeners())) {
446 BranchImpl(thread, method, dex_pc, offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800447 }
448 }
449
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200450 // Inform listeners that we read a field (only supported by the interpreter).
Vladimir Marko19711d42019-04-12 14:05:34 +0100451 void FieldReadEvent(Thread* thread,
452 ObjPtr<mirror::Object> this_object,
453 ArtMethod* method,
454 uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700455 ArtField* field) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700456 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200457 if (UNLIKELY(HasFieldReadListeners())) {
458 FieldReadEventImpl(thread, this_object, method, dex_pc, field);
459 }
460 }
461
462 // Inform listeners that we write a field (only supported by the interpreter).
Vladimir Marko19711d42019-04-12 14:05:34 +0100463 void FieldWriteEvent(Thread* thread,
464 ObjPtr<mirror::Object> this_object,
465 ArtMethod* method,
466 uint32_t dex_pc,
467 ArtField* field,
468 const JValue& field_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700469 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200470 if (UNLIKELY(HasFieldWriteListeners())) {
471 FieldWriteEventImpl(thread, this_object, method, dex_pc, field, field_value);
472 }
473 }
474
Alex Lighte814f9d2017-07-31 16:14:39 -0700475 // Inform listeners that a branch has been taken (only supported by the interpreter).
476 void WatchedFramePopped(Thread* thread, const ShadowFrame& frame) const
477 REQUIRES_SHARED(Locks::mutator_lock_) {
478 if (UNLIKELY(HasWatchedFramePopListeners())) {
479 WatchedFramePopImpl(thread, frame);
480 }
481 }
482
Alex Light6e1607e2017-08-23 10:06:18 -0700483 // Inform listeners that an exception was thrown.
Vladimir Marko19711d42019-04-12 14:05:34 +0100484 void ExceptionThrownEvent(Thread* thread, ObjPtr<mirror::Throwable> exception_object) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700485 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800486
Alex Light9fb1ab12017-09-05 09:32:49 -0700487 // Inform listeners that an exception has been handled. This is not sent for native code or for
488 // exceptions which reach the end of the thread's stack.
Vladimir Marko19711d42019-04-12 14:05:34 +0100489 void ExceptionHandledEvent(Thread* thread, ObjPtr<mirror::Throwable> exception_object) const
Alex Light9fb1ab12017-09-05 09:32:49 -0700490 REQUIRES_SHARED(Locks::mutator_lock_);
491
Ian Rogers62d6c772013-02-27 08:32:07 -0800492 // Called when an instrumented method is entered. The intended link register (lr) is saved so
493 // that returning causes a branch to the method exit stub. Generates method enter events.
Vladimir Marko19711d42019-04-12 14:05:34 +0100494 void PushInstrumentationStackFrame(Thread* self,
495 ObjPtr<mirror::Object> this_object,
496 ArtMethod* method,
497 uintptr_t lr,
Jeff Hao9a916d32013-06-27 18:45:37 -0700498 bool interpreter_entry)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700499 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800500
Mingyao Yang2ee17902017-08-30 11:37:08 -0700501 DeoptimizationMethodType GetDeoptimizationMethodType(ArtMethod* method)
502 REQUIRES_SHARED(Locks::mutator_lock_);
503
Ian Rogers62d6c772013-02-27 08:32:07 -0800504 // Called when an instrumented method is exited. Removes the pushed instrumentation frame
Alex Lightb7edcda2017-04-27 13:20:31 -0700505 // returning the intended link register. Generates method exit events. The gpr_result and
506 // fpr_result pointers are pointers to the locations where the integer/pointer and floating point
507 // result values of the function are stored. Both pointers must always be valid but the values
508 // held there will only be meaningful if interpreted as the appropriate type given the function
509 // being returned from.
Andreas Gamped58342c2014-06-05 14:18:08 -0700510 TwoWordReturn PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc,
Alex Lightb7edcda2017-04-27 13:20:31 -0700511 uint64_t* gpr_result, uint64_t* fpr_result)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800512 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!GetDeoptimizedMethodsLock());
Ian Rogers62d6c772013-02-27 08:32:07 -0800513
Alex Light2c8206f2018-06-08 14:51:09 -0700514 // Pops nframes instrumentation frames from the current thread. Returns the return pc for the last
515 // instrumentation frame that's popped.
516 uintptr_t PopFramesForDeoptimization(Thread* self, size_t nframes) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700517 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800518
519 // Call back for configure stubs.
Vladimir Marko19711d42019-04-12 14:05:34 +0100520 void InstallStubsForClass(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800521 REQUIRES(!GetDeoptimizedMethodsLock());
jeffhao725a9572012-11-13 18:20:12 -0800522
Mathieu Chartiere401d142015-04-22 13:56:20 -0700523 void InstallStubsForMethod(ArtMethod* method)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800524 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!GetDeoptimizedMethodsLock());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100525
Alex Light40607862019-05-06 18:16:24 +0000526 // Sets up instrumentation to allow single thread deoptimization using ForceInterpreterCount.
527 void EnableSingleThreadDeopt()
528 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
529 REQUIRES(!Locks::thread_list_lock_,
530 !Locks::classlinker_classes_lock_,
531 !GetDeoptimizedMethodsLock());
532
Mingyao Yang99170c62015-07-06 11:10:37 -0700533 // Install instrumentation exit stub on every method of the stack of the given thread.
534 // This is used by the debugger to cause a deoptimization of the thread's stack after updating
535 // local variable(s).
536 void InstrumentThreadStack(Thread* thread)
Alex Light3ae82532017-07-26 13:59:07 -0700537 REQUIRES_SHARED(Locks::mutator_lock_);
Mingyao Yang99170c62015-07-06 11:10:37 -0700538
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000539 static size_t ComputeFrameId(Thread* self,
540 size_t frame_depth,
541 size_t inlined_frames_before_frame)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700542 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000543
Mathieu Chartiereebc3af2016-02-29 18:13:38 -0800544 // Does not hold lock, used to check if someone changed from not instrumented to instrumented
545 // during a GC suspend point.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700546 bool AllocEntrypointsInstrumented() const REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier50e93312016-03-16 11:25:29 -0700547 return alloc_entrypoints_instrumented_;
Mathieu Chartiereebc3af2016-02-29 18:13:38 -0800548 }
549
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200550 InstrumentationLevel GetCurrentInstrumentationLevel() const;
551
Alex Lightdba61482016-12-21 08:20:29 -0800552 private:
553 // Returns true if moving to the given instrumentation level requires the installation of stubs.
554 // False otherwise.
555 bool RequiresInstrumentationInstallation(InstrumentationLevel new_level) const;
556
Ian Rogers62d6c772013-02-27 08:32:07 -0800557 // Does the job of installing or removing instrumentation code within methods.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200558 // In order to support multiple clients using instrumentation at the same time,
559 // the caller must pass a unique key (a string) identifying it so we remind which
560 // instrumentation level it needs. Therefore the current instrumentation level
561 // becomes the highest instrumentation level required by a client.
562 void ConfigureStubs(const char* key, InstrumentationLevel desired_instrumentation_level)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700563 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800564 REQUIRES(!GetDeoptimizedMethodsLock(),
Mathieu Chartieraa516822015-10-02 15:53:37 -0700565 !Locks::thread_list_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700566 !Locks::classlinker_classes_lock_);
Alex Light40607862019-05-06 18:16:24 +0000567 void UpdateStubs() REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
568 REQUIRES(!GetDeoptimizedMethodsLock(),
569 !Locks::thread_list_lock_,
570 !Locks::classlinker_classes_lock_);
571 void UpdateInstrumentationLevels(InstrumentationLevel level)
572 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
573 REQUIRES(!GetDeoptimizedMethodsLock(),
574 !Locks::thread_list_lock_,
575 !Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800576
Mathieu Chartier90443472015-07-16 20:32:27 -0700577 void UpdateInterpreterHandlerTable() REQUIRES(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800578 /*
579 * TUNING: Dalvik's mterp stashes the actual current handler table base in a
580 * tls field. For Arm, this enables all suspend, debug & tracing checks to be
581 * collapsed into a single conditionally-executed ldw instruction.
582 * Move to Dalvik-style handler-table management for both the goto interpreter and
583 * mterp.
584 */
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200585 interpreter_handler_table_ = IsActive() ? kAlternativeHandlerTable : kMainHandlerTable;
586 }
587
Mathieu Chartier661974a2014-01-09 11:23:53 -0800588 // No thread safety analysis to get around SetQuickAllocEntryPointsInstrumented requiring
589 // exclusive access to mutator lock which you can't get if the runtime isn't started.
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700590 void SetEntrypointsInstrumented(bool instrumented) NO_THREAD_SAFETY_ANALYSIS;
Mathieu Chartier661974a2014-01-09 11:23:53 -0800591
Alex Lightd7661582017-05-01 13:48:16 -0700592 void MethodEnterEventImpl(Thread* thread,
593 ObjPtr<mirror::Object> this_object,
594 ArtMethod* method,
595 uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700596 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lightb7c640d2019-03-20 15:52:13 -0700597 template <typename T>
Alex Lightd7661582017-05-01 13:48:16 -0700598 void MethodExitEventImpl(Thread* thread,
599 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700600 ArtMethod* method,
Alex Lightd7661582017-05-01 13:48:16 -0700601 uint32_t dex_pc,
Alex Lightb7c640d2019-03-20 15:52:13 -0700602 OptionalFrame frame,
603 T& return_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700604 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lightd7661582017-05-01 13:48:16 -0700605 void DexPcMovedEventImpl(Thread* thread,
606 ObjPtr<mirror::Object> this_object,
607 ArtMethod* method,
608 uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700609 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000610 void BranchImpl(Thread* thread, ArtMethod* method, uint32_t dex_pc, int32_t offset) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700611 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lighte814f9d2017-07-31 16:14:39 -0700612 void WatchedFramePopImpl(Thread* thread, const ShadowFrame& frame) const
613 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lightd7661582017-05-01 13:48:16 -0700614 void FieldReadEventImpl(Thread* thread,
615 ObjPtr<mirror::Object> this_object,
616 ArtMethod* method,
617 uint32_t dex_pc,
618 ArtField* field) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700619 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lightd7661582017-05-01 13:48:16 -0700620 void FieldWriteEventImpl(Thread* thread,
621 ObjPtr<mirror::Object> this_object,
622 ArtMethod* method,
623 uint32_t dex_pc,
624 ArtField* field,
625 const JValue& field_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700626 REQUIRES_SHARED(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800627
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700628 // Read barrier-aware utility functions for accessing deoptimized_methods_
Mathieu Chartiere401d142015-04-22 13:56:20 -0700629 bool AddDeoptimizedMethod(ArtMethod* method)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800630 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(GetDeoptimizedMethodsLock());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700631 bool IsDeoptimizedMethod(ArtMethod* method)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800632 REQUIRES_SHARED(Locks::mutator_lock_, GetDeoptimizedMethodsLock());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700633 bool RemoveDeoptimizedMethod(ArtMethod* method)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800634 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(GetDeoptimizedMethodsLock());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700635 ArtMethod* BeginDeoptimizedMethod()
Andreas Gampe7e56a072018-11-29 10:40:06 -0800636 REQUIRES_SHARED(Locks::mutator_lock_, GetDeoptimizedMethodsLock());
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700637 bool IsDeoptimizedMethodsEmpty() const
Andreas Gampe7e56a072018-11-29 10:40:06 -0800638 REQUIRES_SHARED(Locks::mutator_lock_, GetDeoptimizedMethodsLock());
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700639 void UpdateMethodsCodeImpl(ArtMethod* method, const void* quick_code)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800640 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!GetDeoptimizedMethodsLock());
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700641
Andreas Gampe7e56a072018-11-29 10:40:06 -0800642 ReaderWriterMutex* GetDeoptimizedMethodsLock() const {
643 return deoptimized_methods_lock_.get();
644 }
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700645
Brian Carlstromea46f952013-07-30 01:26:50 -0700646 // Have we hijacked ArtMethod::code_ so that it calls instrumentation/interpreter code?
Ian Rogers62d6c772013-02-27 08:32:07 -0800647 bool instrumentation_stubs_installed_;
648
Brian Carlstromea46f952013-07-30 01:26:50 -0700649 // Have we hijacked ArtMethod::code_ to reference the enter/exit stubs?
Ian Rogers62d6c772013-02-27 08:32:07 -0800650 bool entry_exit_stubs_installed_;
651
Brian Carlstromea46f952013-07-30 01:26:50 -0700652 // Have we hijacked ArtMethod::code_ to reference the enter interpreter stub?
Ian Rogers62d6c772013-02-27 08:32:07 -0800653 bool interpreter_stubs_installed_;
654
655 // Do we need the fidelity of events that we only get from running within the interpreter?
656 bool interpret_only_;
657
658 // Did the runtime request we only run in the interpreter? ie -Xint mode.
659 bool forced_interpret_only_;
660
661 // Do we have any listeners for method entry events? Short-cut to avoid taking the
662 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200663 bool have_method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800664
665 // Do we have any listeners for method exit events? Short-cut to avoid taking the
666 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200667 bool have_method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800668
669 // Do we have any listeners for method unwind events? Short-cut to avoid taking the
670 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200671 bool have_method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800672
673 // Do we have any listeners for dex move events? Short-cut to avoid taking the
674 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200675 bool have_dex_pc_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800676
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200677 // Do we have any listeners for field read events? Short-cut to avoid taking the
678 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200679 bool have_field_read_listeners_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200680
681 // Do we have any listeners for field write events? Short-cut to avoid taking the
682 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200683 bool have_field_write_listeners_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200684
Alex Light6e1607e2017-08-23 10:06:18 -0700685 // Do we have any exception thrown listeners? Short-cut to avoid taking the instrumentation_lock_.
686 bool have_exception_thrown_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800687
Alex Lighte814f9d2017-07-31 16:14:39 -0700688 // Do we have any frame pop listeners? Short-cut to avoid taking the instrumentation_lock_.
689 bool have_watched_frame_pop_listeners_ GUARDED_BY(Locks::mutator_lock_);
690
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000691 // Do we have any branch listeners? Short-cut to avoid taking the instrumentation_lock_.
692 bool have_branch_listeners_ GUARDED_BY(Locks::mutator_lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800693
Alex Light9fb1ab12017-09-05 09:32:49 -0700694 // Do we have any exception handled listeners? Short-cut to avoid taking the
695 // instrumentation_lock_.
696 bool have_exception_handled_listeners_ GUARDED_BY(Locks::mutator_lock_);
697
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200698 // Contains the instrumentation level required by each client of the instrumentation identified
699 // by a string key.
700 typedef SafeMap<const char*, InstrumentationLevel> InstrumentationLevelTable;
701 InstrumentationLevelTable requested_instrumentation_levels_ GUARDED_BY(Locks::mutator_lock_);
702
Ian Rogers62d6c772013-02-27 08:32:07 -0800703 // The event listeners, written to with the mutator_lock_ exclusively held.
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000704 // Mutators must be able to iterate over these lists concurrently, that is, with listeners being
705 // added or removed while iterating. The modifying thread holds exclusive lock,
706 // so other threads cannot iterate (i.e. read the data of the list) at the same time but they
707 // do keep iterators that need to remain valid. This is the reason these listeners are std::list
708 // and not for example std::vector: the existing storage for a std::list does not move.
709 // Note that mutators cannot make a copy of these lists before iterating, as the instrumentation
710 // listeners can also be deleted concurrently.
711 // As a result, these lists are never trimmed. That's acceptable given the low number of
712 // listeners we have.
Ian Rogers62d6c772013-02-27 08:32:07 -0800713 std::list<InstrumentationListener*> method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_);
714 std::list<InstrumentationListener*> method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_);
715 std::list<InstrumentationListener*> method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000716 std::list<InstrumentationListener*> branch_listeners_ GUARDED_BY(Locks::mutator_lock_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000717 std::list<InstrumentationListener*> dex_pc_listeners_ GUARDED_BY(Locks::mutator_lock_);
718 std::list<InstrumentationListener*> field_read_listeners_ GUARDED_BY(Locks::mutator_lock_);
719 std::list<InstrumentationListener*> field_write_listeners_ GUARDED_BY(Locks::mutator_lock_);
Alex Light6e1607e2017-08-23 10:06:18 -0700720 std::list<InstrumentationListener*> exception_thrown_listeners_ GUARDED_BY(Locks::mutator_lock_);
Alex Lighte814f9d2017-07-31 16:14:39 -0700721 std::list<InstrumentationListener*> watched_frame_pop_listeners_ GUARDED_BY(Locks::mutator_lock_);
Alex Light9fb1ab12017-09-05 09:32:49 -0700722 std::list<InstrumentationListener*> exception_handled_listeners_ GUARDED_BY(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800723
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100724 // The set of methods being deoptimized (by the debugger) which must be executed with interpreter
725 // only.
Andreas Gampe7e56a072018-11-29 10:40:06 -0800726 mutable std::unique_ptr<ReaderWriterMutex> deoptimized_methods_lock_ BOTTOM_MUTEX_ACQUIRED_AFTER;
727 std::unordered_set<ArtMethod*> deoptimized_methods_ GUARDED_BY(GetDeoptimizedMethodsLock());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100728 bool deoptimization_enabled_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100729
Ian Rogersfa824272013-11-05 16:12:57 -0800730 // Current interpreter handler table. This is updated each time the thread state flags are
731 // modified.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200732 InterpreterHandlerTable interpreter_handler_table_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200733
Ian Rogersfa824272013-11-05 16:12:57 -0800734 // Greater than 0 if quick alloc entry points instrumented.
Mathieu Chartiereebc3af2016-02-29 18:13:38 -0800735 size_t quick_alloc_entry_points_instrumentation_counter_;
Mathieu Chartier50e93312016-03-16 11:25:29 -0700736
737 // alloc_entrypoints_instrumented_ is only updated with all the threads suspended, this is done
738 // to prevent races with the GC where the GC relies on thread suspension only see
739 // alloc_entrypoints_instrumented_ change during suspend points.
740 bool alloc_entrypoints_instrumented_;
741
Alex Light40607862019-05-06 18:16:24 +0000742 // If we can use instrumentation trampolines. After the first time we instrument something with
743 // the interpreter we can no longer use trampolines because it can lead to stack corruption.
744 // TODO Figure out a way to remove the need for this.
745 bool can_use_instrumentation_trampolines_;
746
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200747 friend class InstrumentationTest; // For GetCurrentInstrumentationLevel and ConfigureStubs.
Alex Light2c8206f2018-06-08 14:51:09 -0700748 friend class InstrumentationStackPopper; // For popping instrumentation frames.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200749
jeffhao725a9572012-11-13 18:20:12 -0800750 DISALLOW_COPY_AND_ASSIGN(Instrumentation);
751};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700752std::ostream& operator<<(std::ostream& os, const Instrumentation::InstrumentationEvent& rhs);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200753std::ostream& operator<<(std::ostream& os, const Instrumentation::InstrumentationLevel& rhs);
jeffhao725a9572012-11-13 18:20:12 -0800754
Ian Rogers62d6c772013-02-27 08:32:07 -0800755// An element in the instrumentation side stack maintained in art::Thread.
756struct InstrumentationStackFrame {
Mingyao Yang2ee17902017-08-30 11:37:08 -0700757 InstrumentationStackFrame(mirror::Object* this_object,
758 ArtMethod* method,
759 uintptr_t return_pc,
760 size_t frame_id,
761 bool interpreter_entry)
762 : this_object_(this_object),
763 method_(method),
764 return_pc_(return_pc),
765 frame_id_(frame_id),
Jeff Hao9a916d32013-06-27 18:45:37 -0700766 interpreter_entry_(interpreter_entry) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800767 }
768
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700769 std::string Dump() const REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800770
771 mirror::Object* this_object_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700772 ArtMethod* method_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100773 uintptr_t return_pc_;
774 size_t frame_id_;
775 bool interpreter_entry_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800776};
777
778} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -0800779} // namespace art
780
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700781#endif // ART_RUNTIME_INSTRUMENTATION_H_