blob: 0e366d3672278c002a92cdbc1c692d3ae61a6ad0 [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>
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include <unordered_set>
Ian Rogers576ca0c2014-06-06 15:58:22 -070023
Ian Rogersd582fa42014-11-05 23:46:43 -080024#include "arch/instruction_set.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070025#include "base/enums.h"
Elliott Hughes76160052012-12-12 16:31:20 -080026#include "base/macros.h"
Ian Rogers719d1a32014-03-06 12:13:39 -080027#include "base/mutex.h"
David Sehr67bf42e2018-02-26 16:43:04 -080028#include "base/safe_map.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070029#include "gc_root.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030
jeffhao725a9572012-11-13 18:20:12 -080031namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032namespace mirror {
Igor Murashkin2ffb7032017-11-08 13:35:21 -080033class Class;
34class Object;
35class Throwable;
Ian Rogers62d6c772013-02-27 08:32:07 -080036} // namespace mirror
Mathieu Chartierc7853442015-03-27 14:35:38 -070037class ArtField;
Mathieu Chartiere401d142015-04-22 13:56:20 -070038class ArtMethod;
Alex Lightd7661582017-05-01 13:48:16 -070039template <typename T> class Handle;
Alex Light2c8206f2018-06-08 14:51:09 -070040template <typename T> class MutableHandle;
Ian Rogers62d6c772013-02-27 08:32:07 -080041union JValue;
Alex Lighte814f9d2017-07-31 16:14:39 -070042class ShadowFrame;
jeffhao725a9572012-11-13 18:20:12 -080043class Thread;
Mingyao Yang2ee17902017-08-30 11:37:08 -070044enum class DeoptimizationMethodType;
jeffhao725a9572012-11-13 18:20:12 -080045
Ian Rogers62d6c772013-02-27 08:32:07 -080046namespace instrumentation {
jeffhao725a9572012-11-13 18:20:12 -080047
Sebastien Hertzee1997a2013-09-19 14:47:09 +020048// Interpreter handler tables.
49enum InterpreterHandlerTable {
50 kMainHandlerTable = 0, // Main handler table: no suspend check, no instrumentation.
51 kAlternativeHandlerTable = 1, // Alternative handler table: suspend check and/or instrumentation
52 // enabled.
53 kNumHandlerTables
54};
55
Andreas Gampe40da2862015-02-27 12:49:04 -080056// Do we want to deoptimize for method entry and exit listeners or just try to intercept
57// invocations? Deoptimization forces all code to run in the interpreter and considerably hurts the
58// application's performance.
59static constexpr bool kDeoptimizeForAccurateMethodEntryExitListeners = true;
60
Ian Rogers62d6c772013-02-27 08:32:07 -080061// Instrumentation event listener API. Registered listeners will get the appropriate call back for
62// the events they are listening for. The call backs supply the thread, method and dex_pc the event
63// occurred upon. The thread may or may not be Thread::Current().
64struct InstrumentationListener {
65 InstrumentationListener() {}
66 virtual ~InstrumentationListener() {}
67
68 // Call-back for when a method is entered.
Alex Lightd7661582017-05-01 13:48:16 -070069 virtual void MethodEntered(Thread* thread,
70 Handle<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -070071 ArtMethod* method,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070072 uint32_t dex_pc) REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -080073
Alex Lightd7661582017-05-01 13:48:16 -070074 virtual void MethodExited(Thread* thread,
75 Handle<mirror::Object> this_object,
76 ArtMethod* method,
77 uint32_t dex_pc,
78 Handle<mirror::Object> return_value)
79 REQUIRES_SHARED(Locks::mutator_lock_);
80
81 // Call-back for when a method is exited. The implementor should either handler-ize the return
82 // value (if appropriate) or use the alternate MethodExited callback instead if they need to
83 // go through a suspend point.
84 virtual void MethodExited(Thread* thread,
85 Handle<mirror::Object> this_object,
86 ArtMethod* method,
87 uint32_t dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -080088 const JValue& return_value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070089 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -080090
91 // Call-back for when a method is popped due to an exception throw. A method will either cause a
92 // MethodExited call-back or a MethodUnwind call-back when its activation is removed.
Alex Lightd7661582017-05-01 13:48:16 -070093 virtual void MethodUnwind(Thread* thread,
94 Handle<mirror::Object> this_object,
95 ArtMethod* method,
96 uint32_t dex_pc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070097 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -080098
99 // Call-back for when the dex pc moves in a method.
Alex Lightd7661582017-05-01 13:48:16 -0700100 virtual void DexPcMoved(Thread* thread,
101 Handle<mirror::Object> this_object,
102 ArtMethod* method,
103 uint32_t new_dex_pc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700104 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -0800105
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200106 // Call-back for when we read from a field.
Alex Lightd7661582017-05-01 13:48:16 -0700107 virtual void FieldRead(Thread* thread,
108 Handle<mirror::Object> this_object,
109 ArtMethod* method,
110 uint32_t dex_pc,
111 ArtField* field) = 0;
112
113 virtual void FieldWritten(Thread* thread,
114 Handle<mirror::Object> this_object,
115 ArtMethod* method,
116 uint32_t dex_pc,
117 ArtField* field,
118 Handle<mirror::Object> field_value)
119 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200120
121 // Call-back for when we write into a field.
Alex Lightd7661582017-05-01 13:48:16 -0700122 virtual void FieldWritten(Thread* thread,
123 Handle<mirror::Object> this_object,
124 ArtMethod* method,
125 uint32_t dex_pc,
126 ArtField* field,
127 const JValue& field_value)
128 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200129
Alex Light6e1607e2017-08-23 10:06:18 -0700130 // Call-back when an exception is thrown.
131 virtual void ExceptionThrown(Thread* thread,
Alex Lightd7661582017-05-01 13:48:16 -0700132 Handle<mirror::Throwable> exception_object)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700133 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800134
Alex Light9fb1ab12017-09-05 09:32:49 -0700135 // Call-back when an exception is caught/handled by java code.
136 virtual void ExceptionHandled(Thread* thread, Handle<mirror::Throwable> exception_object)
137 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
138
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000139 // Call-back for when we execute a branch.
140 virtual void Branch(Thread* thread,
141 ArtMethod* method,
142 uint32_t dex_pc,
143 int32_t dex_pc_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700144 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100145
146 // Call-back for when we get an invokevirtual or an invokeinterface.
147 virtual void InvokeVirtualOrInterface(Thread* thread,
Alex Lightd7661582017-05-01 13:48:16 -0700148 Handle<mirror::Object> this_object,
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100149 ArtMethod* caller,
150 uint32_t dex_pc,
151 ArtMethod* callee)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700152 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Alex Lighte814f9d2017-07-31 16:14:39 -0700153
154 // Call-back when a shadow_frame with the needs_notify_pop_ boolean set is popped off the stack by
155 // either return or exceptions. Normally instrumentation listeners should ensure that there are
156 // shadow-frames by deoptimizing stacks.
157 virtual void WatchedFramePop(Thread* thread ATTRIBUTE_UNUSED,
158 const ShadowFrame& frame ATTRIBUTE_UNUSED)
Alex Light05f47742017-09-14 00:34:44 +0000159 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
jeffhao725a9572012-11-13 18:20:12 -0800160};
161
Alex Light2c8206f2018-06-08 14:51:09 -0700162class Instrumentation;
163// A helper to send instrumentation events while popping the stack in a safe way.
164class InstrumentationStackPopper {
165 public:
166 explicit InstrumentationStackPopper(Thread* self);
167 ~InstrumentationStackPopper() REQUIRES_SHARED(Locks::mutator_lock_);
168
169 // Increase the number of frames being popped to 'desired_pops' return true if the frames were
170 // popped without any exceptions, false otherwise. The exception that caused the pop is
171 // 'exception'.
172 bool PopFramesTo(uint32_t desired_pops, /*in-out*/MutableHandle<mirror::Throwable>& exception)
173 REQUIRES_SHARED(Locks::mutator_lock_);
174
175 private:
176 Thread* self_;
177 Instrumentation* instrumentation_;
178 uint32_t frames_to_remove_;
179};
180
Ian Rogers62d6c772013-02-27 08:32:07 -0800181// Instrumentation is a catch-all for when extra information is required from the runtime. The
182// typical use for instrumentation is for profiling and debugging. Instrumentation may add stubs
183// to method entry and exit, it may also force execution to be switched to the interpreter and
184// trigger deoptimization.
jeffhao725a9572012-11-13 18:20:12 -0800185class Instrumentation {
186 public:
Ian Rogers62d6c772013-02-27 08:32:07 -0800187 enum InstrumentationEvent {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800188 kMethodEntered = 0x1,
189 kMethodExited = 0x2,
190 kMethodUnwind = 0x4,
191 kDexPcMoved = 0x8,
192 kFieldRead = 0x10,
193 kFieldWritten = 0x20,
Alex Light6e1607e2017-08-23 10:06:18 -0700194 kExceptionThrown = 0x40,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000195 kBranch = 0x80,
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100196 kInvokeVirtualOrInterface = 0x100,
Alex Lighte814f9d2017-07-31 16:14:39 -0700197 kWatchedFramePop = 0x200,
Alex Light9fb1ab12017-09-05 09:32:49 -0700198 kExceptionHandled = 0x400,
Ian Rogers62d6c772013-02-27 08:32:07 -0800199 };
jeffhao725a9572012-11-13 18:20:12 -0800200
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200201 enum class InstrumentationLevel {
202 kInstrumentNothing, // execute without instrumentation
203 kInstrumentWithInstrumentationStubs, // execute with instrumentation entry/exit stubs
204 kInstrumentWithInterpreter // execute with interpreter
205 };
206
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700207 Instrumentation();
jeffhao725a9572012-11-13 18:20:12 -0800208
Ian Rogers62d6c772013-02-27 08:32:07 -0800209 // Add a listener to be notified of the masked together sent of instrumentation events. This
210 // suspend the runtime to install stubs. You are expected to hold the mutator lock as a proxy
211 // for saying you should have suspended all threads (installing stubs while threads are running
212 // will break).
213 void AddListener(InstrumentationListener* listener, uint32_t events)
Mathieu Chartier90443472015-07-16 20:32:27 -0700214 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800215
Ian Rogers62d6c772013-02-27 08:32:07 -0800216 // Removes a listener possibly removing instrumentation stubs.
217 void RemoveListener(InstrumentationListener* listener, uint32_t events)
Mathieu Chartier90443472015-07-16 20:32:27 -0700218 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800219
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100220 // Deoptimization.
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100221 void EnableDeoptimization()
Mathieu Chartieraa516822015-10-02 15:53:37 -0700222 REQUIRES(Locks::mutator_lock_)
223 REQUIRES(!deoptimized_methods_lock_);
224 // Calls UndeoptimizeEverything which may visit class linker classes through ConfigureStubs.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200225 void DisableDeoptimization(const char* key)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700226 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
227 REQUIRES(!deoptimized_methods_lock_);
228
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100229 bool AreAllMethodsDeoptimized() const {
230 return interpreter_stubs_installed_;
231 }
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700232 bool ShouldNotifyMethodEnterExitEvents() const REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100233
Alex Lightbebd7bd2017-07-25 14:05:52 -0700234 bool CanDeoptimize() {
235 return deoptimization_enabled_;
236 }
237
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100238 // Executes everything with interpreter.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200239 void DeoptimizeEverything(const char* key)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700240 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
241 REQUIRES(!Locks::thread_list_lock_,
242 !Locks::classlinker_classes_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700243 !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100244
Mathieu Chartieraa516822015-10-02 15:53:37 -0700245 // Executes everything with compiled code (or interpreter if there is no code). May visit class
246 // linker classes through ConfigureStubs.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200247 void UndeoptimizeEverything(const char* key)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700248 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
249 REQUIRES(!Locks::thread_list_lock_,
250 !Locks::classlinker_classes_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700251 !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100252
253 // Deoptimize a method by forcing its execution with the interpreter. Nevertheless, a static
254 // method (except a class initializer) set to the resolution trampoline will be deoptimized only
255 // once its declaring class is initialized.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700256 void Deoptimize(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700257 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100258
259 // Undeoptimze the method by restoring its entrypoints. Nevertheless, a static method
260 // (except a class initializer) set to the resolution trampoline will be updated only once its
261 // declaring class is initialized.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700262 void Undeoptimize(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700263 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100264
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200265 // Indicates whether the method has been deoptimized so it is executed with the interpreter.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700266 bool IsDeoptimized(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700267 REQUIRES(!deoptimized_methods_lock_) REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100268
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200269 // Enable method tracing by installing instrumentation entry/exit stubs or interpreter.
270 void EnableMethodTracing(const char* key,
271 bool needs_interpreter = kDeoptimizeForAccurateMethodEntryExitListeners)
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_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700275 !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100276
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200277 // Disable method tracing by uninstalling instrumentation entry/exit stubs or interpreter.
278 void DisableMethodTracing(const char* key)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700279 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
280 REQUIRES(!Locks::thread_list_lock_,
281 !Locks::classlinker_classes_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700282 !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100283
Sebastien Hertzed2be172014-08-19 15:33:43 +0200284 InterpreterHandlerTable GetInterpreterHandlerTable() const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700285 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200286 return interpreter_handler_table_;
287 }
288
Mathieu Chartier90443472015-07-16 20:32:27 -0700289 void InstrumentQuickAllocEntryPoints() REQUIRES(!Locks::instrument_entrypoints_lock_);
290 void UninstrumentQuickAllocEntryPoints() REQUIRES(!Locks::instrument_entrypoints_lock_);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700291 void InstrumentQuickAllocEntryPointsLocked()
Mathieu Chartier90443472015-07-16 20:32:27 -0700292 REQUIRES(Locks::instrument_entrypoints_lock_, !Locks::thread_list_lock_,
293 !Locks::runtime_shutdown_lock_);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700294 void UninstrumentQuickAllocEntryPointsLocked()
Mathieu Chartier90443472015-07-16 20:32:27 -0700295 REQUIRES(Locks::instrument_entrypoints_lock_, !Locks::thread_list_lock_,
296 !Locks::runtime_shutdown_lock_);
297 void ResetQuickAllocEntryPoints() REQUIRES(Locks::runtime_shutdown_lock_);
Ian Rogersfa824272013-11-05 16:12:57 -0800298
Ian Rogers62d6c772013-02-27 08:32:07 -0800299 // Update the code of a method respecting any installed stubs.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700300 void UpdateMethodsCode(ArtMethod* method, const void* quick_code)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700301 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800302
Nicolas Geoffraya6e0e7d2018-01-26 13:16:50 +0000303 // Update the code of a native method to a JITed stub.
304 void UpdateNativeMethodsCodeToJitCode(ArtMethod* method, const void* quick_code)
305 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
306
Alex Light0a5ec3d2017-07-25 16:50:26 -0700307 // Update the code of a method to the interpreter respecting any installed stubs from debugger.
308 void UpdateMethodsCodeToInterpreterEntryPoint(ArtMethod* method)
309 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
310
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700311 // Update the code of a method respecting any installed stubs from debugger.
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000312 void UpdateMethodsCodeForJavaDebuggable(ArtMethod* method, const void* quick_code)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700313 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700314
Alex Light2d441b12018-06-08 15:33:21 -0700315 // Return the code that we can execute for an invoke including from the JIT.
316 const void* GetCodeForInvoke(ArtMethod* method) const
317 REQUIRES_SHARED(Locks::mutator_lock_);
318
Ian Rogers62d6c772013-02-27 08:32:07 -0800319 // Get the quick code for the given method. More efficient than asking the class linker as it
320 // will short-cut to GetCode if instrumentation and static method resolution stubs aren't
321 // installed.
Andreas Gampe542451c2016-07-26 09:02:02 -0700322 const void* GetQuickCodeFor(ArtMethod* method, PointerSize pointer_size) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700323 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800324
325 void ForceInterpretOnly() {
326 interpret_only_ = true;
327 forced_interpret_only_ = true;
328 }
329
Brian Carlstromea46f952013-07-30 01:26:50 -0700330 // Called by ArtMethod::Invoke to determine dispatch mechanism.
Ian Rogers62d6c772013-02-27 08:32:07 -0800331 bool InterpretOnly() const {
332 return interpret_only_;
333 }
334
Hiroshi Yamauchi563b47c2014-02-28 17:18:37 -0800335 bool IsForcedInterpretOnly() const {
336 return forced_interpret_only_;
337 }
338
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800339 // Code is in boot image oat file which isn't compiled as debuggable.
340 // Need debug version (interpreter or jitted) if that's the case.
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000341 bool NeedDebugVersionFor(ArtMethod* method) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700342 REQUIRES_SHARED(Locks::mutator_lock_);
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800343
Ian Rogers62d6c772013-02-27 08:32:07 -0800344 bool AreExitStubsInstalled() const {
345 return instrumentation_stubs_installed_;
346 }
347
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700348 bool HasMethodEntryListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200349 return have_method_entry_listeners_;
350 }
351
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700352 bool HasMethodExitListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200353 return have_method_exit_listeners_;
354 }
355
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700356 bool HasMethodUnwindListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200357 return have_method_unwind_listeners_;
358 }
359
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700360 bool HasDexPcListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200361 return have_dex_pc_listeners_;
362 }
363
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700364 bool HasFieldReadListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200365 return have_field_read_listeners_;
366 }
367
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700368 bool HasFieldWriteListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200369 return have_field_write_listeners_;
370 }
371
Alex Light6e1607e2017-08-23 10:06:18 -0700372 bool HasExceptionThrownListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
373 return have_exception_thrown_listeners_;
Sebastien Hertz9f102032014-05-23 08:59:42 +0200374 }
375
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700376 bool HasBranchListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000377 return have_branch_listeners_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800378 }
379
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700380 bool HasInvokeVirtualOrInterfaceListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100381 return have_invoke_virtual_or_interface_listeners_;
382 }
383
Alex Lighte814f9d2017-07-31 16:14:39 -0700384 bool HasWatchedFramePopListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
385 return have_watched_frame_pop_listeners_;
386 }
387
Alex Light9fb1ab12017-09-05 09:32:49 -0700388 bool HasExceptionHandledListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
389 return have_exception_handled_listeners_;
390 }
391
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700392 bool IsActive() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200393 return have_dex_pc_listeners_ || have_method_entry_listeners_ || have_method_exit_listeners_ ||
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200394 have_field_read_listeners_ || have_field_write_listeners_ ||
Alex Light6e1607e2017-08-23 10:06:18 -0700395 have_exception_thrown_listeners_ || have_method_unwind_listeners_ ||
Alex Lighte814f9d2017-07-31 16:14:39 -0700396 have_branch_listeners_ || have_invoke_virtual_or_interface_listeners_ ||
Alex Light9fb1ab12017-09-05 09:32:49 -0700397 have_watched_frame_pop_listeners_ || have_exception_handled_listeners_;
Bill Buzbeefd522f92016-02-11 22:37:42 +0000398 }
399
400 // Any instrumentation *other* than what is needed for Jit profiling active?
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700401 bool NonJitProfilingActive() const REQUIRES_SHARED(Locks::mutator_lock_) {
Bill Buzbeefd522f92016-02-11 22:37:42 +0000402 return have_dex_pc_listeners_ || have_method_exit_listeners_ ||
403 have_field_read_listeners_ || have_field_write_listeners_ ||
Alex Light6e1607e2017-08-23 10:06:18 -0700404 have_exception_thrown_listeners_ || have_method_unwind_listeners_ ||
Alex Light9fb1ab12017-09-05 09:32:49 -0700405 have_branch_listeners_ || have_watched_frame_pop_listeners_ ||
406 have_exception_handled_listeners_;
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200407 }
408
Ian Rogers62d6c772013-02-27 08:32:07 -0800409 // Inform listeners that a method has been entered. A dex PC is provided as we may install
410 // listeners into executing code and get method enter events for methods already on the stack.
411 void MethodEnterEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700412 ArtMethod* method, uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700413 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200414 if (UNLIKELY(HasMethodEntryListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800415 MethodEnterEventImpl(thread, this_object, method, dex_pc);
416 }
417 }
418
419 // Inform listeners that a method has been exited.
Alex Lightd7661582017-05-01 13:48:16 -0700420 void MethodExitEvent(Thread* thread,
421 mirror::Object* this_object,
422 ArtMethod* method,
423 uint32_t dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800424 const JValue& return_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700425 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200426 if (UNLIKELY(HasMethodExitListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800427 MethodExitEventImpl(thread, this_object, method, dex_pc, return_value);
428 }
429 }
430
431 // Inform listeners that a method has been exited due to an exception.
432 void MethodUnwindEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700433 ArtMethod* method, uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700434 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800435
436 // Inform listeners that the dex pc has moved (only supported by the interpreter).
437 void DexPcMovedEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700438 ArtMethod* method, uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700439 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200440 if (UNLIKELY(HasDexPcListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800441 DexPcMovedEventImpl(thread, this_object, method, dex_pc);
442 }
443 }
444
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000445 // Inform listeners that a branch has been taken (only supported by the interpreter).
446 void Branch(Thread* thread, ArtMethod* method, uint32_t dex_pc, int32_t offset) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700447 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000448 if (UNLIKELY(HasBranchListeners())) {
449 BranchImpl(thread, method, dex_pc, offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800450 }
451 }
452
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200453 // Inform listeners that we read a field (only supported by the interpreter).
454 void FieldReadEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700455 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700456 ArtField* field) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700457 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200458 if (UNLIKELY(HasFieldReadListeners())) {
459 FieldReadEventImpl(thread, this_object, method, dex_pc, field);
460 }
461 }
462
463 // Inform listeners that we write a field (only supported by the interpreter).
464 void FieldWriteEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700465 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700466 ArtField* field, const JValue& field_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700467 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200468 if (UNLIKELY(HasFieldWriteListeners())) {
469 FieldWriteEventImpl(thread, this_object, method, dex_pc, field, field_value);
470 }
471 }
472
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100473 void InvokeVirtualOrInterface(Thread* thread,
474 mirror::Object* this_object,
475 ArtMethod* caller,
476 uint32_t dex_pc,
477 ArtMethod* callee) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700478 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100479 if (UNLIKELY(HasInvokeVirtualOrInterfaceListeners())) {
480 InvokeVirtualOrInterfaceImpl(thread, this_object, caller, dex_pc, callee);
481 }
482 }
483
Alex Lighte814f9d2017-07-31 16:14:39 -0700484 // Inform listeners that a branch has been taken (only supported by the interpreter).
485 void WatchedFramePopped(Thread* thread, const ShadowFrame& frame) const
486 REQUIRES_SHARED(Locks::mutator_lock_) {
487 if (UNLIKELY(HasWatchedFramePopListeners())) {
488 WatchedFramePopImpl(thread, frame);
489 }
490 }
491
Alex Light6e1607e2017-08-23 10:06:18 -0700492 // Inform listeners that an exception was thrown.
493 void ExceptionThrownEvent(Thread* thread, mirror::Throwable* exception_object) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700494 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800495
Alex Light9fb1ab12017-09-05 09:32:49 -0700496 // Inform listeners that an exception has been handled. This is not sent for native code or for
497 // exceptions which reach the end of the thread's stack.
498 void ExceptionHandledEvent(Thread* thread, mirror::Throwable* exception_object) const
499 REQUIRES_SHARED(Locks::mutator_lock_);
500
Ian Rogers62d6c772013-02-27 08:32:07 -0800501 // Called when an instrumented method is entered. The intended link register (lr) is saved so
502 // that returning causes a branch to the method exit stub. Generates method enter events.
503 void PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700504 ArtMethod* method, uintptr_t lr,
Jeff Hao9a916d32013-06-27 18:45:37 -0700505 bool interpreter_entry)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700506 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800507
Mingyao Yang2ee17902017-08-30 11:37:08 -0700508 DeoptimizationMethodType GetDeoptimizationMethodType(ArtMethod* method)
509 REQUIRES_SHARED(Locks::mutator_lock_);
510
Ian Rogers62d6c772013-02-27 08:32:07 -0800511 // Called when an instrumented method is exited. Removes the pushed instrumentation frame
Alex Lightb7edcda2017-04-27 13:20:31 -0700512 // returning the intended link register. Generates method exit events. The gpr_result and
513 // fpr_result pointers are pointers to the locations where the integer/pointer and floating point
514 // result values of the function are stored. Both pointers must always be valid but the values
515 // held there will only be meaningful if interpreted as the appropriate type given the function
516 // being returned from.
Andreas Gamped58342c2014-06-05 14:18:08 -0700517 TwoWordReturn PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc,
Alex Lightb7edcda2017-04-27 13:20:31 -0700518 uint64_t* gpr_result, uint64_t* fpr_result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700519 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800520
Alex Light2c8206f2018-06-08 14:51:09 -0700521 // Pops nframes instrumentation frames from the current thread. Returns the return pc for the last
522 // instrumentation frame that's popped.
523 uintptr_t PopFramesForDeoptimization(Thread* self, size_t nframes) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700524 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800525
526 // Call back for configure stubs.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700527 void InstallStubsForClass(mirror::Class* klass) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700528 REQUIRES(!deoptimized_methods_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800529
Mathieu Chartiere401d142015-04-22 13:56:20 -0700530 void InstallStubsForMethod(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700531 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100532
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_)
564 REQUIRES(!deoptimized_methods_lock_,
565 !Locks::thread_list_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700566 !Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800567
Mathieu Chartier90443472015-07-16 20:32:27 -0700568 void UpdateInterpreterHandlerTable() REQUIRES(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800569 /*
570 * TUNING: Dalvik's mterp stashes the actual current handler table base in a
571 * tls field. For Arm, this enables all suspend, debug & tracing checks to be
572 * collapsed into a single conditionally-executed ldw instruction.
573 * Move to Dalvik-style handler-table management for both the goto interpreter and
574 * mterp.
575 */
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200576 interpreter_handler_table_ = IsActive() ? kAlternativeHandlerTable : kMainHandlerTable;
577 }
578
Mathieu Chartier661974a2014-01-09 11:23:53 -0800579 // No thread safety analysis to get around SetQuickAllocEntryPointsInstrumented requiring
580 // exclusive access to mutator lock which you can't get if the runtime isn't started.
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700581 void SetEntrypointsInstrumented(bool instrumented) NO_THREAD_SAFETY_ANALYSIS;
Mathieu Chartier661974a2014-01-09 11:23:53 -0800582
Alex Lightd7661582017-05-01 13:48:16 -0700583 void MethodEnterEventImpl(Thread* thread,
584 ObjPtr<mirror::Object> this_object,
585 ArtMethod* method,
586 uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700587 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lightd7661582017-05-01 13:48:16 -0700588 void MethodExitEventImpl(Thread* thread,
589 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700590 ArtMethod* method,
Alex Lightd7661582017-05-01 13:48:16 -0700591 uint32_t dex_pc,
592 const JValue& return_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700593 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lightd7661582017-05-01 13:48:16 -0700594 void DexPcMovedEventImpl(Thread* thread,
595 ObjPtr<mirror::Object> this_object,
596 ArtMethod* method,
597 uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700598 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000599 void BranchImpl(Thread* thread, ArtMethod* method, uint32_t dex_pc, int32_t offset) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700600 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100601 void InvokeVirtualOrInterfaceImpl(Thread* thread,
Alex Lightd7661582017-05-01 13:48:16 -0700602 ObjPtr<mirror::Object> this_object,
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100603 ArtMethod* caller,
604 uint32_t dex_pc,
605 ArtMethod* callee) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700606 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lighte814f9d2017-07-31 16:14:39 -0700607 void WatchedFramePopImpl(Thread* thread, const ShadowFrame& frame) const
608 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lightd7661582017-05-01 13:48:16 -0700609 void FieldReadEventImpl(Thread* thread,
610 ObjPtr<mirror::Object> this_object,
611 ArtMethod* method,
612 uint32_t dex_pc,
613 ArtField* field) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700614 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lightd7661582017-05-01 13:48:16 -0700615 void FieldWriteEventImpl(Thread* thread,
616 ObjPtr<mirror::Object> this_object,
617 ArtMethod* method,
618 uint32_t dex_pc,
619 ArtField* field,
620 const JValue& field_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700621 REQUIRES_SHARED(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800622
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700623 // Read barrier-aware utility functions for accessing deoptimized_methods_
Mathieu Chartiere401d142015-04-22 13:56:20 -0700624 bool AddDeoptimizedMethod(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700625 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700626 bool IsDeoptimizedMethod(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700627 REQUIRES_SHARED(Locks::mutator_lock_, deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700628 bool RemoveDeoptimizedMethod(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700629 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700630 ArtMethod* BeginDeoptimizedMethod()
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700631 REQUIRES_SHARED(Locks::mutator_lock_, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700632 bool IsDeoptimizedMethodsEmpty() const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700633 REQUIRES_SHARED(Locks::mutator_lock_, deoptimized_methods_lock_);
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700634 void UpdateMethodsCodeImpl(ArtMethod* method, const void* quick_code)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700635 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700636
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700637
Brian Carlstromea46f952013-07-30 01:26:50 -0700638 // Have we hijacked ArtMethod::code_ so that it calls instrumentation/interpreter code?
Ian Rogers62d6c772013-02-27 08:32:07 -0800639 bool instrumentation_stubs_installed_;
640
Brian Carlstromea46f952013-07-30 01:26:50 -0700641 // Have we hijacked ArtMethod::code_ to reference the enter/exit stubs?
Ian Rogers62d6c772013-02-27 08:32:07 -0800642 bool entry_exit_stubs_installed_;
643
Brian Carlstromea46f952013-07-30 01:26:50 -0700644 // Have we hijacked ArtMethod::code_ to reference the enter interpreter stub?
Ian Rogers62d6c772013-02-27 08:32:07 -0800645 bool interpreter_stubs_installed_;
646
647 // Do we need the fidelity of events that we only get from running within the interpreter?
648 bool interpret_only_;
649
650 // Did the runtime request we only run in the interpreter? ie -Xint mode.
651 bool forced_interpret_only_;
652
653 // Do we have any listeners for method entry events? Short-cut to avoid taking the
654 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200655 bool have_method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800656
657 // Do we have any listeners for method exit events? Short-cut to avoid taking the
658 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200659 bool have_method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800660
661 // Do we have any listeners for method unwind events? Short-cut to avoid taking the
662 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200663 bool have_method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800664
665 // Do we have any listeners for dex move events? Short-cut to avoid taking the
666 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200667 bool have_dex_pc_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800668
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200669 // Do we have any listeners for field read events? Short-cut to avoid taking the
670 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200671 bool have_field_read_listeners_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200672
673 // Do we have any listeners for field write events? Short-cut to avoid taking the
674 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200675 bool have_field_write_listeners_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200676
Alex Light6e1607e2017-08-23 10:06:18 -0700677 // Do we have any exception thrown listeners? Short-cut to avoid taking the instrumentation_lock_.
678 bool have_exception_thrown_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800679
Alex Lighte814f9d2017-07-31 16:14:39 -0700680 // Do we have any frame pop listeners? Short-cut to avoid taking the instrumentation_lock_.
681 bool have_watched_frame_pop_listeners_ GUARDED_BY(Locks::mutator_lock_);
682
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000683 // Do we have any branch listeners? Short-cut to avoid taking the instrumentation_lock_.
684 bool have_branch_listeners_ GUARDED_BY(Locks::mutator_lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800685
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100686 // Do we have any invoke listeners? Short-cut to avoid taking the instrumentation_lock_.
687 bool have_invoke_virtual_or_interface_listeners_ GUARDED_BY(Locks::mutator_lock_);
688
Alex Light9fb1ab12017-09-05 09:32:49 -0700689 // Do we have any exception handled listeners? Short-cut to avoid taking the
690 // instrumentation_lock_.
691 bool have_exception_handled_listeners_ GUARDED_BY(Locks::mutator_lock_);
692
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200693 // Contains the instrumentation level required by each client of the instrumentation identified
694 // by a string key.
695 typedef SafeMap<const char*, InstrumentationLevel> InstrumentationLevelTable;
696 InstrumentationLevelTable requested_instrumentation_levels_ GUARDED_BY(Locks::mutator_lock_);
697
Ian Rogers62d6c772013-02-27 08:32:07 -0800698 // The event listeners, written to with the mutator_lock_ exclusively held.
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000699 // Mutators must be able to iterate over these lists concurrently, that is, with listeners being
700 // added or removed while iterating. The modifying thread holds exclusive lock,
701 // so other threads cannot iterate (i.e. read the data of the list) at the same time but they
702 // do keep iterators that need to remain valid. This is the reason these listeners are std::list
703 // and not for example std::vector: the existing storage for a std::list does not move.
704 // Note that mutators cannot make a copy of these lists before iterating, as the instrumentation
705 // listeners can also be deleted concurrently.
706 // As a result, these lists are never trimmed. That's acceptable given the low number of
707 // listeners we have.
Ian Rogers62d6c772013-02-27 08:32:07 -0800708 std::list<InstrumentationListener*> method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_);
709 std::list<InstrumentationListener*> method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_);
710 std::list<InstrumentationListener*> method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000711 std::list<InstrumentationListener*> branch_listeners_ GUARDED_BY(Locks::mutator_lock_);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100712 std::list<InstrumentationListener*> invoke_virtual_or_interface_listeners_
713 GUARDED_BY(Locks::mutator_lock_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000714 std::list<InstrumentationListener*> dex_pc_listeners_ GUARDED_BY(Locks::mutator_lock_);
715 std::list<InstrumentationListener*> field_read_listeners_ GUARDED_BY(Locks::mutator_lock_);
716 std::list<InstrumentationListener*> field_write_listeners_ GUARDED_BY(Locks::mutator_lock_);
Alex Light6e1607e2017-08-23 10:06:18 -0700717 std::list<InstrumentationListener*> exception_thrown_listeners_ GUARDED_BY(Locks::mutator_lock_);
Alex Lighte814f9d2017-07-31 16:14:39 -0700718 std::list<InstrumentationListener*> watched_frame_pop_listeners_ GUARDED_BY(Locks::mutator_lock_);
Alex Light9fb1ab12017-09-05 09:32:49 -0700719 std::list<InstrumentationListener*> exception_handled_listeners_ GUARDED_BY(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800720
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100721 // The set of methods being deoptimized (by the debugger) which must be executed with interpreter
722 // only.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700723 mutable ReaderWriterMutex deoptimized_methods_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700724 std::unordered_set<ArtMethod*> deoptimized_methods_ GUARDED_BY(deoptimized_methods_lock_);
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100725 bool deoptimization_enabled_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100726
Ian Rogersfa824272013-11-05 16:12:57 -0800727 // Current interpreter handler table. This is updated each time the thread state flags are
728 // modified.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200729 InterpreterHandlerTable interpreter_handler_table_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200730
Ian Rogersfa824272013-11-05 16:12:57 -0800731 // Greater than 0 if quick alloc entry points instrumented.
Mathieu Chartiereebc3af2016-02-29 18:13:38 -0800732 size_t quick_alloc_entry_points_instrumentation_counter_;
Mathieu Chartier50e93312016-03-16 11:25:29 -0700733
734 // alloc_entrypoints_instrumented_ is only updated with all the threads suspended, this is done
735 // to prevent races with the GC where the GC relies on thread suspension only see
736 // alloc_entrypoints_instrumented_ change during suspend points.
737 bool alloc_entrypoints_instrumented_;
738
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200739 friend class InstrumentationTest; // For GetCurrentInstrumentationLevel and ConfigureStubs.
Alex Light2c8206f2018-06-08 14:51:09 -0700740 friend class InstrumentationStackPopper; // For popping instrumentation frames.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200741
jeffhao725a9572012-11-13 18:20:12 -0800742 DISALLOW_COPY_AND_ASSIGN(Instrumentation);
743};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700744std::ostream& operator<<(std::ostream& os, const Instrumentation::InstrumentationEvent& rhs);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200745std::ostream& operator<<(std::ostream& os, const Instrumentation::InstrumentationLevel& rhs);
jeffhao725a9572012-11-13 18:20:12 -0800746
Ian Rogers62d6c772013-02-27 08:32:07 -0800747// An element in the instrumentation side stack maintained in art::Thread.
748struct InstrumentationStackFrame {
Mingyao Yang2ee17902017-08-30 11:37:08 -0700749 InstrumentationStackFrame(mirror::Object* this_object,
750 ArtMethod* method,
751 uintptr_t return_pc,
752 size_t frame_id,
753 bool interpreter_entry)
754 : this_object_(this_object),
755 method_(method),
756 return_pc_(return_pc),
757 frame_id_(frame_id),
Jeff Hao9a916d32013-06-27 18:45:37 -0700758 interpreter_entry_(interpreter_entry) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800759 }
760
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700761 std::string Dump() const REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800762
763 mirror::Object* this_object_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700764 ArtMethod* method_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100765 uintptr_t return_pc_;
766 size_t frame_id_;
767 bool interpreter_entry_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800768};
769
770} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -0800771} // namespace art
772
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700773#endif // ART_RUNTIME_INSTRUMENTATION_H_