blob: 9969489648ab958253596d78966e39af9de2531c [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;
jeffhao725a9572012-11-13 18:20:12 -080041class Thread;
42
Ian Rogers62d6c772013-02-27 08:32:07 -080043namespace instrumentation {
jeffhao725a9572012-11-13 18:20:12 -080044
Sebastien Hertzee1997a2013-09-19 14:47:09 +020045// Interpreter handler tables.
46enum InterpreterHandlerTable {
47 kMainHandlerTable = 0, // Main handler table: no suspend check, no instrumentation.
48 kAlternativeHandlerTable = 1, // Alternative handler table: suspend check and/or instrumentation
49 // enabled.
50 kNumHandlerTables
51};
52
Andreas Gampe40da2862015-02-27 12:49:04 -080053// Do we want to deoptimize for method entry and exit listeners or just try to intercept
54// invocations? Deoptimization forces all code to run in the interpreter and considerably hurts the
55// application's performance.
56static constexpr bool kDeoptimizeForAccurateMethodEntryExitListeners = true;
57
Ian Rogers62d6c772013-02-27 08:32:07 -080058// Instrumentation event listener API. Registered listeners will get the appropriate call back for
59// the events they are listening for. The call backs supply the thread, method and dex_pc the event
60// occurred upon. The thread may or may not be Thread::Current().
61struct InstrumentationListener {
62 InstrumentationListener() {}
63 virtual ~InstrumentationListener() {}
64
65 // Call-back for when a method is entered.
Alex Lightd7661582017-05-01 13:48:16 -070066 virtual void MethodEntered(Thread* thread,
67 Handle<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -070068 ArtMethod* method,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070069 uint32_t dex_pc) REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -080070
Alex Lightd7661582017-05-01 13:48:16 -070071 virtual void MethodExited(Thread* thread,
72 Handle<mirror::Object> this_object,
73 ArtMethod* method,
74 uint32_t dex_pc,
75 Handle<mirror::Object> return_value)
76 REQUIRES_SHARED(Locks::mutator_lock_);
77
78 // Call-back for when a method is exited. The implementor should either handler-ize the return
79 // value (if appropriate) or use the alternate MethodExited callback instead if they need to
80 // go through a suspend point.
81 virtual void MethodExited(Thread* thread,
82 Handle<mirror::Object> this_object,
83 ArtMethod* method,
84 uint32_t dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -080085 const JValue& return_value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070086 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -080087
88 // Call-back for when a method is popped due to an exception throw. A method will either cause a
89 // MethodExited call-back or a MethodUnwind call-back when its activation is removed.
Alex Lightd7661582017-05-01 13:48:16 -070090 virtual void MethodUnwind(Thread* thread,
91 Handle<mirror::Object> this_object,
92 ArtMethod* method,
93 uint32_t dex_pc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070094 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -080095
96 // Call-back for when the dex pc moves in a method.
Alex Lightd7661582017-05-01 13:48:16 -070097 virtual void DexPcMoved(Thread* thread,
98 Handle<mirror::Object> this_object,
99 ArtMethod* method,
100 uint32_t new_dex_pc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700101 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -0800102
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200103 // Call-back for when we read from a field.
Alex Lightd7661582017-05-01 13:48:16 -0700104 virtual void FieldRead(Thread* thread,
105 Handle<mirror::Object> this_object,
106 ArtMethod* method,
107 uint32_t dex_pc,
108 ArtField* field) = 0;
109
110 virtual void FieldWritten(Thread* thread,
111 Handle<mirror::Object> this_object,
112 ArtMethod* method,
113 uint32_t dex_pc,
114 ArtField* field,
115 Handle<mirror::Object> field_value)
116 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200117
118 // Call-back for when we write into a field.
Alex Lightd7661582017-05-01 13:48:16 -0700119 virtual void FieldWritten(Thread* thread,
120 Handle<mirror::Object> this_object,
121 ArtMethod* method,
122 uint32_t dex_pc,
123 ArtField* field,
124 const JValue& field_value)
125 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200126
Ian Rogers62d6c772013-02-27 08:32:07 -0800127 // Call-back when an exception is caught.
Alex Lightd7661582017-05-01 13:48:16 -0700128 virtual void ExceptionCaught(Thread* thread,
129 Handle<mirror::Throwable> exception_object)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700130 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800131
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000132 // Call-back for when we execute a branch.
133 virtual void Branch(Thread* thread,
134 ArtMethod* method,
135 uint32_t dex_pc,
136 int32_t dex_pc_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700137 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100138
139 // Call-back for when we get an invokevirtual or an invokeinterface.
140 virtual void InvokeVirtualOrInterface(Thread* thread,
Alex Lightd7661582017-05-01 13:48:16 -0700141 Handle<mirror::Object> this_object,
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100142 ArtMethod* caller,
143 uint32_t dex_pc,
144 ArtMethod* callee)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700145 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
jeffhao725a9572012-11-13 18:20:12 -0800146};
147
Ian Rogers62d6c772013-02-27 08:32:07 -0800148// Instrumentation is a catch-all for when extra information is required from the runtime. The
149// typical use for instrumentation is for profiling and debugging. Instrumentation may add stubs
150// to method entry and exit, it may also force execution to be switched to the interpreter and
151// trigger deoptimization.
jeffhao725a9572012-11-13 18:20:12 -0800152class Instrumentation {
153 public:
Ian Rogers62d6c772013-02-27 08:32:07 -0800154 enum InstrumentationEvent {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800155 kMethodEntered = 0x1,
156 kMethodExited = 0x2,
157 kMethodUnwind = 0x4,
158 kDexPcMoved = 0x8,
159 kFieldRead = 0x10,
160 kFieldWritten = 0x20,
161 kExceptionCaught = 0x40,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000162 kBranch = 0x80,
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100163 kInvokeVirtualOrInterface = 0x100,
Ian Rogers62d6c772013-02-27 08:32:07 -0800164 };
jeffhao725a9572012-11-13 18:20:12 -0800165
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200166 enum class InstrumentationLevel {
167 kInstrumentNothing, // execute without instrumentation
168 kInstrumentWithInstrumentationStubs, // execute with instrumentation entry/exit stubs
169 kInstrumentWithInterpreter // execute with interpreter
170 };
171
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700172 Instrumentation();
jeffhao725a9572012-11-13 18:20:12 -0800173
Ian Rogers62d6c772013-02-27 08:32:07 -0800174 // Add a listener to be notified of the masked together sent of instrumentation events. This
175 // suspend the runtime to install stubs. You are expected to hold the mutator lock as a proxy
176 // for saying you should have suspended all threads (installing stubs while threads are running
177 // will break).
178 void AddListener(InstrumentationListener* listener, uint32_t events)
Mathieu Chartier90443472015-07-16 20:32:27 -0700179 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800180
Ian Rogers62d6c772013-02-27 08:32:07 -0800181 // Removes a listener possibly removing instrumentation stubs.
182 void RemoveListener(InstrumentationListener* listener, uint32_t events)
Mathieu Chartier90443472015-07-16 20:32:27 -0700183 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800184
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100185 // Deoptimization.
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100186 void EnableDeoptimization()
Mathieu Chartieraa516822015-10-02 15:53:37 -0700187 REQUIRES(Locks::mutator_lock_)
188 REQUIRES(!deoptimized_methods_lock_);
189 // Calls UndeoptimizeEverything which may visit class linker classes through ConfigureStubs.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200190 void DisableDeoptimization(const char* key)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700191 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
192 REQUIRES(!deoptimized_methods_lock_);
193
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100194 bool AreAllMethodsDeoptimized() const {
195 return interpreter_stubs_installed_;
196 }
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700197 bool ShouldNotifyMethodEnterExitEvents() const REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100198
Alex Lightbebd7bd2017-07-25 14:05:52 -0700199 bool CanDeoptimize() {
200 return deoptimization_enabled_;
201 }
202
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100203 // Executes everything with interpreter.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200204 void DeoptimizeEverything(const char* key)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700205 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
206 REQUIRES(!Locks::thread_list_lock_,
207 !Locks::classlinker_classes_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700208 !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100209
Mathieu Chartieraa516822015-10-02 15:53:37 -0700210 // Executes everything with compiled code (or interpreter if there is no code). May visit class
211 // linker classes through ConfigureStubs.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200212 void UndeoptimizeEverything(const char* key)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700213 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
214 REQUIRES(!Locks::thread_list_lock_,
215 !Locks::classlinker_classes_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700216 !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100217
218 // Deoptimize a method by forcing its execution with the interpreter. Nevertheless, a static
219 // method (except a class initializer) set to the resolution trampoline will be deoptimized only
220 // once its declaring class is initialized.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700221 void Deoptimize(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700222 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100223
224 // Undeoptimze the method by restoring its entrypoints. Nevertheless, a static method
225 // (except a class initializer) set to the resolution trampoline will be updated only once its
226 // declaring class is initialized.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700227 void Undeoptimize(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700228 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100229
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200230 // Indicates whether the method has been deoptimized so it is executed with the interpreter.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700231 bool IsDeoptimized(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700232 REQUIRES(!deoptimized_methods_lock_) REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100233
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200234 // Enable method tracing by installing instrumentation entry/exit stubs or interpreter.
235 void EnableMethodTracing(const char* key,
236 bool needs_interpreter = kDeoptimizeForAccurateMethodEntryExitListeners)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700237 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
238 REQUIRES(!Locks::thread_list_lock_,
239 !Locks::classlinker_classes_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700240 !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100241
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200242 // Disable method tracing by uninstalling instrumentation entry/exit stubs or interpreter.
243 void DisableMethodTracing(const char* key)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700244 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
245 REQUIRES(!Locks::thread_list_lock_,
246 !Locks::classlinker_classes_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700247 !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100248
Sebastien Hertzed2be172014-08-19 15:33:43 +0200249 InterpreterHandlerTable GetInterpreterHandlerTable() const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700250 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200251 return interpreter_handler_table_;
252 }
253
Mathieu Chartier90443472015-07-16 20:32:27 -0700254 void InstrumentQuickAllocEntryPoints() REQUIRES(!Locks::instrument_entrypoints_lock_);
255 void UninstrumentQuickAllocEntryPoints() REQUIRES(!Locks::instrument_entrypoints_lock_);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700256 void InstrumentQuickAllocEntryPointsLocked()
Mathieu Chartier90443472015-07-16 20:32:27 -0700257 REQUIRES(Locks::instrument_entrypoints_lock_, !Locks::thread_list_lock_,
258 !Locks::runtime_shutdown_lock_);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700259 void UninstrumentQuickAllocEntryPointsLocked()
Mathieu Chartier90443472015-07-16 20:32:27 -0700260 REQUIRES(Locks::instrument_entrypoints_lock_, !Locks::thread_list_lock_,
261 !Locks::runtime_shutdown_lock_);
262 void ResetQuickAllocEntryPoints() REQUIRES(Locks::runtime_shutdown_lock_);
Ian Rogersfa824272013-11-05 16:12:57 -0800263
Ian Rogers62d6c772013-02-27 08:32:07 -0800264 // Update the code of a method respecting any installed stubs.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700265 void UpdateMethodsCode(ArtMethod* method, const void* quick_code)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700266 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800267
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700268 // Update the code of a method respecting any installed stubs from debugger.
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000269 void UpdateMethodsCodeForJavaDebuggable(ArtMethod* method, const void* quick_code)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700270 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700271
Ian Rogers62d6c772013-02-27 08:32:07 -0800272 // Get the quick code for the given method. More efficient than asking the class linker as it
273 // will short-cut to GetCode if instrumentation and static method resolution stubs aren't
274 // installed.
Andreas Gampe542451c2016-07-26 09:02:02 -0700275 const void* GetQuickCodeFor(ArtMethod* method, PointerSize pointer_size) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700276 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800277
278 void ForceInterpretOnly() {
279 interpret_only_ = true;
280 forced_interpret_only_ = true;
281 }
282
Brian Carlstromea46f952013-07-30 01:26:50 -0700283 // Called by ArtMethod::Invoke to determine dispatch mechanism.
Ian Rogers62d6c772013-02-27 08:32:07 -0800284 bool InterpretOnly() const {
285 return interpret_only_;
286 }
287
Hiroshi Yamauchi563b47c2014-02-28 17:18:37 -0800288 bool IsForcedInterpretOnly() const {
289 return forced_interpret_only_;
290 }
291
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800292 // Code is in boot image oat file which isn't compiled as debuggable.
293 // Need debug version (interpreter or jitted) if that's the case.
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000294 bool NeedDebugVersionFor(ArtMethod* method) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700295 REQUIRES_SHARED(Locks::mutator_lock_);
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800296
Ian Rogers62d6c772013-02-27 08:32:07 -0800297 bool AreExitStubsInstalled() const {
298 return instrumentation_stubs_installed_;
299 }
300
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700301 bool HasMethodEntryListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200302 return have_method_entry_listeners_;
303 }
304
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700305 bool HasMethodExitListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200306 return have_method_exit_listeners_;
307 }
308
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700309 bool HasMethodUnwindListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200310 return have_method_unwind_listeners_;
311 }
312
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700313 bool HasDexPcListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200314 return have_dex_pc_listeners_;
315 }
316
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700317 bool HasFieldReadListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200318 return have_field_read_listeners_;
319 }
320
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700321 bool HasFieldWriteListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200322 return have_field_write_listeners_;
323 }
324
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700325 bool HasExceptionCaughtListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz9f102032014-05-23 08:59:42 +0200326 return have_exception_caught_listeners_;
327 }
328
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700329 bool HasBranchListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000330 return have_branch_listeners_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800331 }
332
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700333 bool HasInvokeVirtualOrInterfaceListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100334 return have_invoke_virtual_or_interface_listeners_;
335 }
336
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700337 bool IsActive() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200338 return have_dex_pc_listeners_ || have_method_entry_listeners_ || have_method_exit_listeners_ ||
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200339 have_field_read_listeners_ || have_field_write_listeners_ ||
Bill Buzbeefd522f92016-02-11 22:37:42 +0000340 have_exception_caught_listeners_ || have_method_unwind_listeners_ ||
341 have_branch_listeners_ || have_invoke_virtual_or_interface_listeners_;
342 }
343
344 // Any instrumentation *other* than what is needed for Jit profiling active?
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700345 bool NonJitProfilingActive() const REQUIRES_SHARED(Locks::mutator_lock_) {
Bill Buzbeefd522f92016-02-11 22:37:42 +0000346 return have_dex_pc_listeners_ || have_method_exit_listeners_ ||
347 have_field_read_listeners_ || have_field_write_listeners_ ||
Bill Buzbee1d011d92016-04-04 16:59:29 +0000348 have_exception_caught_listeners_ || have_method_unwind_listeners_ ||
349 have_branch_listeners_;
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200350 }
351
Ian Rogers62d6c772013-02-27 08:32:07 -0800352 // Inform listeners that a method has been entered. A dex PC is provided as we may install
353 // listeners into executing code and get method enter events for methods already on the stack.
354 void MethodEnterEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700355 ArtMethod* method, uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700356 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200357 if (UNLIKELY(HasMethodEntryListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800358 MethodEnterEventImpl(thread, this_object, method, dex_pc);
359 }
360 }
361
362 // Inform listeners that a method has been exited.
Alex Lightd7661582017-05-01 13:48:16 -0700363 void MethodExitEvent(Thread* thread,
364 mirror::Object* this_object,
365 ArtMethod* method,
366 uint32_t dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800367 const JValue& return_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700368 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200369 if (UNLIKELY(HasMethodExitListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800370 MethodExitEventImpl(thread, this_object, method, dex_pc, return_value);
371 }
372 }
373
374 // Inform listeners that a method has been exited due to an exception.
375 void MethodUnwindEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700376 ArtMethod* method, uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700377 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800378
379 // Inform listeners that the dex pc has moved (only supported by the interpreter).
380 void DexPcMovedEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700381 ArtMethod* method, uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700382 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200383 if (UNLIKELY(HasDexPcListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800384 DexPcMovedEventImpl(thread, this_object, method, dex_pc);
385 }
386 }
387
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000388 // Inform listeners that a branch has been taken (only supported by the interpreter).
389 void Branch(Thread* thread, ArtMethod* method, uint32_t dex_pc, int32_t offset) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700390 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000391 if (UNLIKELY(HasBranchListeners())) {
392 BranchImpl(thread, method, dex_pc, offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800393 }
394 }
395
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200396 // Inform listeners that we read a field (only supported by the interpreter).
397 void FieldReadEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700398 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700399 ArtField* field) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700400 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200401 if (UNLIKELY(HasFieldReadListeners())) {
402 FieldReadEventImpl(thread, this_object, method, dex_pc, field);
403 }
404 }
405
406 // Inform listeners that we write a field (only supported by the interpreter).
407 void FieldWriteEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700408 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700409 ArtField* field, const JValue& field_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700410 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200411 if (UNLIKELY(HasFieldWriteListeners())) {
412 FieldWriteEventImpl(thread, this_object, method, dex_pc, field, field_value);
413 }
414 }
415
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100416 void InvokeVirtualOrInterface(Thread* thread,
417 mirror::Object* this_object,
418 ArtMethod* caller,
419 uint32_t dex_pc,
420 ArtMethod* callee) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700421 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100422 if (UNLIKELY(HasInvokeVirtualOrInterfaceListeners())) {
423 InvokeVirtualOrInterfaceImpl(thread, this_object, caller, dex_pc, callee);
424 }
425 }
426
Ian Rogers62d6c772013-02-27 08:32:07 -0800427 // Inform listeners that an exception was caught.
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000428 void ExceptionCaughtEvent(Thread* thread, mirror::Throwable* exception_object) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700429 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800430
431 // Called when an instrumented method is entered. The intended link register (lr) is saved so
432 // that returning causes a branch to the method exit stub. Generates method enter events.
433 void PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700434 ArtMethod* method, uintptr_t lr,
Jeff Hao9a916d32013-06-27 18:45:37 -0700435 bool interpreter_entry)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700436 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800437
438 // Called when an instrumented method is exited. Removes the pushed instrumentation frame
Alex Lightb7edcda2017-04-27 13:20:31 -0700439 // returning the intended link register. Generates method exit events. The gpr_result and
440 // fpr_result pointers are pointers to the locations where the integer/pointer and floating point
441 // result values of the function are stored. Both pointers must always be valid but the values
442 // held there will only be meaningful if interpreted as the appropriate type given the function
443 // being returned from.
Andreas Gamped58342c2014-06-05 14:18:08 -0700444 TwoWordReturn PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc,
Alex Lightb7edcda2017-04-27 13:20:31 -0700445 uint64_t* gpr_result, uint64_t* fpr_result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700446 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800447
448 // Pops an instrumentation frame from the current thread and generate an unwind event.
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700449 // Returns the return pc for the instrumentation frame that's popped.
450 uintptr_t PopMethodForUnwind(Thread* self, bool is_deoptimization) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700451 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800452
453 // Call back for configure stubs.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700454 void InstallStubsForClass(mirror::Class* klass) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700455 REQUIRES(!deoptimized_methods_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800456
Mathieu Chartiere401d142015-04-22 13:56:20 -0700457 void InstallStubsForMethod(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700458 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100459
Mingyao Yang99170c62015-07-06 11:10:37 -0700460 // Install instrumentation exit stub on every method of the stack of the given thread.
461 // This is used by the debugger to cause a deoptimization of the thread's stack after updating
462 // local variable(s).
463 void InstrumentThreadStack(Thread* thread)
Alex Light3ae82532017-07-26 13:59:07 -0700464 REQUIRES_SHARED(Locks::mutator_lock_);
Mingyao Yang99170c62015-07-06 11:10:37 -0700465
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000466 static size_t ComputeFrameId(Thread* self,
467 size_t frame_depth,
468 size_t inlined_frames_before_frame)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700469 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000470
Mathieu Chartiereebc3af2016-02-29 18:13:38 -0800471 // Does not hold lock, used to check if someone changed from not instrumented to instrumented
472 // during a GC suspend point.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700473 bool AllocEntrypointsInstrumented() const REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier50e93312016-03-16 11:25:29 -0700474 return alloc_entrypoints_instrumented_;
Mathieu Chartiereebc3af2016-02-29 18:13:38 -0800475 }
476
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200477 InstrumentationLevel GetCurrentInstrumentationLevel() const;
478
Alex Lightdba61482016-12-21 08:20:29 -0800479 private:
480 // Returns true if moving to the given instrumentation level requires the installation of stubs.
481 // False otherwise.
482 bool RequiresInstrumentationInstallation(InstrumentationLevel new_level) const;
483
Ian Rogers62d6c772013-02-27 08:32:07 -0800484 // Does the job of installing or removing instrumentation code within methods.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200485 // In order to support multiple clients using instrumentation at the same time,
486 // the caller must pass a unique key (a string) identifying it so we remind which
487 // instrumentation level it needs. Therefore the current instrumentation level
488 // becomes the highest instrumentation level required by a client.
489 void ConfigureStubs(const char* key, InstrumentationLevel desired_instrumentation_level)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700490 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
491 REQUIRES(!deoptimized_methods_lock_,
492 !Locks::thread_list_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700493 !Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800494
Mathieu Chartier90443472015-07-16 20:32:27 -0700495 void UpdateInterpreterHandlerTable() REQUIRES(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800496 /*
497 * TUNING: Dalvik's mterp stashes the actual current handler table base in a
498 * tls field. For Arm, this enables all suspend, debug & tracing checks to be
499 * collapsed into a single conditionally-executed ldw instruction.
500 * Move to Dalvik-style handler-table management for both the goto interpreter and
501 * mterp.
502 */
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200503 interpreter_handler_table_ = IsActive() ? kAlternativeHandlerTable : kMainHandlerTable;
504 }
505
Mathieu Chartier661974a2014-01-09 11:23:53 -0800506 // No thread safety analysis to get around SetQuickAllocEntryPointsInstrumented requiring
507 // exclusive access to mutator lock which you can't get if the runtime isn't started.
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700508 void SetEntrypointsInstrumented(bool instrumented) NO_THREAD_SAFETY_ANALYSIS;
Mathieu Chartier661974a2014-01-09 11:23:53 -0800509
Alex Lightd7661582017-05-01 13:48:16 -0700510 void MethodEnterEventImpl(Thread* thread,
511 ObjPtr<mirror::Object> this_object,
512 ArtMethod* method,
513 uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700514 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lightd7661582017-05-01 13:48:16 -0700515 void MethodExitEventImpl(Thread* thread,
516 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700517 ArtMethod* method,
Alex Lightd7661582017-05-01 13:48:16 -0700518 uint32_t dex_pc,
519 const JValue& return_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700520 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lightd7661582017-05-01 13:48:16 -0700521 void DexPcMovedEventImpl(Thread* thread,
522 ObjPtr<mirror::Object> this_object,
523 ArtMethod* method,
524 uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700525 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000526 void BranchImpl(Thread* thread, ArtMethod* method, uint32_t dex_pc, int32_t offset) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700527 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100528 void InvokeVirtualOrInterfaceImpl(Thread* thread,
Alex Lightd7661582017-05-01 13:48:16 -0700529 ObjPtr<mirror::Object> this_object,
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100530 ArtMethod* caller,
531 uint32_t dex_pc,
532 ArtMethod* callee) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700533 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lightd7661582017-05-01 13:48:16 -0700534 void FieldReadEventImpl(Thread* thread,
535 ObjPtr<mirror::Object> this_object,
536 ArtMethod* method,
537 uint32_t dex_pc,
538 ArtField* field) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700539 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lightd7661582017-05-01 13:48:16 -0700540 void FieldWriteEventImpl(Thread* thread,
541 ObjPtr<mirror::Object> this_object,
542 ArtMethod* method,
543 uint32_t dex_pc,
544 ArtField* field,
545 const JValue& field_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700546 REQUIRES_SHARED(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800547
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700548 // Read barrier-aware utility functions for accessing deoptimized_methods_
Mathieu Chartiere401d142015-04-22 13:56:20 -0700549 bool AddDeoptimizedMethod(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700550 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700551 bool IsDeoptimizedMethod(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700552 REQUIRES_SHARED(Locks::mutator_lock_, deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700553 bool RemoveDeoptimizedMethod(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700554 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700555 ArtMethod* BeginDeoptimizedMethod()
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700556 REQUIRES_SHARED(Locks::mutator_lock_, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700557 bool IsDeoptimizedMethodsEmpty() const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700558 REQUIRES_SHARED(Locks::mutator_lock_, deoptimized_methods_lock_);
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700559 void UpdateMethodsCodeImpl(ArtMethod* method, const void* quick_code)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700560 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700561
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700562
Brian Carlstromea46f952013-07-30 01:26:50 -0700563 // Have we hijacked ArtMethod::code_ so that it calls instrumentation/interpreter code?
Ian Rogers62d6c772013-02-27 08:32:07 -0800564 bool instrumentation_stubs_installed_;
565
Brian Carlstromea46f952013-07-30 01:26:50 -0700566 // Have we hijacked ArtMethod::code_ to reference the enter/exit stubs?
Ian Rogers62d6c772013-02-27 08:32:07 -0800567 bool entry_exit_stubs_installed_;
568
Brian Carlstromea46f952013-07-30 01:26:50 -0700569 // Have we hijacked ArtMethod::code_ to reference the enter interpreter stub?
Ian Rogers62d6c772013-02-27 08:32:07 -0800570 bool interpreter_stubs_installed_;
571
572 // Do we need the fidelity of events that we only get from running within the interpreter?
573 bool interpret_only_;
574
575 // Did the runtime request we only run in the interpreter? ie -Xint mode.
576 bool forced_interpret_only_;
577
578 // Do we have any listeners for method entry events? Short-cut to avoid taking the
579 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200580 bool have_method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800581
582 // Do we have any listeners for method exit events? Short-cut to avoid taking the
583 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200584 bool have_method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800585
586 // Do we have any listeners for method unwind events? Short-cut to avoid taking the
587 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200588 bool have_method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800589
590 // Do we have any listeners for dex move events? Short-cut to avoid taking the
591 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200592 bool have_dex_pc_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800593
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200594 // Do we have any listeners for field read events? Short-cut to avoid taking the
595 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200596 bool have_field_read_listeners_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200597
598 // Do we have any listeners for field write events? Short-cut to avoid taking the
599 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200600 bool have_field_write_listeners_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200601
Ian Rogers62d6c772013-02-27 08:32:07 -0800602 // Do we have any exception caught listeners? Short-cut to avoid taking the instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200603 bool have_exception_caught_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800604
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000605 // Do we have any branch listeners? Short-cut to avoid taking the instrumentation_lock_.
606 bool have_branch_listeners_ GUARDED_BY(Locks::mutator_lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800607
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100608 // Do we have any invoke listeners? Short-cut to avoid taking the instrumentation_lock_.
609 bool have_invoke_virtual_or_interface_listeners_ GUARDED_BY(Locks::mutator_lock_);
610
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200611 // Contains the instrumentation level required by each client of the instrumentation identified
612 // by a string key.
613 typedef SafeMap<const char*, InstrumentationLevel> InstrumentationLevelTable;
614 InstrumentationLevelTable requested_instrumentation_levels_ GUARDED_BY(Locks::mutator_lock_);
615
Ian Rogers62d6c772013-02-27 08:32:07 -0800616 // The event listeners, written to with the mutator_lock_ exclusively held.
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000617 // Mutators must be able to iterate over these lists concurrently, that is, with listeners being
618 // added or removed while iterating. The modifying thread holds exclusive lock,
619 // so other threads cannot iterate (i.e. read the data of the list) at the same time but they
620 // do keep iterators that need to remain valid. This is the reason these listeners are std::list
621 // and not for example std::vector: the existing storage for a std::list does not move.
622 // Note that mutators cannot make a copy of these lists before iterating, as the instrumentation
623 // listeners can also be deleted concurrently.
624 // As a result, these lists are never trimmed. That's acceptable given the low number of
625 // listeners we have.
Ian Rogers62d6c772013-02-27 08:32:07 -0800626 std::list<InstrumentationListener*> method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_);
627 std::list<InstrumentationListener*> method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_);
628 std::list<InstrumentationListener*> method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000629 std::list<InstrumentationListener*> branch_listeners_ GUARDED_BY(Locks::mutator_lock_);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100630 std::list<InstrumentationListener*> invoke_virtual_or_interface_listeners_
631 GUARDED_BY(Locks::mutator_lock_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000632 std::list<InstrumentationListener*> dex_pc_listeners_ GUARDED_BY(Locks::mutator_lock_);
633 std::list<InstrumentationListener*> field_read_listeners_ GUARDED_BY(Locks::mutator_lock_);
634 std::list<InstrumentationListener*> field_write_listeners_ GUARDED_BY(Locks::mutator_lock_);
635 std::list<InstrumentationListener*> exception_caught_listeners_ GUARDED_BY(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800636
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100637 // The set of methods being deoptimized (by the debugger) which must be executed with interpreter
638 // only.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700639 mutable ReaderWriterMutex deoptimized_methods_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700640 std::unordered_set<ArtMethod*> deoptimized_methods_ GUARDED_BY(deoptimized_methods_lock_);
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100641 bool deoptimization_enabled_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100642
Ian Rogersfa824272013-11-05 16:12:57 -0800643 // Current interpreter handler table. This is updated each time the thread state flags are
644 // modified.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200645 InterpreterHandlerTable interpreter_handler_table_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200646
Ian Rogersfa824272013-11-05 16:12:57 -0800647 // Greater than 0 if quick alloc entry points instrumented.
Mathieu Chartiereebc3af2016-02-29 18:13:38 -0800648 size_t quick_alloc_entry_points_instrumentation_counter_;
Mathieu Chartier50e93312016-03-16 11:25:29 -0700649
650 // alloc_entrypoints_instrumented_ is only updated with all the threads suspended, this is done
651 // to prevent races with the GC where the GC relies on thread suspension only see
652 // alloc_entrypoints_instrumented_ change during suspend points.
653 bool alloc_entrypoints_instrumented_;
654
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200655 friend class InstrumentationTest; // For GetCurrentInstrumentationLevel and ConfigureStubs.
656
jeffhao725a9572012-11-13 18:20:12 -0800657 DISALLOW_COPY_AND_ASSIGN(Instrumentation);
658};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700659std::ostream& operator<<(std::ostream& os, const Instrumentation::InstrumentationEvent& rhs);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200660std::ostream& operator<<(std::ostream& os, const Instrumentation::InstrumentationLevel& rhs);
jeffhao725a9572012-11-13 18:20:12 -0800661
Ian Rogers62d6c772013-02-27 08:32:07 -0800662// An element in the instrumentation side stack maintained in art::Thread.
663struct InstrumentationStackFrame {
Nicolas Geoffray07c70282017-08-30 08:09:42 +0000664 InstrumentationStackFrame(mirror::Object* this_object, ArtMethod* method,
665 uintptr_t return_pc, size_t frame_id, bool interpreter_entry)
666 : this_object_(this_object), method_(method), return_pc_(return_pc), frame_id_(frame_id),
Jeff Hao9a916d32013-06-27 18:45:37 -0700667 interpreter_entry_(interpreter_entry) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800668 }
669
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700670 std::string Dump() const REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800671
672 mirror::Object* this_object_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700673 ArtMethod* method_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100674 uintptr_t return_pc_;
675 size_t frame_id_;
676 bool interpreter_entry_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800677};
678
679} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -0800680} // namespace art
681
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700682#endif // ART_RUNTIME_INSTRUMENTATION_H_