blob: 5763a41a05131c50efb2fbeae33946cf29ab2bf6 [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"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070028#include "gc_root.h"
Sebastien Hertz0462c4c2015-04-01 16:34:17 +020029#include "safe_map.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 {
Brian Carlstromea46f952013-07-30 01:26:50 -070033 class Class;
34 class Object;
35 class 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;
Ian Rogers62d6c772013-02-27 08:32:07 -080040union JValue;
Alex Lighte814f9d2017-07-31 16:14:39 -070041class ShadowFrame;
jeffhao725a9572012-11-13 18:20:12 -080042class Thread;
Mingyao Yang2ee17902017-08-30 11:37:08 -070043enum class DeoptimizationMethodType;
jeffhao725a9572012-11-13 18:20:12 -080044
Ian Rogers62d6c772013-02-27 08:32:07 -080045namespace instrumentation {
jeffhao725a9572012-11-13 18:20:12 -080046
Sebastien Hertzee1997a2013-09-19 14:47:09 +020047// Interpreter handler tables.
48enum InterpreterHandlerTable {
49 kMainHandlerTable = 0, // Main handler table: no suspend check, no instrumentation.
50 kAlternativeHandlerTable = 1, // Alternative handler table: suspend check and/or instrumentation
51 // enabled.
52 kNumHandlerTables
53};
54
Andreas Gampe40da2862015-02-27 12:49:04 -080055// Do we want to deoptimize for method entry and exit listeners or just try to intercept
56// invocations? Deoptimization forces all code to run in the interpreter and considerably hurts the
57// application's performance.
58static constexpr bool kDeoptimizeForAccurateMethodEntryExitListeners = true;
59
Ian Rogers62d6c772013-02-27 08:32:07 -080060// Instrumentation event listener API. Registered listeners will get the appropriate call back for
61// the events they are listening for. The call backs supply the thread, method and dex_pc the event
62// occurred upon. The thread may or may not be Thread::Current().
63struct InstrumentationListener {
64 InstrumentationListener() {}
65 virtual ~InstrumentationListener() {}
66
67 // Call-back for when a method is entered.
Alex Lightd7661582017-05-01 13:48:16 -070068 virtual void MethodEntered(Thread* thread,
69 Handle<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -070070 ArtMethod* method,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070071 uint32_t dex_pc) REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -080072
Alex Lightd7661582017-05-01 13:48:16 -070073 virtual void MethodExited(Thread* thread,
74 Handle<mirror::Object> this_object,
75 ArtMethod* method,
76 uint32_t dex_pc,
77 Handle<mirror::Object> return_value)
78 REQUIRES_SHARED(Locks::mutator_lock_);
79
80 // Call-back for when a method is exited. The implementor should either handler-ize the return
81 // value (if appropriate) or use the alternate MethodExited callback instead if they need to
82 // go through a suspend point.
83 virtual void MethodExited(Thread* thread,
84 Handle<mirror::Object> this_object,
85 ArtMethod* method,
86 uint32_t dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -080087 const JValue& return_value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070088 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -080089
90 // Call-back for when a method is popped due to an exception throw. A method will either cause a
91 // MethodExited call-back or a MethodUnwind call-back when its activation is removed.
Alex Lightd7661582017-05-01 13:48:16 -070092 virtual void MethodUnwind(Thread* thread,
93 Handle<mirror::Object> this_object,
94 ArtMethod* method,
95 uint32_t dex_pc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070096 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -080097
98 // Call-back for when the dex pc moves in a method.
Alex Lightd7661582017-05-01 13:48:16 -070099 virtual void DexPcMoved(Thread* thread,
100 Handle<mirror::Object> this_object,
101 ArtMethod* method,
102 uint32_t new_dex_pc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700103 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -0800104
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200105 // Call-back for when we read from a field.
Alex Lightd7661582017-05-01 13:48:16 -0700106 virtual void FieldRead(Thread* thread,
107 Handle<mirror::Object> this_object,
108 ArtMethod* method,
109 uint32_t dex_pc,
110 ArtField* field) = 0;
111
112 virtual void FieldWritten(Thread* thread,
113 Handle<mirror::Object> this_object,
114 ArtMethod* method,
115 uint32_t dex_pc,
116 ArtField* field,
117 Handle<mirror::Object> field_value)
118 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200119
120 // Call-back for when we write into a field.
Alex Lightd7661582017-05-01 13:48:16 -0700121 virtual void FieldWritten(Thread* thread,
122 Handle<mirror::Object> this_object,
123 ArtMethod* method,
124 uint32_t dex_pc,
125 ArtField* field,
126 const JValue& field_value)
127 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200128
Alex Light6e1607e2017-08-23 10:06:18 -0700129 // Call-back when an exception is thrown.
130 virtual void ExceptionThrown(Thread* thread,
Alex Lightd7661582017-05-01 13:48:16 -0700131 Handle<mirror::Throwable> exception_object)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700132 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800133
Alex Light9fb1ab12017-09-05 09:32:49 -0700134 // Call-back when an exception is caught/handled by java code.
135 virtual void ExceptionHandled(Thread* thread, Handle<mirror::Throwable> exception_object)
136 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
137
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000138 // Call-back for when we execute a branch.
139 virtual void Branch(Thread* thread,
140 ArtMethod* method,
141 uint32_t dex_pc,
142 int32_t dex_pc_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700143 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100144
145 // Call-back for when we get an invokevirtual or an invokeinterface.
146 virtual void InvokeVirtualOrInterface(Thread* thread,
Alex Lightd7661582017-05-01 13:48:16 -0700147 Handle<mirror::Object> this_object,
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100148 ArtMethod* caller,
149 uint32_t dex_pc,
150 ArtMethod* callee)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700151 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Alex Lighte814f9d2017-07-31 16:14:39 -0700152
153 // Call-back when a shadow_frame with the needs_notify_pop_ boolean set is popped off the stack by
154 // either return or exceptions. Normally instrumentation listeners should ensure that there are
155 // shadow-frames by deoptimizing stacks.
156 virtual void WatchedFramePop(Thread* thread ATTRIBUTE_UNUSED,
157 const ShadowFrame& frame ATTRIBUTE_UNUSED)
158 REQUIRES_SHARED(Locks::mutator_lock_) {
159 return;
160 }
jeffhao725a9572012-11-13 18:20:12 -0800161};
162
Ian Rogers62d6c772013-02-27 08:32:07 -0800163// Instrumentation is a catch-all for when extra information is required from the runtime. The
164// typical use for instrumentation is for profiling and debugging. Instrumentation may add stubs
165// to method entry and exit, it may also force execution to be switched to the interpreter and
166// trigger deoptimization.
jeffhao725a9572012-11-13 18:20:12 -0800167class Instrumentation {
168 public:
Ian Rogers62d6c772013-02-27 08:32:07 -0800169 enum InstrumentationEvent {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800170 kMethodEntered = 0x1,
171 kMethodExited = 0x2,
172 kMethodUnwind = 0x4,
173 kDexPcMoved = 0x8,
174 kFieldRead = 0x10,
175 kFieldWritten = 0x20,
Alex Light6e1607e2017-08-23 10:06:18 -0700176 kExceptionThrown = 0x40,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000177 kBranch = 0x80,
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100178 kInvokeVirtualOrInterface = 0x100,
Alex Lighte814f9d2017-07-31 16:14:39 -0700179 kWatchedFramePop = 0x200,
Alex Light9fb1ab12017-09-05 09:32:49 -0700180 kExceptionHandled = 0x400,
Ian Rogers62d6c772013-02-27 08:32:07 -0800181 };
jeffhao725a9572012-11-13 18:20:12 -0800182
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200183 enum class InstrumentationLevel {
184 kInstrumentNothing, // execute without instrumentation
185 kInstrumentWithInstrumentationStubs, // execute with instrumentation entry/exit stubs
186 kInstrumentWithInterpreter // execute with interpreter
187 };
188
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700189 Instrumentation();
jeffhao725a9572012-11-13 18:20:12 -0800190
Ian Rogers62d6c772013-02-27 08:32:07 -0800191 // Add a listener to be notified of the masked together sent of instrumentation events. This
192 // suspend the runtime to install stubs. You are expected to hold the mutator lock as a proxy
193 // for saying you should have suspended all threads (installing stubs while threads are running
194 // will break).
195 void AddListener(InstrumentationListener* listener, uint32_t events)
Mathieu Chartier90443472015-07-16 20:32:27 -0700196 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800197
Ian Rogers62d6c772013-02-27 08:32:07 -0800198 // Removes a listener possibly removing instrumentation stubs.
199 void RemoveListener(InstrumentationListener* listener, uint32_t events)
Mathieu Chartier90443472015-07-16 20:32:27 -0700200 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800201
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100202 // Deoptimization.
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100203 void EnableDeoptimization()
Mathieu Chartieraa516822015-10-02 15:53:37 -0700204 REQUIRES(Locks::mutator_lock_)
205 REQUIRES(!deoptimized_methods_lock_);
206 // Calls UndeoptimizeEverything which may visit class linker classes through ConfigureStubs.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200207 void DisableDeoptimization(const char* key)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700208 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
209 REQUIRES(!deoptimized_methods_lock_);
210
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100211 bool AreAllMethodsDeoptimized() const {
212 return interpreter_stubs_installed_;
213 }
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700214 bool ShouldNotifyMethodEnterExitEvents() const REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100215
Alex Lightbebd7bd2017-07-25 14:05:52 -0700216 bool CanDeoptimize() {
217 return deoptimization_enabled_;
218 }
219
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100220 // Executes everything with interpreter.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200221 void DeoptimizeEverything(const char* key)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700222 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
223 REQUIRES(!Locks::thread_list_lock_,
224 !Locks::classlinker_classes_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700225 !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100226
Mathieu Chartieraa516822015-10-02 15:53:37 -0700227 // Executes everything with compiled code (or interpreter if there is no code). May visit class
228 // linker classes through ConfigureStubs.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200229 void UndeoptimizeEverything(const char* key)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700230 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
231 REQUIRES(!Locks::thread_list_lock_,
232 !Locks::classlinker_classes_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700233 !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100234
235 // Deoptimize a method by forcing its execution with the interpreter. Nevertheless, a static
236 // method (except a class initializer) set to the resolution trampoline will be deoptimized only
237 // once its declaring class is initialized.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700238 void Deoptimize(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700239 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100240
241 // Undeoptimze the method by restoring its entrypoints. Nevertheless, a static method
242 // (except a class initializer) set to the resolution trampoline will be updated only once its
243 // declaring class is initialized.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700244 void Undeoptimize(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700245 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100246
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200247 // Indicates whether the method has been deoptimized so it is executed with the interpreter.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700248 bool IsDeoptimized(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700249 REQUIRES(!deoptimized_methods_lock_) REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100250
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200251 // Enable method tracing by installing instrumentation entry/exit stubs or interpreter.
252 void EnableMethodTracing(const char* key,
253 bool needs_interpreter = kDeoptimizeForAccurateMethodEntryExitListeners)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700254 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
255 REQUIRES(!Locks::thread_list_lock_,
256 !Locks::classlinker_classes_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700257 !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100258
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200259 // Disable method tracing by uninstalling instrumentation entry/exit stubs or interpreter.
260 void DisableMethodTracing(const char* key)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700261 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
262 REQUIRES(!Locks::thread_list_lock_,
263 !Locks::classlinker_classes_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700264 !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100265
Sebastien Hertzed2be172014-08-19 15:33:43 +0200266 InterpreterHandlerTable GetInterpreterHandlerTable() const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700267 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200268 return interpreter_handler_table_;
269 }
270
Mathieu Chartier90443472015-07-16 20:32:27 -0700271 void InstrumentQuickAllocEntryPoints() REQUIRES(!Locks::instrument_entrypoints_lock_);
272 void UninstrumentQuickAllocEntryPoints() REQUIRES(!Locks::instrument_entrypoints_lock_);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700273 void InstrumentQuickAllocEntryPointsLocked()
Mathieu Chartier90443472015-07-16 20:32:27 -0700274 REQUIRES(Locks::instrument_entrypoints_lock_, !Locks::thread_list_lock_,
275 !Locks::runtime_shutdown_lock_);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700276 void UninstrumentQuickAllocEntryPointsLocked()
Mathieu Chartier90443472015-07-16 20:32:27 -0700277 REQUIRES(Locks::instrument_entrypoints_lock_, !Locks::thread_list_lock_,
278 !Locks::runtime_shutdown_lock_);
279 void ResetQuickAllocEntryPoints() REQUIRES(Locks::runtime_shutdown_lock_);
Ian Rogersfa824272013-11-05 16:12:57 -0800280
Ian Rogers62d6c772013-02-27 08:32:07 -0800281 // Update the code of a method respecting any installed stubs.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700282 void UpdateMethodsCode(ArtMethod* method, const void* quick_code)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700283 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800284
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700285 // Update the code of a method respecting any installed stubs from debugger.
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000286 void UpdateMethodsCodeForJavaDebuggable(ArtMethod* method, const void* quick_code)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700287 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700288
Ian Rogers62d6c772013-02-27 08:32:07 -0800289 // Get the quick code for the given method. More efficient than asking the class linker as it
290 // will short-cut to GetCode if instrumentation and static method resolution stubs aren't
291 // installed.
Andreas Gampe542451c2016-07-26 09:02:02 -0700292 const void* GetQuickCodeFor(ArtMethod* method, PointerSize pointer_size) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700293 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800294
295 void ForceInterpretOnly() {
296 interpret_only_ = true;
297 forced_interpret_only_ = true;
298 }
299
Brian Carlstromea46f952013-07-30 01:26:50 -0700300 // Called by ArtMethod::Invoke to determine dispatch mechanism.
Ian Rogers62d6c772013-02-27 08:32:07 -0800301 bool InterpretOnly() const {
302 return interpret_only_;
303 }
304
Hiroshi Yamauchi563b47c2014-02-28 17:18:37 -0800305 bool IsForcedInterpretOnly() const {
306 return forced_interpret_only_;
307 }
308
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800309 // Code is in boot image oat file which isn't compiled as debuggable.
310 // Need debug version (interpreter or jitted) if that's the case.
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000311 bool NeedDebugVersionFor(ArtMethod* method) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700312 REQUIRES_SHARED(Locks::mutator_lock_);
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800313
Ian Rogers62d6c772013-02-27 08:32:07 -0800314 bool AreExitStubsInstalled() const {
315 return instrumentation_stubs_installed_;
316 }
317
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700318 bool HasMethodEntryListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200319 return have_method_entry_listeners_;
320 }
321
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700322 bool HasMethodExitListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200323 return have_method_exit_listeners_;
324 }
325
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700326 bool HasMethodUnwindListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200327 return have_method_unwind_listeners_;
328 }
329
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700330 bool HasDexPcListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200331 return have_dex_pc_listeners_;
332 }
333
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700334 bool HasFieldReadListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200335 return have_field_read_listeners_;
336 }
337
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700338 bool HasFieldWriteListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200339 return have_field_write_listeners_;
340 }
341
Alex Light6e1607e2017-08-23 10:06:18 -0700342 bool HasExceptionThrownListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
343 return have_exception_thrown_listeners_;
Sebastien Hertz9f102032014-05-23 08:59:42 +0200344 }
345
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700346 bool HasBranchListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000347 return have_branch_listeners_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800348 }
349
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700350 bool HasInvokeVirtualOrInterfaceListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100351 return have_invoke_virtual_or_interface_listeners_;
352 }
353
Alex Lighte814f9d2017-07-31 16:14:39 -0700354 bool HasWatchedFramePopListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
355 return have_watched_frame_pop_listeners_;
356 }
357
Alex Light9fb1ab12017-09-05 09:32:49 -0700358 bool HasExceptionHandledListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
359 return have_exception_handled_listeners_;
360 }
361
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700362 bool IsActive() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200363 return have_dex_pc_listeners_ || have_method_entry_listeners_ || have_method_exit_listeners_ ||
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200364 have_field_read_listeners_ || have_field_write_listeners_ ||
Alex Light6e1607e2017-08-23 10:06:18 -0700365 have_exception_thrown_listeners_ || have_method_unwind_listeners_ ||
Alex Lighte814f9d2017-07-31 16:14:39 -0700366 have_branch_listeners_ || have_invoke_virtual_or_interface_listeners_ ||
Alex Light9fb1ab12017-09-05 09:32:49 -0700367 have_watched_frame_pop_listeners_ || have_exception_handled_listeners_;
Bill Buzbeefd522f92016-02-11 22:37:42 +0000368 }
369
370 // Any instrumentation *other* than what is needed for Jit profiling active?
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700371 bool NonJitProfilingActive() const REQUIRES_SHARED(Locks::mutator_lock_) {
Bill Buzbeefd522f92016-02-11 22:37:42 +0000372 return have_dex_pc_listeners_ || have_method_exit_listeners_ ||
373 have_field_read_listeners_ || have_field_write_listeners_ ||
Alex Light6e1607e2017-08-23 10:06:18 -0700374 have_exception_thrown_listeners_ || have_method_unwind_listeners_ ||
Alex Light9fb1ab12017-09-05 09:32:49 -0700375 have_branch_listeners_ || have_watched_frame_pop_listeners_ ||
376 have_exception_handled_listeners_;
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200377 }
378
Ian Rogers62d6c772013-02-27 08:32:07 -0800379 // Inform listeners that a method has been entered. A dex PC is provided as we may install
380 // listeners into executing code and get method enter events for methods already on the stack.
381 void MethodEnterEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700382 ArtMethod* method, uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700383 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200384 if (UNLIKELY(HasMethodEntryListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800385 MethodEnterEventImpl(thread, this_object, method, dex_pc);
386 }
387 }
388
389 // Inform listeners that a method has been exited.
Alex Lightd7661582017-05-01 13:48:16 -0700390 void MethodExitEvent(Thread* thread,
391 mirror::Object* this_object,
392 ArtMethod* method,
393 uint32_t dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800394 const JValue& return_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700395 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200396 if (UNLIKELY(HasMethodExitListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800397 MethodExitEventImpl(thread, this_object, method, dex_pc, return_value);
398 }
399 }
400
401 // Inform listeners that a method has been exited due to an exception.
402 void MethodUnwindEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700403 ArtMethod* method, uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700404 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800405
406 // Inform listeners that the dex pc has moved (only supported by the interpreter).
407 void DexPcMovedEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700408 ArtMethod* method, uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700409 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200410 if (UNLIKELY(HasDexPcListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800411 DexPcMovedEventImpl(thread, this_object, method, dex_pc);
412 }
413 }
414
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000415 // Inform listeners that a branch has been taken (only supported by the interpreter).
416 void Branch(Thread* thread, ArtMethod* method, uint32_t dex_pc, int32_t offset) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700417 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000418 if (UNLIKELY(HasBranchListeners())) {
419 BranchImpl(thread, method, dex_pc, offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800420 }
421 }
422
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200423 // Inform listeners that we read a field (only supported by the interpreter).
424 void FieldReadEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700425 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700426 ArtField* field) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700427 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200428 if (UNLIKELY(HasFieldReadListeners())) {
429 FieldReadEventImpl(thread, this_object, method, dex_pc, field);
430 }
431 }
432
433 // Inform listeners that we write a field (only supported by the interpreter).
434 void FieldWriteEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700435 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700436 ArtField* field, const JValue& field_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700437 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200438 if (UNLIKELY(HasFieldWriteListeners())) {
439 FieldWriteEventImpl(thread, this_object, method, dex_pc, field, field_value);
440 }
441 }
442
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100443 void InvokeVirtualOrInterface(Thread* thread,
444 mirror::Object* this_object,
445 ArtMethod* caller,
446 uint32_t dex_pc,
447 ArtMethod* callee) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700448 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100449 if (UNLIKELY(HasInvokeVirtualOrInterfaceListeners())) {
450 InvokeVirtualOrInterfaceImpl(thread, this_object, caller, dex_pc, callee);
451 }
452 }
453
Alex Lighte814f9d2017-07-31 16:14:39 -0700454 // Inform listeners that a branch has been taken (only supported by the interpreter).
455 void WatchedFramePopped(Thread* thread, const ShadowFrame& frame) const
456 REQUIRES_SHARED(Locks::mutator_lock_) {
457 if (UNLIKELY(HasWatchedFramePopListeners())) {
458 WatchedFramePopImpl(thread, frame);
459 }
460 }
461
Alex Light6e1607e2017-08-23 10:06:18 -0700462 // Inform listeners that an exception was thrown.
463 void ExceptionThrownEvent(Thread* thread, mirror::Throwable* exception_object) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700464 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800465
Alex Light9fb1ab12017-09-05 09:32:49 -0700466 // Inform listeners that an exception has been handled. This is not sent for native code or for
467 // exceptions which reach the end of the thread's stack.
468 void ExceptionHandledEvent(Thread* thread, mirror::Throwable* exception_object) const
469 REQUIRES_SHARED(Locks::mutator_lock_);
470
Ian Rogers62d6c772013-02-27 08:32:07 -0800471 // Called when an instrumented method is entered. The intended link register (lr) is saved so
472 // that returning causes a branch to the method exit stub. Generates method enter events.
473 void PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700474 ArtMethod* method, uintptr_t lr,
Jeff Hao9a916d32013-06-27 18:45:37 -0700475 bool interpreter_entry)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700476 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800477
Mingyao Yang2ee17902017-08-30 11:37:08 -0700478 DeoptimizationMethodType GetDeoptimizationMethodType(ArtMethod* method)
479 REQUIRES_SHARED(Locks::mutator_lock_);
480
Ian Rogers62d6c772013-02-27 08:32:07 -0800481 // Called when an instrumented method is exited. Removes the pushed instrumentation frame
Alex Lightb7edcda2017-04-27 13:20:31 -0700482 // returning the intended link register. Generates method exit events. The gpr_result and
483 // fpr_result pointers are pointers to the locations where the integer/pointer and floating point
484 // result values of the function are stored. Both pointers must always be valid but the values
485 // held there will only be meaningful if interpreted as the appropriate type given the function
486 // being returned from.
Andreas Gamped58342c2014-06-05 14:18:08 -0700487 TwoWordReturn PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc,
Alex Lightb7edcda2017-04-27 13:20:31 -0700488 uint64_t* gpr_result, uint64_t* fpr_result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700489 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800490
491 // Pops an instrumentation frame from the current thread and generate an unwind event.
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700492 // Returns the return pc for the instrumentation frame that's popped.
493 uintptr_t PopMethodForUnwind(Thread* self, bool is_deoptimization) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700494 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800495
496 // Call back for configure stubs.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700497 void InstallStubsForClass(mirror::Class* klass) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700498 REQUIRES(!deoptimized_methods_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800499
Mathieu Chartiere401d142015-04-22 13:56:20 -0700500 void InstallStubsForMethod(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700501 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100502
Mingyao Yang99170c62015-07-06 11:10:37 -0700503 // Install instrumentation exit stub on every method of the stack of the given thread.
504 // This is used by the debugger to cause a deoptimization of the thread's stack after updating
505 // local variable(s).
506 void InstrumentThreadStack(Thread* thread)
Alex Light3ae82532017-07-26 13:59:07 -0700507 REQUIRES_SHARED(Locks::mutator_lock_);
Mingyao Yang99170c62015-07-06 11:10:37 -0700508
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000509 static size_t ComputeFrameId(Thread* self,
510 size_t frame_depth,
511 size_t inlined_frames_before_frame)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700512 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000513
Mathieu Chartiereebc3af2016-02-29 18:13:38 -0800514 // Does not hold lock, used to check if someone changed from not instrumented to instrumented
515 // during a GC suspend point.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700516 bool AllocEntrypointsInstrumented() const REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier50e93312016-03-16 11:25:29 -0700517 return alloc_entrypoints_instrumented_;
Mathieu Chartiereebc3af2016-02-29 18:13:38 -0800518 }
519
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200520 InstrumentationLevel GetCurrentInstrumentationLevel() const;
521
Alex Lightdba61482016-12-21 08:20:29 -0800522 private:
523 // Returns true if moving to the given instrumentation level requires the installation of stubs.
524 // False otherwise.
525 bool RequiresInstrumentationInstallation(InstrumentationLevel new_level) const;
526
Ian Rogers62d6c772013-02-27 08:32:07 -0800527 // Does the job of installing or removing instrumentation code within methods.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200528 // In order to support multiple clients using instrumentation at the same time,
529 // the caller must pass a unique key (a string) identifying it so we remind which
530 // instrumentation level it needs. Therefore the current instrumentation level
531 // becomes the highest instrumentation level required by a client.
532 void ConfigureStubs(const char* key, InstrumentationLevel desired_instrumentation_level)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700533 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
534 REQUIRES(!deoptimized_methods_lock_,
535 !Locks::thread_list_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700536 !Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800537
Mathieu Chartier90443472015-07-16 20:32:27 -0700538 void UpdateInterpreterHandlerTable() REQUIRES(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800539 /*
540 * TUNING: Dalvik's mterp stashes the actual current handler table base in a
541 * tls field. For Arm, this enables all suspend, debug & tracing checks to be
542 * collapsed into a single conditionally-executed ldw instruction.
543 * Move to Dalvik-style handler-table management for both the goto interpreter and
544 * mterp.
545 */
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200546 interpreter_handler_table_ = IsActive() ? kAlternativeHandlerTable : kMainHandlerTable;
547 }
548
Mathieu Chartier661974a2014-01-09 11:23:53 -0800549 // No thread safety analysis to get around SetQuickAllocEntryPointsInstrumented requiring
550 // exclusive access to mutator lock which you can't get if the runtime isn't started.
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700551 void SetEntrypointsInstrumented(bool instrumented) NO_THREAD_SAFETY_ANALYSIS;
Mathieu Chartier661974a2014-01-09 11:23:53 -0800552
Alex Lightd7661582017-05-01 13:48:16 -0700553 void MethodEnterEventImpl(Thread* thread,
554 ObjPtr<mirror::Object> this_object,
555 ArtMethod* method,
556 uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700557 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lightd7661582017-05-01 13:48:16 -0700558 void MethodExitEventImpl(Thread* thread,
559 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700560 ArtMethod* method,
Alex Lightd7661582017-05-01 13:48:16 -0700561 uint32_t dex_pc,
562 const JValue& return_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700563 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lightd7661582017-05-01 13:48:16 -0700564 void DexPcMovedEventImpl(Thread* thread,
565 ObjPtr<mirror::Object> this_object,
566 ArtMethod* method,
567 uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700568 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000569 void BranchImpl(Thread* thread, ArtMethod* method, uint32_t dex_pc, int32_t offset) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700570 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100571 void InvokeVirtualOrInterfaceImpl(Thread* thread,
Alex Lightd7661582017-05-01 13:48:16 -0700572 ObjPtr<mirror::Object> this_object,
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100573 ArtMethod* caller,
574 uint32_t dex_pc,
575 ArtMethod* callee) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700576 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lighte814f9d2017-07-31 16:14:39 -0700577 void WatchedFramePopImpl(Thread* thread, const ShadowFrame& frame) const
578 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lightd7661582017-05-01 13:48:16 -0700579 void FieldReadEventImpl(Thread* thread,
580 ObjPtr<mirror::Object> this_object,
581 ArtMethod* method,
582 uint32_t dex_pc,
583 ArtField* field) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700584 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lightd7661582017-05-01 13:48:16 -0700585 void FieldWriteEventImpl(Thread* thread,
586 ObjPtr<mirror::Object> this_object,
587 ArtMethod* method,
588 uint32_t dex_pc,
589 ArtField* field,
590 const JValue& field_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700591 REQUIRES_SHARED(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800592
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700593 // Read barrier-aware utility functions for accessing deoptimized_methods_
Mathieu Chartiere401d142015-04-22 13:56:20 -0700594 bool AddDeoptimizedMethod(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700595 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700596 bool IsDeoptimizedMethod(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700597 REQUIRES_SHARED(Locks::mutator_lock_, deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700598 bool RemoveDeoptimizedMethod(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700599 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700600 ArtMethod* BeginDeoptimizedMethod()
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700601 REQUIRES_SHARED(Locks::mutator_lock_, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700602 bool IsDeoptimizedMethodsEmpty() const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700603 REQUIRES_SHARED(Locks::mutator_lock_, deoptimized_methods_lock_);
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700604 void UpdateMethodsCodeImpl(ArtMethod* method, const void* quick_code)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700605 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700606
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700607
Brian Carlstromea46f952013-07-30 01:26:50 -0700608 // Have we hijacked ArtMethod::code_ so that it calls instrumentation/interpreter code?
Ian Rogers62d6c772013-02-27 08:32:07 -0800609 bool instrumentation_stubs_installed_;
610
Brian Carlstromea46f952013-07-30 01:26:50 -0700611 // Have we hijacked ArtMethod::code_ to reference the enter/exit stubs?
Ian Rogers62d6c772013-02-27 08:32:07 -0800612 bool entry_exit_stubs_installed_;
613
Brian Carlstromea46f952013-07-30 01:26:50 -0700614 // Have we hijacked ArtMethod::code_ to reference the enter interpreter stub?
Ian Rogers62d6c772013-02-27 08:32:07 -0800615 bool interpreter_stubs_installed_;
616
617 // Do we need the fidelity of events that we only get from running within the interpreter?
618 bool interpret_only_;
619
620 // Did the runtime request we only run in the interpreter? ie -Xint mode.
621 bool forced_interpret_only_;
622
623 // Do we have any listeners for method entry events? Short-cut to avoid taking the
624 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200625 bool have_method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800626
627 // Do we have any listeners for method exit events? Short-cut to avoid taking the
628 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200629 bool have_method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800630
631 // Do we have any listeners for method unwind events? Short-cut to avoid taking the
632 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200633 bool have_method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800634
635 // Do we have any listeners for dex move events? Short-cut to avoid taking the
636 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200637 bool have_dex_pc_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800638
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200639 // Do we have any listeners for field read events? Short-cut to avoid taking the
640 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200641 bool have_field_read_listeners_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200642
643 // Do we have any listeners for field write events? Short-cut to avoid taking the
644 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200645 bool have_field_write_listeners_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200646
Alex Light6e1607e2017-08-23 10:06:18 -0700647 // Do we have any exception thrown listeners? Short-cut to avoid taking the instrumentation_lock_.
648 bool have_exception_thrown_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800649
Alex Lighte814f9d2017-07-31 16:14:39 -0700650 // Do we have any frame pop listeners? Short-cut to avoid taking the instrumentation_lock_.
651 bool have_watched_frame_pop_listeners_ GUARDED_BY(Locks::mutator_lock_);
652
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000653 // Do we have any branch listeners? Short-cut to avoid taking the instrumentation_lock_.
654 bool have_branch_listeners_ GUARDED_BY(Locks::mutator_lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800655
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100656 // Do we have any invoke listeners? Short-cut to avoid taking the instrumentation_lock_.
657 bool have_invoke_virtual_or_interface_listeners_ GUARDED_BY(Locks::mutator_lock_);
658
Alex Light9fb1ab12017-09-05 09:32:49 -0700659 // Do we have any exception handled listeners? Short-cut to avoid taking the
660 // instrumentation_lock_.
661 bool have_exception_handled_listeners_ GUARDED_BY(Locks::mutator_lock_);
662
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200663 // Contains the instrumentation level required by each client of the instrumentation identified
664 // by a string key.
665 typedef SafeMap<const char*, InstrumentationLevel> InstrumentationLevelTable;
666 InstrumentationLevelTable requested_instrumentation_levels_ GUARDED_BY(Locks::mutator_lock_);
667
Ian Rogers62d6c772013-02-27 08:32:07 -0800668 // The event listeners, written to with the mutator_lock_ exclusively held.
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000669 // Mutators must be able to iterate over these lists concurrently, that is, with listeners being
670 // added or removed while iterating. The modifying thread holds exclusive lock,
671 // so other threads cannot iterate (i.e. read the data of the list) at the same time but they
672 // do keep iterators that need to remain valid. This is the reason these listeners are std::list
673 // and not for example std::vector: the existing storage for a std::list does not move.
674 // Note that mutators cannot make a copy of these lists before iterating, as the instrumentation
675 // listeners can also be deleted concurrently.
676 // As a result, these lists are never trimmed. That's acceptable given the low number of
677 // listeners we have.
Ian Rogers62d6c772013-02-27 08:32:07 -0800678 std::list<InstrumentationListener*> method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_);
679 std::list<InstrumentationListener*> method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_);
680 std::list<InstrumentationListener*> method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000681 std::list<InstrumentationListener*> branch_listeners_ GUARDED_BY(Locks::mutator_lock_);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100682 std::list<InstrumentationListener*> invoke_virtual_or_interface_listeners_
683 GUARDED_BY(Locks::mutator_lock_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000684 std::list<InstrumentationListener*> dex_pc_listeners_ GUARDED_BY(Locks::mutator_lock_);
685 std::list<InstrumentationListener*> field_read_listeners_ GUARDED_BY(Locks::mutator_lock_);
686 std::list<InstrumentationListener*> field_write_listeners_ GUARDED_BY(Locks::mutator_lock_);
Alex Light6e1607e2017-08-23 10:06:18 -0700687 std::list<InstrumentationListener*> exception_thrown_listeners_ GUARDED_BY(Locks::mutator_lock_);
Alex Lighte814f9d2017-07-31 16:14:39 -0700688 std::list<InstrumentationListener*> watched_frame_pop_listeners_ GUARDED_BY(Locks::mutator_lock_);
Alex Light9fb1ab12017-09-05 09:32:49 -0700689 std::list<InstrumentationListener*> exception_handled_listeners_ GUARDED_BY(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800690
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100691 // The set of methods being deoptimized (by the debugger) which must be executed with interpreter
692 // only.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700693 mutable ReaderWriterMutex deoptimized_methods_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700694 std::unordered_set<ArtMethod*> deoptimized_methods_ GUARDED_BY(deoptimized_methods_lock_);
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100695 bool deoptimization_enabled_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100696
Ian Rogersfa824272013-11-05 16:12:57 -0800697 // Current interpreter handler table. This is updated each time the thread state flags are
698 // modified.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200699 InterpreterHandlerTable interpreter_handler_table_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200700
Ian Rogersfa824272013-11-05 16:12:57 -0800701 // Greater than 0 if quick alloc entry points instrumented.
Mathieu Chartiereebc3af2016-02-29 18:13:38 -0800702 size_t quick_alloc_entry_points_instrumentation_counter_;
Mathieu Chartier50e93312016-03-16 11:25:29 -0700703
704 // alloc_entrypoints_instrumented_ is only updated with all the threads suspended, this is done
705 // to prevent races with the GC where the GC relies on thread suspension only see
706 // alloc_entrypoints_instrumented_ change during suspend points.
707 bool alloc_entrypoints_instrumented_;
708
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200709 friend class InstrumentationTest; // For GetCurrentInstrumentationLevel and ConfigureStubs.
710
jeffhao725a9572012-11-13 18:20:12 -0800711 DISALLOW_COPY_AND_ASSIGN(Instrumentation);
712};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700713std::ostream& operator<<(std::ostream& os, const Instrumentation::InstrumentationEvent& rhs);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200714std::ostream& operator<<(std::ostream& os, const Instrumentation::InstrumentationLevel& rhs);
jeffhao725a9572012-11-13 18:20:12 -0800715
Ian Rogers62d6c772013-02-27 08:32:07 -0800716// An element in the instrumentation side stack maintained in art::Thread.
717struct InstrumentationStackFrame {
Mingyao Yang2ee17902017-08-30 11:37:08 -0700718 InstrumentationStackFrame(mirror::Object* this_object,
719 ArtMethod* method,
720 uintptr_t return_pc,
721 size_t frame_id,
722 bool interpreter_entry)
723 : this_object_(this_object),
724 method_(method),
725 return_pc_(return_pc),
726 frame_id_(frame_id),
Jeff Hao9a916d32013-06-27 18:45:37 -0700727 interpreter_entry_(interpreter_entry) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800728 }
729
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700730 std::string Dump() const REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800731
732 mirror::Object* this_object_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700733 ArtMethod* method_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100734 uintptr_t return_pc_;
735 size_t frame_id_;
736 bool interpreter_entry_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800737};
738
739} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -0800740} // namespace art
741
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700742#endif // ART_RUNTIME_INSTRUMENTATION_H_