blob: 9bb49ea7c71e38509565b005d68909f01b8a21b6 [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;
43
Ian Rogers62d6c772013-02-27 08:32:07 -080044namespace instrumentation {
jeffhao725a9572012-11-13 18:20:12 -080045
Sebastien Hertzee1997a2013-09-19 14:47:09 +020046// Interpreter handler tables.
47enum InterpreterHandlerTable {
48 kMainHandlerTable = 0, // Main handler table: no suspend check, no instrumentation.
49 kAlternativeHandlerTable = 1, // Alternative handler table: suspend check and/or instrumentation
50 // enabled.
51 kNumHandlerTables
52};
53
Andreas Gampe40da2862015-02-27 12:49:04 -080054// Do we want to deoptimize for method entry and exit listeners or just try to intercept
55// invocations? Deoptimization forces all code to run in the interpreter and considerably hurts the
56// application's performance.
57static constexpr bool kDeoptimizeForAccurateMethodEntryExitListeners = true;
58
Ian Rogers62d6c772013-02-27 08:32:07 -080059// Instrumentation event listener API. Registered listeners will get the appropriate call back for
60// the events they are listening for. The call backs supply the thread, method and dex_pc the event
61// occurred upon. The thread may or may not be Thread::Current().
62struct InstrumentationListener {
63 InstrumentationListener() {}
64 virtual ~InstrumentationListener() {}
65
66 // Call-back for when a method is entered.
Alex Lightd7661582017-05-01 13:48:16 -070067 virtual void MethodEntered(Thread* thread,
68 Handle<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -070069 ArtMethod* method,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070070 uint32_t dex_pc) REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -080071
Alex Lightd7661582017-05-01 13:48:16 -070072 virtual void MethodExited(Thread* thread,
73 Handle<mirror::Object> this_object,
74 ArtMethod* method,
75 uint32_t dex_pc,
76 Handle<mirror::Object> return_value)
77 REQUIRES_SHARED(Locks::mutator_lock_);
78
79 // Call-back for when a method is exited. The implementor should either handler-ize the return
80 // value (if appropriate) or use the alternate MethodExited callback instead if they need to
81 // go through a suspend point.
82 virtual void MethodExited(Thread* thread,
83 Handle<mirror::Object> this_object,
84 ArtMethod* method,
85 uint32_t dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -080086 const JValue& return_value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070087 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -080088
89 // Call-back for when a method is popped due to an exception throw. A method will either cause a
90 // MethodExited call-back or a MethodUnwind call-back when its activation is removed.
Alex Lightd7661582017-05-01 13:48:16 -070091 virtual void MethodUnwind(Thread* thread,
92 Handle<mirror::Object> this_object,
93 ArtMethod* method,
94 uint32_t dex_pc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070095 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -080096
97 // Call-back for when the dex pc moves in a method.
Alex Lightd7661582017-05-01 13:48:16 -070098 virtual void DexPcMoved(Thread* thread,
99 Handle<mirror::Object> this_object,
100 ArtMethod* method,
101 uint32_t new_dex_pc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700102 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Ian Rogers62d6c772013-02-27 08:32:07 -0800103
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200104 // Call-back for when we read from a field.
Alex Lightd7661582017-05-01 13:48:16 -0700105 virtual void FieldRead(Thread* thread,
106 Handle<mirror::Object> this_object,
107 ArtMethod* method,
108 uint32_t dex_pc,
109 ArtField* field) = 0;
110
111 virtual void FieldWritten(Thread* thread,
112 Handle<mirror::Object> this_object,
113 ArtMethod* method,
114 uint32_t dex_pc,
115 ArtField* field,
116 Handle<mirror::Object> field_value)
117 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200118
119 // Call-back for when we write into a field.
Alex Lightd7661582017-05-01 13:48:16 -0700120 virtual void FieldWritten(Thread* thread,
121 Handle<mirror::Object> this_object,
122 ArtMethod* method,
123 uint32_t dex_pc,
124 ArtField* field,
125 const JValue& field_value)
126 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200127
Alex Light6e1607e2017-08-23 10:06:18 -0700128 // Call-back when an exception is thrown.
129 virtual void ExceptionThrown(Thread* thread,
Alex Lightd7661582017-05-01 13:48:16 -0700130 Handle<mirror::Throwable> exception_object)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700131 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800132
Alex Light798eab02017-08-23 12:54:53 -0700133 // Call-back when an exception is caught/handled by java code.
134 virtual void ExceptionHandled(Thread* thread, Handle<mirror::Throwable> exception_object)
135 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
136
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000137 // Call-back for when we execute a branch.
138 virtual void Branch(Thread* thread,
139 ArtMethod* method,
140 uint32_t dex_pc,
141 int32_t dex_pc_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700142 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100143
144 // Call-back for when we get an invokevirtual or an invokeinterface.
145 virtual void InvokeVirtualOrInterface(Thread* thread,
Alex Lightd7661582017-05-01 13:48:16 -0700146 Handle<mirror::Object> this_object,
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100147 ArtMethod* caller,
148 uint32_t dex_pc,
149 ArtMethod* callee)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700150 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Alex Lighte814f9d2017-07-31 16:14:39 -0700151
152 // Call-back when a shadow_frame with the needs_notify_pop_ boolean set is popped off the stack by
153 // either return or exceptions. Normally instrumentation listeners should ensure that there are
154 // shadow-frames by deoptimizing stacks.
155 virtual void WatchedFramePop(Thread* thread ATTRIBUTE_UNUSED,
156 const ShadowFrame& frame ATTRIBUTE_UNUSED)
Alex Light05f47742017-09-14 00:34:44 +0000157 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
jeffhao725a9572012-11-13 18:20:12 -0800158};
159
Ian Rogers62d6c772013-02-27 08:32:07 -0800160// Instrumentation is a catch-all for when extra information is required from the runtime. The
161// typical use for instrumentation is for profiling and debugging. Instrumentation may add stubs
162// to method entry and exit, it may also force execution to be switched to the interpreter and
163// trigger deoptimization.
jeffhao725a9572012-11-13 18:20:12 -0800164class Instrumentation {
165 public:
Ian Rogers62d6c772013-02-27 08:32:07 -0800166 enum InstrumentationEvent {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800167 kMethodEntered = 0x1,
168 kMethodExited = 0x2,
169 kMethodUnwind = 0x4,
170 kDexPcMoved = 0x8,
171 kFieldRead = 0x10,
172 kFieldWritten = 0x20,
Alex Light6e1607e2017-08-23 10:06:18 -0700173 kExceptionThrown = 0x40,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000174 kBranch = 0x80,
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100175 kInvokeVirtualOrInterface = 0x100,
Alex Lighte814f9d2017-07-31 16:14:39 -0700176 kWatchedFramePop = 0x200,
Alex Light798eab02017-08-23 12:54:53 -0700177 kExceptionHandled = 0x400,
Ian Rogers62d6c772013-02-27 08:32:07 -0800178 };
jeffhao725a9572012-11-13 18:20:12 -0800179
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200180 enum class InstrumentationLevel {
181 kInstrumentNothing, // execute without instrumentation
182 kInstrumentWithInstrumentationStubs, // execute with instrumentation entry/exit stubs
183 kInstrumentWithInterpreter // execute with interpreter
184 };
185
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700186 Instrumentation();
jeffhao725a9572012-11-13 18:20:12 -0800187
Ian Rogers62d6c772013-02-27 08:32:07 -0800188 // Add a listener to be notified of the masked together sent of instrumentation events. This
189 // suspend the runtime to install stubs. You are expected to hold the mutator lock as a proxy
190 // for saying you should have suspended all threads (installing stubs while threads are running
191 // will break).
192 void AddListener(InstrumentationListener* listener, uint32_t events)
Mathieu Chartier90443472015-07-16 20:32:27 -0700193 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800194
Ian Rogers62d6c772013-02-27 08:32:07 -0800195 // Removes a listener possibly removing instrumentation stubs.
196 void RemoveListener(InstrumentationListener* listener, uint32_t events)
Mathieu Chartier90443472015-07-16 20:32:27 -0700197 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800198
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100199 // Deoptimization.
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100200 void EnableDeoptimization()
Mathieu Chartieraa516822015-10-02 15:53:37 -0700201 REQUIRES(Locks::mutator_lock_)
202 REQUIRES(!deoptimized_methods_lock_);
203 // Calls UndeoptimizeEverything which may visit class linker classes through ConfigureStubs.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200204 void DisableDeoptimization(const char* key)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700205 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
206 REQUIRES(!deoptimized_methods_lock_);
207
Sebastien Hertza76a6d42014-03-20 16:40:17 +0100208 bool AreAllMethodsDeoptimized() const {
209 return interpreter_stubs_installed_;
210 }
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700211 bool ShouldNotifyMethodEnterExitEvents() const REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100212
Alex Lightbebd7bd2017-07-25 14:05:52 -0700213 bool CanDeoptimize() {
214 return deoptimization_enabled_;
215 }
216
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100217 // Executes everything with interpreter.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200218 void DeoptimizeEverything(const char* key)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700219 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
220 REQUIRES(!Locks::thread_list_lock_,
221 !Locks::classlinker_classes_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700222 !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100223
Mathieu Chartieraa516822015-10-02 15:53:37 -0700224 // Executes everything with compiled code (or interpreter if there is no code). May visit class
225 // linker classes through ConfigureStubs.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200226 void UndeoptimizeEverything(const char* key)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700227 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
228 REQUIRES(!Locks::thread_list_lock_,
229 !Locks::classlinker_classes_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700230 !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100231
232 // Deoptimize a method by forcing its execution with the interpreter. Nevertheless, a static
233 // method (except a class initializer) set to the resolution trampoline will be deoptimized only
234 // once its declaring class is initialized.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700235 void Deoptimize(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700236 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100237
238 // Undeoptimze the method by restoring its entrypoints. Nevertheless, a static method
239 // (except a class initializer) set to the resolution trampoline will be updated only once its
240 // declaring class is initialized.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700241 void Undeoptimize(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700242 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100243
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200244 // Indicates whether the method has been deoptimized so it is executed with the interpreter.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700245 bool IsDeoptimized(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700246 REQUIRES(!deoptimized_methods_lock_) REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100247
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200248 // Enable method tracing by installing instrumentation entry/exit stubs or interpreter.
249 void EnableMethodTracing(const char* key,
250 bool needs_interpreter = kDeoptimizeForAccurateMethodEntryExitListeners)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700251 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
252 REQUIRES(!Locks::thread_list_lock_,
253 !Locks::classlinker_classes_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700254 !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100255
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200256 // Disable method tracing by uninstalling instrumentation entry/exit stubs or interpreter.
257 void DisableMethodTracing(const char* key)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700258 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
259 REQUIRES(!Locks::thread_list_lock_,
260 !Locks::classlinker_classes_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700261 !deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100262
Sebastien Hertzed2be172014-08-19 15:33:43 +0200263 InterpreterHandlerTable GetInterpreterHandlerTable() const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700264 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200265 return interpreter_handler_table_;
266 }
267
Mathieu Chartier90443472015-07-16 20:32:27 -0700268 void InstrumentQuickAllocEntryPoints() REQUIRES(!Locks::instrument_entrypoints_lock_);
269 void UninstrumentQuickAllocEntryPoints() REQUIRES(!Locks::instrument_entrypoints_lock_);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700270 void InstrumentQuickAllocEntryPointsLocked()
Mathieu Chartier90443472015-07-16 20:32:27 -0700271 REQUIRES(Locks::instrument_entrypoints_lock_, !Locks::thread_list_lock_,
272 !Locks::runtime_shutdown_lock_);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700273 void UninstrumentQuickAllocEntryPointsLocked()
Mathieu Chartier90443472015-07-16 20:32:27 -0700274 REQUIRES(Locks::instrument_entrypoints_lock_, !Locks::thread_list_lock_,
275 !Locks::runtime_shutdown_lock_);
276 void ResetQuickAllocEntryPoints() REQUIRES(Locks::runtime_shutdown_lock_);
Ian Rogersfa824272013-11-05 16:12:57 -0800277
Ian Rogers62d6c772013-02-27 08:32:07 -0800278 // Update the code of a method respecting any installed stubs.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700279 void UpdateMethodsCode(ArtMethod* method, const void* quick_code)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700280 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800281
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700282 // Update the code of a method respecting any installed stubs from debugger.
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000283 void UpdateMethodsCodeForJavaDebuggable(ArtMethod* method, const void* quick_code)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700284 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700285
Ian Rogers62d6c772013-02-27 08:32:07 -0800286 // Get the quick code for the given method. More efficient than asking the class linker as it
287 // will short-cut to GetCode if instrumentation and static method resolution stubs aren't
288 // installed.
Andreas Gampe542451c2016-07-26 09:02:02 -0700289 const void* GetQuickCodeFor(ArtMethod* method, PointerSize pointer_size) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700290 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800291
292 void ForceInterpretOnly() {
293 interpret_only_ = true;
294 forced_interpret_only_ = true;
295 }
296
Brian Carlstromea46f952013-07-30 01:26:50 -0700297 // Called by ArtMethod::Invoke to determine dispatch mechanism.
Ian Rogers62d6c772013-02-27 08:32:07 -0800298 bool InterpretOnly() const {
299 return interpret_only_;
300 }
301
Hiroshi Yamauchi563b47c2014-02-28 17:18:37 -0800302 bool IsForcedInterpretOnly() const {
303 return forced_interpret_only_;
304 }
305
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800306 // Code is in boot image oat file which isn't compiled as debuggable.
307 // Need debug version (interpreter or jitted) if that's the case.
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000308 bool NeedDebugVersionFor(ArtMethod* method) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700309 REQUIRES_SHARED(Locks::mutator_lock_);
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800310
Ian Rogers62d6c772013-02-27 08:32:07 -0800311 bool AreExitStubsInstalled() const {
312 return instrumentation_stubs_installed_;
313 }
314
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700315 bool HasMethodEntryListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200316 return have_method_entry_listeners_;
317 }
318
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700319 bool HasMethodExitListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200320 return have_method_exit_listeners_;
321 }
322
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700323 bool HasMethodUnwindListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200324 return have_method_unwind_listeners_;
325 }
326
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700327 bool HasDexPcListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200328 return have_dex_pc_listeners_;
329 }
330
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700331 bool HasFieldReadListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200332 return have_field_read_listeners_;
333 }
334
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700335 bool HasFieldWriteListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200336 return have_field_write_listeners_;
337 }
338
Alex Light6e1607e2017-08-23 10:06:18 -0700339 bool HasExceptionThrownListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
340 return have_exception_thrown_listeners_;
Sebastien Hertz9f102032014-05-23 08:59:42 +0200341 }
342
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700343 bool HasBranchListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000344 return have_branch_listeners_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800345 }
346
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700347 bool HasInvokeVirtualOrInterfaceListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100348 return have_invoke_virtual_or_interface_listeners_;
349 }
350
Alex Lighte814f9d2017-07-31 16:14:39 -0700351 bool HasWatchedFramePopListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
352 return have_watched_frame_pop_listeners_;
353 }
354
Alex Light798eab02017-08-23 12:54:53 -0700355 bool HasExceptionHandledListeners() const REQUIRES_SHARED(Locks::mutator_lock_) {
356 return have_exception_handled_listeners_;
357 }
358
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700359 bool IsActive() const REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200360 return have_dex_pc_listeners_ || have_method_entry_listeners_ || have_method_exit_listeners_ ||
Sebastien Hertz42cd43f2014-05-13 14:15:41 +0200361 have_field_read_listeners_ || have_field_write_listeners_ ||
Alex Light6e1607e2017-08-23 10:06:18 -0700362 have_exception_thrown_listeners_ || have_method_unwind_listeners_ ||
Alex Lighte814f9d2017-07-31 16:14:39 -0700363 have_branch_listeners_ || have_invoke_virtual_or_interface_listeners_ ||
Alex Light798eab02017-08-23 12:54:53 -0700364 have_watched_frame_pop_listeners_ || have_exception_handled_listeners_;
Bill Buzbeefd522f92016-02-11 22:37:42 +0000365 }
366
367 // Any instrumentation *other* than what is needed for Jit profiling active?
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700368 bool NonJitProfilingActive() const REQUIRES_SHARED(Locks::mutator_lock_) {
Bill Buzbeefd522f92016-02-11 22:37:42 +0000369 return have_dex_pc_listeners_ || have_method_exit_listeners_ ||
370 have_field_read_listeners_ || have_field_write_listeners_ ||
Alex Light6e1607e2017-08-23 10:06:18 -0700371 have_exception_thrown_listeners_ || have_method_unwind_listeners_ ||
Alex Light798eab02017-08-23 12:54:53 -0700372 have_branch_listeners_ || have_watched_frame_pop_listeners_ ||
373 have_exception_handled_listeners_;
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200374 }
375
Ian Rogers62d6c772013-02-27 08:32:07 -0800376 // Inform listeners that a method has been entered. A dex PC is provided as we may install
377 // listeners into executing code and get method enter events for methods already on the stack.
378 void MethodEnterEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700379 ArtMethod* method, uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700380 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200381 if (UNLIKELY(HasMethodEntryListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800382 MethodEnterEventImpl(thread, this_object, method, dex_pc);
383 }
384 }
385
386 // Inform listeners that a method has been exited.
Alex Lightd7661582017-05-01 13:48:16 -0700387 void MethodExitEvent(Thread* thread,
388 mirror::Object* this_object,
389 ArtMethod* method,
390 uint32_t dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800391 const JValue& return_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700392 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200393 if (UNLIKELY(HasMethodExitListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800394 MethodExitEventImpl(thread, this_object, method, dex_pc, return_value);
395 }
396 }
397
398 // Inform listeners that a method has been exited due to an exception.
399 void MethodUnwindEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700400 ArtMethod* method, uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700401 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800402
403 // Inform listeners that the dex pc has moved (only supported by the interpreter).
404 void DexPcMovedEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700405 ArtMethod* method, uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700406 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz74109f62013-06-07 17:40:09 +0200407 if (UNLIKELY(HasDexPcListeners())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800408 DexPcMovedEventImpl(thread, this_object, method, dex_pc);
409 }
410 }
411
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000412 // Inform listeners that a branch has been taken (only supported by the interpreter).
413 void Branch(Thread* thread, ArtMethod* method, uint32_t dex_pc, int32_t offset) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700414 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000415 if (UNLIKELY(HasBranchListeners())) {
416 BranchImpl(thread, method, dex_pc, offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800417 }
418 }
419
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200420 // Inform listeners that we read a field (only supported by the interpreter).
421 void FieldReadEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700422 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700423 ArtField* field) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700424 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200425 if (UNLIKELY(HasFieldReadListeners())) {
426 FieldReadEventImpl(thread, this_object, method, dex_pc, field);
427 }
428 }
429
430 // Inform listeners that we write a field (only supported by the interpreter).
431 void FieldWriteEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700432 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700433 ArtField* field, const JValue& field_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700434 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200435 if (UNLIKELY(HasFieldWriteListeners())) {
436 FieldWriteEventImpl(thread, this_object, method, dex_pc, field, field_value);
437 }
438 }
439
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100440 void InvokeVirtualOrInterface(Thread* thread,
441 mirror::Object* this_object,
442 ArtMethod* caller,
443 uint32_t dex_pc,
444 ArtMethod* callee) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700445 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100446 if (UNLIKELY(HasInvokeVirtualOrInterfaceListeners())) {
447 InvokeVirtualOrInterfaceImpl(thread, this_object, caller, dex_pc, callee);
448 }
449 }
450
Alex Lighte814f9d2017-07-31 16:14:39 -0700451 // Inform listeners that a branch has been taken (only supported by the interpreter).
452 void WatchedFramePopped(Thread* thread, const ShadowFrame& frame) const
453 REQUIRES_SHARED(Locks::mutator_lock_) {
454 if (UNLIKELY(HasWatchedFramePopListeners())) {
455 WatchedFramePopImpl(thread, frame);
456 }
457 }
458
Alex Light6e1607e2017-08-23 10:06:18 -0700459 // Inform listeners that an exception was thrown.
460 void ExceptionThrownEvent(Thread* thread, mirror::Throwable* exception_object) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700461 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800462
Alex Light798eab02017-08-23 12:54:53 -0700463 // Inform listeners that an exception has been handled. This is not sent for native code or for
464 // exceptions which reach the end of the thread's stack.
465 void ExceptionHandledEvent(Thread* thread, mirror::Throwable* exception_object) const
466 REQUIRES_SHARED(Locks::mutator_lock_);
467
Ian Rogers62d6c772013-02-27 08:32:07 -0800468 // Called when an instrumented method is entered. The intended link register (lr) is saved so
469 // that returning causes a branch to the method exit stub. Generates method enter events.
470 void PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700471 ArtMethod* method, uintptr_t lr,
Jeff Hao9a916d32013-06-27 18:45:37 -0700472 bool interpreter_entry)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700473 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800474
475 // Called when an instrumented method is exited. Removes the pushed instrumentation frame
Alex Lightb7edcda2017-04-27 13:20:31 -0700476 // returning the intended link register. Generates method exit events. The gpr_result and
477 // fpr_result pointers are pointers to the locations where the integer/pointer and floating point
478 // result values of the function are stored. Both pointers must always be valid but the values
479 // held there will only be meaningful if interpreted as the appropriate type given the function
480 // being returned from.
Andreas Gamped58342c2014-06-05 14:18:08 -0700481 TwoWordReturn PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc,
Alex Lightb7edcda2017-04-27 13:20:31 -0700482 uint64_t* gpr_result, uint64_t* fpr_result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700483 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800484
485 // Pops an instrumentation frame from the current thread and generate an unwind event.
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700486 // Returns the return pc for the instrumentation frame that's popped.
487 uintptr_t PopMethodForUnwind(Thread* self, bool is_deoptimization) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700488 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800489
490 // Call back for configure stubs.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700491 void InstallStubsForClass(mirror::Class* klass) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700492 REQUIRES(!deoptimized_methods_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800493
Mathieu Chartiere401d142015-04-22 13:56:20 -0700494 void InstallStubsForMethod(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700495 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100496
Mingyao Yang99170c62015-07-06 11:10:37 -0700497 // Install instrumentation exit stub on every method of the stack of the given thread.
498 // This is used by the debugger to cause a deoptimization of the thread's stack after updating
499 // local variable(s).
500 void InstrumentThreadStack(Thread* thread)
Alex Light3ae82532017-07-26 13:59:07 -0700501 REQUIRES_SHARED(Locks::mutator_lock_);
Mingyao Yang99170c62015-07-06 11:10:37 -0700502
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000503 static size_t ComputeFrameId(Thread* self,
504 size_t frame_depth,
505 size_t inlined_frames_before_frame)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700506 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000507
Mathieu Chartiereebc3af2016-02-29 18:13:38 -0800508 // Does not hold lock, used to check if someone changed from not instrumented to instrumented
509 // during a GC suspend point.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700510 bool AllocEntrypointsInstrumented() const REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier50e93312016-03-16 11:25:29 -0700511 return alloc_entrypoints_instrumented_;
Mathieu Chartiereebc3af2016-02-29 18:13:38 -0800512 }
513
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200514 InstrumentationLevel GetCurrentInstrumentationLevel() const;
515
Alex Lightdba61482016-12-21 08:20:29 -0800516 private:
517 // Returns true if moving to the given instrumentation level requires the installation of stubs.
518 // False otherwise.
519 bool RequiresInstrumentationInstallation(InstrumentationLevel new_level) const;
520
Ian Rogers62d6c772013-02-27 08:32:07 -0800521 // Does the job of installing or removing instrumentation code within methods.
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200522 // In order to support multiple clients using instrumentation at the same time,
523 // the caller must pass a unique key (a string) identifying it so we remind which
524 // instrumentation level it needs. Therefore the current instrumentation level
525 // becomes the highest instrumentation level required by a client.
526 void ConfigureStubs(const char* key, InstrumentationLevel desired_instrumentation_level)
Mathieu Chartieraa516822015-10-02 15:53:37 -0700527 REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_)
528 REQUIRES(!deoptimized_methods_lock_,
529 !Locks::thread_list_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700530 !Locks::classlinker_classes_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800531
Mathieu Chartier90443472015-07-16 20:32:27 -0700532 void UpdateInterpreterHandlerTable() REQUIRES(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800533 /*
534 * TUNING: Dalvik's mterp stashes the actual current handler table base in a
535 * tls field. For Arm, this enables all suspend, debug & tracing checks to be
536 * collapsed into a single conditionally-executed ldw instruction.
537 * Move to Dalvik-style handler-table management for both the goto interpreter and
538 * mterp.
539 */
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200540 interpreter_handler_table_ = IsActive() ? kAlternativeHandlerTable : kMainHandlerTable;
541 }
542
Mathieu Chartier661974a2014-01-09 11:23:53 -0800543 // No thread safety analysis to get around SetQuickAllocEntryPointsInstrumented requiring
544 // exclusive access to mutator lock which you can't get if the runtime isn't started.
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700545 void SetEntrypointsInstrumented(bool instrumented) NO_THREAD_SAFETY_ANALYSIS;
Mathieu Chartier661974a2014-01-09 11:23:53 -0800546
Alex Lightd7661582017-05-01 13:48:16 -0700547 void MethodEnterEventImpl(Thread* thread,
548 ObjPtr<mirror::Object> this_object,
549 ArtMethod* method,
550 uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700551 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lightd7661582017-05-01 13:48:16 -0700552 void MethodExitEventImpl(Thread* thread,
553 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700554 ArtMethod* method,
Alex Lightd7661582017-05-01 13:48:16 -0700555 uint32_t dex_pc,
556 const JValue& return_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700557 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lightd7661582017-05-01 13:48:16 -0700558 void DexPcMovedEventImpl(Thread* thread,
559 ObjPtr<mirror::Object> this_object,
560 ArtMethod* method,
561 uint32_t dex_pc) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700562 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000563 void BranchImpl(Thread* thread, ArtMethod* method, uint32_t dex_pc, int32_t offset) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700564 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100565 void InvokeVirtualOrInterfaceImpl(Thread* thread,
Alex Lightd7661582017-05-01 13:48:16 -0700566 ObjPtr<mirror::Object> this_object,
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100567 ArtMethod* caller,
568 uint32_t dex_pc,
569 ArtMethod* callee) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700570 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lighte814f9d2017-07-31 16:14:39 -0700571 void WatchedFramePopImpl(Thread* thread, const ShadowFrame& frame) const
572 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lightd7661582017-05-01 13:48:16 -0700573 void FieldReadEventImpl(Thread* thread,
574 ObjPtr<mirror::Object> this_object,
575 ArtMethod* method,
576 uint32_t dex_pc,
577 ArtField* field) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700578 REQUIRES_SHARED(Locks::mutator_lock_);
Alex Lightd7661582017-05-01 13:48:16 -0700579 void FieldWriteEventImpl(Thread* thread,
580 ObjPtr<mirror::Object> this_object,
581 ArtMethod* method,
582 uint32_t dex_pc,
583 ArtField* field,
584 const JValue& field_value) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700585 REQUIRES_SHARED(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800586
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700587 // Read barrier-aware utility functions for accessing deoptimized_methods_
Mathieu Chartiere401d142015-04-22 13:56:20 -0700588 bool AddDeoptimizedMethod(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700589 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700590 bool IsDeoptimizedMethod(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700591 REQUIRES_SHARED(Locks::mutator_lock_, deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700592 bool RemoveDeoptimizedMethod(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700593 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700594 ArtMethod* BeginDeoptimizedMethod()
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700595 REQUIRES_SHARED(Locks::mutator_lock_, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700596 bool IsDeoptimizedMethodsEmpty() const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700597 REQUIRES_SHARED(Locks::mutator_lock_, deoptimized_methods_lock_);
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700598 void UpdateMethodsCodeImpl(ArtMethod* method, const void* quick_code)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700599 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_);
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700600
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700601
Brian Carlstromea46f952013-07-30 01:26:50 -0700602 // Have we hijacked ArtMethod::code_ so that it calls instrumentation/interpreter code?
Ian Rogers62d6c772013-02-27 08:32:07 -0800603 bool instrumentation_stubs_installed_;
604
Brian Carlstromea46f952013-07-30 01:26:50 -0700605 // Have we hijacked ArtMethod::code_ to reference the enter/exit stubs?
Ian Rogers62d6c772013-02-27 08:32:07 -0800606 bool entry_exit_stubs_installed_;
607
Brian Carlstromea46f952013-07-30 01:26:50 -0700608 // Have we hijacked ArtMethod::code_ to reference the enter interpreter stub?
Ian Rogers62d6c772013-02-27 08:32:07 -0800609 bool interpreter_stubs_installed_;
610
611 // Do we need the fidelity of events that we only get from running within the interpreter?
612 bool interpret_only_;
613
614 // Did the runtime request we only run in the interpreter? ie -Xint mode.
615 bool forced_interpret_only_;
616
617 // Do we have any listeners for method entry events? Short-cut to avoid taking the
618 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200619 bool have_method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800620
621 // Do we have any listeners for method exit events? Short-cut to avoid taking the
622 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200623 bool have_method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800624
625 // Do we have any listeners for method unwind events? Short-cut to avoid taking the
626 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200627 bool have_method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800628
629 // Do we have any listeners for dex move events? Short-cut to avoid taking the
630 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200631 bool have_dex_pc_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800632
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200633 // Do we have any listeners for field read events? Short-cut to avoid taking the
634 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200635 bool have_field_read_listeners_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200636
637 // Do we have any listeners for field write events? Short-cut to avoid taking the
638 // instrumentation_lock_.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200639 bool have_field_write_listeners_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200640
Alex Light6e1607e2017-08-23 10:06:18 -0700641 // Do we have any exception thrown listeners? Short-cut to avoid taking the instrumentation_lock_.
642 bool have_exception_thrown_listeners_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800643
Alex Lighte814f9d2017-07-31 16:14:39 -0700644 // Do we have any frame pop listeners? Short-cut to avoid taking the instrumentation_lock_.
645 bool have_watched_frame_pop_listeners_ GUARDED_BY(Locks::mutator_lock_);
646
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000647 // Do we have any branch listeners? Short-cut to avoid taking the instrumentation_lock_.
648 bool have_branch_listeners_ GUARDED_BY(Locks::mutator_lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800649
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100650 // Do we have any invoke listeners? Short-cut to avoid taking the instrumentation_lock_.
651 bool have_invoke_virtual_or_interface_listeners_ GUARDED_BY(Locks::mutator_lock_);
652
Alex Light798eab02017-08-23 12:54:53 -0700653 // Do we have any exception handled listeners? Short-cut to avoid taking the
654 // instrumentation_lock_.
655 bool have_exception_handled_listeners_ GUARDED_BY(Locks::mutator_lock_);
656
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200657 // Contains the instrumentation level required by each client of the instrumentation identified
658 // by a string key.
659 typedef SafeMap<const char*, InstrumentationLevel> InstrumentationLevelTable;
660 InstrumentationLevelTable requested_instrumentation_levels_ GUARDED_BY(Locks::mutator_lock_);
661
Ian Rogers62d6c772013-02-27 08:32:07 -0800662 // The event listeners, written to with the mutator_lock_ exclusively held.
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000663 // Mutators must be able to iterate over these lists concurrently, that is, with listeners being
664 // added or removed while iterating. The modifying thread holds exclusive lock,
665 // so other threads cannot iterate (i.e. read the data of the list) at the same time but they
666 // do keep iterators that need to remain valid. This is the reason these listeners are std::list
667 // and not for example std::vector: the existing storage for a std::list does not move.
668 // Note that mutators cannot make a copy of these lists before iterating, as the instrumentation
669 // listeners can also be deleted concurrently.
670 // As a result, these lists are never trimmed. That's acceptable given the low number of
671 // listeners we have.
Ian Rogers62d6c772013-02-27 08:32:07 -0800672 std::list<InstrumentationListener*> method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_);
673 std::list<InstrumentationListener*> method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_);
674 std::list<InstrumentationListener*> method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000675 std::list<InstrumentationListener*> branch_listeners_ GUARDED_BY(Locks::mutator_lock_);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100676 std::list<InstrumentationListener*> invoke_virtual_or_interface_listeners_
677 GUARDED_BY(Locks::mutator_lock_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000678 std::list<InstrumentationListener*> dex_pc_listeners_ GUARDED_BY(Locks::mutator_lock_);
679 std::list<InstrumentationListener*> field_read_listeners_ GUARDED_BY(Locks::mutator_lock_);
680 std::list<InstrumentationListener*> field_write_listeners_ GUARDED_BY(Locks::mutator_lock_);
Alex Light6e1607e2017-08-23 10:06:18 -0700681 std::list<InstrumentationListener*> exception_thrown_listeners_ GUARDED_BY(Locks::mutator_lock_);
Alex Lighte814f9d2017-07-31 16:14:39 -0700682 std::list<InstrumentationListener*> watched_frame_pop_listeners_ GUARDED_BY(Locks::mutator_lock_);
Alex Light798eab02017-08-23 12:54:53 -0700683 std::list<InstrumentationListener*> exception_handled_listeners_ GUARDED_BY(Locks::mutator_lock_);
jeffhao725a9572012-11-13 18:20:12 -0800684
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100685 // The set of methods being deoptimized (by the debugger) which must be executed with interpreter
686 // only.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700687 mutable ReaderWriterMutex deoptimized_methods_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700688 std::unordered_set<ArtMethod*> deoptimized_methods_ GUARDED_BY(deoptimized_methods_lock_);
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100689 bool deoptimization_enabled_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100690
Ian Rogersfa824272013-11-05 16:12:57 -0800691 // Current interpreter handler table. This is updated each time the thread state flags are
692 // modified.
Sebastien Hertzed2be172014-08-19 15:33:43 +0200693 InterpreterHandlerTable interpreter_handler_table_ GUARDED_BY(Locks::mutator_lock_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200694
Ian Rogersfa824272013-11-05 16:12:57 -0800695 // Greater than 0 if quick alloc entry points instrumented.
Mathieu Chartiereebc3af2016-02-29 18:13:38 -0800696 size_t quick_alloc_entry_points_instrumentation_counter_;
Mathieu Chartier50e93312016-03-16 11:25:29 -0700697
698 // alloc_entrypoints_instrumented_ is only updated with all the threads suspended, this is done
699 // to prevent races with the GC where the GC relies on thread suspension only see
700 // alloc_entrypoints_instrumented_ change during suspend points.
701 bool alloc_entrypoints_instrumented_;
702
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200703 friend class InstrumentationTest; // For GetCurrentInstrumentationLevel and ConfigureStubs.
704
jeffhao725a9572012-11-13 18:20:12 -0800705 DISALLOW_COPY_AND_ASSIGN(Instrumentation);
706};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700707std::ostream& operator<<(std::ostream& os, const Instrumentation::InstrumentationEvent& rhs);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200708std::ostream& operator<<(std::ostream& os, const Instrumentation::InstrumentationLevel& rhs);
jeffhao725a9572012-11-13 18:20:12 -0800709
Ian Rogers62d6c772013-02-27 08:32:07 -0800710// An element in the instrumentation side stack maintained in art::Thread.
711struct InstrumentationStackFrame {
Nicolas Geoffray07c70282017-08-30 08:09:42 +0000712 InstrumentationStackFrame(mirror::Object* this_object, ArtMethod* method,
713 uintptr_t return_pc, size_t frame_id, bool interpreter_entry)
714 : this_object_(this_object), method_(method), return_pc_(return_pc), frame_id_(frame_id),
Jeff Hao9a916d32013-06-27 18:45:37 -0700715 interpreter_entry_(interpreter_entry) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800716 }
717
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700718 std::string Dump() const REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800719
720 mirror::Object* this_object_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700721 ArtMethod* method_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100722 uintptr_t return_pc_;
723 size_t frame_id_;
724 bool interpreter_entry_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800725};
726
727} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -0800728} // namespace art
729
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700730#endif // ART_RUNTIME_INSTRUMENTATION_H_