blob: fc64c49f7adea1a1ddc8b1e6d13ec8565957d2d6 [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
Ian Rogers576ca0c2014-06-06 15:58:22 -070020#include <stdint.h>
Ian Rogers576ca0c2014-06-06 15:58:22 -070021#include <list>
Andreas Gampe7e56a072018-11-29 10:40:06 -080022#include <memory>
Mathieu Chartiere401d142015-04-22 13:56:20 -070023#include <unordered_set>
Ian Rogers576ca0c2014-06-06 15:58:22 -070024
Ian Rogersd582fa42014-11-05 23:46:43 -080025#include "arch/instruction_set.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070026#include "base/enums.h"
Andreas Gampe7e56a072018-11-29 10:40:06 -080027#include "base/locks.h"
Elliott Hughes76160052012-12-12 16:31:20 -080028#include "base/macros.h"
David Sehr67bf42e2018-02-26 16:43:04 -080029#include "base/safe_map.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070030#include "gc_root.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031
jeffhao725a9572012-11-13 18:20:12 -080032namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033namespace mirror {
Igor Murashkin2ffb7032017-11-08 13:35:21 -080034class Class;
35class Object;
36class Throwable;
Ian Rogers62d6c772013-02-27 08:32:07 -080037} // namespace mirror
Mathieu Chartierc7853442015-03-27 14:35:38 -070038class ArtField;
Mathieu Chartiere401d142015-04-22 13:56:20 -070039class ArtMethod;
Alex Lightd7661582017-05-01 13:48:16 -070040template <typename T> class Handle;
Alex Light2c8206f2018-06-08 14:51:09 -070041template <typename T> class MutableHandle;
Ian Rogers62d6c772013-02-27 08:32:07 -080042union JValue;
Andreas Gampe7e56a072018-11-29 10:40:06 -080043class SHARED_LOCKABLE ReaderWriterMutex;
Alex Lighte814f9d2017-07-31 16:14:39 -070044class ShadowFrame;
jeffhao725a9572012-11-13 18:20:12 -080045class Thread;
Mingyao Yang2ee17902017-08-30 11:37:08 -070046enum class DeoptimizationMethodType;
jeffhao725a9572012-11-13 18:20:12 -080047
Ian Rogers62d6c772013-02-27 08:32:07 -080048namespace instrumentation {
jeffhao725a9572012-11-13 18:20:12 -080049
Sebastien Hertzee1997a2013-09-19 14:47:09 +020050// Interpreter handler tables.
51enum InterpreterHandlerTable {
52 kMainHandlerTable = 0, // Main handler table: no suspend check, no instrumentation.
53 kAlternativeHandlerTable = 1, // Alternative handler table: suspend check and/or instrumentation
54 // enabled.
55 kNumHandlerTables
56};
57
Andreas Gampe40da2862015-02-27 12:49:04 -080058// Do we want to deoptimize for method entry and exit listeners or just try to intercept
59// invocations? Deoptimization forces all code to run in the interpreter and considerably hurts the
60// application's performance.
61static constexpr bool kDeoptimizeForAccurateMethodEntryExitListeners = true;
62
Ian Rogers62d6c772013-02-27 08:32:07 -080063// Instrumentation event listener API. Registered listeners will get the appropriate call back for
64// the events they are listening for. The call backs supply the thread, method and dex_pc the event
65// occurred upon. The thread may or may not be Thread::Current().
66struct InstrumentationListener {
67 InstrumentationListener() {}
68 virtual ~InstrumentationListener() {}
69
70 // Call-back for when a method is entered.
Alex Lightd7661582017-05-01 13:48:16 -070071 virtual void MethodEntered(Thread* thread,
72 Handle<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -070073 ArtMethod* method,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070074 uint32_t dex_pc) REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -080075
Alex Lightd7661582017-05-01 13:48:16 -070076 virtual void MethodExited(Thread* thread,
77 Handle<mirror::Object> this_object,
78 ArtMethod* method,
79 uint32_t dex_pc,
80 Handle<mirror::Object> return_value)
81 REQUIRES_SHARED(Locks::mutator_lock_);
82
83 // Call-back for when a method is exited. The implementor should either handler-ize the return
84 // value (if appropriate) or use the alternate MethodExited callback instead if they need to
85 // go through a suspend point.
86 virtual void MethodExited(Thread* thread,
87 Handle<mirror::Object> this_object,
88 ArtMethod* method,
89 uint32_t dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -080090 const JValue& return_value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070091 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -080092
93 // Call-back for when a method is popped due to an exception throw. A method will either cause a
94 // MethodExited call-back or a MethodUnwind call-back when its activation is removed.
Alex Lightd7661582017-05-01 13:48:16 -070095 virtual void MethodUnwind(Thread* thread,
96 Handle<mirror::Object> this_object,
97 ArtMethod* method,
98 uint32_t dex_pc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070099 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -0800100
101 // Call-back for when the dex pc moves in a method.
Alex Lightd7661582017-05-01 13:48:16 -0700102 virtual void DexPcMoved(Thread* thread,
103 Handle<mirror::Object> this_object,
104 ArtMethod* method,
105 uint32_t new_dex_pc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700106 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -0800107
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200108 // Call-back for when we read from a field.
Alex Lightd7661582017-05-01 13:48:16 -0700109 virtual void FieldRead(Thread* thread,
110 Handle<mirror::Object> this_object,
111 ArtMethod* method,
112 uint32_t dex_pc,
113 ArtField* field) = 0;
114
115 virtual void FieldWritten(Thread* thread,
116 Handle<mirror::Object> this_object,
117 ArtMethod* method,
118 uint32_t dex_pc,
119 ArtField* field,
120 Handle<mirror::Object> field_value)
121 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200122
123 // Call-back for when we write into a field.
Alex Lightd7661582017-05-01 13:48:16 -0700124 virtual void FieldWritten(Thread* thread,
125 Handle<mirror::Object> this_object,
126 ArtMethod* method,
127 uint32_t dex_pc,
128 ArtField* field,
129 const JValue& field_value)
130 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200131
Alex Light6e1607e2017-08-23 10:06:18 -0700132 // Call-back when an exception is thrown.
133 virtual void ExceptionThrown(Thread* thread,
Alex Lightd7661582017-05-01 13:48:16 -0700134 Handle<mirror::Throwable> exception_object)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700135 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800136
Alex Light9fb1ab12017-09-05 09:32:49 -0700137 // Call-back when an exception is caught/handled by java code.
138 virtual void ExceptionHandled(Thread* thread, Handle<mirror::Throwable> exception_object)
139 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
140
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000141 // Call-back for when we execute a branch.
142 virtual void Branch(Thread* thread,
143 ArtMethod* method,
144 uint32_t dex_pc,
145 int32_t dex_pc_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700146 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100147
Alex Lighte814f9d2017-07-31 16:14:39 -0700148 // Call-back when a shadow_frame with the needs_notify_pop_ boolean set is popped off the stack by
149 // either return or exceptions. Normally instrumentation listeners should ensure that there are
150 // shadow-frames by deoptimizing stacks.
151 virtual void WatchedFramePop(Thread* thread ATTRIBUTE_UNUSED,
152 const ShadowFrame& frame ATTRIBUTE_UNUSED)
Alex Light05f47742017-09-14 00:34:44 +0000153 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
jeffhao725a9572012-11-13 18:20:12 -0800154};
155
Alex Light2c8206f2018-06-08 14:51:09 -0700156class Instrumentation;
157// A helper to send instrumentation events while popping the stack in a safe way.
158class InstrumentationStackPopper {
159 public:
160 explicit InstrumentationStackPopper(Thread* self);
161 ~InstrumentationStackPopper() REQUIRES_SHARED(Locks::mutator_lock_);
162
163 // Increase the number of frames being popped to 'desired_pops' return true if the frames were
164 // popped without any exceptions, false otherwise. The exception that caused the pop is
165 // 'exception'.
166 bool PopFramesTo(uint32_t desired_pops, /*in-out*/MutableHandle<mirror::Throwable>& exception)
167 REQUIRES_SHARED(Locks::mutator_lock_);
168
169 private:
170 Thread* self_;
171 Instrumentation* instrumentation_;
172 uint32_t frames_to_remove_;
173};
174
Ian Rogers62d6c772013-02-27 08:32:07 -0800175// Instrumentation is a catch-all for when extra information is required from the runtime. The
176// typical use for instrumentation is for profiling and debugging. Instrumentation may add stubs
177// to method entry and exit, it may also force execution to be switched to the interpreter and
178// trigger deoptimization.
jeffhao725a9572012-11-13 18:20:12 -0800179class Instrumentation {
180 public:
Ian Rogers62d6c772013-02-27 08:32:07 -0800181 enum InstrumentationEvent {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800182 kMethodEntered = 0x1,
183 kMethodExited = 0x2,
184 kMethodUnwind = 0x4,
185 kDexPcMoved = 0x8,
186 kFieldRead = 0x10,
187 kFieldWritten = 0x20,
Alex Light6e1607e2017-08-23 10:06:18 -0700188 kExceptionThrown = 0x40,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000189 kBranch = 0x80,
Alex Lighte814f9d2017-07-31 16:14:39 -0700190 kWatchedFramePop = 0x200,
Alex Light9fb1ab12017-09-05 09:32:49 -0700191 kExceptionHandled = 0x400,
Ian Rogers62d6c772013-02-27 08:32:07 -0800192 };
jeffhao725a9572012-11-13 18:20:12 -0800193
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200194 enum class InstrumentationLevel {
195 kInstrumentNothing, // execute without instrumentation
196 kInstrumentWithInstrumentationStubs, // execute with instrumentation entry/exit stubs
197 kInstrumentWithInterpreter // execute with interpreter
198 };
199
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700200 Instrumentation();
jeffhao725a9572012-11-13 18:20:12 -0800201
Ian Rogers62d6c772013-02-27 08:32:07 -0800202 // Add a listener to be notified of the masked together sent of instrumentation events. This
203 // suspend the runtime to install stubs. You are expected to hold the mutator lock as a proxy
204 // for saying you should have suspended all threads (installing stubs while threads are running
205 // will break).
206 void AddListener(InstrumentationListener* listener, uint32_t events)
Mathieu Chartier90443472015-07-16 20:32:27 -0700207 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800208
Ian Rogers62d6c772013-02-27 08:32:07 -0800209 // Removes a listener possibly removing instrumentation stubs.
210 void RemoveListener(InstrumentationListener* listener, uint32_t events)
Mathieu Chartier90443472015-07-16 20:32:27 -0700211 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800212
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100213 // Deoptimization.
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100214 void EnableDeoptimization()
Mathieu Chartieraa516822015-10-02 15:53:37 -0700215 REQUIRES(Locks::mutator_lock_)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800216 REQUIRES(!GetDeoptimizedMethodsLock());
Mathieu Chartieraa516822015-10-02 15:53:37 -0700217 // Calls UndeoptimizeEverything which may visit class linker classes through ConfigureStubs.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200218 void DisableDeoptimization(const char* key)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700219 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800220 REQUIRES(!GetDeoptimizedMethodsLock());
Mathieu Chartieraa516822015-10-02 15:53:37 -0700221
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100222 bool AreAllMethodsDeoptimized() const {
223 return interpreter_stubs_installed_;
224 }
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700225 bool ShouldNotifyMethodEnterExitEvents() const REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100226
Alex Lightbebd7bd2017-07-25 14:05:52 -0700227 bool CanDeoptimize() {
228 return deoptimization_enabled_;
229 }
230
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100231 // Executes everything with interpreter.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200232 void DeoptimizeEverything(const char* key)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700233 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
234 REQUIRES(!Locks::thread_list_lock_,
235 !Locks::classlinker_classes_lock_,
Andreas Gampe7e56a072018-11-29 10:40:06 -0800236 !GetDeoptimizedMethodsLock());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100237
Mathieu Chartieraa516822015-10-02 15:53:37 -0700238 // Executes everything with compiled code (or interpreter if there is no code). May visit class
239 // linker classes through ConfigureStubs.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200240 void UndeoptimizeEverything(const char* key)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700241 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
242 REQUIRES(!Locks::thread_list_lock_,
243 !Locks::classlinker_classes_lock_,
Andreas Gampe7e56a072018-11-29 10:40:06 -0800244 !GetDeoptimizedMethodsLock());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100245
246 // Deoptimize a method by forcing its execution with the interpreter. Nevertheless, a static
247 // method (except a class initializer) set to the resolution trampoline will be deoptimized only
248 // once its declaring class is initialized.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700249 void Deoptimize(ArtMethod* method)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800250 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !GetDeoptimizedMethodsLock());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100251
252 // Undeoptimze the method by restoring its entrypoints. Nevertheless, a static method
253 // (except a class initializer) set to the resolution trampoline will be updated only once its
254 // declaring class is initialized.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700255 void Undeoptimize(ArtMethod* method)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800256 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !GetDeoptimizedMethodsLock());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100257
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200258 // Indicates whether the method has been deoptimized so it is executed with the interpreter.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700259 bool IsDeoptimized(ArtMethod* method)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800260 REQUIRES(!GetDeoptimizedMethodsLock()) REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100261
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200262 // Enable method tracing by installing instrumentation entry/exit stubs or interpreter.
263 void EnableMethodTracing(const char* key,
264 bool needs_interpreter = kDeoptimizeForAccurateMethodEntryExitListeners)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700265 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
266 REQUIRES(!Locks::thread_list_lock_,
267 !Locks::classlinker_classes_lock_,
Andreas Gampe7e56a072018-11-29 10:40:06 -0800268 !GetDeoptimizedMethodsLock());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100269
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200270 // Disable method tracing by uninstalling instrumentation entry/exit stubs or interpreter.
271 void DisableMethodTracing(const char* key)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700272 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
273 REQUIRES(!Locks::thread_list_lock_,
274 !Locks::classlinker_classes_lock_,
Andreas Gampe7e56a072018-11-29 10:40:06 -0800275 !GetDeoptimizedMethodsLock());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100276
Sebastien Hertzed2be172014-08-19 15:33:43 +0200277 InterpreterHandlerTable GetInterpreterHandlerTable() const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700278 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200279 return interpreter_handler_table_;
280 }
281
Mathieu Chartier90443472015-07-16 20:32:27 -0700282 void InstrumentQuickAllocEntryPoints() REQUIRES(!Locks::instrument_entrypoints_lock_);
283 void UninstrumentQuickAllocEntryPoints() REQUIRES(!Locks::instrument_entrypoints_lock_);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700284 void InstrumentQuickAllocEntryPointsLocked()
Mathieu Chartier90443472015-07-16 20:32:27 -0700285 REQUIRES(Locks::instrument_entrypoints_lock_, !Locks::thread_list_lock_,
286 !Locks::runtime_shutdown_lock_);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700287 void UninstrumentQuickAllocEntryPointsLocked()
Mathieu Chartier90443472015-07-16 20:32:27 -0700288 REQUIRES(Locks::instrument_entrypoints_lock_, !Locks::thread_list_lock_,
289 !Locks::runtime_shutdown_lock_);
290 void ResetQuickAllocEntryPoints() REQUIRES(Locks::runtime_shutdown_lock_);
Ian Rogersfa824272013-11-05 16:12:57 -0800291
Ian Rogers62d6c772013-02-27 08:32:07 -0800292 // Update the code of a method respecting any installed stubs.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700293 void UpdateMethodsCode(ArtMethod* method, const void* quick_code)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800294 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!GetDeoptimizedMethodsLock());
Ian Rogers62d6c772013-02-27 08:32:07 -0800295
Nicolas Geoffraya6e0e7d2018-01-26 13:16:50 +0000296 // Update the code of a native method to a JITed stub.
297 void UpdateNativeMethodsCodeToJitCode(ArtMethod* method, const void* quick_code)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800298 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!GetDeoptimizedMethodsLock());
Nicolas Geoffraya6e0e7d2018-01-26 13:16:50 +0000299
Alex Light0a5ec3d2017-07-25 16:50:26 -0700300 // Update the code of a method to the interpreter respecting any installed stubs from debugger.
301 void UpdateMethodsCodeToInterpreterEntryPoint(ArtMethod* method)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800302 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!GetDeoptimizedMethodsLock());
Alex Light0a5ec3d2017-07-25 16:50:26 -0700303
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700304 // Update the code of a method respecting any installed stubs from debugger.
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000305 void UpdateMethodsCodeForJavaDebuggable(ArtMethod* method, const void* quick_code)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800306 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!GetDeoptimizedMethodsLock());
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700307
Alex Light2d441b12018-06-08 15:33:21 -0700308 // Return the code that we can execute for an invoke including from the JIT.
309 const void* GetCodeForInvoke(ArtMethod* method) const
310 REQUIRES_SHARED(Locks::mutator_lock_);
311
Ian Rogers62d6c772013-02-27 08:32:07 -0800312 // Get the quick code for the given method. More efficient than asking the class linker as it
313 // will short-cut to GetCode if instrumentation and static method resolution stubs aren't
314 // installed.
Andreas Gampe542451c2016-07-26 09:02:02 -0700315 const void* GetQuickCodeFor(ArtMethod* method, PointerSize pointer_size) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700316 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800317
318 void ForceInterpretOnly() {
319 interpret_only_ = true;
320 forced_interpret_only_ = true;
321 }
322
Brian Carlstromea46f952013-07-30 01:26:50 -0700323 // Called by ArtMethod::Invoke to determine dispatch mechanism.
Ian Rogers62d6c772013-02-27 08:32:07 -0800324 bool InterpretOnly() const {
325 return interpret_only_;
326 }
327
Hiroshi Yamauchi563b47c2014-02-28 17:18:37 -0800328 bool IsForcedInterpretOnly() const {
329 return forced_interpret_only_;
330 }
331
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800332 // Code is in boot image oat file which isn't compiled as debuggable.
333 // Need debug version (interpreter or jitted) if that's the case.
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000334 bool NeedDebugVersionFor(ArtMethod* method) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700335 REQUIRES_SHARED(Locks::mutator_lock_);
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800336
Ian Rogers62d6c772013-02-27 08:32:07 -0800337 bool AreExitStubsInstalled() const {
338 return instrumentation_stubs_installed_;
339 }
340
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700341 bool HasMethodEntryListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200342 return have_method_entry_listeners_;
343 }
344
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700345 bool HasMethodExitListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200346 return have_method_exit_listeners_;
347 }
348
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700349 bool HasMethodUnwindListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200350 return have_method_unwind_listeners_;
351 }
352
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700353 bool HasDexPcListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200354 return have_dex_pc_listeners_;
355 }
356
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700357 bool HasFieldReadListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200358 return have_field_read_listeners_;
359 }
360
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700361 bool HasFieldWriteListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200362 return have_field_write_listeners_;
363 }
364
Alex Light6e1607e2017-08-23 10:06:18 -0700365 bool HasExceptionThrownListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
366 return have_exception_thrown_listeners_;
Sebastien Hertz9f102032014-05-23 08:59:42 +0200367 }
368
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700369 bool HasBranchListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000370 return have_branch_listeners_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800371 }
372
Alex Lighte814f9d2017-07-31 16:14:39 -0700373 bool HasWatchedFramePopListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
374 return have_watched_frame_pop_listeners_;
375 }
376
Alex Light9fb1ab12017-09-05 09:32:49 -0700377 bool HasExceptionHandledListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
378 return have_exception_handled_listeners_;
379 }
380
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700381 bool IsActive() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200382 return have_dex_pc_listeners_ || have_method_entry_listeners_ || have_method_exit_listeners_ ||
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200383 have_field_read_listeners_ || have_field_write_listeners_ ||
Alex Light6e1607e2017-08-23 10:06:18 -0700384 have_exception_thrown_listeners_ || have_method_unwind_listeners_ ||
David Srbecky99f97332018-10-03 15:44:24 +0100385 have_branch_listeners_ || have_watched_frame_pop_listeners_ ||
386 have_exception_handled_listeners_;
Bill Buzbeefd522f92016-02-11 22:37:42 +0000387 }
388
Ian Rogers62d6c772013-02-27 08:32:07 -0800389 // Inform listeners that a method has been entered. A dex PC is provided as we may install
390 // listeners into executing code and get method enter events for methods already on the stack.
Vladimir Marko19711d42019-04-12 14:05:34 +0100391 void MethodEnterEvent(Thread* thread,
392 ObjPtr<mirror::Object> this_object,
393 ArtMethod* method,
394 uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700395 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200396 if (UNLIKELY(HasMethodEntryListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800397 MethodEnterEventImpl(thread, this_object, method, dex_pc);
398 }
399 }
400
401 // Inform listeners that a method has been exited.
Alex Lightd7661582017-05-01 13:48:16 -0700402 void MethodExitEvent(Thread* thread,
Vladimir Marko19711d42019-04-12 14:05:34 +0100403 ObjPtr<mirror::Object> this_object,
Alex Lightd7661582017-05-01 13:48:16 -0700404 ArtMethod* method,
405 uint32_t dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800406 const JValue& return_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700407 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200408 if (UNLIKELY(HasMethodExitListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800409 MethodExitEventImpl(thread, this_object, method, dex_pc, return_value);
410 }
411 }
412
413 // Inform listeners that a method has been exited due to an exception.
Vladimir Marko19711d42019-04-12 14:05:34 +0100414 void MethodUnwindEvent(Thread* thread,
415 ObjPtr<mirror::Object> this_object,
416 ArtMethod* method,
417 uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700418 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800419
420 // Inform listeners that the dex pc has moved (only supported by the interpreter).
Vladimir Marko19711d42019-04-12 14:05:34 +0100421 void DexPcMovedEvent(Thread* thread,
422 ObjPtr<mirror::Object> this_object,
423 ArtMethod* method,
424 uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700425 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200426 if (UNLIKELY(HasDexPcListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800427 DexPcMovedEventImpl(thread, this_object, method, dex_pc);
428 }
429 }
430
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000431 // Inform listeners that a branch has been taken (only supported by the interpreter).
432 void Branch(Thread* thread, ArtMethod* method, uint32_t dex_pc, int32_t offset) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700433 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000434 if (UNLIKELY(HasBranchListeners())) {
435 BranchImpl(thread, method, dex_pc, offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800436 }
437 }
438
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200439 // Inform listeners that we read a field (only supported by the interpreter).
Vladimir Marko19711d42019-04-12 14:05:34 +0100440 void FieldReadEvent(Thread* thread,
441 ObjPtr<mirror::Object> this_object,
442 ArtMethod* method,
443 uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700444 ArtField* field) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700445 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200446 if (UNLIKELY(HasFieldReadListeners())) {
447 FieldReadEventImpl(thread, this_object, method, dex_pc, field);
448 }
449 }
450
451 // Inform listeners that we write a field (only supported by the interpreter).
Vladimir Marko19711d42019-04-12 14:05:34 +0100452 void FieldWriteEvent(Thread* thread,
453 ObjPtr<mirror::Object> this_object,
454 ArtMethod* method,
455 uint32_t dex_pc,
456 ArtField* field,
457 const JValue& field_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700458 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200459 if (UNLIKELY(HasFieldWriteListeners())) {
460 FieldWriteEventImpl(thread, this_object, method, dex_pc, field, field_value);
461 }
462 }
463
Alex Lighte814f9d2017-07-31 16:14:39 -0700464 // Inform listeners that a branch has been taken (only supported by the interpreter).
465 void WatchedFramePopped(Thread* thread, const ShadowFrame& frame) const
466 REQUIRES_SHARED(Locks::mutator_lock_) {
467 if (UNLIKELY(HasWatchedFramePopListeners())) {
468 WatchedFramePopImpl(thread, frame);
469 }
470 }
471
Alex Light6e1607e2017-08-23 10:06:18 -0700472 // Inform listeners that an exception was thrown.
Vladimir Marko19711d42019-04-12 14:05:34 +0100473 void ExceptionThrownEvent(Thread* thread, ObjPtr<mirror::Throwable> exception_object) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700474 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800475
Alex Light9fb1ab12017-09-05 09:32:49 -0700476 // Inform listeners that an exception has been handled. This is not sent for native code or for
477 // exceptions which reach the end of the thread's stack.
Vladimir Marko19711d42019-04-12 14:05:34 +0100478 void ExceptionHandledEvent(Thread* thread, ObjPtr<mirror::Throwable> exception_object) const
Alex Light9fb1ab12017-09-05 09:32:49 -0700479 REQUIRES_SHARED(Locks::mutator_lock_);
480
Ian Rogers62d6c772013-02-27 08:32:07 -0800481 // Called when an instrumented method is entered. The intended link register (lr) is saved so
482 // that returning causes a branch to the method exit stub. Generates method enter events.
Vladimir Marko19711d42019-04-12 14:05:34 +0100483 void PushInstrumentationStackFrame(Thread* self,
484 ObjPtr<mirror::Object> this_object,
485 ArtMethod* method,
486 uintptr_t lr,
Jeff Hao9a916d32013-06-27 18:45:37 -0700487 bool interpreter_entry)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700488 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800489
Mingyao Yang2ee17902017-08-30 11:37:08 -0700490 DeoptimizationMethodType GetDeoptimizationMethodType(ArtMethod* method)
491 REQUIRES_SHARED(Locks::mutator_lock_);
492
Ian Rogers62d6c772013-02-27 08:32:07 -0800493 // Called when an instrumented method is exited. Removes the pushed instrumentation frame
Alex Lightb7edcda2017-04-27 13:20:31 -0700494 // returning the intended link register. Generates method exit events. The gpr_result and
495 // fpr_result pointers are pointers to the locations where the integer/pointer and floating point
496 // result values of the function are stored. Both pointers must always be valid but the values
497 // held there will only be meaningful if interpreted as the appropriate type given the function
498 // being returned from.
Andreas Gamped58342c2014-06-05 14:18:08 -0700499 TwoWordReturn PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc,
Alex Lightb7edcda2017-04-27 13:20:31 -0700500 uint64_t* gpr_result, uint64_t* fpr_result)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800501 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!GetDeoptimizedMethodsLock());
Ian Rogers62d6c772013-02-27 08:32:07 -0800502
Alex Light2c8206f2018-06-08 14:51:09 -0700503 // Pops nframes instrumentation frames from the current thread. Returns the return pc for the last
504 // instrumentation frame that's popped.
505 uintptr_t PopFramesForDeoptimization(Thread* self, size_t nframes) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700506 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800507
508 // Call back for configure stubs.
Vladimir Marko19711d42019-04-12 14:05:34 +0100509 void InstallStubsForClass(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800510 REQUIRES(!GetDeoptimizedMethodsLock());
jeffhao725a9572012-11-13 18:20:12 -0800511
Mathieu Chartiere401d142015-04-22 13:56:20 -0700512 void InstallStubsForMethod(ArtMethod* method)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800513 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!GetDeoptimizedMethodsLock());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100514
Alex Light40607862019-05-06 18:16:24 +0000515 // Sets up instrumentation to allow single thread deoptimization using ForceInterpreterCount.
516 void EnableSingleThreadDeopt()
517 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
518 REQUIRES(!Locks::thread_list_lock_,
519 !Locks::classlinker_classes_lock_,
520 !GetDeoptimizedMethodsLock());
521
Mingyao Yang99170c62015-07-06 11:10:37 -0700522 // Install instrumentation exit stub on every method of the stack of the given thread.
523 // This is used by the debugger to cause a deoptimization of the thread's stack after updating
524 // local variable(s).
525 void InstrumentThreadStack(Thread* thread)
Alex Light3ae82532017-07-26 13:59:07 -0700526 REQUIRES_SHARED(Locks::mutator_lock_);
Mingyao Yang99170c62015-07-06 11:10:37 -0700527
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000528 static size_t ComputeFrameId(Thread* self,
529 size_t frame_depth,
530 size_t inlined_frames_before_frame)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700531 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000532
Mathieu Chartiereebc3af2016-02-29 18:13:38 -0800533 // Does not hold lock, used to check if someone changed from not instrumented to instrumented
534 // during a GC suspend point.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700535 bool AllocEntrypointsInstrumented() const REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier50e93312016-03-16 11:25:29 -0700536 return alloc_entrypoints_instrumented_;
Mathieu Chartiereebc3af2016-02-29 18:13:38 -0800537 }
538
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200539 InstrumentationLevel GetCurrentInstrumentationLevel() const;
540
Alex Lightdba61482016-12-21 08:20:29 -0800541 private:
542 // Returns true if moving to the given instrumentation level requires the installation of stubs.
543 // False otherwise.
544 bool RequiresInstrumentationInstallation(InstrumentationLevel new_level) const;
545
Ian Rogers62d6c772013-02-27 08:32:07 -0800546 // Does the job of installing or removing instrumentation code within methods.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200547 // In order to support multiple clients using instrumentation at the same time,
548 // the caller must pass a unique key (a string) identifying it so we remind which
549 // instrumentation level it needs. Therefore the current instrumentation level
550 // becomes the highest instrumentation level required by a client.
551 void ConfigureStubs(const char* key, InstrumentationLevel desired_instrumentation_level)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700552 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800553 REQUIRES(!GetDeoptimizedMethodsLock(),
Mathieu Chartieraa516822015-10-02 15:53:37 -0700554 !Locks::thread_list_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700555 !Locks::classlinker_classes_lock_);
Alex Light40607862019-05-06 18:16:24 +0000556 void UpdateStubs() REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
557 REQUIRES(!GetDeoptimizedMethodsLock(),
558 !Locks::thread_list_lock_,
559 !Locks::classlinker_classes_lock_);
560 void UpdateInstrumentationLevels(InstrumentationLevel level)
561 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
562 REQUIRES(!GetDeoptimizedMethodsLock(),
563 !Locks::thread_list_lock_,
564 !Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800565
Mathieu Chartier90443472015-07-16 20:32:27 -0700566 void UpdateInterpreterHandlerTable() REQUIRES(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800567 /*
568 * TUNING: Dalvik's mterp stashes the actual current handler table base in a
569 * tls field. For Arm, this enables all suspend, debug & tracing checks to be
570 * collapsed into a single conditionally-executed ldw instruction.
571 * Move to Dalvik-style handler-table management for both the goto interpreter and
572 * mterp.
573 */
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200574 interpreter_handler_table_ = IsActive() ? kAlternativeHandlerTable : kMainHandlerTable;
575 }
576
Mathieu Chartier661974a2014-01-09 11:23:53 -0800577 // No thread safety analysis to get around SetQuickAllocEntryPointsInstrumented requiring
578 // exclusive access to mutator lock which you can't get if the runtime isn't started.
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700579 void SetEntrypointsInstrumented(bool instrumented) NO_THREAD_SAFETY_ANALYSIS;
Mathieu Chartier661974a2014-01-09 11:23:53 -0800580
Alex Lightd7661582017-05-01 13:48:16 -0700581 void MethodEnterEventImpl(Thread* thread,
582 ObjPtr<mirror::Object> this_object,
583 ArtMethod* method,
584 uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700585 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lightd7661582017-05-01 13:48:16 -0700586 void MethodExitEventImpl(Thread* thread,
587 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700588 ArtMethod* method,
Alex Lightd7661582017-05-01 13:48:16 -0700589 uint32_t dex_pc,
590 const JValue& return_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700591 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lightd7661582017-05-01 13:48:16 -0700592 void DexPcMovedEventImpl(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_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000597 void BranchImpl(Thread* thread, ArtMethod* method, uint32_t dex_pc, int32_t offset) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700598 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lighte814f9d2017-07-31 16:14:39 -0700599 void WatchedFramePopImpl(Thread* thread, const ShadowFrame& frame) const
600 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lightd7661582017-05-01 13:48:16 -0700601 void FieldReadEventImpl(Thread* thread,
602 ObjPtr<mirror::Object> this_object,
603 ArtMethod* method,
604 uint32_t dex_pc,
605 ArtField* field) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700606 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lightd7661582017-05-01 13:48:16 -0700607 void FieldWriteEventImpl(Thread* thread,
608 ObjPtr<mirror::Object> this_object,
609 ArtMethod* method,
610 uint32_t dex_pc,
611 ArtField* field,
612 const JValue& field_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700613 REQUIRES_SHARED(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800614
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700615 // Read barrier-aware utility functions for accessing deoptimized_methods_
Mathieu Chartiere401d142015-04-22 13:56:20 -0700616 bool AddDeoptimizedMethod(ArtMethod* method)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800617 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(GetDeoptimizedMethodsLock());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700618 bool IsDeoptimizedMethod(ArtMethod* method)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800619 REQUIRES_SHARED(Locks::mutator_lock_, GetDeoptimizedMethodsLock());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700620 bool RemoveDeoptimizedMethod(ArtMethod* method)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800621 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(GetDeoptimizedMethodsLock());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700622 ArtMethod* BeginDeoptimizedMethod()
Andreas Gampe7e56a072018-11-29 10:40:06 -0800623 REQUIRES_SHARED(Locks::mutator_lock_, GetDeoptimizedMethodsLock());
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700624 bool IsDeoptimizedMethodsEmpty() const
Andreas Gampe7e56a072018-11-29 10:40:06 -0800625 REQUIRES_SHARED(Locks::mutator_lock_, GetDeoptimizedMethodsLock());
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700626 void UpdateMethodsCodeImpl(ArtMethod* method, const void* quick_code)
Andreas Gampe7e56a072018-11-29 10:40:06 -0800627 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!GetDeoptimizedMethodsLock());
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700628
Andreas Gampe7e56a072018-11-29 10:40:06 -0800629 ReaderWriterMutex* GetDeoptimizedMethodsLock() const {
630 return deoptimized_methods_lock_.get();
631 }
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700632
Brian Carlstromea46f952013-07-30 01:26:50 -0700633 // Have we hijacked ArtMethod::code_ so that it calls instrumentation/interpreter code?
Ian Rogers62d6c772013-02-27 08:32:07 -0800634 bool instrumentation_stubs_installed_;
635
Brian Carlstromea46f952013-07-30 01:26:50 -0700636 // Have we hijacked ArtMethod::code_ to reference the enter/exit stubs?
Ian Rogers62d6c772013-02-27 08:32:07 -0800637 bool entry_exit_stubs_installed_;
638
Brian Carlstromea46f952013-07-30 01:26:50 -0700639 // Have we hijacked ArtMethod::code_ to reference the enter interpreter stub?
Ian Rogers62d6c772013-02-27 08:32:07 -0800640 bool interpreter_stubs_installed_;
641
642 // Do we need the fidelity of events that we only get from running within the interpreter?
643 bool interpret_only_;
644
645 // Did the runtime request we only run in the interpreter? ie -Xint mode.
646 bool forced_interpret_only_;
647
648 // Do we have any listeners for method entry events? Short-cut to avoid taking the
649 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200650 bool have_method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800651
652 // Do we have any listeners for method exit events? Short-cut to avoid taking the
653 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200654 bool have_method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800655
656 // Do we have any listeners for method unwind events? Short-cut to avoid taking the
657 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200658 bool have_method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800659
660 // Do we have any listeners for dex move events? Short-cut to avoid taking the
661 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200662 bool have_dex_pc_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800663
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200664 // Do we have any listeners for field read events? Short-cut to avoid taking the
665 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200666 bool have_field_read_listeners_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200667
668 // Do we have any listeners for field write events? Short-cut to avoid taking the
669 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200670 bool have_field_write_listeners_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200671
Alex Light6e1607e2017-08-23 10:06:18 -0700672 // Do we have any exception thrown listeners? Short-cut to avoid taking the instrumentation_lock_.
673 bool have_exception_thrown_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800674
Alex Lighte814f9d2017-07-31 16:14:39 -0700675 // Do we have any frame pop listeners? Short-cut to avoid taking the instrumentation_lock_.
676 bool have_watched_frame_pop_listeners_ GUARDED_BY(Locks::mutator_lock_);
677
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000678 // Do we have any branch listeners? Short-cut to avoid taking the instrumentation_lock_.
679 bool have_branch_listeners_ GUARDED_BY(Locks::mutator_lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800680
Alex Light9fb1ab12017-09-05 09:32:49 -0700681 // Do we have any exception handled listeners? Short-cut to avoid taking the
682 // instrumentation_lock_.
683 bool have_exception_handled_listeners_ GUARDED_BY(Locks::mutator_lock_);
684
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200685 // Contains the instrumentation level required by each client of the instrumentation identified
686 // by a string key.
687 typedef SafeMap<const char*, InstrumentationLevel> InstrumentationLevelTable;
688 InstrumentationLevelTable requested_instrumentation_levels_ GUARDED_BY(Locks::mutator_lock_);
689
Ian Rogers62d6c772013-02-27 08:32:07 -0800690 // The event listeners, written to with the mutator_lock_ exclusively held.
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000691 // Mutators must be able to iterate over these lists concurrently, that is, with listeners being
692 // added or removed while iterating. The modifying thread holds exclusive lock,
693 // so other threads cannot iterate (i.e. read the data of the list) at the same time but they
694 // do keep iterators that need to remain valid. This is the reason these listeners are std::list
695 // and not for example std::vector: the existing storage for a std::list does not move.
696 // Note that mutators cannot make a copy of these lists before iterating, as the instrumentation
697 // listeners can also be deleted concurrently.
698 // As a result, these lists are never trimmed. That's acceptable given the low number of
699 // listeners we have.
Ian Rogers62d6c772013-02-27 08:32:07 -0800700 std::list<InstrumentationListener*> method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_);
701 std::list<InstrumentationListener*> method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_);
702 std::list<InstrumentationListener*> method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000703 std::list<InstrumentationListener*> branch_listeners_ GUARDED_BY(Locks::mutator_lock_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000704 std::list<InstrumentationListener*> dex_pc_listeners_ GUARDED_BY(Locks::mutator_lock_);
705 std::list<InstrumentationListener*> field_read_listeners_ GUARDED_BY(Locks::mutator_lock_);
706 std::list<InstrumentationListener*> field_write_listeners_ GUARDED_BY(Locks::mutator_lock_);
Alex Light6e1607e2017-08-23 10:06:18 -0700707 std::list<InstrumentationListener*> exception_thrown_listeners_ GUARDED_BY(Locks::mutator_lock_);
Alex Lighte814f9d2017-07-31 16:14:39 -0700708 std::list<InstrumentationListener*> watched_frame_pop_listeners_ GUARDED_BY(Locks::mutator_lock_);
Alex Light9fb1ab12017-09-05 09:32:49 -0700709 std::list<InstrumentationListener*> exception_handled_listeners_ GUARDED_BY(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800710
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100711 // The set of methods being deoptimized (by the debugger) which must be executed with interpreter
712 // only.
Andreas Gampe7e56a072018-11-29 10:40:06 -0800713 mutable std::unique_ptr<ReaderWriterMutex> deoptimized_methods_lock_ BOTTOM_MUTEX_ACQUIRED_AFTER;
714 std::unordered_set<ArtMethod*> deoptimized_methods_ GUARDED_BY(GetDeoptimizedMethodsLock());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100715 bool deoptimization_enabled_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100716
Ian Rogersfa824272013-11-05 16:12:57 -0800717 // Current interpreter handler table. This is updated each time the thread state flags are
718 // modified.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200719 InterpreterHandlerTable interpreter_handler_table_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200720
Ian Rogersfa824272013-11-05 16:12:57 -0800721 // Greater than 0 if quick alloc entry points instrumented.
Mathieu Chartiereebc3af2016-02-29 18:13:38 -0800722 size_t quick_alloc_entry_points_instrumentation_counter_;
Mathieu Chartier50e93312016-03-16 11:25:29 -0700723
724 // alloc_entrypoints_instrumented_ is only updated with all the threads suspended, this is done
725 // to prevent races with the GC where the GC relies on thread suspension only see
726 // alloc_entrypoints_instrumented_ change during suspend points.
727 bool alloc_entrypoints_instrumented_;
728
Alex Light40607862019-05-06 18:16:24 +0000729 // If we can use instrumentation trampolines. After the first time we instrument something with
730 // the interpreter we can no longer use trampolines because it can lead to stack corruption.
731 // TODO Figure out a way to remove the need for this.
732 bool can_use_instrumentation_trampolines_;
733
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200734 friend class InstrumentationTest; // For GetCurrentInstrumentationLevel and ConfigureStubs.
Alex Light2c8206f2018-06-08 14:51:09 -0700735 friend class InstrumentationStackPopper; // For popping instrumentation frames.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200736
jeffhao725a9572012-11-13 18:20:12 -0800737 DISALLOW_COPY_AND_ASSIGN(Instrumentation);
738};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700739std::ostream& operator<<(std::ostream& os, const Instrumentation::InstrumentationEvent& rhs);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200740std::ostream& operator<<(std::ostream& os, const Instrumentation::InstrumentationLevel& rhs);
jeffhao725a9572012-11-13 18:20:12 -0800741
Ian Rogers62d6c772013-02-27 08:32:07 -0800742// An element in the instrumentation side stack maintained in art::Thread.
743struct InstrumentationStackFrame {
Mingyao Yang2ee17902017-08-30 11:37:08 -0700744 InstrumentationStackFrame(mirror::Object* this_object,
745 ArtMethod* method,
746 uintptr_t return_pc,
747 size_t frame_id,
748 bool interpreter_entry)
749 : this_object_(this_object),
750 method_(method),
751 return_pc_(return_pc),
752 frame_id_(frame_id),
Jeff Hao9a916d32013-06-27 18:45:37 -0700753 interpreter_entry_(interpreter_entry) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800754 }
755
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700756 std::string Dump() const REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800757
758 mirror::Object* this_object_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700759 ArtMethod* method_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100760 uintptr_t return_pc_;
761 size_t frame_id_;
762 bool interpreter_entry_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800763};
764
765} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -0800766} // namespace art
767
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700768#endif // ART_RUNTIME_INSTRUMENTATION_H_