blob: 43e29cffac482e17d621e5cbce05b0f5a6aa20f2 [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 Yang047abb22017-08-23 15:26:57 -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
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000134 // Call-back for when we execute a branch.
135 virtual void Branch(Thread* thread,
136 ArtMethod* method,
137 uint32_t dex_pc,
138 int32_t dex_pc_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700139 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100140
141 // Call-back for when we get an invokevirtual or an invokeinterface.
142 virtual void InvokeVirtualOrInterface(Thread* thread,
Alex Lightd7661582017-05-01 13:48:16 -0700143 Handle<mirror::Object> this_object,
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100144 ArtMethod* caller,
145 uint32_t dex_pc,
146 ArtMethod* callee)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700147 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Alex Lighte814f9d2017-07-31 16:14:39 -0700148
149 // Call-back when a shadow_frame with the needs_notify_pop_ boolean set is popped off the stack by
150 // either return or exceptions. Normally instrumentation listeners should ensure that there are
151 // shadow-frames by deoptimizing stacks.
152 virtual void WatchedFramePop(Thread* thread ATTRIBUTE_UNUSED,
153 const ShadowFrame& frame ATTRIBUTE_UNUSED)
154 REQUIRES_SHARED(Locks::mutator_lock_) {
155 return;
156 }
jeffhao725a9572012-11-13 18:20:12 -0800157};
158
Ian Rogers62d6c772013-02-27 08:32:07 -0800159// Instrumentation is a catch-all for when extra information is required from the runtime. The
160// typical use for instrumentation is for profiling and debugging. Instrumentation may add stubs
161// to method entry and exit, it may also force execution to be switched to the interpreter and
162// trigger deoptimization.
jeffhao725a9572012-11-13 18:20:12 -0800163class Instrumentation {
164 public:
Ian Rogers62d6c772013-02-27 08:32:07 -0800165 enum InstrumentationEvent {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800166 kMethodEntered = 0x1,
167 kMethodExited = 0x2,
168 kMethodUnwind = 0x4,
169 kDexPcMoved = 0x8,
170 kFieldRead = 0x10,
171 kFieldWritten = 0x20,
Alex Light6e1607e2017-08-23 10:06:18 -0700172 kExceptionThrown = 0x40,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000173 kBranch = 0x80,
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100174 kInvokeVirtualOrInterface = 0x100,
Alex Lighte814f9d2017-07-31 16:14:39 -0700175 kWatchedFramePop = 0x200,
Ian Rogers62d6c772013-02-27 08:32:07 -0800176 };
jeffhao725a9572012-11-13 18:20:12 -0800177
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200178 enum class InstrumentationLevel {
179 kInstrumentNothing, // execute without instrumentation
180 kInstrumentWithInstrumentationStubs, // execute with instrumentation entry/exit stubs
181 kInstrumentWithInterpreter // execute with interpreter
182 };
183
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700184 Instrumentation();
jeffhao725a9572012-11-13 18:20:12 -0800185
Ian Rogers62d6c772013-02-27 08:32:07 -0800186 // Add a listener to be notified of the masked together sent of instrumentation events. This
187 // suspend the runtime to install stubs. You are expected to hold the mutator lock as a proxy
188 // for saying you should have suspended all threads (installing stubs while threads are running
189 // will break).
190 void AddListener(InstrumentationListener* listener, uint32_t events)
Mathieu Chartier90443472015-07-16 20:32:27 -0700191 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800192
Ian Rogers62d6c772013-02-27 08:32:07 -0800193 // Removes a listener possibly removing instrumentation stubs.
194 void RemoveListener(InstrumentationListener* listener, uint32_t events)
Mathieu Chartier90443472015-07-16 20:32:27 -0700195 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800196
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100197 // Deoptimization.
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100198 void EnableDeoptimization()
Mathieu Chartieraa516822015-10-02 15:53:37 -0700199 REQUIRES(Locks::mutator_lock_)
200 REQUIRES(!deoptimized_methods_lock_);
201 // Calls UndeoptimizeEverything which may visit class linker classes through ConfigureStubs.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200202 void DisableDeoptimization(const char* key)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700203 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
204 REQUIRES(!deoptimized_methods_lock_);
205
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100206 bool AreAllMethodsDeoptimized() const {
207 return interpreter_stubs_installed_;
208 }
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700209 bool ShouldNotifyMethodEnterExitEvents() const REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100210
Alex Lightbebd7bd2017-07-25 14:05:52 -0700211 bool CanDeoptimize() {
212 return deoptimization_enabled_;
213 }
214
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100215 // Executes everything with interpreter.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200216 void DeoptimizeEverything(const char* key)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700217 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
218 REQUIRES(!Locks::thread_list_lock_,
219 !Locks::classlinker_classes_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700220 !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100221
Mathieu Chartieraa516822015-10-02 15:53:37 -0700222 // Executes everything with compiled code (or interpreter if there is no code). May visit class
223 // linker classes through ConfigureStubs.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200224 void UndeoptimizeEverything(const char* key)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700225 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
226 REQUIRES(!Locks::thread_list_lock_,
227 !Locks::classlinker_classes_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700228 !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100229
230 // Deoptimize a method by forcing its execution with the interpreter. Nevertheless, a static
231 // method (except a class initializer) set to the resolution trampoline will be deoptimized only
232 // once its declaring class is initialized.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700233 void Deoptimize(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700234 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100235
236 // Undeoptimze the method by restoring its entrypoints. Nevertheless, a static method
237 // (except a class initializer) set to the resolution trampoline will be updated only once its
238 // declaring class is initialized.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700239 void Undeoptimize(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700240 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100241
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200242 // Indicates whether the method has been deoptimized so it is executed with the interpreter.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700243 bool IsDeoptimized(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700244 REQUIRES(!deoptimized_methods_lock_) REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100245
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200246 // Enable method tracing by installing instrumentation entry/exit stubs or interpreter.
247 void EnableMethodTracing(const char* key,
248 bool needs_interpreter = kDeoptimizeForAccurateMethodEntryExitListeners)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700249 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
250 REQUIRES(!Locks::thread_list_lock_,
251 !Locks::classlinker_classes_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700252 !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100253
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200254 // Disable method tracing by uninstalling instrumentation entry/exit stubs or interpreter.
255 void DisableMethodTracing(const char* key)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700256 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
257 REQUIRES(!Locks::thread_list_lock_,
258 !Locks::classlinker_classes_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700259 !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100260
Sebastien Hertzed2be172014-08-19 15:33:43 +0200261 InterpreterHandlerTable GetInterpreterHandlerTable() const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700262 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200263 return interpreter_handler_table_;
264 }
265
Mathieu Chartier90443472015-07-16 20:32:27 -0700266 void InstrumentQuickAllocEntryPoints() REQUIRES(!Locks::instrument_entrypoints_lock_);
267 void UninstrumentQuickAllocEntryPoints() REQUIRES(!Locks::instrument_entrypoints_lock_);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700268 void InstrumentQuickAllocEntryPointsLocked()
Mathieu Chartier90443472015-07-16 20:32:27 -0700269 REQUIRES(Locks::instrument_entrypoints_lock_, !Locks::thread_list_lock_,
270 !Locks::runtime_shutdown_lock_);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700271 void UninstrumentQuickAllocEntryPointsLocked()
Mathieu Chartier90443472015-07-16 20:32:27 -0700272 REQUIRES(Locks::instrument_entrypoints_lock_, !Locks::thread_list_lock_,
273 !Locks::runtime_shutdown_lock_);
274 void ResetQuickAllocEntryPoints() REQUIRES(Locks::runtime_shutdown_lock_);
Ian Rogersfa824272013-11-05 16:12:57 -0800275
Ian Rogers62d6c772013-02-27 08:32:07 -0800276 // Update the code of a method respecting any installed stubs.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700277 void UpdateMethodsCode(ArtMethod* method, const void* quick_code)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700278 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800279
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700280 // Update the code of a method respecting any installed stubs from debugger.
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000281 void UpdateMethodsCodeForJavaDebuggable(ArtMethod* method, const void* quick_code)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700282 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700283
Ian Rogers62d6c772013-02-27 08:32:07 -0800284 // Get the quick code for the given method. More efficient than asking the class linker as it
285 // will short-cut to GetCode if instrumentation and static method resolution stubs aren't
286 // installed.
Andreas Gampe542451c2016-07-26 09:02:02 -0700287 const void* GetQuickCodeFor(ArtMethod* method, PointerSize pointer_size) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700288 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800289
290 void ForceInterpretOnly() {
291 interpret_only_ = true;
292 forced_interpret_only_ = true;
293 }
294
Brian Carlstromea46f952013-07-30 01:26:50 -0700295 // Called by ArtMethod::Invoke to determine dispatch mechanism.
Ian Rogers62d6c772013-02-27 08:32:07 -0800296 bool InterpretOnly() const {
297 return interpret_only_;
298 }
299
Hiroshi Yamauchi563b47c2014-02-28 17:18:37 -0800300 bool IsForcedInterpretOnly() const {
301 return forced_interpret_only_;
302 }
303
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800304 // Code is in boot image oat file which isn't compiled as debuggable.
305 // Need debug version (interpreter or jitted) if that's the case.
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000306 bool NeedDebugVersionFor(ArtMethod* method) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700307 REQUIRES_SHARED(Locks::mutator_lock_);
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800308
Ian Rogers62d6c772013-02-27 08:32:07 -0800309 bool AreExitStubsInstalled() const {
310 return instrumentation_stubs_installed_;
311 }
312
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700313 bool HasMethodEntryListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200314 return have_method_entry_listeners_;
315 }
316
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700317 bool HasMethodExitListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200318 return have_method_exit_listeners_;
319 }
320
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700321 bool HasMethodUnwindListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200322 return have_method_unwind_listeners_;
323 }
324
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700325 bool HasDexPcListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200326 return have_dex_pc_listeners_;
327 }
328
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700329 bool HasFieldReadListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200330 return have_field_read_listeners_;
331 }
332
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700333 bool HasFieldWriteListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200334 return have_field_write_listeners_;
335 }
336
Alex Light6e1607e2017-08-23 10:06:18 -0700337 bool HasExceptionThrownListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
338 return have_exception_thrown_listeners_;
Sebastien Hertz9f102032014-05-23 08:59:42 +0200339 }
340
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700341 bool HasBranchListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000342 return have_branch_listeners_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800343 }
344
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700345 bool HasInvokeVirtualOrInterfaceListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100346 return have_invoke_virtual_or_interface_listeners_;
347 }
348
Alex Lighte814f9d2017-07-31 16:14:39 -0700349 bool HasWatchedFramePopListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
350 return have_watched_frame_pop_listeners_;
351 }
352
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700353 bool IsActive() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200354 return have_dex_pc_listeners_ || have_method_entry_listeners_ || have_method_exit_listeners_ ||
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200355 have_field_read_listeners_ || have_field_write_listeners_ ||
Alex Light6e1607e2017-08-23 10:06:18 -0700356 have_exception_thrown_listeners_ || have_method_unwind_listeners_ ||
Alex Lighte814f9d2017-07-31 16:14:39 -0700357 have_branch_listeners_ || have_invoke_virtual_or_interface_listeners_ ||
358 have_watched_frame_pop_listeners_;
Bill Buzbeefd522f92016-02-11 22:37:42 +0000359 }
360
361 // Any instrumentation *other* than what is needed for Jit profiling active?
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700362 bool NonJitProfilingActive() const REQUIRES_SHARED(Locks::mutator_lock_) {
Bill Buzbeefd522f92016-02-11 22:37:42 +0000363 return have_dex_pc_listeners_ || have_method_exit_listeners_ ||
364 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_watched_frame_pop_listeners_;
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200367 }
368
Ian Rogers62d6c772013-02-27 08:32:07 -0800369 // Inform listeners that a method has been entered. A dex PC is provided as we may install
370 // listeners into executing code and get method enter events for methods already on the stack.
371 void MethodEnterEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700372 ArtMethod* method, uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700373 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200374 if (UNLIKELY(HasMethodEntryListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800375 MethodEnterEventImpl(thread, this_object, method, dex_pc);
376 }
377 }
378
379 // Inform listeners that a method has been exited.
Alex Lightd7661582017-05-01 13:48:16 -0700380 void MethodExitEvent(Thread* thread,
381 mirror::Object* this_object,
382 ArtMethod* method,
383 uint32_t dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800384 const JValue& return_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700385 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200386 if (UNLIKELY(HasMethodExitListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800387 MethodExitEventImpl(thread, this_object, method, dex_pc, return_value);
388 }
389 }
390
391 // Inform listeners that a method has been exited due to an exception.
392 void MethodUnwindEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700393 ArtMethod* method, uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700394 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800395
396 // Inform listeners that the dex pc has moved (only supported by the interpreter).
397 void DexPcMovedEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700398 ArtMethod* method, uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700399 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200400 if (UNLIKELY(HasDexPcListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800401 DexPcMovedEventImpl(thread, this_object, method, dex_pc);
402 }
403 }
404
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000405 // Inform listeners that a branch has been taken (only supported by the interpreter).
406 void Branch(Thread* thread, ArtMethod* method, uint32_t dex_pc, int32_t offset) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700407 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000408 if (UNLIKELY(HasBranchListeners())) {
409 BranchImpl(thread, method, dex_pc, offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800410 }
411 }
412
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200413 // Inform listeners that we read a field (only supported by the interpreter).
414 void FieldReadEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700415 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700416 ArtField* field) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700417 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200418 if (UNLIKELY(HasFieldReadListeners())) {
419 FieldReadEventImpl(thread, this_object, method, dex_pc, field);
420 }
421 }
422
423 // Inform listeners that we write a field (only supported by the interpreter).
424 void FieldWriteEvent(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 JValue& field_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700427 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200428 if (UNLIKELY(HasFieldWriteListeners())) {
429 FieldWriteEventImpl(thread, this_object, method, dex_pc, field, field_value);
430 }
431 }
432
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100433 void InvokeVirtualOrInterface(Thread* thread,
434 mirror::Object* this_object,
435 ArtMethod* caller,
436 uint32_t dex_pc,
437 ArtMethod* callee) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700438 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100439 if (UNLIKELY(HasInvokeVirtualOrInterfaceListeners())) {
440 InvokeVirtualOrInterfaceImpl(thread, this_object, caller, dex_pc, callee);
441 }
442 }
443
Alex Lighte814f9d2017-07-31 16:14:39 -0700444 // Inform listeners that a branch has been taken (only supported by the interpreter).
445 void WatchedFramePopped(Thread* thread, const ShadowFrame& frame) const
446 REQUIRES_SHARED(Locks::mutator_lock_) {
447 if (UNLIKELY(HasWatchedFramePopListeners())) {
448 WatchedFramePopImpl(thread, frame);
449 }
450 }
451
Alex Light6e1607e2017-08-23 10:06:18 -0700452 // Inform listeners that an exception was thrown.
453 void ExceptionThrownEvent(Thread* thread, mirror::Throwable* exception_object) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700454 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800455
456 // Called when an instrumented method is entered. The intended link register (lr) is saved so
457 // that returning causes a branch to the method exit stub. Generates method enter events.
458 void PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700459 ArtMethod* method, uintptr_t lr,
Jeff Hao9a916d32013-06-27 18:45:37 -0700460 bool interpreter_entry)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700461 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800462
Mingyao Yang047abb22017-08-23 15:26:57 -0700463 DeoptimizationMethodType GetDeoptimizationMethodType(ArtMethod* method)
464 REQUIRES_SHARED(Locks::mutator_lock_);
465
Ian Rogers62d6c772013-02-27 08:32:07 -0800466 // Called when an instrumented method is exited. Removes the pushed instrumentation frame
Alex Lightb7edcda2017-04-27 13:20:31 -0700467 // returning the intended link register. Generates method exit events. The gpr_result and
468 // fpr_result pointers are pointers to the locations where the integer/pointer and floating point
469 // result values of the function are stored. Both pointers must always be valid but the values
470 // held there will only be meaningful if interpreted as the appropriate type given the function
471 // being returned from.
Andreas Gamped58342c2014-06-05 14:18:08 -0700472 TwoWordReturn PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc,
Alex Lightb7edcda2017-04-27 13:20:31 -0700473 uint64_t* gpr_result, uint64_t* fpr_result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700474 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800475
476 // Pops an instrumentation frame from the current thread and generate an unwind event.
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700477 // Returns the return pc for the instrumentation frame that's popped.
478 uintptr_t PopMethodForUnwind(Thread* self, bool is_deoptimization) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700479 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800480
481 // Call back for configure stubs.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700482 void InstallStubsForClass(mirror::Class* klass) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700483 REQUIRES(!deoptimized_methods_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800484
Mathieu Chartiere401d142015-04-22 13:56:20 -0700485 void InstallStubsForMethod(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700486 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100487
Mingyao Yang99170c62015-07-06 11:10:37 -0700488 // Install instrumentation exit stub on every method of the stack of the given thread.
489 // This is used by the debugger to cause a deoptimization of the thread's stack after updating
490 // local variable(s).
491 void InstrumentThreadStack(Thread* thread)
Alex Light3ae82532017-07-26 13:59:07 -0700492 REQUIRES_SHARED(Locks::mutator_lock_);
Mingyao Yang99170c62015-07-06 11:10:37 -0700493
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000494 static size_t ComputeFrameId(Thread* self,
495 size_t frame_depth,
496 size_t inlined_frames_before_frame)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700497 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000498
Mathieu Chartiereebc3af2016-02-29 18:13:38 -0800499 // Does not hold lock, used to check if someone changed from not instrumented to instrumented
500 // during a GC suspend point.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700501 bool AllocEntrypointsInstrumented() const REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier50e93312016-03-16 11:25:29 -0700502 return alloc_entrypoints_instrumented_;
Mathieu Chartiereebc3af2016-02-29 18:13:38 -0800503 }
504
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200505 InstrumentationLevel GetCurrentInstrumentationLevel() const;
506
Alex Lightdba61482016-12-21 08:20:29 -0800507 private:
508 // Returns true if moving to the given instrumentation level requires the installation of stubs.
509 // False otherwise.
510 bool RequiresInstrumentationInstallation(InstrumentationLevel new_level) const;
511
Ian Rogers62d6c772013-02-27 08:32:07 -0800512 // Does the job of installing or removing instrumentation code within methods.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200513 // In order to support multiple clients using instrumentation at the same time,
514 // the caller must pass a unique key (a string) identifying it so we remind which
515 // instrumentation level it needs. Therefore the current instrumentation level
516 // becomes the highest instrumentation level required by a client.
517 void ConfigureStubs(const char* key, InstrumentationLevel desired_instrumentation_level)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700518 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
519 REQUIRES(!deoptimized_methods_lock_,
520 !Locks::thread_list_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700521 !Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800522
Mathieu Chartier90443472015-07-16 20:32:27 -0700523 void UpdateInterpreterHandlerTable() REQUIRES(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800524 /*
525 * TUNING: Dalvik's mterp stashes the actual current handler table base in a
526 * tls field. For Arm, this enables all suspend, debug & tracing checks to be
527 * collapsed into a single conditionally-executed ldw instruction.
528 * Move to Dalvik-style handler-table management for both the goto interpreter and
529 * mterp.
530 */
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200531 interpreter_handler_table_ = IsActive() ? kAlternativeHandlerTable : kMainHandlerTable;
532 }
533
Mathieu Chartier661974a2014-01-09 11:23:53 -0800534 // No thread safety analysis to get around SetQuickAllocEntryPointsInstrumented requiring
535 // exclusive access to mutator lock which you can't get if the runtime isn't started.
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700536 void SetEntrypointsInstrumented(bool instrumented) NO_THREAD_SAFETY_ANALYSIS;
Mathieu Chartier661974a2014-01-09 11:23:53 -0800537
Alex Lightd7661582017-05-01 13:48:16 -0700538 void MethodEnterEventImpl(Thread* thread,
539 ObjPtr<mirror::Object> this_object,
540 ArtMethod* method,
541 uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700542 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lightd7661582017-05-01 13:48:16 -0700543 void MethodExitEventImpl(Thread* thread,
544 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700545 ArtMethod* method,
Alex Lightd7661582017-05-01 13:48:16 -0700546 uint32_t dex_pc,
547 const JValue& return_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700548 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lightd7661582017-05-01 13:48:16 -0700549 void DexPcMovedEventImpl(Thread* thread,
550 ObjPtr<mirror::Object> this_object,
551 ArtMethod* method,
552 uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700553 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000554 void BranchImpl(Thread* thread, ArtMethod* method, uint32_t dex_pc, int32_t offset) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700555 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100556 void InvokeVirtualOrInterfaceImpl(Thread* thread,
Alex Lightd7661582017-05-01 13:48:16 -0700557 ObjPtr<mirror::Object> this_object,
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100558 ArtMethod* caller,
559 uint32_t dex_pc,
560 ArtMethod* callee) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700561 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lighte814f9d2017-07-31 16:14:39 -0700562 void WatchedFramePopImpl(Thread* thread, const ShadowFrame& frame) const
563 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lightd7661582017-05-01 13:48:16 -0700564 void FieldReadEventImpl(Thread* thread,
565 ObjPtr<mirror::Object> this_object,
566 ArtMethod* method,
567 uint32_t dex_pc,
568 ArtField* field) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700569 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lightd7661582017-05-01 13:48:16 -0700570 void FieldWriteEventImpl(Thread* thread,
571 ObjPtr<mirror::Object> this_object,
572 ArtMethod* method,
573 uint32_t dex_pc,
574 ArtField* field,
575 const JValue& field_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700576 REQUIRES_SHARED(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800577
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700578 // Read barrier-aware utility functions for accessing deoptimized_methods_
Mathieu Chartiere401d142015-04-22 13:56:20 -0700579 bool AddDeoptimizedMethod(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700580 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700581 bool IsDeoptimizedMethod(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700582 REQUIRES_SHARED(Locks::mutator_lock_, deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700583 bool RemoveDeoptimizedMethod(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700584 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700585 ArtMethod* BeginDeoptimizedMethod()
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700586 REQUIRES_SHARED(Locks::mutator_lock_, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700587 bool IsDeoptimizedMethodsEmpty() const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700588 REQUIRES_SHARED(Locks::mutator_lock_, deoptimized_methods_lock_);
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700589 void UpdateMethodsCodeImpl(ArtMethod* method, const void* quick_code)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700590 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700591
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700592
Brian Carlstromea46f952013-07-30 01:26:50 -0700593 // Have we hijacked ArtMethod::code_ so that it calls instrumentation/interpreter code?
Ian Rogers62d6c772013-02-27 08:32:07 -0800594 bool instrumentation_stubs_installed_;
595
Brian Carlstromea46f952013-07-30 01:26:50 -0700596 // Have we hijacked ArtMethod::code_ to reference the enter/exit stubs?
Ian Rogers62d6c772013-02-27 08:32:07 -0800597 bool entry_exit_stubs_installed_;
598
Brian Carlstromea46f952013-07-30 01:26:50 -0700599 // Have we hijacked ArtMethod::code_ to reference the enter interpreter stub?
Ian Rogers62d6c772013-02-27 08:32:07 -0800600 bool interpreter_stubs_installed_;
601
602 // Do we need the fidelity of events that we only get from running within the interpreter?
603 bool interpret_only_;
604
605 // Did the runtime request we only run in the interpreter? ie -Xint mode.
606 bool forced_interpret_only_;
607
608 // Do we have any listeners for method entry events? Short-cut to avoid taking the
609 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200610 bool have_method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800611
612 // Do we have any listeners for method exit events? Short-cut to avoid taking the
613 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200614 bool have_method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800615
616 // Do we have any listeners for method unwind events? Short-cut to avoid taking the
617 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200618 bool have_method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800619
620 // Do we have any listeners for dex move events? Short-cut to avoid taking the
621 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200622 bool have_dex_pc_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800623
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200624 // Do we have any listeners for field read events? Short-cut to avoid taking the
625 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200626 bool have_field_read_listeners_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200627
628 // Do we have any listeners for field write events? Short-cut to avoid taking the
629 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200630 bool have_field_write_listeners_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200631
Alex Light6e1607e2017-08-23 10:06:18 -0700632 // Do we have any exception thrown listeners? Short-cut to avoid taking the instrumentation_lock_.
633 bool have_exception_thrown_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800634
Alex Lighte814f9d2017-07-31 16:14:39 -0700635 // Do we have any frame pop listeners? Short-cut to avoid taking the instrumentation_lock_.
636 bool have_watched_frame_pop_listeners_ GUARDED_BY(Locks::mutator_lock_);
637
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000638 // Do we have any branch listeners? Short-cut to avoid taking the instrumentation_lock_.
639 bool have_branch_listeners_ GUARDED_BY(Locks::mutator_lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800640
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100641 // Do we have any invoke listeners? Short-cut to avoid taking the instrumentation_lock_.
642 bool have_invoke_virtual_or_interface_listeners_ GUARDED_BY(Locks::mutator_lock_);
643
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200644 // Contains the instrumentation level required by each client of the instrumentation identified
645 // by a string key.
646 typedef SafeMap<const char*, InstrumentationLevel> InstrumentationLevelTable;
647 InstrumentationLevelTable requested_instrumentation_levels_ GUARDED_BY(Locks::mutator_lock_);
648
Ian Rogers62d6c772013-02-27 08:32:07 -0800649 // The event listeners, written to with the mutator_lock_ exclusively held.
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000650 // Mutators must be able to iterate over these lists concurrently, that is, with listeners being
651 // added or removed while iterating. The modifying thread holds exclusive lock,
652 // so other threads cannot iterate (i.e. read the data of the list) at the same time but they
653 // do keep iterators that need to remain valid. This is the reason these listeners are std::list
654 // and not for example std::vector: the existing storage for a std::list does not move.
655 // Note that mutators cannot make a copy of these lists before iterating, as the instrumentation
656 // listeners can also be deleted concurrently.
657 // As a result, these lists are never trimmed. That's acceptable given the low number of
658 // listeners we have.
Ian Rogers62d6c772013-02-27 08:32:07 -0800659 std::list<InstrumentationListener*> method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_);
660 std::list<InstrumentationListener*> method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_);
661 std::list<InstrumentationListener*> method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000662 std::list<InstrumentationListener*> branch_listeners_ GUARDED_BY(Locks::mutator_lock_);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100663 std::list<InstrumentationListener*> invoke_virtual_or_interface_listeners_
664 GUARDED_BY(Locks::mutator_lock_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000665 std::list<InstrumentationListener*> dex_pc_listeners_ GUARDED_BY(Locks::mutator_lock_);
666 std::list<InstrumentationListener*> field_read_listeners_ GUARDED_BY(Locks::mutator_lock_);
667 std::list<InstrumentationListener*> field_write_listeners_ GUARDED_BY(Locks::mutator_lock_);
Alex Light6e1607e2017-08-23 10:06:18 -0700668 std::list<InstrumentationListener*> exception_thrown_listeners_ GUARDED_BY(Locks::mutator_lock_);
Alex Lighte814f9d2017-07-31 16:14:39 -0700669 std::list<InstrumentationListener*> watched_frame_pop_listeners_ GUARDED_BY(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800670
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100671 // The set of methods being deoptimized (by the debugger) which must be executed with interpreter
672 // only.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700673 mutable ReaderWriterMutex deoptimized_methods_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700674 std::unordered_set<ArtMethod*> deoptimized_methods_ GUARDED_BY(deoptimized_methods_lock_);
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100675 bool deoptimization_enabled_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100676
Ian Rogersfa824272013-11-05 16:12:57 -0800677 // Current interpreter handler table. This is updated each time the thread state flags are
678 // modified.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200679 InterpreterHandlerTable interpreter_handler_table_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200680
Ian Rogersfa824272013-11-05 16:12:57 -0800681 // Greater than 0 if quick alloc entry points instrumented.
Mathieu Chartiereebc3af2016-02-29 18:13:38 -0800682 size_t quick_alloc_entry_points_instrumentation_counter_;
Mathieu Chartier50e93312016-03-16 11:25:29 -0700683
684 // alloc_entrypoints_instrumented_ is only updated with all the threads suspended, this is done
685 // to prevent races with the GC where the GC relies on thread suspension only see
686 // alloc_entrypoints_instrumented_ change during suspend points.
687 bool alloc_entrypoints_instrumented_;
688
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200689 friend class InstrumentationTest; // For GetCurrentInstrumentationLevel and ConfigureStubs.
690
jeffhao725a9572012-11-13 18:20:12 -0800691 DISALLOW_COPY_AND_ASSIGN(Instrumentation);
692};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700693std::ostream& operator<<(std::ostream& os, const Instrumentation::InstrumentationEvent& rhs);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200694std::ostream& operator<<(std::ostream& os, const Instrumentation::InstrumentationLevel& rhs);
jeffhao725a9572012-11-13 18:20:12 -0800695
Ian Rogers62d6c772013-02-27 08:32:07 -0800696// An element in the instrumentation side stack maintained in art::Thread.
697struct InstrumentationStackFrame {
Mingyao Yang047abb22017-08-23 15:26:57 -0700698 InstrumentationStackFrame(mirror::Object* this_object,
699 ArtMethod* method,
700 uintptr_t return_pc,
701 size_t frame_id,
702 bool interpreter_entry)
703 : this_object_(this_object),
704 method_(method),
705 return_pc_(return_pc),
706 frame_id_(frame_id),
Jeff Hao9a916d32013-06-27 18:45:37 -0700707 interpreter_entry_(interpreter_entry) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800708 }
709
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700710 std::string Dump() const REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800711
712 mirror::Object* this_object_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700713 ArtMethod* method_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100714 uintptr_t return_pc_;
715 size_t frame_id_;
716 bool interpreter_entry_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800717};
718
719} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -0800720} // namespace art
721
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700722#endif // ART_RUNTIME_INSTRUMENTATION_H_