blob: 3c5bfca316576adc8ef504756758a14bba2c938c [file] [log] [blame]
jeffhao725a9572012-11-13 18:20:12 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_INSTRUMENTATION_H_
18#define ART_RUNTIME_INSTRUMENTATION_H_
jeffhao725a9572012-11-13 18:20:12 -080019
Ian Rogers576ca0c2014-06-06 15:58:22 -070020#include <stdint.h>
Ian Rogers576ca0c2014-06-06 15:58:22 -070021#include <list>
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include <unordered_set>
Ian Rogers576ca0c2014-06-06 15:58:22 -070023
Ian Rogersd582fa42014-11-05 23:46:43 -080024#include "arch/instruction_set.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070025#include "base/enums.h"
Elliott Hughes76160052012-12-12 16:31:20 -080026#include "base/macros.h"
Ian Rogers719d1a32014-03-06 12:13:39 -080027#include "base/mutex.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070028#include "gc_root.h"
Sebastien Hertz0462c4c2015-04-01 16:34:17 +020029#include "safe_map.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030
jeffhao725a9572012-11-13 18:20:12 -080031namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032namespace mirror {
Brian Carlstromea46f952013-07-30 01:26:50 -070033 class Class;
34 class Object;
35 class Throwable;
Ian Rogers62d6c772013-02-27 08:32:07 -080036} // namespace mirror
Mathieu Chartierc7853442015-03-27 14:35:38 -070037class ArtField;
Mathieu Chartiere401d142015-04-22 13:56:20 -070038class ArtMethod;
Alex Lightd7661582017-05-01 13:48:16 -070039template <typename T> class Handle;
Ian Rogers62d6c772013-02-27 08:32:07 -080040union JValue;
Alex Lighte814f9d2017-07-31 16:14:39 -070041class ShadowFrame;
jeffhao725a9572012-11-13 18:20:12 -080042class Thread;
Mingyao Yang2ee17902017-08-30 11:37:08 -070043enum class DeoptimizationMethodType;
jeffhao725a9572012-11-13 18:20:12 -080044
Ian Rogers62d6c772013-02-27 08:32:07 -080045namespace instrumentation {
jeffhao725a9572012-11-13 18:20:12 -080046
Sebastien Hertzee1997a2013-09-19 14:47:09 +020047// Interpreter handler tables.
48enum InterpreterHandlerTable {
49 kMainHandlerTable = 0, // Main handler table: no suspend check, no instrumentation.
50 kAlternativeHandlerTable = 1, // Alternative handler table: suspend check and/or instrumentation
51 // enabled.
52 kNumHandlerTables
53};
54
Andreas Gampe40da2862015-02-27 12:49:04 -080055// Do we want to deoptimize for method entry and exit listeners or just try to intercept
56// invocations? Deoptimization forces all code to run in the interpreter and considerably hurts the
57// application's performance.
58static constexpr bool kDeoptimizeForAccurateMethodEntryExitListeners = true;
59
Ian Rogers62d6c772013-02-27 08:32:07 -080060// Instrumentation event listener API. Registered listeners will get the appropriate call back for
61// the events they are listening for. The call backs supply the thread, method and dex_pc the event
62// occurred upon. The thread may or may not be Thread::Current().
63struct InstrumentationListener {
64 InstrumentationListener() {}
65 virtual ~InstrumentationListener() {}
66
67 // Call-back for when a method is entered.
Alex Lightd7661582017-05-01 13:48:16 -070068 virtual void MethodEntered(Thread* thread,
69 Handle<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -070070 ArtMethod* method,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070071 uint32_t dex_pc) REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -080072
Alex Lightd7661582017-05-01 13:48:16 -070073 virtual void MethodExited(Thread* thread,
74 Handle<mirror::Object> this_object,
75 ArtMethod* method,
76 uint32_t dex_pc,
77 Handle<mirror::Object> return_value)
78 REQUIRES_SHARED(Locks::mutator_lock_);
79
80 // Call-back for when a method is exited. The implementor should either handler-ize the return
81 // value (if appropriate) or use the alternate MethodExited callback instead if they need to
82 // go through a suspend point.
83 virtual void MethodExited(Thread* thread,
84 Handle<mirror::Object> this_object,
85 ArtMethod* method,
86 uint32_t dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -080087 const JValue& return_value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070088 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -080089
90 // Call-back for when a method is popped due to an exception throw. A method will either cause a
91 // MethodExited call-back or a MethodUnwind call-back when its activation is removed.
Alex Lightd7661582017-05-01 13:48:16 -070092 virtual void MethodUnwind(Thread* thread,
93 Handle<mirror::Object> this_object,
94 ArtMethod* method,
95 uint32_t dex_pc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070096 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -080097
98 // Call-back for when the dex pc moves in a method.
Alex Lightd7661582017-05-01 13:48:16 -070099 virtual void DexPcMoved(Thread* thread,
100 Handle<mirror::Object> this_object,
101 ArtMethod* method,
102 uint32_t new_dex_pc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700103 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -0800104
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200105 // Call-back for when we read from a field.
Alex Lightd7661582017-05-01 13:48:16 -0700106 virtual void FieldRead(Thread* thread,
107 Handle<mirror::Object> this_object,
108 ArtMethod* method,
109 uint32_t dex_pc,
110 ArtField* field) = 0;
111
112 virtual void FieldWritten(Thread* thread,
113 Handle<mirror::Object> this_object,
114 ArtMethod* method,
115 uint32_t dex_pc,
116 ArtField* field,
117 Handle<mirror::Object> field_value)
118 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200119
120 // Call-back for when we write into a field.
Alex Lightd7661582017-05-01 13:48:16 -0700121 virtual void FieldWritten(Thread* thread,
122 Handle<mirror::Object> this_object,
123 ArtMethod* method,
124 uint32_t dex_pc,
125 ArtField* field,
126 const JValue& field_value)
127 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200128
Alex Light6e1607e2017-08-23 10:06:18 -0700129 // Call-back when an exception is thrown.
130 virtual void ExceptionThrown(Thread* thread,
Alex Lightd7661582017-05-01 13:48:16 -0700131 Handle<mirror::Throwable> exception_object)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700132 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800133
Alex Light9fb1ab12017-09-05 09:32:49 -0700134 // Call-back when an exception is caught/handled by java code.
135 virtual void ExceptionHandled(Thread* thread, Handle<mirror::Throwable> exception_object)
136 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
137
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000138 // Call-back for when we execute a branch.
139 virtual void Branch(Thread* thread,
140 ArtMethod* method,
141 uint32_t dex_pc,
142 int32_t dex_pc_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700143 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100144
145 // Call-back for when we get an invokevirtual or an invokeinterface.
146 virtual void InvokeVirtualOrInterface(Thread* thread,
Alex Lightd7661582017-05-01 13:48:16 -0700147 Handle<mirror::Object> this_object,
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100148 ArtMethod* caller,
149 uint32_t dex_pc,
150 ArtMethod* callee)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700151 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Alex Lighte814f9d2017-07-31 16:14:39 -0700152
153 // Call-back when a shadow_frame with the needs_notify_pop_ boolean set is popped off the stack by
154 // either return or exceptions. Normally instrumentation listeners should ensure that there are
155 // shadow-frames by deoptimizing stacks.
156 virtual void WatchedFramePop(Thread* thread ATTRIBUTE_UNUSED,
157 const ShadowFrame& frame ATTRIBUTE_UNUSED)
158 REQUIRES_SHARED(Locks::mutator_lock_) {
159 return;
160 }
jeffhao725a9572012-11-13 18:20:12 -0800161};
162
Ian Rogers62d6c772013-02-27 08:32:07 -0800163// Instrumentation is a catch-all for when extra information is required from the runtime. The
164// typical use for instrumentation is for profiling and debugging. Instrumentation may add stubs
165// to method entry and exit, it may also force execution to be switched to the interpreter and
166// trigger deoptimization.
jeffhao725a9572012-11-13 18:20:12 -0800167class Instrumentation {
168 public:
Ian Rogers62d6c772013-02-27 08:32:07 -0800169 enum InstrumentationEvent {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800170 kMethodEntered = 0x1,
171 kMethodExited = 0x2,
172 kMethodUnwind = 0x4,
173 kDexPcMoved = 0x8,
174 kFieldRead = 0x10,
175 kFieldWritten = 0x20,
Alex Light6e1607e2017-08-23 10:06:18 -0700176 kExceptionThrown = 0x40,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000177 kBranch = 0x80,
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100178 kInvokeVirtualOrInterface = 0x100,
Alex Lighte814f9d2017-07-31 16:14:39 -0700179 kWatchedFramePop = 0x200,
Alex Light9fb1ab12017-09-05 09:32:49 -0700180 kExceptionHandled = 0x400,
Ian Rogers62d6c772013-02-27 08:32:07 -0800181 };
jeffhao725a9572012-11-13 18:20:12 -0800182
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200183 enum class InstrumentationLevel {
184 kInstrumentNothing, // execute without instrumentation
185 kInstrumentWithInstrumentationStubs, // execute with instrumentation entry/exit stubs
186 kInstrumentWithInterpreter // execute with interpreter
187 };
188
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700189 Instrumentation();
jeffhao725a9572012-11-13 18:20:12 -0800190
Ian Rogers62d6c772013-02-27 08:32:07 -0800191 // Add a listener to be notified of the masked together sent of instrumentation events. This
192 // suspend the runtime to install stubs. You are expected to hold the mutator lock as a proxy
193 // for saying you should have suspended all threads (installing stubs while threads are running
194 // will break).
195 void AddListener(InstrumentationListener* listener, uint32_t events)
Mathieu Chartier90443472015-07-16 20:32:27 -0700196 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800197
Ian Rogers62d6c772013-02-27 08:32:07 -0800198 // Removes a listener possibly removing instrumentation stubs.
199 void RemoveListener(InstrumentationListener* listener, uint32_t events)
Mathieu Chartier90443472015-07-16 20:32:27 -0700200 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800201
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100202 // Deoptimization.
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100203 void EnableDeoptimization()
Mathieu Chartieraa516822015-10-02 15:53:37 -0700204 REQUIRES(Locks::mutator_lock_)
205 REQUIRES(!deoptimized_methods_lock_);
206 // Calls UndeoptimizeEverything which may visit class linker classes through ConfigureStubs.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200207 void DisableDeoptimization(const char* key)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700208 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
209 REQUIRES(!deoptimized_methods_lock_);
210
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100211 bool AreAllMethodsDeoptimized() const {
212 return interpreter_stubs_installed_;
213 }
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700214 bool ShouldNotifyMethodEnterExitEvents() const REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100215
Alex Lightbebd7bd2017-07-25 14:05:52 -0700216 bool CanDeoptimize() {
217 return deoptimization_enabled_;
218 }
219
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100220 // Executes everything with interpreter.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200221 void DeoptimizeEverything(const char* key)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700222 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
223 REQUIRES(!Locks::thread_list_lock_,
224 !Locks::classlinker_classes_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700225 !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100226
Mathieu Chartieraa516822015-10-02 15:53:37 -0700227 // Executes everything with compiled code (or interpreter if there is no code). May visit class
228 // linker classes through ConfigureStubs.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200229 void UndeoptimizeEverything(const char* key)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700230 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
231 REQUIRES(!Locks::thread_list_lock_,
232 !Locks::classlinker_classes_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700233 !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100234
235 // Deoptimize a method by forcing its execution with the interpreter. Nevertheless, a static
236 // method (except a class initializer) set to the resolution trampoline will be deoptimized only
237 // once its declaring class is initialized.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700238 void Deoptimize(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700239 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100240
241 // Undeoptimze the method by restoring its entrypoints. Nevertheless, a static method
242 // (except a class initializer) set to the resolution trampoline will be updated only once its
243 // declaring class is initialized.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700244 void Undeoptimize(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700245 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100246
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200247 // Indicates whether the method has been deoptimized so it is executed with the interpreter.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700248 bool IsDeoptimized(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700249 REQUIRES(!deoptimized_methods_lock_) REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100250
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200251 // Enable method tracing by installing instrumentation entry/exit stubs or interpreter.
252 void EnableMethodTracing(const char* key,
253 bool needs_interpreter = kDeoptimizeForAccurateMethodEntryExitListeners)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700254 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
255 REQUIRES(!Locks::thread_list_lock_,
256 !Locks::classlinker_classes_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700257 !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100258
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200259 // Disable method tracing by uninstalling instrumentation entry/exit stubs or interpreter.
260 void DisableMethodTracing(const char* key)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700261 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
262 REQUIRES(!Locks::thread_list_lock_,
263 !Locks::classlinker_classes_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700264 !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100265
Sebastien Hertzed2be172014-08-19 15:33:43 +0200266 InterpreterHandlerTable GetInterpreterHandlerTable() const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700267 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200268 return interpreter_handler_table_;
269 }
270
Mathieu Chartier90443472015-07-16 20:32:27 -0700271 void InstrumentQuickAllocEntryPoints() REQUIRES(!Locks::instrument_entrypoints_lock_);
272 void UninstrumentQuickAllocEntryPoints() REQUIRES(!Locks::instrument_entrypoints_lock_);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700273 void InstrumentQuickAllocEntryPointsLocked()
Mathieu Chartier90443472015-07-16 20:32:27 -0700274 REQUIRES(Locks::instrument_entrypoints_lock_, !Locks::thread_list_lock_,
275 !Locks::runtime_shutdown_lock_);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700276 void UninstrumentQuickAllocEntryPointsLocked()
Mathieu Chartier90443472015-07-16 20:32:27 -0700277 REQUIRES(Locks::instrument_entrypoints_lock_, !Locks::thread_list_lock_,
278 !Locks::runtime_shutdown_lock_);
279 void ResetQuickAllocEntryPoints() REQUIRES(Locks::runtime_shutdown_lock_);
Ian Rogersfa824272013-11-05 16:12:57 -0800280
Ian Rogers62d6c772013-02-27 08:32:07 -0800281 // Update the code of a method respecting any installed stubs.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700282 void UpdateMethodsCode(ArtMethod* method, const void* quick_code)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700283 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800284
Alex Light0a5ec3d2017-07-25 16:50:26 -0700285 // Update the code of a method to the interpreter respecting any installed stubs from debugger.
286 void UpdateMethodsCodeToInterpreterEntryPoint(ArtMethod* method)
287 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
288
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700289 // Update the code of a method respecting any installed stubs from debugger.
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000290 void UpdateMethodsCodeForJavaDebuggable(ArtMethod* method, const void* quick_code)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700291 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700292
Ian Rogers62d6c772013-02-27 08:32:07 -0800293 // Get the quick code for the given method. More efficient than asking the class linker as it
294 // will short-cut to GetCode if instrumentation and static method resolution stubs aren't
295 // installed.
Andreas Gampe542451c2016-07-26 09:02:02 -0700296 const void* GetQuickCodeFor(ArtMethod* method, PointerSize pointer_size) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700297 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800298
299 void ForceInterpretOnly() {
300 interpret_only_ = true;
301 forced_interpret_only_ = true;
302 }
303
Brian Carlstromea46f952013-07-30 01:26:50 -0700304 // Called by ArtMethod::Invoke to determine dispatch mechanism.
Ian Rogers62d6c772013-02-27 08:32:07 -0800305 bool InterpretOnly() const {
306 return interpret_only_;
307 }
308
Hiroshi Yamauchi563b47c2014-02-28 17:18:37 -0800309 bool IsForcedInterpretOnly() const {
310 return forced_interpret_only_;
311 }
312
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800313 // Code is in boot image oat file which isn't compiled as debuggable.
314 // Need debug version (interpreter or jitted) if that's the case.
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000315 bool NeedDebugVersionFor(ArtMethod* method) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700316 REQUIRES_SHARED(Locks::mutator_lock_);
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800317
Ian Rogers62d6c772013-02-27 08:32:07 -0800318 bool AreExitStubsInstalled() const {
319 return instrumentation_stubs_installed_;
320 }
321
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700322 bool HasMethodEntryListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200323 return have_method_entry_listeners_;
324 }
325
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700326 bool HasMethodExitListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200327 return have_method_exit_listeners_;
328 }
329
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700330 bool HasMethodUnwindListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200331 return have_method_unwind_listeners_;
332 }
333
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700334 bool HasDexPcListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200335 return have_dex_pc_listeners_;
336 }
337
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700338 bool HasFieldReadListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200339 return have_field_read_listeners_;
340 }
341
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700342 bool HasFieldWriteListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200343 return have_field_write_listeners_;
344 }
345
Alex Light6e1607e2017-08-23 10:06:18 -0700346 bool HasExceptionThrownListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
347 return have_exception_thrown_listeners_;
Sebastien Hertz9f102032014-05-23 08:59:42 +0200348 }
349
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700350 bool HasBranchListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000351 return have_branch_listeners_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800352 }
353
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700354 bool HasInvokeVirtualOrInterfaceListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100355 return have_invoke_virtual_or_interface_listeners_;
356 }
357
Alex Lighte814f9d2017-07-31 16:14:39 -0700358 bool HasWatchedFramePopListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
359 return have_watched_frame_pop_listeners_;
360 }
361
Alex Light9fb1ab12017-09-05 09:32:49 -0700362 bool HasExceptionHandledListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
363 return have_exception_handled_listeners_;
364 }
365
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700366 bool IsActive() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200367 return have_dex_pc_listeners_ || have_method_entry_listeners_ || have_method_exit_listeners_ ||
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200368 have_field_read_listeners_ || have_field_write_listeners_ ||
Alex Light6e1607e2017-08-23 10:06:18 -0700369 have_exception_thrown_listeners_ || have_method_unwind_listeners_ ||
Alex Lighte814f9d2017-07-31 16:14:39 -0700370 have_branch_listeners_ || have_invoke_virtual_or_interface_listeners_ ||
Alex Light9fb1ab12017-09-05 09:32:49 -0700371 have_watched_frame_pop_listeners_ || have_exception_handled_listeners_;
Bill Buzbeefd522f92016-02-11 22:37:42 +0000372 }
373
374 // Any instrumentation *other* than what is needed for Jit profiling active?
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700375 bool NonJitProfilingActive() const REQUIRES_SHARED(Locks::mutator_lock_) {
Bill Buzbeefd522f92016-02-11 22:37:42 +0000376 return have_dex_pc_listeners_ || have_method_exit_listeners_ ||
377 have_field_read_listeners_ || have_field_write_listeners_ ||
Alex Light6e1607e2017-08-23 10:06:18 -0700378 have_exception_thrown_listeners_ || have_method_unwind_listeners_ ||
Alex Light9fb1ab12017-09-05 09:32:49 -0700379 have_branch_listeners_ || have_watched_frame_pop_listeners_ ||
380 have_exception_handled_listeners_;
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200381 }
382
Ian Rogers62d6c772013-02-27 08:32:07 -0800383 // Inform listeners that a method has been entered. A dex PC is provided as we may install
384 // listeners into executing code and get method enter events for methods already on the stack.
385 void MethodEnterEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700386 ArtMethod* method, uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700387 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200388 if (UNLIKELY(HasMethodEntryListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800389 MethodEnterEventImpl(thread, this_object, method, dex_pc);
390 }
391 }
392
393 // Inform listeners that a method has been exited.
Alex Lightd7661582017-05-01 13:48:16 -0700394 void MethodExitEvent(Thread* thread,
395 mirror::Object* this_object,
396 ArtMethod* method,
397 uint32_t dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800398 const JValue& return_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700399 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200400 if (UNLIKELY(HasMethodExitListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800401 MethodExitEventImpl(thread, this_object, method, dex_pc, return_value);
402 }
403 }
404
405 // Inform listeners that a method has been exited due to an exception.
406 void MethodUnwindEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700407 ArtMethod* method, uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700408 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800409
410 // Inform listeners that the dex pc has moved (only supported by the interpreter).
411 void DexPcMovedEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700412 ArtMethod* method, uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700413 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200414 if (UNLIKELY(HasDexPcListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800415 DexPcMovedEventImpl(thread, this_object, method, dex_pc);
416 }
417 }
418
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000419 // Inform listeners that a branch has been taken (only supported by the interpreter).
420 void Branch(Thread* thread, ArtMethod* method, uint32_t dex_pc, int32_t offset) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700421 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000422 if (UNLIKELY(HasBranchListeners())) {
423 BranchImpl(thread, method, dex_pc, offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800424 }
425 }
426
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200427 // Inform listeners that we read a field (only supported by the interpreter).
428 void FieldReadEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700429 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700430 ArtField* field) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700431 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200432 if (UNLIKELY(HasFieldReadListeners())) {
433 FieldReadEventImpl(thread, this_object, method, dex_pc, field);
434 }
435 }
436
437 // Inform listeners that we write a field (only supported by the interpreter).
438 void FieldWriteEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700439 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700440 ArtField* field, const JValue& field_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700441 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200442 if (UNLIKELY(HasFieldWriteListeners())) {
443 FieldWriteEventImpl(thread, this_object, method, dex_pc, field, field_value);
444 }
445 }
446
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100447 void InvokeVirtualOrInterface(Thread* thread,
448 mirror::Object* this_object,
449 ArtMethod* caller,
450 uint32_t dex_pc,
451 ArtMethod* callee) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700452 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100453 if (UNLIKELY(HasInvokeVirtualOrInterfaceListeners())) {
454 InvokeVirtualOrInterfaceImpl(thread, this_object, caller, dex_pc, callee);
455 }
456 }
457
Alex Lighte814f9d2017-07-31 16:14:39 -0700458 // Inform listeners that a branch has been taken (only supported by the interpreter).
459 void WatchedFramePopped(Thread* thread, const ShadowFrame& frame) const
460 REQUIRES_SHARED(Locks::mutator_lock_) {
461 if (UNLIKELY(HasWatchedFramePopListeners())) {
462 WatchedFramePopImpl(thread, frame);
463 }
464 }
465
Alex Light6e1607e2017-08-23 10:06:18 -0700466 // Inform listeners that an exception was thrown.
467 void ExceptionThrownEvent(Thread* thread, mirror::Throwable* exception_object) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700468 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800469
Alex Light9fb1ab12017-09-05 09:32:49 -0700470 // Inform listeners that an exception has been handled. This is not sent for native code or for
471 // exceptions which reach the end of the thread's stack.
472 void ExceptionHandledEvent(Thread* thread, mirror::Throwable* exception_object) const
473 REQUIRES_SHARED(Locks::mutator_lock_);
474
Ian Rogers62d6c772013-02-27 08:32:07 -0800475 // Called when an instrumented method is entered. The intended link register (lr) is saved so
476 // that returning causes a branch to the method exit stub. Generates method enter events.
477 void PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700478 ArtMethod* method, uintptr_t lr,
Jeff Hao9a916d32013-06-27 18:45:37 -0700479 bool interpreter_entry)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700480 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800481
Mingyao Yang2ee17902017-08-30 11:37:08 -0700482 DeoptimizationMethodType GetDeoptimizationMethodType(ArtMethod* method)
483 REQUIRES_SHARED(Locks::mutator_lock_);
484
Ian Rogers62d6c772013-02-27 08:32:07 -0800485 // Called when an instrumented method is exited. Removes the pushed instrumentation frame
Alex Lightb7edcda2017-04-27 13:20:31 -0700486 // returning the intended link register. Generates method exit events. The gpr_result and
487 // fpr_result pointers are pointers to the locations where the integer/pointer and floating point
488 // result values of the function are stored. Both pointers must always be valid but the values
489 // held there will only be meaningful if interpreted as the appropriate type given the function
490 // being returned from.
Andreas Gamped58342c2014-06-05 14:18:08 -0700491 TwoWordReturn PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc,
Alex Lightb7edcda2017-04-27 13:20:31 -0700492 uint64_t* gpr_result, uint64_t* fpr_result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700493 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800494
495 // Pops an instrumentation frame from the current thread and generate an unwind event.
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700496 // Returns the return pc for the instrumentation frame that's popped.
497 uintptr_t PopMethodForUnwind(Thread* self, bool is_deoptimization) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700498 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800499
500 // Call back for configure stubs.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700501 void InstallStubsForClass(mirror::Class* klass) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700502 REQUIRES(!deoptimized_methods_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800503
Mathieu Chartiere401d142015-04-22 13:56:20 -0700504 void InstallStubsForMethod(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700505 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100506
Mingyao Yang99170c62015-07-06 11:10:37 -0700507 // Install instrumentation exit stub on every method of the stack of the given thread.
508 // This is used by the debugger to cause a deoptimization of the thread's stack after updating
509 // local variable(s).
510 void InstrumentThreadStack(Thread* thread)
Alex Light3ae82532017-07-26 13:59:07 -0700511 REQUIRES_SHARED(Locks::mutator_lock_);
Mingyao Yang99170c62015-07-06 11:10:37 -0700512
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000513 static size_t ComputeFrameId(Thread* self,
514 size_t frame_depth,
515 size_t inlined_frames_before_frame)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700516 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000517
Mathieu Chartiereebc3af2016-02-29 18:13:38 -0800518 // Does not hold lock, used to check if someone changed from not instrumented to instrumented
519 // during a GC suspend point.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700520 bool AllocEntrypointsInstrumented() const REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier50e93312016-03-16 11:25:29 -0700521 return alloc_entrypoints_instrumented_;
Mathieu Chartiereebc3af2016-02-29 18:13:38 -0800522 }
523
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200524 InstrumentationLevel GetCurrentInstrumentationLevel() const;
525
Alex Lightdba61482016-12-21 08:20:29 -0800526 private:
527 // Returns true if moving to the given instrumentation level requires the installation of stubs.
528 // False otherwise.
529 bool RequiresInstrumentationInstallation(InstrumentationLevel new_level) const;
530
Ian Rogers62d6c772013-02-27 08:32:07 -0800531 // Does the job of installing or removing instrumentation code within methods.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200532 // In order to support multiple clients using instrumentation at the same time,
533 // the caller must pass a unique key (a string) identifying it so we remind which
534 // instrumentation level it needs. Therefore the current instrumentation level
535 // becomes the highest instrumentation level required by a client.
536 void ConfigureStubs(const char* key, InstrumentationLevel desired_instrumentation_level)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700537 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
538 REQUIRES(!deoptimized_methods_lock_,
539 !Locks::thread_list_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700540 !Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800541
Mathieu Chartier90443472015-07-16 20:32:27 -0700542 void UpdateInterpreterHandlerTable() REQUIRES(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800543 /*
544 * TUNING: Dalvik's mterp stashes the actual current handler table base in a
545 * tls field. For Arm, this enables all suspend, debug & tracing checks to be
546 * collapsed into a single conditionally-executed ldw instruction.
547 * Move to Dalvik-style handler-table management for both the goto interpreter and
548 * mterp.
549 */
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200550 interpreter_handler_table_ = IsActive() ? kAlternativeHandlerTable : kMainHandlerTable;
551 }
552
Mathieu Chartier661974a2014-01-09 11:23:53 -0800553 // No thread safety analysis to get around SetQuickAllocEntryPointsInstrumented requiring
554 // exclusive access to mutator lock which you can't get if the runtime isn't started.
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700555 void SetEntrypointsInstrumented(bool instrumented) NO_THREAD_SAFETY_ANALYSIS;
Mathieu Chartier661974a2014-01-09 11:23:53 -0800556
Alex Lightd7661582017-05-01 13:48:16 -0700557 void MethodEnterEventImpl(Thread* thread,
558 ObjPtr<mirror::Object> this_object,
559 ArtMethod* method,
560 uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700561 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lightd7661582017-05-01 13:48:16 -0700562 void MethodExitEventImpl(Thread* thread,
563 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700564 ArtMethod* method,
Alex Lightd7661582017-05-01 13:48:16 -0700565 uint32_t dex_pc,
566 const JValue& return_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700567 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lightd7661582017-05-01 13:48:16 -0700568 void DexPcMovedEventImpl(Thread* thread,
569 ObjPtr<mirror::Object> this_object,
570 ArtMethod* method,
571 uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700572 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000573 void BranchImpl(Thread* thread, ArtMethod* method, uint32_t dex_pc, int32_t offset) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700574 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100575 void InvokeVirtualOrInterfaceImpl(Thread* thread,
Alex Lightd7661582017-05-01 13:48:16 -0700576 ObjPtr<mirror::Object> this_object,
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100577 ArtMethod* caller,
578 uint32_t dex_pc,
579 ArtMethod* callee) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700580 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lighte814f9d2017-07-31 16:14:39 -0700581 void WatchedFramePopImpl(Thread* thread, const ShadowFrame& frame) const
582 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lightd7661582017-05-01 13:48:16 -0700583 void FieldReadEventImpl(Thread* thread,
584 ObjPtr<mirror::Object> this_object,
585 ArtMethod* method,
586 uint32_t dex_pc,
587 ArtField* field) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700588 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lightd7661582017-05-01 13:48:16 -0700589 void FieldWriteEventImpl(Thread* thread,
590 ObjPtr<mirror::Object> this_object,
591 ArtMethod* method,
592 uint32_t dex_pc,
593 ArtField* field,
594 const JValue& field_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700595 REQUIRES_SHARED(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800596
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700597 // Read barrier-aware utility functions for accessing deoptimized_methods_
Mathieu Chartiere401d142015-04-22 13:56:20 -0700598 bool AddDeoptimizedMethod(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700599 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700600 bool IsDeoptimizedMethod(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700601 REQUIRES_SHARED(Locks::mutator_lock_, deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700602 bool RemoveDeoptimizedMethod(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700603 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700604 ArtMethod* BeginDeoptimizedMethod()
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700605 REQUIRES_SHARED(Locks::mutator_lock_, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700606 bool IsDeoptimizedMethodsEmpty() const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700607 REQUIRES_SHARED(Locks::mutator_lock_, deoptimized_methods_lock_);
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700608 void UpdateMethodsCodeImpl(ArtMethod* method, const void* quick_code)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700609 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700610
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700611
Brian Carlstromea46f952013-07-30 01:26:50 -0700612 // Have we hijacked ArtMethod::code_ so that it calls instrumentation/interpreter code?
Ian Rogers62d6c772013-02-27 08:32:07 -0800613 bool instrumentation_stubs_installed_;
614
Brian Carlstromea46f952013-07-30 01:26:50 -0700615 // Have we hijacked ArtMethod::code_ to reference the enter/exit stubs?
Ian Rogers62d6c772013-02-27 08:32:07 -0800616 bool entry_exit_stubs_installed_;
617
Brian Carlstromea46f952013-07-30 01:26:50 -0700618 // Have we hijacked ArtMethod::code_ to reference the enter interpreter stub?
Ian Rogers62d6c772013-02-27 08:32:07 -0800619 bool interpreter_stubs_installed_;
620
621 // Do we need the fidelity of events that we only get from running within the interpreter?
622 bool interpret_only_;
623
624 // Did the runtime request we only run in the interpreter? ie -Xint mode.
625 bool forced_interpret_only_;
626
627 // Do we have any listeners for method entry events? Short-cut to avoid taking the
628 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200629 bool have_method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800630
631 // Do we have any listeners for method exit events? Short-cut to avoid taking the
632 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200633 bool have_method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800634
635 // Do we have any listeners for method unwind events? Short-cut to avoid taking the
636 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200637 bool have_method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800638
639 // Do we have any listeners for dex move events? Short-cut to avoid taking the
640 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200641 bool have_dex_pc_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800642
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200643 // Do we have any listeners for field read events? Short-cut to avoid taking the
644 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200645 bool have_field_read_listeners_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200646
647 // Do we have any listeners for field write events? Short-cut to avoid taking the
648 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200649 bool have_field_write_listeners_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200650
Alex Light6e1607e2017-08-23 10:06:18 -0700651 // Do we have any exception thrown listeners? Short-cut to avoid taking the instrumentation_lock_.
652 bool have_exception_thrown_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800653
Alex Lighte814f9d2017-07-31 16:14:39 -0700654 // Do we have any frame pop listeners? Short-cut to avoid taking the instrumentation_lock_.
655 bool have_watched_frame_pop_listeners_ GUARDED_BY(Locks::mutator_lock_);
656
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000657 // Do we have any branch listeners? Short-cut to avoid taking the instrumentation_lock_.
658 bool have_branch_listeners_ GUARDED_BY(Locks::mutator_lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800659
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100660 // Do we have any invoke listeners? Short-cut to avoid taking the instrumentation_lock_.
661 bool have_invoke_virtual_or_interface_listeners_ GUARDED_BY(Locks::mutator_lock_);
662
Alex Light9fb1ab12017-09-05 09:32:49 -0700663 // Do we have any exception handled listeners? Short-cut to avoid taking the
664 // instrumentation_lock_.
665 bool have_exception_handled_listeners_ GUARDED_BY(Locks::mutator_lock_);
666
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200667 // Contains the instrumentation level required by each client of the instrumentation identified
668 // by a string key.
669 typedef SafeMap<const char*, InstrumentationLevel> InstrumentationLevelTable;
670 InstrumentationLevelTable requested_instrumentation_levels_ GUARDED_BY(Locks::mutator_lock_);
671
Ian Rogers62d6c772013-02-27 08:32:07 -0800672 // The event listeners, written to with the mutator_lock_ exclusively held.
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000673 // Mutators must be able to iterate over these lists concurrently, that is, with listeners being
674 // added or removed while iterating. The modifying thread holds exclusive lock,
675 // so other threads cannot iterate (i.e. read the data of the list) at the same time but they
676 // do keep iterators that need to remain valid. This is the reason these listeners are std::list
677 // and not for example std::vector: the existing storage for a std::list does not move.
678 // Note that mutators cannot make a copy of these lists before iterating, as the instrumentation
679 // listeners can also be deleted concurrently.
680 // As a result, these lists are never trimmed. That's acceptable given the low number of
681 // listeners we have.
Ian Rogers62d6c772013-02-27 08:32:07 -0800682 std::list<InstrumentationListener*> method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_);
683 std::list<InstrumentationListener*> method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_);
684 std::list<InstrumentationListener*> method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000685 std::list<InstrumentationListener*> branch_listeners_ GUARDED_BY(Locks::mutator_lock_);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100686 std::list<InstrumentationListener*> invoke_virtual_or_interface_listeners_
687 GUARDED_BY(Locks::mutator_lock_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000688 std::list<InstrumentationListener*> dex_pc_listeners_ GUARDED_BY(Locks::mutator_lock_);
689 std::list<InstrumentationListener*> field_read_listeners_ GUARDED_BY(Locks::mutator_lock_);
690 std::list<InstrumentationListener*> field_write_listeners_ GUARDED_BY(Locks::mutator_lock_);
Alex Light6e1607e2017-08-23 10:06:18 -0700691 std::list<InstrumentationListener*> exception_thrown_listeners_ GUARDED_BY(Locks::mutator_lock_);
Alex Lighte814f9d2017-07-31 16:14:39 -0700692 std::list<InstrumentationListener*> watched_frame_pop_listeners_ GUARDED_BY(Locks::mutator_lock_);
Alex Light9fb1ab12017-09-05 09:32:49 -0700693 std::list<InstrumentationListener*> exception_handled_listeners_ GUARDED_BY(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800694
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100695 // The set of methods being deoptimized (by the debugger) which must be executed with interpreter
696 // only.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700697 mutable ReaderWriterMutex deoptimized_methods_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700698 std::unordered_set<ArtMethod*> deoptimized_methods_ GUARDED_BY(deoptimized_methods_lock_);
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100699 bool deoptimization_enabled_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100700
Ian Rogersfa824272013-11-05 16:12:57 -0800701 // Current interpreter handler table. This is updated each time the thread state flags are
702 // modified.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200703 InterpreterHandlerTable interpreter_handler_table_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200704
Ian Rogersfa824272013-11-05 16:12:57 -0800705 // Greater than 0 if quick alloc entry points instrumented.
Mathieu Chartiereebc3af2016-02-29 18:13:38 -0800706 size_t quick_alloc_entry_points_instrumentation_counter_;
Mathieu Chartier50e93312016-03-16 11:25:29 -0700707
708 // alloc_entrypoints_instrumented_ is only updated with all the threads suspended, this is done
709 // to prevent races with the GC where the GC relies on thread suspension only see
710 // alloc_entrypoints_instrumented_ change during suspend points.
711 bool alloc_entrypoints_instrumented_;
712
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200713 friend class InstrumentationTest; // For GetCurrentInstrumentationLevel and ConfigureStubs.
714
jeffhao725a9572012-11-13 18:20:12 -0800715 DISALLOW_COPY_AND_ASSIGN(Instrumentation);
716};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700717std::ostream& operator<<(std::ostream& os, const Instrumentation::InstrumentationEvent& rhs);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200718std::ostream& operator<<(std::ostream& os, const Instrumentation::InstrumentationLevel& rhs);
jeffhao725a9572012-11-13 18:20:12 -0800719
Ian Rogers62d6c772013-02-27 08:32:07 -0800720// An element in the instrumentation side stack maintained in art::Thread.
721struct InstrumentationStackFrame {
Mingyao Yang2ee17902017-08-30 11:37:08 -0700722 InstrumentationStackFrame(mirror::Object* this_object,
723 ArtMethod* method,
724 uintptr_t return_pc,
725 size_t frame_id,
726 bool interpreter_entry)
727 : this_object_(this_object),
728 method_(method),
729 return_pc_(return_pc),
730 frame_id_(frame_id),
Jeff Hao9a916d32013-06-27 18:45:37 -0700731 interpreter_entry_(interpreter_entry) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800732 }
733
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700734 std::string Dump() const REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800735
736 mirror::Object* this_object_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700737 ArtMethod* method_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100738 uintptr_t return_pc_;
739 size_t frame_id_;
740 bool interpreter_entry_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800741};
742
743} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -0800744} // namespace art
745
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700746#endif // ART_RUNTIME_INSTRUMENTATION_H_