blob: 05c0aaa081d3c7a31f515a9e4ffe4bdfd49b6dcf [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;
Ian Rogers62d6c772013-02-27 08:32:07 -080039union JValue;
jeffhao725a9572012-11-13 18:20:12 -080040class Thread;
41
Ian Rogers62d6c772013-02-27 08:32:07 -080042namespace instrumentation {
jeffhao725a9572012-11-13 18:20:12 -080043
Sebastien Hertzee1997a2013-09-19 14:47:09 +020044// Interpreter handler tables.
45enum InterpreterHandlerTable {
46 kMainHandlerTable = 0, // Main handler table: no suspend check, no instrumentation.
47 kAlternativeHandlerTable = 1, // Alternative handler table: suspend check and/or instrumentation
48 // enabled.
49 kNumHandlerTables
50};
51
Andreas Gampe40da2862015-02-27 12:49:04 -080052// Do we want to deoptimize for method entry and exit listeners or just try to intercept
53// invocations? Deoptimization forces all code to run in the interpreter and considerably hurts the
54// application's performance.
55static constexpr bool kDeoptimizeForAccurateMethodEntryExitListeners = true;
56
Ian Rogers62d6c772013-02-27 08:32:07 -080057// Instrumentation event listener API. Registered listeners will get the appropriate call back for
58// the events they are listening for. The call backs supply the thread, method and dex_pc the event
59// occurred upon. The thread may or may not be Thread::Current().
60struct InstrumentationListener {
61 InstrumentationListener() {}
62 virtual ~InstrumentationListener() {}
63
64 // Call-back for when a method is entered.
65 virtual void MethodEntered(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -070066 ArtMethod* method,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070067 uint32_t dex_pc) REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -080068
69 // Call-back for when a method is exited.
Ian Rogers62d6c772013-02-27 08:32:07 -080070 virtual void MethodExited(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -070071 ArtMethod* method, uint32_t dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -080072 const JValue& return_value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070073 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -080074
75 // Call-back for when a method is popped due to an exception throw. A method will either cause a
76 // MethodExited call-back or a MethodUnwind call-back when its activation is removed.
Sebastien Hertz51db44a2013-11-19 10:00:29 +010077 virtual void MethodUnwind(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -070078 ArtMethod* method, uint32_t dex_pc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070079 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -080080
81 // Call-back for when the dex pc moves in a method.
82 virtual void DexPcMoved(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -070083 ArtMethod* method, uint32_t new_dex_pc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070084 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -080085
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +020086 // Call-back for when we read from a field.
Mathieu Chartiere401d142015-04-22 13:56:20 -070087 virtual void FieldRead(Thread* thread, mirror::Object* this_object, ArtMethod* method,
Mathieu Chartierc7853442015-03-27 14:35:38 -070088 uint32_t dex_pc, ArtField* field) = 0;
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +020089
90 // Call-back for when we write into a field.
Mathieu Chartiere401d142015-04-22 13:56:20 -070091 virtual void FieldWritten(Thread* thread, mirror::Object* this_object, ArtMethod* method,
Mathieu Chartierc7853442015-03-27 14:35:38 -070092 uint32_t dex_pc, ArtField* field, const JValue& field_value) = 0;
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +020093
Ian Rogers62d6c772013-02-27 08:32:07 -080094 // Call-back when an exception is caught.
Nicolas Geoffray14691c52015-03-05 10:40:17 +000095 virtual void ExceptionCaught(Thread* thread, mirror::Throwable* exception_object)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070096 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080097
Nicolas Geoffray81f0f952016-01-20 16:25:19 +000098 // Call-back for when we execute a branch.
99 virtual void Branch(Thread* thread,
100 ArtMethod* method,
101 uint32_t dex_pc,
102 int32_t dex_pc_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700103 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100104
105 // Call-back for when we get an invokevirtual or an invokeinterface.
106 virtual void InvokeVirtualOrInterface(Thread* thread,
107 mirror::Object* this_object,
108 ArtMethod* caller,
109 uint32_t dex_pc,
110 ArtMethod* callee)
Mathieu Chartier3fdb3fe2016-01-14 10:24:28 -0800111 REQUIRES(Roles::uninterruptible_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700112 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
jeffhao725a9572012-11-13 18:20:12 -0800113};
114
Ian Rogers62d6c772013-02-27 08:32:07 -0800115// Instrumentation is a catch-all for when extra information is required from the runtime. The
116// typical use for instrumentation is for profiling and debugging. Instrumentation may add stubs
117// to method entry and exit, it may also force execution to be switched to the interpreter and
118// trigger deoptimization.
jeffhao725a9572012-11-13 18:20:12 -0800119class Instrumentation {
120 public:
Ian Rogers62d6c772013-02-27 08:32:07 -0800121 enum InstrumentationEvent {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800122 kMethodEntered = 0x1,
123 kMethodExited = 0x2,
124 kMethodUnwind = 0x4,
125 kDexPcMoved = 0x8,
126 kFieldRead = 0x10,
127 kFieldWritten = 0x20,
128 kExceptionCaught = 0x40,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000129 kBranch = 0x80,
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100130 kInvokeVirtualOrInterface = 0x100,
Ian Rogers62d6c772013-02-27 08:32:07 -0800131 };
jeffhao725a9572012-11-13 18:20:12 -0800132
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200133 enum class InstrumentationLevel {
134 kInstrumentNothing, // execute without instrumentation
135 kInstrumentWithInstrumentationStubs, // execute with instrumentation entry/exit stubs
Alex Lightdba61482016-12-21 08:20:29 -0800136 kInstrumentWithInterpreterAndJit, // execute with interpreter initially and later the JIT
137 // (if it is enabled). This level is special in that it
138 // always requires re-instrumentation.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200139 kInstrumentWithInterpreter // execute with interpreter
140 };
141
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700142 Instrumentation();
jeffhao725a9572012-11-13 18:20:12 -0800143
Ian Rogers62d6c772013-02-27 08:32:07 -0800144 // Add a listener to be notified of the masked together sent of instrumentation events. This
145 // suspend the runtime to install stubs. You are expected to hold the mutator lock as a proxy
146 // for saying you should have suspended all threads (installing stubs while threads are running
147 // will break).
148 void AddListener(InstrumentationListener* listener, uint32_t events)
Mathieu Chartier90443472015-07-16 20:32:27 -0700149 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800150
Ian Rogers62d6c772013-02-27 08:32:07 -0800151 // Removes a listener possibly removing instrumentation stubs.
152 void RemoveListener(InstrumentationListener* listener, uint32_t events)
Mathieu Chartier90443472015-07-16 20:32:27 -0700153 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800154
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100155 // Deoptimization.
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100156 void EnableDeoptimization()
Mathieu Chartieraa516822015-10-02 15:53:37 -0700157 REQUIRES(Locks::mutator_lock_)
158 REQUIRES(!deoptimized_methods_lock_);
159 // Calls UndeoptimizeEverything which may visit class linker classes through ConfigureStubs.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200160 void DisableDeoptimization(const char* key)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700161 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
162 REQUIRES(!deoptimized_methods_lock_);
163
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100164 bool AreAllMethodsDeoptimized() const {
165 return interpreter_stubs_installed_;
166 }
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700167 bool ShouldNotifyMethodEnterExitEvents() const REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100168
Alex Lightdba61482016-12-21 08:20:29 -0800169 // Executes everything with the interpreter/jit (if available).
170 void ReJitEverything(const char* key)
171 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
172 REQUIRES(!Locks::thread_list_lock_,
173 !Locks::classlinker_classes_lock_,
174 !deoptimized_methods_lock_);
175
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100176 // Executes everything with interpreter.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200177 void DeoptimizeEverything(const char* key)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700178 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
179 REQUIRES(!Locks::thread_list_lock_,
180 !Locks::classlinker_classes_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700181 !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100182
Mathieu Chartieraa516822015-10-02 15:53:37 -0700183 // Executes everything with compiled code (or interpreter if there is no code). May visit class
184 // linker classes through ConfigureStubs.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200185 void UndeoptimizeEverything(const char* key)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700186 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
187 REQUIRES(!Locks::thread_list_lock_,
188 !Locks::classlinker_classes_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700189 !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100190
191 // Deoptimize a method by forcing its execution with the interpreter. Nevertheless, a static
192 // method (except a class initializer) set to the resolution trampoline will be deoptimized only
193 // once its declaring class is initialized.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700194 void Deoptimize(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700195 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100196
197 // Undeoptimze the method by restoring its entrypoints. Nevertheless, a static method
198 // (except a class initializer) set to the resolution trampoline will be updated only once its
199 // declaring class is initialized.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700200 void Undeoptimize(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700201 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100202
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200203 // Indicates whether the method has been deoptimized so it is executed with the interpreter.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700204 bool IsDeoptimized(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700205 REQUIRES(!deoptimized_methods_lock_) REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100206
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200207 // Enable method tracing by installing instrumentation entry/exit stubs or interpreter.
208 void EnableMethodTracing(const char* key,
209 bool needs_interpreter = kDeoptimizeForAccurateMethodEntryExitListeners)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700210 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
211 REQUIRES(!Locks::thread_list_lock_,
212 !Locks::classlinker_classes_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700213 !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100214
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200215 // Disable method tracing by uninstalling instrumentation entry/exit stubs or interpreter.
216 void DisableMethodTracing(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
Sebastien Hertzed2be172014-08-19 15:33:43 +0200222 InterpreterHandlerTable GetInterpreterHandlerTable() const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700223 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200224 return interpreter_handler_table_;
225 }
226
Mathieu Chartier90443472015-07-16 20:32:27 -0700227 void InstrumentQuickAllocEntryPoints() REQUIRES(!Locks::instrument_entrypoints_lock_);
228 void UninstrumentQuickAllocEntryPoints() REQUIRES(!Locks::instrument_entrypoints_lock_);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700229 void InstrumentQuickAllocEntryPointsLocked()
Mathieu Chartier90443472015-07-16 20:32:27 -0700230 REQUIRES(Locks::instrument_entrypoints_lock_, !Locks::thread_list_lock_,
231 !Locks::runtime_shutdown_lock_);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700232 void UninstrumentQuickAllocEntryPointsLocked()
Mathieu Chartier90443472015-07-16 20:32:27 -0700233 REQUIRES(Locks::instrument_entrypoints_lock_, !Locks::thread_list_lock_,
234 !Locks::runtime_shutdown_lock_);
235 void ResetQuickAllocEntryPoints() REQUIRES(Locks::runtime_shutdown_lock_);
Ian Rogersfa824272013-11-05 16:12:57 -0800236
Ian Rogers62d6c772013-02-27 08:32:07 -0800237 // Update the code of a method respecting any installed stubs.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700238 void UpdateMethodsCode(ArtMethod* method, const void* quick_code)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700239 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800240
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700241 // Update the code of a method respecting any installed stubs from debugger.
242 void UpdateMethodsCodeFromDebugger(ArtMethod* method, const void* quick_code)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700243 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700244
Ian Rogers62d6c772013-02-27 08:32:07 -0800245 // Get the quick code for the given method. More efficient than asking the class linker as it
246 // will short-cut to GetCode if instrumentation and static method resolution stubs aren't
247 // installed.
Andreas Gampe542451c2016-07-26 09:02:02 -0700248 const void* GetQuickCodeFor(ArtMethod* method, PointerSize pointer_size) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700249 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800250
251 void ForceInterpretOnly() {
252 interpret_only_ = true;
253 forced_interpret_only_ = true;
254 }
255
Brian Carlstromea46f952013-07-30 01:26:50 -0700256 // Called by ArtMethod::Invoke to determine dispatch mechanism.
Ian Rogers62d6c772013-02-27 08:32:07 -0800257 bool InterpretOnly() const {
258 return interpret_only_;
259 }
260
Hiroshi Yamauchi563b47c2014-02-28 17:18:37 -0800261 bool IsForcedInterpretOnly() const {
262 return forced_interpret_only_;
263 }
264
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800265 // Code is in boot image oat file which isn't compiled as debuggable.
266 // Need debug version (interpreter or jitted) if that's the case.
267 bool NeedDebugVersionForBootImageCode(ArtMethod* method, const void* code) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700268 REQUIRES_SHARED(Locks::mutator_lock_);
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800269
Ian Rogers62d6c772013-02-27 08:32:07 -0800270 bool AreExitStubsInstalled() const {
271 return instrumentation_stubs_installed_;
272 }
273
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700274 bool HasMethodEntryListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200275 return have_method_entry_listeners_;
276 }
277
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700278 bool HasMethodExitListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200279 return have_method_exit_listeners_;
280 }
281
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700282 bool HasMethodUnwindListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200283 return have_method_unwind_listeners_;
284 }
285
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700286 bool HasDexPcListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200287 return have_dex_pc_listeners_;
288 }
289
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700290 bool HasFieldReadListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200291 return have_field_read_listeners_;
292 }
293
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700294 bool HasFieldWriteListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200295 return have_field_write_listeners_;
296 }
297
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700298 bool HasExceptionCaughtListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz9f102032014-05-23 08:59:42 +0200299 return have_exception_caught_listeners_;
300 }
301
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700302 bool HasBranchListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000303 return have_branch_listeners_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800304 }
305
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700306 bool HasInvokeVirtualOrInterfaceListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100307 return have_invoke_virtual_or_interface_listeners_;
308 }
309
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700310 bool IsActive() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200311 return have_dex_pc_listeners_ || have_method_entry_listeners_ || have_method_exit_listeners_ ||
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200312 have_field_read_listeners_ || have_field_write_listeners_ ||
Bill Buzbeefd522f92016-02-11 22:37:42 +0000313 have_exception_caught_listeners_ || have_method_unwind_listeners_ ||
314 have_branch_listeners_ || have_invoke_virtual_or_interface_listeners_;
315 }
316
317 // Any instrumentation *other* than what is needed for Jit profiling active?
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700318 bool NonJitProfilingActive() const REQUIRES_SHARED(Locks::mutator_lock_) {
Bill Buzbeefd522f92016-02-11 22:37:42 +0000319 return have_dex_pc_listeners_ || have_method_exit_listeners_ ||
320 have_field_read_listeners_ || have_field_write_listeners_ ||
Bill Buzbee1d011d92016-04-04 16:59:29 +0000321 have_exception_caught_listeners_ || have_method_unwind_listeners_ ||
322 have_branch_listeners_;
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200323 }
324
Ian Rogers62d6c772013-02-27 08:32:07 -0800325 // Inform listeners that a method has been entered. A dex PC is provided as we may install
326 // listeners into executing code and get method enter events for methods already on the stack.
327 void MethodEnterEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700328 ArtMethod* method, uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700329 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200330 if (UNLIKELY(HasMethodEntryListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800331 MethodEnterEventImpl(thread, this_object, method, dex_pc);
332 }
333 }
334
335 // Inform listeners that a method has been exited.
336 void MethodExitEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700337 ArtMethod* method, uint32_t dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800338 const JValue& return_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700339 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200340 if (UNLIKELY(HasMethodExitListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800341 MethodExitEventImpl(thread, this_object, method, dex_pc, return_value);
342 }
343 }
344
345 // Inform listeners that a method has been exited due to an exception.
346 void MethodUnwindEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700347 ArtMethod* method, uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700348 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800349
350 // Inform listeners that the dex pc has moved (only supported by the interpreter).
351 void DexPcMovedEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700352 ArtMethod* method, uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700353 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200354 if (UNLIKELY(HasDexPcListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800355 DexPcMovedEventImpl(thread, this_object, method, dex_pc);
356 }
357 }
358
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000359 // Inform listeners that a branch has been taken (only supported by the interpreter).
360 void Branch(Thread* thread, ArtMethod* method, uint32_t dex_pc, int32_t offset) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700361 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000362 if (UNLIKELY(HasBranchListeners())) {
363 BranchImpl(thread, method, dex_pc, offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800364 }
365 }
366
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200367 // Inform listeners that we read a field (only supported by the interpreter).
368 void FieldReadEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700369 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700370 ArtField* field) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700371 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200372 if (UNLIKELY(HasFieldReadListeners())) {
373 FieldReadEventImpl(thread, this_object, method, dex_pc, field);
374 }
375 }
376
377 // Inform listeners that we write a field (only supported by the interpreter).
378 void FieldWriteEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700379 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700380 ArtField* field, const JValue& field_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700381 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200382 if (UNLIKELY(HasFieldWriteListeners())) {
383 FieldWriteEventImpl(thread, this_object, method, dex_pc, field, field_value);
384 }
385 }
386
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100387 void InvokeVirtualOrInterface(Thread* thread,
388 mirror::Object* this_object,
389 ArtMethod* caller,
390 uint32_t dex_pc,
391 ArtMethod* callee) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700392 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100393 if (UNLIKELY(HasInvokeVirtualOrInterfaceListeners())) {
394 InvokeVirtualOrInterfaceImpl(thread, this_object, caller, dex_pc, callee);
395 }
396 }
397
Ian Rogers62d6c772013-02-27 08:32:07 -0800398 // Inform listeners that an exception was caught.
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000399 void ExceptionCaughtEvent(Thread* thread, mirror::Throwable* exception_object) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700400 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800401
402 // Called when an instrumented method is entered. The intended link register (lr) is saved so
403 // that returning causes a branch to the method exit stub. Generates method enter events.
404 void PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700405 ArtMethod* method, uintptr_t lr,
Jeff Hao9a916d32013-06-27 18:45:37 -0700406 bool interpreter_entry)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700407 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800408
409 // Called when an instrumented method is exited. Removes the pushed instrumentation frame
410 // returning the intended link register. Generates method exit events.
Andreas Gamped58342c2014-06-05 14:18:08 -0700411 TwoWordReturn PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc,
412 uint64_t gpr_result, uint64_t fpr_result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700413 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800414
415 // Pops an instrumentation frame from the current thread and generate an unwind event.
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700416 // Returns the return pc for the instrumentation frame that's popped.
417 uintptr_t PopMethodForUnwind(Thread* self, bool is_deoptimization) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700418 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800419
420 // Call back for configure stubs.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700421 void InstallStubsForClass(mirror::Class* klass) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700422 REQUIRES(!deoptimized_methods_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800423
Mathieu Chartiere401d142015-04-22 13:56:20 -0700424 void InstallStubsForMethod(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700425 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100426
Mingyao Yang99170c62015-07-06 11:10:37 -0700427 // Install instrumentation exit stub on every method of the stack of the given thread.
428 // This is used by the debugger to cause a deoptimization of the thread's stack after updating
429 // local variable(s).
430 void InstrumentThreadStack(Thread* thread)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700431 REQUIRES_SHARED(Locks::mutator_lock_)
Mingyao Yang99170c62015-07-06 11:10:37 -0700432 REQUIRES(!Locks::thread_list_lock_);
433
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000434 static size_t ComputeFrameId(Thread* self,
435 size_t frame_depth,
436 size_t inlined_frames_before_frame)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700437 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000438
Mathieu Chartiereebc3af2016-02-29 18:13:38 -0800439 // Does not hold lock, used to check if someone changed from not instrumented to instrumented
440 // during a GC suspend point.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700441 bool AllocEntrypointsInstrumented() const REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier50e93312016-03-16 11:25:29 -0700442 return alloc_entrypoints_instrumented_;
Mathieu Chartiereebc3af2016-02-29 18:13:38 -0800443 }
444
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200445 InstrumentationLevel GetCurrentInstrumentationLevel() const;
446
Alex Lightdba61482016-12-21 08:20:29 -0800447 private:
448 // Returns true if moving to the given instrumentation level requires the installation of stubs.
449 // False otherwise.
450 bool RequiresInstrumentationInstallation(InstrumentationLevel new_level) const;
451
Ian Rogers62d6c772013-02-27 08:32:07 -0800452 // Does the job of installing or removing instrumentation code within methods.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200453 // In order to support multiple clients using instrumentation at the same time,
454 // the caller must pass a unique key (a string) identifying it so we remind which
455 // instrumentation level it needs. Therefore the current instrumentation level
456 // becomes the highest instrumentation level required by a client.
457 void ConfigureStubs(const char* key, InstrumentationLevel desired_instrumentation_level)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700458 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
459 REQUIRES(!deoptimized_methods_lock_,
460 !Locks::thread_list_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700461 !Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800462
Mathieu Chartier90443472015-07-16 20:32:27 -0700463 void UpdateInterpreterHandlerTable() REQUIRES(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800464 /*
465 * TUNING: Dalvik's mterp stashes the actual current handler table base in a
466 * tls field. For Arm, this enables all suspend, debug & tracing checks to be
467 * collapsed into a single conditionally-executed ldw instruction.
468 * Move to Dalvik-style handler-table management for both the goto interpreter and
469 * mterp.
470 */
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200471 interpreter_handler_table_ = IsActive() ? kAlternativeHandlerTable : kMainHandlerTable;
472 }
473
Mathieu Chartier661974a2014-01-09 11:23:53 -0800474 // No thread safety analysis to get around SetQuickAllocEntryPointsInstrumented requiring
475 // exclusive access to mutator lock which you can't get if the runtime isn't started.
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700476 void SetEntrypointsInstrumented(bool instrumented) NO_THREAD_SAFETY_ANALYSIS;
Mathieu Chartier661974a2014-01-09 11:23:53 -0800477
Ian Rogers62d6c772013-02-27 08:32:07 -0800478 void MethodEnterEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700479 ArtMethod* method, uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700480 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800481 void MethodExitEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700482 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800483 uint32_t dex_pc, const JValue& return_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700484 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800485 void DexPcMovedEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700486 ArtMethod* method, uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700487 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000488 void BranchImpl(Thread* thread, ArtMethod* method, uint32_t dex_pc, int32_t offset) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700489 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100490 void InvokeVirtualOrInterfaceImpl(Thread* thread,
491 mirror::Object* this_object,
492 ArtMethod* caller,
493 uint32_t dex_pc,
494 ArtMethod* callee) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700495 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200496 void FieldReadEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700497 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700498 ArtField* field) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700499 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200500 void FieldWriteEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700501 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700502 ArtField* field, const JValue& field_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700503 REQUIRES_SHARED(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800504
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700505 // Read barrier-aware utility functions for accessing deoptimized_methods_
Mathieu Chartiere401d142015-04-22 13:56:20 -0700506 bool AddDeoptimizedMethod(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700507 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700508 bool IsDeoptimizedMethod(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700509 REQUIRES_SHARED(Locks::mutator_lock_, deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700510 bool RemoveDeoptimizedMethod(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700511 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700512 ArtMethod* BeginDeoptimizedMethod()
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700513 REQUIRES_SHARED(Locks::mutator_lock_, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700514 bool IsDeoptimizedMethodsEmpty() const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700515 REQUIRES_SHARED(Locks::mutator_lock_, deoptimized_methods_lock_);
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700516 void UpdateMethodsCodeImpl(ArtMethod* method, const void* quick_code)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700517 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700518
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700519
Brian Carlstromea46f952013-07-30 01:26:50 -0700520 // Have we hijacked ArtMethod::code_ so that it calls instrumentation/interpreter code?
Ian Rogers62d6c772013-02-27 08:32:07 -0800521 bool instrumentation_stubs_installed_;
522
Brian Carlstromea46f952013-07-30 01:26:50 -0700523 // Have we hijacked ArtMethod::code_ to reference the enter/exit stubs?
Ian Rogers62d6c772013-02-27 08:32:07 -0800524 bool entry_exit_stubs_installed_;
525
Brian Carlstromea46f952013-07-30 01:26:50 -0700526 // Have we hijacked ArtMethod::code_ to reference the enter interpreter stub?
Ian Rogers62d6c772013-02-27 08:32:07 -0800527 bool interpreter_stubs_installed_;
528
529 // Do we need the fidelity of events that we only get from running within the interpreter?
530 bool interpret_only_;
531
532 // Did the runtime request we only run in the interpreter? ie -Xint mode.
533 bool forced_interpret_only_;
534
535 // Do we have any listeners for method entry events? Short-cut to avoid taking the
536 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200537 bool have_method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800538
539 // Do we have any listeners for method exit events? Short-cut to avoid taking the
540 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200541 bool have_method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800542
543 // Do we have any listeners for method unwind events? Short-cut to avoid taking the
544 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200545 bool have_method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800546
547 // Do we have any listeners for dex move events? Short-cut to avoid taking the
548 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200549 bool have_dex_pc_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800550
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200551 // Do we have any listeners for field read events? Short-cut to avoid taking the
552 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200553 bool have_field_read_listeners_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200554
555 // Do we have any listeners for field write events? Short-cut to avoid taking the
556 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200557 bool have_field_write_listeners_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200558
Ian Rogers62d6c772013-02-27 08:32:07 -0800559 // Do we have any exception caught listeners? Short-cut to avoid taking the instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200560 bool have_exception_caught_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800561
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000562 // Do we have any branch listeners? Short-cut to avoid taking the instrumentation_lock_.
563 bool have_branch_listeners_ GUARDED_BY(Locks::mutator_lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800564
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100565 // Do we have any invoke listeners? Short-cut to avoid taking the instrumentation_lock_.
566 bool have_invoke_virtual_or_interface_listeners_ GUARDED_BY(Locks::mutator_lock_);
567
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200568 // Contains the instrumentation level required by each client of the instrumentation identified
569 // by a string key.
570 typedef SafeMap<const char*, InstrumentationLevel> InstrumentationLevelTable;
571 InstrumentationLevelTable requested_instrumentation_levels_ GUARDED_BY(Locks::mutator_lock_);
572
Ian Rogers62d6c772013-02-27 08:32:07 -0800573 // The event listeners, written to with the mutator_lock_ exclusively held.
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000574 // Mutators must be able to iterate over these lists concurrently, that is, with listeners being
575 // added or removed while iterating. The modifying thread holds exclusive lock,
576 // so other threads cannot iterate (i.e. read the data of the list) at the same time but they
577 // do keep iterators that need to remain valid. This is the reason these listeners are std::list
578 // and not for example std::vector: the existing storage for a std::list does not move.
579 // Note that mutators cannot make a copy of these lists before iterating, as the instrumentation
580 // listeners can also be deleted concurrently.
581 // As a result, these lists are never trimmed. That's acceptable given the low number of
582 // listeners we have.
Ian Rogers62d6c772013-02-27 08:32:07 -0800583 std::list<InstrumentationListener*> method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_);
584 std::list<InstrumentationListener*> method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_);
585 std::list<InstrumentationListener*> method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000586 std::list<InstrumentationListener*> branch_listeners_ GUARDED_BY(Locks::mutator_lock_);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100587 std::list<InstrumentationListener*> invoke_virtual_or_interface_listeners_
588 GUARDED_BY(Locks::mutator_lock_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000589 std::list<InstrumentationListener*> dex_pc_listeners_ GUARDED_BY(Locks::mutator_lock_);
590 std::list<InstrumentationListener*> field_read_listeners_ GUARDED_BY(Locks::mutator_lock_);
591 std::list<InstrumentationListener*> field_write_listeners_ GUARDED_BY(Locks::mutator_lock_);
592 std::list<InstrumentationListener*> exception_caught_listeners_ GUARDED_BY(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800593
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100594 // The set of methods being deoptimized (by the debugger) which must be executed with interpreter
595 // only.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700596 mutable ReaderWriterMutex deoptimized_methods_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700597 std::unordered_set<ArtMethod*> deoptimized_methods_ GUARDED_BY(deoptimized_methods_lock_);
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100598 bool deoptimization_enabled_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100599
Ian Rogersfa824272013-11-05 16:12:57 -0800600 // Current interpreter handler table. This is updated each time the thread state flags are
601 // modified.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200602 InterpreterHandlerTable interpreter_handler_table_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200603
Ian Rogersfa824272013-11-05 16:12:57 -0800604 // Greater than 0 if quick alloc entry points instrumented.
Mathieu Chartiereebc3af2016-02-29 18:13:38 -0800605 size_t quick_alloc_entry_points_instrumentation_counter_;
Mathieu Chartier50e93312016-03-16 11:25:29 -0700606
607 // alloc_entrypoints_instrumented_ is only updated with all the threads suspended, this is done
608 // to prevent races with the GC where the GC relies on thread suspension only see
609 // alloc_entrypoints_instrumented_ change during suspend points.
610 bool alloc_entrypoints_instrumented_;
611
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200612 friend class InstrumentationTest; // For GetCurrentInstrumentationLevel and ConfigureStubs.
613
jeffhao725a9572012-11-13 18:20:12 -0800614 DISALLOW_COPY_AND_ASSIGN(Instrumentation);
615};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700616std::ostream& operator<<(std::ostream& os, const Instrumentation::InstrumentationEvent& rhs);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200617std::ostream& operator<<(std::ostream& os, const Instrumentation::InstrumentationLevel& rhs);
jeffhao725a9572012-11-13 18:20:12 -0800618
Ian Rogers62d6c772013-02-27 08:32:07 -0800619// An element in the instrumentation side stack maintained in art::Thread.
620struct InstrumentationStackFrame {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700621 InstrumentationStackFrame(mirror::Object* this_object, ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -0700622 uintptr_t return_pc, size_t frame_id, bool interpreter_entry)
623 : this_object_(this_object), method_(method), return_pc_(return_pc), frame_id_(frame_id),
624 interpreter_entry_(interpreter_entry) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800625 }
626
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700627 std::string Dump() const REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800628
629 mirror::Object* this_object_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700630 ArtMethod* method_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100631 uintptr_t return_pc_;
632 size_t frame_id_;
633 bool interpreter_entry_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800634};
635
636} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -0800637} // namespace art
638
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700639#endif // ART_RUNTIME_INSTRUMENTATION_H_