jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 1 | /* |
| 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 Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_INSTRUMENTATION_H_ |
| 18 | #define ART_RUNTIME_INSTRUMENTATION_H_ |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 19 | |
Mythri Alle | 72be14e | 2021-11-01 11:48:06 +0000 | [diff] [blame] | 20 | #include <stdint.h> |
Mythri Alle | 5097f83 | 2021-11-02 14:52:30 +0000 | [diff] [blame] | 21 | |
| 22 | #include <functional> |
Ian Rogers | 576ca0c | 2014-06-06 15:58:22 -0700 | [diff] [blame] | 23 | #include <list> |
Andreas Gampe | 7e56a07 | 2018-11-29 10:40:06 -0800 | [diff] [blame] | 24 | #include <memory> |
Mythri Alle | 72be14e | 2021-11-01 11:48:06 +0000 | [diff] [blame] | 25 | #include <optional> |
Mythri Alle | 5097f83 | 2021-11-02 14:52:30 +0000 | [diff] [blame] | 26 | #include <unordered_set> |
Ian Rogers | 576ca0c | 2014-06-06 15:58:22 -0700 | [diff] [blame] | 27 | |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 28 | #include "arch/instruction_set.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 29 | #include "base/enums.h" |
Andreas Gampe | 7e56a07 | 2018-11-29 10:40:06 -0800 | [diff] [blame] | 30 | #include "base/locks.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 31 | #include "base/macros.h" |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 32 | #include "base/safe_map.h" |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 33 | #include "gc_root.h" |
Mythri Alle | 5097f83 | 2021-11-02 14:52:30 +0000 | [diff] [blame] | 34 | #include "offsets.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 35 | |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 36 | namespace art { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 37 | namespace mirror { |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 38 | class Class; |
| 39 | class Object; |
| 40 | class Throwable; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 41 | } // namespace mirror |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 42 | class ArtField; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 43 | class ArtMethod; |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 44 | template <typename T> class Handle; |
Alex Light | 2c8206f | 2018-06-08 14:51:09 -0700 | [diff] [blame] | 45 | template <typename T> class MutableHandle; |
Mythri Alle | 5097f83 | 2021-11-02 14:52:30 +0000 | [diff] [blame] | 46 | struct NthCallerVisitor; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 47 | union JValue; |
Andreas Gampe | 7e56a07 | 2018-11-29 10:40:06 -0800 | [diff] [blame] | 48 | class SHARED_LOCKABLE ReaderWriterMutex; |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 49 | class ShadowFrame; |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 50 | class Thread; |
Mingyao Yang | 2ee1790 | 2017-08-30 11:37:08 -0700 | [diff] [blame] | 51 | enum class DeoptimizationMethodType; |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 52 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 53 | namespace instrumentation { |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 54 | |
Sebastien Hertz | ee1997a | 2013-09-19 14:47:09 +0200 | [diff] [blame] | 55 | |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 56 | // Do we want to deoptimize for method entry and exit listeners or just try to intercept |
| 57 | // invocations? Deoptimization forces all code to run in the interpreter and considerably hurts the |
| 58 | // application's performance. |
| 59 | static constexpr bool kDeoptimizeForAccurateMethodEntryExitListeners = true; |
| 60 | |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 61 | // an optional frame is either Some(const ShadowFrame& current_frame) or None depending on if the |
| 62 | // method being exited has a shadow-frame associed with the current stack frame. In cases where |
| 63 | // there is no shadow-frame associated with this stack frame this will be None. |
| 64 | using OptionalFrame = std::optional<std::reference_wrapper<const ShadowFrame>>; |
| 65 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 66 | // Instrumentation event listener API. Registered listeners will get the appropriate call back for |
| 67 | // the events they are listening for. The call backs supply the thread, method and dex_pc the event |
| 68 | // occurred upon. The thread may or may not be Thread::Current(). |
| 69 | struct InstrumentationListener { |
| 70 | InstrumentationListener() {} |
| 71 | virtual ~InstrumentationListener() {} |
| 72 | |
| 73 | // Call-back for when a method is entered. |
Mythri Alle | 9cc65df | 2021-09-21 15:09:58 +0000 | [diff] [blame] | 74 | virtual void MethodEntered(Thread* thread, ArtMethod* method) |
| 75 | REQUIRES_SHARED(Locks::mutator_lock_) = 0; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 76 | |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 77 | virtual void MethodExited(Thread* thread, |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 78 | ArtMethod* method, |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 79 | OptionalFrame frame, |
| 80 | MutableHandle<mirror::Object>& return_value) |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 81 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 82 | |
| 83 | // Call-back for when a method is exited. The implementor should either handler-ize the return |
| 84 | // value (if appropriate) or use the alternate MethodExited callback instead if they need to |
| 85 | // go through a suspend point. |
| 86 | virtual void MethodExited(Thread* thread, |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 87 | ArtMethod* method, |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 88 | OptionalFrame frame, |
| 89 | JValue& return_value) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 90 | REQUIRES_SHARED(Locks::mutator_lock_) = 0; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 91 | |
| 92 | // Call-back for when a method is popped due to an exception throw. A method will either cause a |
| 93 | // MethodExited call-back or a MethodUnwind call-back when its activation is removed. |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 94 | virtual void MethodUnwind(Thread* thread, |
| 95 | Handle<mirror::Object> this_object, |
| 96 | ArtMethod* method, |
| 97 | uint32_t dex_pc) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 98 | REQUIRES_SHARED(Locks::mutator_lock_) = 0; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 99 | |
| 100 | // Call-back for when the dex pc moves in a method. |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 101 | virtual void DexPcMoved(Thread* thread, |
| 102 | Handle<mirror::Object> this_object, |
| 103 | ArtMethod* method, |
| 104 | uint32_t new_dex_pc) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 105 | REQUIRES_SHARED(Locks::mutator_lock_) = 0; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 106 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 107 | // Call-back for when we read from a field. |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 108 | virtual void FieldRead(Thread* thread, |
| 109 | Handle<mirror::Object> this_object, |
| 110 | ArtMethod* method, |
| 111 | uint32_t dex_pc, |
| 112 | ArtField* field) = 0; |
| 113 | |
| 114 | virtual void FieldWritten(Thread* thread, |
| 115 | Handle<mirror::Object> this_object, |
| 116 | ArtMethod* method, |
| 117 | uint32_t dex_pc, |
| 118 | ArtField* field, |
| 119 | Handle<mirror::Object> field_value) |
| 120 | REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 121 | |
| 122 | // Call-back for when we write into a field. |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 123 | virtual void FieldWritten(Thread* thread, |
| 124 | Handle<mirror::Object> this_object, |
| 125 | ArtMethod* method, |
| 126 | uint32_t dex_pc, |
| 127 | ArtField* field, |
| 128 | const JValue& field_value) |
| 129 | REQUIRES_SHARED(Locks::mutator_lock_) = 0; |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 130 | |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 131 | // Call-back when an exception is thrown. |
| 132 | virtual void ExceptionThrown(Thread* thread, |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 133 | Handle<mirror::Throwable> exception_object) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 134 | REQUIRES_SHARED(Locks::mutator_lock_) = 0; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 135 | |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 136 | // Call-back when an exception is caught/handled by java code. |
| 137 | virtual void ExceptionHandled(Thread* thread, Handle<mirror::Throwable> exception_object) |
| 138 | REQUIRES_SHARED(Locks::mutator_lock_) = 0; |
| 139 | |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 140 | // Call-back for when we execute a branch. |
| 141 | virtual void Branch(Thread* thread, |
| 142 | ArtMethod* method, |
| 143 | uint32_t dex_pc, |
| 144 | int32_t dex_pc_offset) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 145 | REQUIRES_SHARED(Locks::mutator_lock_) = 0; |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 146 | |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 147 | // Call-back when a shadow_frame with the needs_notify_pop_ boolean set is popped off the stack by |
| 148 | // either return or exceptions. Normally instrumentation listeners should ensure that there are |
| 149 | // shadow-frames by deoptimizing stacks. |
| 150 | virtual void WatchedFramePop(Thread* thread ATTRIBUTE_UNUSED, |
| 151 | const ShadowFrame& frame ATTRIBUTE_UNUSED) |
Alex Light | 05f4774 | 2017-09-14 00:34:44 +0000 | [diff] [blame] | 152 | REQUIRES_SHARED(Locks::mutator_lock_) = 0; |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 153 | }; |
| 154 | |
Alex Light | 2c8206f | 2018-06-08 14:51:09 -0700 | [diff] [blame] | 155 | class Instrumentation; |
| 156 | // A helper to send instrumentation events while popping the stack in a safe way. |
| 157 | class InstrumentationStackPopper { |
| 158 | public: |
| 159 | explicit InstrumentationStackPopper(Thread* self); |
| 160 | ~InstrumentationStackPopper() REQUIRES_SHARED(Locks::mutator_lock_); |
| 161 | |
Nicolas Geoffray | e91e795 | 2020-01-23 10:15:56 +0000 | [diff] [blame] | 162 | // Increase the number of frames being popped up to `stack_pointer`. Return true if the |
| 163 | // frames were popped without any exceptions, false otherwise. The exception that caused |
| 164 | // the pop is 'exception'. |
| 165 | bool PopFramesTo(uintptr_t stack_pointer, /*in-out*/MutableHandle<mirror::Throwable>& exception) |
Alex Light | 2c8206f | 2018-06-08 14:51:09 -0700 | [diff] [blame] | 166 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 167 | |
| 168 | private: |
| 169 | Thread* self_; |
| 170 | Instrumentation* instrumentation_; |
Nicolas Geoffray | e91e795 | 2020-01-23 10:15:56 +0000 | [diff] [blame] | 171 | // The stack pointer limit for frames to pop. |
| 172 | uintptr_t pop_until_; |
Alex Light | 2c8206f | 2018-06-08 14:51:09 -0700 | [diff] [blame] | 173 | }; |
| 174 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 175 | // Instrumentation is a catch-all for when extra information is required from the runtime. The |
| 176 | // typical use for instrumentation is for profiling and debugging. Instrumentation may add stubs |
| 177 | // to method entry and exit, it may also force execution to be switched to the interpreter and |
| 178 | // trigger deoptimization. |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 179 | class Instrumentation { |
| 180 | public: |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 181 | enum InstrumentationEvent { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 182 | kMethodEntered = 0x1, |
| 183 | kMethodExited = 0x2, |
| 184 | kMethodUnwind = 0x4, |
| 185 | kDexPcMoved = 0x8, |
| 186 | kFieldRead = 0x10, |
| 187 | kFieldWritten = 0x20, |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 188 | kExceptionThrown = 0x40, |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 189 | kBranch = 0x80, |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 190 | kWatchedFramePop = 0x200, |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 191 | kExceptionHandled = 0x400, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 192 | }; |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 193 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 194 | enum class InstrumentationLevel { |
| 195 | kInstrumentNothing, // execute without instrumentation |
| 196 | kInstrumentWithInstrumentationStubs, // execute with instrumentation entry/exit stubs |
| 197 | kInstrumentWithInterpreter // execute with interpreter |
| 198 | }; |
| 199 | |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 200 | Instrumentation(); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 201 | |
Mythri Alle | 5097f83 | 2021-11-02 14:52:30 +0000 | [diff] [blame] | 202 | static constexpr MemberOffset NeedsEntryExitHooksOffset() { |
Mythri Alle | 9575c12 | 2021-11-12 12:04:41 +0000 | [diff] [blame] | 203 | // Assert that instrumentation_stubs_installed_ is 8bits wide. If the size changes |
| 204 | // update the compare instructions in the code generator when generating checks for |
| 205 | // MethodEntryExitHooks. |
| 206 | static_assert(sizeof(instrumentation_stubs_installed_) == 1, |
| 207 | "instrumentation_stubs_installed_ isn't expected size"); |
Mythri Alle | 5097f83 | 2021-11-02 14:52:30 +0000 | [diff] [blame] | 208 | return MemberOffset(OFFSETOF_MEMBER(Instrumentation, instrumentation_stubs_installed_)); |
| 209 | } |
| 210 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 211 | // Add a listener to be notified of the masked together sent of instrumentation events. This |
| 212 | // suspend the runtime to install stubs. You are expected to hold the mutator lock as a proxy |
| 213 | // for saying you should have suspended all threads (installing stubs while threads are running |
| 214 | // will break). |
| 215 | void AddListener(InstrumentationListener* listener, uint32_t events) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 216 | REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 217 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 218 | // Removes a listener possibly removing instrumentation stubs. |
| 219 | void RemoveListener(InstrumentationListener* listener, uint32_t events) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 220 | REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 221 | |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 222 | // Calls UndeoptimizeEverything which may visit class linker classes through ConfigureStubs. |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 223 | void DisableDeoptimization(const char* key) |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 224 | REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_) |
Andreas Gampe | 7e56a07 | 2018-11-29 10:40:06 -0800 | [diff] [blame] | 225 | REQUIRES(!GetDeoptimizedMethodsLock()); |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 226 | |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 227 | bool AreAllMethodsDeoptimized() const { |
Mythri Alle | 9575c12 | 2021-11-12 12:04:41 +0000 | [diff] [blame] | 228 | return InterpreterStubsInstalled(); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 229 | } |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 230 | bool ShouldNotifyMethodEnterExitEvents() const REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 231 | |
| 232 | // Executes everything with interpreter. |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 233 | void DeoptimizeEverything(const char* key) |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 234 | REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_) |
| 235 | REQUIRES(!Locks::thread_list_lock_, |
| 236 | !Locks::classlinker_classes_lock_, |
Andreas Gampe | 7e56a07 | 2018-11-29 10:40:06 -0800 | [diff] [blame] | 237 | !GetDeoptimizedMethodsLock()); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 238 | |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 239 | // Executes everything with compiled code (or interpreter if there is no code). May visit class |
| 240 | // linker classes through ConfigureStubs. |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 241 | void UndeoptimizeEverything(const char* key) |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 242 | REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_) |
| 243 | REQUIRES(!Locks::thread_list_lock_, |
| 244 | !Locks::classlinker_classes_lock_, |
Andreas Gampe | 7e56a07 | 2018-11-29 10:40:06 -0800 | [diff] [blame] | 245 | !GetDeoptimizedMethodsLock()); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 246 | |
| 247 | // Deoptimize a method by forcing its execution with the interpreter. Nevertheless, a static |
| 248 | // method (except a class initializer) set to the resolution trampoline will be deoptimized only |
| 249 | // once its declaring class is initialized. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 250 | void Deoptimize(ArtMethod* method) |
Andreas Gampe | 7e56a07 | 2018-11-29 10:40:06 -0800 | [diff] [blame] | 251 | REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !GetDeoptimizedMethodsLock()); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 252 | |
| 253 | // Undeoptimze the method by restoring its entrypoints. Nevertheless, a static method |
| 254 | // (except a class initializer) set to the resolution trampoline will be updated only once its |
| 255 | // declaring class is initialized. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 256 | void Undeoptimize(ArtMethod* method) |
Andreas Gampe | 7e56a07 | 2018-11-29 10:40:06 -0800 | [diff] [blame] | 257 | REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !GetDeoptimizedMethodsLock()); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 258 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 259 | // Indicates whether the method has been deoptimized so it is executed with the interpreter. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 260 | bool IsDeoptimized(ArtMethod* method) |
Andreas Gampe | 7e56a07 | 2018-11-29 10:40:06 -0800 | [diff] [blame] | 261 | REQUIRES(!GetDeoptimizedMethodsLock()) REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 262 | |
Mythri Alle | ab47488 | 2022-01-17 16:43:04 +0000 | [diff] [blame] | 263 | // Indicates if any method needs to be deoptimized. This is used to avoid walking the stack to |
| 264 | // determine if a deoptimization is required. |
| 265 | bool IsDeoptimizedMethodsEmpty() const |
| 266 | REQUIRES(!GetDeoptimizedMethodsLock()) REQUIRES_SHARED(Locks::mutator_lock_); |
| 267 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 268 | // Enable method tracing by installing instrumentation entry/exit stubs or interpreter. |
| 269 | void EnableMethodTracing(const char* key, |
| 270 | bool needs_interpreter = kDeoptimizeForAccurateMethodEntryExitListeners) |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 271 | REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_) |
| 272 | REQUIRES(!Locks::thread_list_lock_, |
| 273 | !Locks::classlinker_classes_lock_, |
Andreas Gampe | 7e56a07 | 2018-11-29 10:40:06 -0800 | [diff] [blame] | 274 | !GetDeoptimizedMethodsLock()); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 275 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 276 | // Disable method tracing by uninstalling instrumentation entry/exit stubs or interpreter. |
| 277 | void DisableMethodTracing(const char* key) |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 278 | REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_) |
| 279 | REQUIRES(!Locks::thread_list_lock_, |
| 280 | !Locks::classlinker_classes_lock_, |
Andreas Gampe | 7e56a07 | 2018-11-29 10:40:06 -0800 | [diff] [blame] | 281 | !GetDeoptimizedMethodsLock()); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 282 | |
Sebastien Hertz | ee1997a | 2013-09-19 14:47:09 +0200 | [diff] [blame] | 283 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 284 | void InstrumentQuickAllocEntryPoints() REQUIRES(!Locks::instrument_entrypoints_lock_); |
| 285 | void UninstrumentQuickAllocEntryPoints() REQUIRES(!Locks::instrument_entrypoints_lock_); |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 286 | void InstrumentQuickAllocEntryPointsLocked() |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 287 | REQUIRES(Locks::instrument_entrypoints_lock_, !Locks::thread_list_lock_, |
| 288 | !Locks::runtime_shutdown_lock_); |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 289 | void UninstrumentQuickAllocEntryPointsLocked() |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 290 | REQUIRES(Locks::instrument_entrypoints_lock_, !Locks::thread_list_lock_, |
| 291 | !Locks::runtime_shutdown_lock_); |
| 292 | void ResetQuickAllocEntryPoints() REQUIRES(Locks::runtime_shutdown_lock_); |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 293 | |
Nicolas Geoffray | 854af03 | 2021-12-21 08:32:42 +0000 | [diff] [blame] | 294 | // Returns a string representation of the given entry point. |
| 295 | static std::string EntryPointString(const void* code); |
| 296 | |
| 297 | // Initialize the entrypoint of the method .`aot_code` is the AOT code. |
| 298 | void InitializeMethodsCode(ArtMethod* method, const void* aot_code) |
| 299 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 300 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 301 | // Update the code of a method respecting any installed stubs. |
Nicolas Geoffray | 854af03 | 2021-12-21 08:32:42 +0000 | [diff] [blame] | 302 | void UpdateMethodsCode(ArtMethod* method, const void* new_code) |
Andreas Gampe | 7e56a07 | 2018-11-29 10:40:06 -0800 | [diff] [blame] | 303 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!GetDeoptimizedMethodsLock()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 304 | |
Nicolas Geoffray | a6e0e7d | 2018-01-26 13:16:50 +0000 | [diff] [blame] | 305 | // Update the code of a native method to a JITed stub. |
Nicolas Geoffray | 854af03 | 2021-12-21 08:32:42 +0000 | [diff] [blame] | 306 | void UpdateNativeMethodsCodeToJitCode(ArtMethod* method, const void* new_code) |
Andreas Gampe | 7e56a07 | 2018-11-29 10:40:06 -0800 | [diff] [blame] | 307 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!GetDeoptimizedMethodsLock()); |
Nicolas Geoffray | a6e0e7d | 2018-01-26 13:16:50 +0000 | [diff] [blame] | 308 | |
Alex Light | 2d441b1 | 2018-06-08 15:33:21 -0700 | [diff] [blame] | 309 | // Return the code that we can execute for an invoke including from the JIT. |
Nicolas Geoffray | c25a9f9 | 2021-12-13 17:22:43 +0000 | [diff] [blame] | 310 | const void* GetCodeForInvoke(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 311 | |
| 312 | void ForceInterpretOnly() { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 313 | forced_interpret_only_ = true; |
| 314 | } |
| 315 | |
Mythri Alle | 9575c12 | 2021-11-12 12:04:41 +0000 | [diff] [blame] | 316 | bool EntryExitStubsInstalled() const { |
| 317 | return instrumentation_level_ == InstrumentationLevel::kInstrumentWithInstrumentationStubs || |
| 318 | instrumentation_level_ == InstrumentationLevel::kInstrumentWithInterpreter; |
| 319 | } |
| 320 | |
| 321 | bool InterpreterStubsInstalled() const { |
| 322 | return instrumentation_level_ == InstrumentationLevel::kInstrumentWithInterpreter; |
| 323 | } |
| 324 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 325 | // Called by ArtMethod::Invoke to determine dispatch mechanism. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 326 | bool InterpretOnly() const { |
Mythri Alle | 9575c12 | 2021-11-12 12:04:41 +0000 | [diff] [blame] | 327 | return forced_interpret_only_ || InterpreterStubsInstalled(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 328 | } |
Nicolas Geoffray | c25a9f9 | 2021-12-13 17:22:43 +0000 | [diff] [blame] | 329 | bool InterpretOnly(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 330 | |
Hiroshi Yamauchi | 563b47c | 2014-02-28 17:18:37 -0800 | [diff] [blame] | 331 | bool IsForcedInterpretOnly() const { |
| 332 | return forced_interpret_only_; |
| 333 | } |
| 334 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 335 | bool AreExitStubsInstalled() const { |
| 336 | return instrumentation_stubs_installed_; |
| 337 | } |
| 338 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 339 | bool HasMethodEntryListeners() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 74109f6 | 2013-06-07 17:40:09 +0200 | [diff] [blame] | 340 | return have_method_entry_listeners_; |
| 341 | } |
| 342 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 343 | bool HasMethodExitListeners() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 74109f6 | 2013-06-07 17:40:09 +0200 | [diff] [blame] | 344 | return have_method_exit_listeners_; |
| 345 | } |
| 346 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 347 | bool HasMethodUnwindListeners() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 348 | return have_method_unwind_listeners_; |
| 349 | } |
| 350 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 351 | bool HasDexPcListeners() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 74109f6 | 2013-06-07 17:40:09 +0200 | [diff] [blame] | 352 | return have_dex_pc_listeners_; |
| 353 | } |
| 354 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 355 | bool HasFieldReadListeners() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 356 | return have_field_read_listeners_; |
| 357 | } |
| 358 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 359 | bool HasFieldWriteListeners() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 360 | return have_field_write_listeners_; |
| 361 | } |
| 362 | |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 363 | bool HasExceptionThrownListeners() const REQUIRES_SHARED(Locks::mutator_lock_) { |
| 364 | return have_exception_thrown_listeners_; |
Sebastien Hertz | 9f10203 | 2014-05-23 08:59:42 +0200 | [diff] [blame] | 365 | } |
| 366 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 367 | bool HasBranchListeners() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 368 | return have_branch_listeners_; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 369 | } |
| 370 | |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 371 | bool HasWatchedFramePopListeners() const REQUIRES_SHARED(Locks::mutator_lock_) { |
| 372 | return have_watched_frame_pop_listeners_; |
| 373 | } |
| 374 | |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 375 | bool HasExceptionHandledListeners() const REQUIRES_SHARED(Locks::mutator_lock_) { |
| 376 | return have_exception_handled_listeners_; |
| 377 | } |
| 378 | |
Mythri Alle | ab47488 | 2022-01-17 16:43:04 +0000 | [diff] [blame] | 379 | bool NeedsSlowInterpreterForListeners() const REQUIRES_SHARED(Locks::mutator_lock_) { |
| 380 | return have_field_read_listeners_ || |
| 381 | have_field_write_listeners_ || |
| 382 | have_watched_frame_pop_listeners_ || |
| 383 | have_exception_handled_listeners_; |
Bill Buzbee | fd522f9 | 2016-02-11 22:37:42 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 386 | // Inform listeners that a method has been entered. A dex PC is provided as we may install |
| 387 | // listeners into executing code and get method enter events for methods already on the stack. |
Mythri Alle | 9cc65df | 2021-09-21 15:09:58 +0000 | [diff] [blame] | 388 | void MethodEnterEvent(Thread* thread, ArtMethod* method) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 389 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 74109f6 | 2013-06-07 17:40:09 +0200 | [diff] [blame] | 390 | if (UNLIKELY(HasMethodEntryListeners())) { |
Mythri Alle | 9cc65df | 2021-09-21 15:09:58 +0000 | [diff] [blame] | 391 | MethodEnterEventImpl(thread, method); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | |
| 395 | // Inform listeners that a method has been exited. |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 396 | template<typename T> |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 397 | void MethodExitEvent(Thread* thread, |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 398 | ArtMethod* method, |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 399 | OptionalFrame frame, |
| 400 | T& return_value) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 401 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 74109f6 | 2013-06-07 17:40:09 +0200 | [diff] [blame] | 402 | if (UNLIKELY(HasMethodExitListeners())) { |
Mythri Alle | 18fba4c | 2021-10-27 10:00:55 +0000 | [diff] [blame] | 403 | MethodExitEventImpl(thread, method, frame, return_value); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | |
| 407 | // Inform listeners that a method has been exited due to an exception. |
Vladimir Marko | 19711d4 | 2019-04-12 14:05:34 +0100 | [diff] [blame] | 408 | void MethodUnwindEvent(Thread* thread, |
| 409 | ObjPtr<mirror::Object> this_object, |
| 410 | ArtMethod* method, |
| 411 | uint32_t dex_pc) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 412 | REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 413 | |
| 414 | // Inform listeners that the dex pc has moved (only supported by the interpreter). |
Vladimir Marko | 19711d4 | 2019-04-12 14:05:34 +0100 | [diff] [blame] | 415 | void DexPcMovedEvent(Thread* thread, |
| 416 | ObjPtr<mirror::Object> this_object, |
| 417 | ArtMethod* method, |
| 418 | uint32_t dex_pc) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 419 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 74109f6 | 2013-06-07 17:40:09 +0200 | [diff] [blame] | 420 | if (UNLIKELY(HasDexPcListeners())) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 421 | DexPcMovedEventImpl(thread, this_object, method, dex_pc); |
| 422 | } |
| 423 | } |
| 424 | |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 425 | // Inform listeners that a branch has been taken (only supported by the interpreter). |
| 426 | void Branch(Thread* thread, ArtMethod* method, uint32_t dex_pc, int32_t offset) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 427 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 428 | if (UNLIKELY(HasBranchListeners())) { |
| 429 | BranchImpl(thread, method, dex_pc, offset); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 433 | // Inform listeners that we read a field (only supported by the interpreter). |
Vladimir Marko | 19711d4 | 2019-04-12 14:05:34 +0100 | [diff] [blame] | 434 | void FieldReadEvent(Thread* thread, |
| 435 | ObjPtr<mirror::Object> this_object, |
| 436 | ArtMethod* method, |
| 437 | uint32_t dex_pc, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 438 | ArtField* field) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 439 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 440 | if (UNLIKELY(HasFieldReadListeners())) { |
| 441 | FieldReadEventImpl(thread, this_object, method, dex_pc, field); |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | // Inform listeners that we write a field (only supported by the interpreter). |
Vladimir Marko | 19711d4 | 2019-04-12 14:05:34 +0100 | [diff] [blame] | 446 | void FieldWriteEvent(Thread* thread, |
| 447 | ObjPtr<mirror::Object> this_object, |
| 448 | ArtMethod* method, |
| 449 | uint32_t dex_pc, |
| 450 | ArtField* field, |
| 451 | const JValue& field_value) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 452 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 453 | if (UNLIKELY(HasFieldWriteListeners())) { |
| 454 | FieldWriteEventImpl(thread, this_object, method, dex_pc, field, field_value); |
| 455 | } |
| 456 | } |
| 457 | |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 458 | // 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 Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 466 | // Inform listeners that an exception was thrown. |
Vladimir Marko | 19711d4 | 2019-04-12 14:05:34 +0100 | [diff] [blame] | 467 | void ExceptionThrownEvent(Thread* thread, ObjPtr<mirror::Throwable> exception_object) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 468 | REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 469 | |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 470 | // 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. |
Vladimir Marko | 19711d4 | 2019-04-12 14:05:34 +0100 | [diff] [blame] | 472 | void ExceptionHandledEvent(Thread* thread, ObjPtr<mirror::Throwable> exception_object) const |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 473 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 474 | |
Mythri Alle | 5097f83 | 2021-11-02 14:52:30 +0000 | [diff] [blame] | 475 | JValue GetReturnValue(Thread* self, |
| 476 | ArtMethod* method, |
| 477 | bool* is_ref, |
| 478 | uint64_t* gpr_result, |
| 479 | uint64_t* fpr_result) REQUIRES_SHARED(Locks::mutator_lock_); |
| 480 | bool ShouldDeoptimizeMethod(Thread* self, const NthCallerVisitor& visitor) |
| 481 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 482 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 483 | // Called when an instrumented method is entered. The intended link register (lr) is saved so |
| 484 | // that returning causes a branch to the method exit stub. Generates method enter events. |
Vladimir Marko | 19711d4 | 2019-04-12 14:05:34 +0100 | [diff] [blame] | 485 | void PushInstrumentationStackFrame(Thread* self, |
| 486 | ObjPtr<mirror::Object> this_object, |
| 487 | ArtMethod* method, |
Nicolas Geoffray | e91e795 | 2020-01-23 10:15:56 +0000 | [diff] [blame] | 488 | uintptr_t stack_pointer, |
Vladimir Marko | 19711d4 | 2019-04-12 14:05:34 +0100 | [diff] [blame] | 489 | uintptr_t lr, |
Jeff Hao | 9a916d3 | 2013-06-27 18:45:37 -0700 | [diff] [blame] | 490 | bool interpreter_entry) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 491 | REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 492 | |
Mingyao Yang | 2ee1790 | 2017-08-30 11:37:08 -0700 | [diff] [blame] | 493 | DeoptimizationMethodType GetDeoptimizationMethodType(ArtMethod* method) |
| 494 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 495 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 496 | // Called when an instrumented method is exited. Removes the pushed instrumentation frame |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 497 | // returning the intended link register. Generates method exit events. The gpr_result and |
| 498 | // fpr_result pointers are pointers to the locations where the integer/pointer and floating point |
| 499 | // result values of the function are stored. Both pointers must always be valid but the values |
| 500 | // held there will only be meaningful if interpreted as the appropriate type given the function |
| 501 | // being returned from. |
Nicolas Geoffray | e91e795 | 2020-01-23 10:15:56 +0000 | [diff] [blame] | 502 | TwoWordReturn PopInstrumentationStackFrame(Thread* self, |
| 503 | uintptr_t* return_pc_addr, |
| 504 | uint64_t* gpr_result, |
| 505 | uint64_t* fpr_result) |
Andreas Gampe | 7e56a07 | 2018-11-29 10:40:06 -0800 | [diff] [blame] | 506 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!GetDeoptimizedMethodsLock()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 507 | |
Alex Light | 2c8206f | 2018-06-08 14:51:09 -0700 | [diff] [blame] | 508 | // Pops nframes instrumentation frames from the current thread. Returns the return pc for the last |
| 509 | // instrumentation frame that's popped. |
Nicolas Geoffray | e91e795 | 2020-01-23 10:15:56 +0000 | [diff] [blame] | 510 | uintptr_t PopFramesForDeoptimization(Thread* self, uintptr_t stack_pointer) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 511 | REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 512 | |
| 513 | // Call back for configure stubs. |
Vladimir Marko | 19711d4 | 2019-04-12 14:05:34 +0100 | [diff] [blame] | 514 | void InstallStubsForClass(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) |
Andreas Gampe | 7e56a07 | 2018-11-29 10:40:06 -0800 | [diff] [blame] | 515 | REQUIRES(!GetDeoptimizedMethodsLock()); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 516 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 517 | void InstallStubsForMethod(ArtMethod* method) |
Andreas Gampe | 7e56a07 | 2018-11-29 10:40:06 -0800 | [diff] [blame] | 518 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!GetDeoptimizedMethodsLock()); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 519 | |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 520 | // Sets up instrumentation to allow single thread deoptimization using ForceInterpreterCount. |
Nicolas Geoffray | e91532e | 2021-12-10 09:52:18 +0000 | [diff] [blame] | 521 | void EnableSingleThreadDeopt(const char* key) |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 522 | REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_) |
| 523 | REQUIRES(!Locks::thread_list_lock_, |
| 524 | !Locks::classlinker_classes_lock_, |
| 525 | !GetDeoptimizedMethodsLock()); |
| 526 | |
Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 527 | // Install instrumentation exit stub on every method of the stack of the given thread. |
Mythri Alle | 5097f83 | 2021-11-02 14:52:30 +0000 | [diff] [blame] | 528 | // This is used by: |
| 529 | // - the debugger to cause a deoptimization of the all frames in thread's stack (for |
| 530 | // example, after updating local variables) |
| 531 | // - to call method entry / exit hooks for tracing. For this we instrument |
| 532 | // the stack frame to run entry / exit hooks but we don't need to deoptimize. |
| 533 | // deopt_all_frames indicates whether the frames need to deoptimize or not. |
| 534 | void InstrumentThreadStack(Thread* thread, bool deopt_all_frames) REQUIRES(Locks::mutator_lock_); |
Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 535 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 536 | // Force all currently running frames to be deoptimized back to interpreter. This should only be |
| 537 | // used in cases where basically all compiled code has been invalidated. |
| 538 | void DeoptimizeAllThreadFrames() REQUIRES(art::Locks::mutator_lock_); |
| 539 | |
Sebastien Hertz | b2feaaf | 2015-10-12 13:40:10 +0000 | [diff] [blame] | 540 | static size_t ComputeFrameId(Thread* self, |
| 541 | size_t frame_depth, |
| 542 | size_t inlined_frames_before_frame) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 543 | REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | b2feaaf | 2015-10-12 13:40:10 +0000 | [diff] [blame] | 544 | |
Mathieu Chartier | eebc3af | 2016-02-29 18:13:38 -0800 | [diff] [blame] | 545 | // Does not hold lock, used to check if someone changed from not instrumented to instrumented |
| 546 | // during a GC suspend point. |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 547 | bool AllocEntrypointsInstrumented() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 50e9331 | 2016-03-16 11:25:29 -0700 | [diff] [blame] | 548 | return alloc_entrypoints_instrumented_; |
Mathieu Chartier | eebc3af | 2016-02-29 18:13:38 -0800 | [diff] [blame] | 549 | } |
| 550 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 551 | InstrumentationLevel GetCurrentInstrumentationLevel() const; |
| 552 | |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 553 | private: |
| 554 | // Returns true if moving to the given instrumentation level requires the installation of stubs. |
| 555 | // False otherwise. |
| 556 | bool RequiresInstrumentationInstallation(InstrumentationLevel new_level) const; |
| 557 | |
Mythri Alle | 5097f83 | 2021-11-02 14:52:30 +0000 | [diff] [blame] | 558 | // Returns true if we need entry exit stub to call entry hooks. JITed code |
| 559 | // directly call entry / exit hooks and don't need the stub. |
Nicolas Geoffray | c8a694d | 2022-01-17 17:12:38 +0000 | [diff] [blame] | 560 | static bool CodeNeedsEntryExitStub(const void* code, ArtMethod* method) |
| 561 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mythri Alle | 5097f83 | 2021-11-02 14:52:30 +0000 | [diff] [blame] | 562 | |
Mythri Alle | 519ff8b | 2021-11-17 13:47:07 +0000 | [diff] [blame] | 563 | // Update the current instrumentation_level_. |
| 564 | void UpdateInstrumentationLevel(InstrumentationLevel level); |
Mythri Alle | 9575c12 | 2021-11-12 12:04:41 +0000 | [diff] [blame] | 565 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 566 | // Does the job of installing or removing instrumentation code within methods. |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 567 | // In order to support multiple clients using instrumentation at the same time, |
| 568 | // the caller must pass a unique key (a string) identifying it so we remind which |
| 569 | // instrumentation level it needs. Therefore the current instrumentation level |
| 570 | // becomes the highest instrumentation level required by a client. |
| 571 | void ConfigureStubs(const char* key, InstrumentationLevel desired_instrumentation_level) |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 572 | REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_) |
Andreas Gampe | 7e56a07 | 2018-11-29 10:40:06 -0800 | [diff] [blame] | 573 | REQUIRES(!GetDeoptimizedMethodsLock(), |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 574 | !Locks::thread_list_lock_, |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 575 | !Locks::classlinker_classes_lock_); |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 576 | void UpdateStubs() REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_) |
| 577 | REQUIRES(!GetDeoptimizedMethodsLock(), |
| 578 | !Locks::thread_list_lock_, |
| 579 | !Locks::classlinker_classes_lock_); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 580 | |
Sebastien Hertz | ee1997a | 2013-09-19 14:47:09 +0200 | [diff] [blame] | 581 | |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 582 | // No thread safety analysis to get around SetQuickAllocEntryPointsInstrumented requiring |
| 583 | // exclusive access to mutator lock which you can't get if the runtime isn't started. |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 584 | void SetEntrypointsInstrumented(bool instrumented) NO_THREAD_SAFETY_ANALYSIS; |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 585 | |
Mythri Alle | 9cc65df | 2021-09-21 15:09:58 +0000 | [diff] [blame] | 586 | void MethodEnterEventImpl(Thread* thread, ArtMethod* method) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 587 | REQUIRES_SHARED(Locks::mutator_lock_); |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 588 | template <typename T> |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 589 | void MethodExitEventImpl(Thread* thread, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 590 | ArtMethod* method, |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 591 | OptionalFrame frame, |
| 592 | T& return_value) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 593 | REQUIRES_SHARED(Locks::mutator_lock_); |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 594 | void DexPcMovedEventImpl(Thread* thread, |
| 595 | ObjPtr<mirror::Object> this_object, |
| 596 | ArtMethod* method, |
| 597 | uint32_t dex_pc) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 598 | REQUIRES_SHARED(Locks::mutator_lock_); |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 599 | void BranchImpl(Thread* thread, ArtMethod* method, uint32_t dex_pc, int32_t offset) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 600 | REQUIRES_SHARED(Locks::mutator_lock_); |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 601 | void WatchedFramePopImpl(Thread* thread, const ShadowFrame& frame) const |
| 602 | REQUIRES_SHARED(Locks::mutator_lock_); |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 603 | void FieldReadEventImpl(Thread* thread, |
| 604 | ObjPtr<mirror::Object> this_object, |
| 605 | ArtMethod* method, |
| 606 | uint32_t dex_pc, |
| 607 | ArtField* field) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 608 | REQUIRES_SHARED(Locks::mutator_lock_); |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 609 | void FieldWriteEventImpl(Thread* thread, |
| 610 | ObjPtr<mirror::Object> this_object, |
| 611 | ArtMethod* method, |
| 612 | uint32_t dex_pc, |
| 613 | ArtField* field, |
| 614 | const JValue& field_value) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 615 | REQUIRES_SHARED(Locks::mutator_lock_); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 616 | |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 617 | // Read barrier-aware utility functions for accessing deoptimized_methods_ |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 618 | bool AddDeoptimizedMethod(ArtMethod* method) |
Andreas Gampe | 7e56a07 | 2018-11-29 10:40:06 -0800 | [diff] [blame] | 619 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(GetDeoptimizedMethodsLock()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 620 | bool IsDeoptimizedMethod(ArtMethod* method) |
Andreas Gampe | 7e56a07 | 2018-11-29 10:40:06 -0800 | [diff] [blame] | 621 | REQUIRES_SHARED(Locks::mutator_lock_, GetDeoptimizedMethodsLock()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 622 | bool RemoveDeoptimizedMethod(ArtMethod* method) |
Andreas Gampe | 7e56a07 | 2018-11-29 10:40:06 -0800 | [diff] [blame] | 623 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(GetDeoptimizedMethodsLock()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 624 | ArtMethod* BeginDeoptimizedMethod() |
Andreas Gampe | 7e56a07 | 2018-11-29 10:40:06 -0800 | [diff] [blame] | 625 | REQUIRES_SHARED(Locks::mutator_lock_, GetDeoptimizedMethodsLock()); |
Mythri Alle | ab47488 | 2022-01-17 16:43:04 +0000 | [diff] [blame] | 626 | bool IsDeoptimizedMethodsEmptyLocked() const |
Andreas Gampe | 7e56a07 | 2018-11-29 10:40:06 -0800 | [diff] [blame] | 627 | REQUIRES_SHARED(Locks::mutator_lock_, GetDeoptimizedMethodsLock()); |
Nicolas Geoffray | 854af03 | 2021-12-21 08:32:42 +0000 | [diff] [blame] | 628 | void UpdateMethodsCodeImpl(ArtMethod* method, const void* new_code) |
Andreas Gampe | 7e56a07 | 2018-11-29 10:40:06 -0800 | [diff] [blame] | 629 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!GetDeoptimizedMethodsLock()); |
Mingyao Yang | 3fd448a | 2016-05-10 14:30:41 -0700 | [diff] [blame] | 630 | |
Andreas Gampe | 7e56a07 | 2018-11-29 10:40:06 -0800 | [diff] [blame] | 631 | ReaderWriterMutex* GetDeoptimizedMethodsLock() const { |
| 632 | return deoptimized_methods_lock_.get(); |
| 633 | } |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 634 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 635 | // A counter that's incremented every time a DeoptimizeAllFrames. We check each |
| 636 | // InstrumentationStackFrames creation id against this number and if they differ we deopt even if |
| 637 | // we could otherwise continue running. |
| 638 | uint64_t current_force_deopt_id_ GUARDED_BY(Locks::mutator_lock_); |
| 639 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 640 | // Have we hijacked ArtMethod::code_ so that it calls instrumentation/interpreter code? |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 641 | bool instrumentation_stubs_installed_; |
| 642 | |
Mythri Alle | 9575c12 | 2021-11-12 12:04:41 +0000 | [diff] [blame] | 643 | // The required level of instrumentation. This could be one of the following values: |
| 644 | // kInstrumentNothing: no instrumentation support is needed |
| 645 | // kInstrumentWithInstrumentationStubs: needs support to call method entry/exit stubs. |
| 646 | // kInstrumentWithInterpreter: only execute with interpreter |
| 647 | Instrumentation::InstrumentationLevel instrumentation_level_; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 648 | |
| 649 | // Did the runtime request we only run in the interpreter? ie -Xint mode. |
| 650 | bool forced_interpret_only_; |
| 651 | |
| 652 | // Do we have any listeners for method entry events? Short-cut to avoid taking the |
| 653 | // instrumentation_lock_. |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 654 | bool have_method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 655 | |
| 656 | // Do we have any listeners for method exit events? Short-cut to avoid taking the |
| 657 | // instrumentation_lock_. |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 658 | bool have_method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 659 | |
| 660 | // Do we have any listeners for method unwind events? Short-cut to avoid taking the |
| 661 | // instrumentation_lock_. |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 662 | bool have_method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 663 | |
| 664 | // Do we have any listeners for dex move events? Short-cut to avoid taking the |
| 665 | // instrumentation_lock_. |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 666 | bool have_dex_pc_listeners_ GUARDED_BY(Locks::mutator_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 667 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 668 | // Do we have any listeners for field read events? Short-cut to avoid taking the |
| 669 | // instrumentation_lock_. |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 670 | bool have_field_read_listeners_ GUARDED_BY(Locks::mutator_lock_); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 671 | |
| 672 | // Do we have any listeners for field write events? Short-cut to avoid taking the |
| 673 | // instrumentation_lock_. |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 674 | bool have_field_write_listeners_ GUARDED_BY(Locks::mutator_lock_); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 675 | |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 676 | // Do we have any exception thrown listeners? Short-cut to avoid taking the instrumentation_lock_. |
| 677 | bool have_exception_thrown_listeners_ GUARDED_BY(Locks::mutator_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 678 | |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 679 | // Do we have any frame pop listeners? Short-cut to avoid taking the instrumentation_lock_. |
| 680 | bool have_watched_frame_pop_listeners_ GUARDED_BY(Locks::mutator_lock_); |
| 681 | |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 682 | // Do we have any branch listeners? Short-cut to avoid taking the instrumentation_lock_. |
| 683 | bool have_branch_listeners_ GUARDED_BY(Locks::mutator_lock_); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 684 | |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 685 | // Do we have any exception handled listeners? Short-cut to avoid taking the |
| 686 | // instrumentation_lock_. |
| 687 | bool have_exception_handled_listeners_ GUARDED_BY(Locks::mutator_lock_); |
| 688 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 689 | // Contains the instrumentation level required by each client of the instrumentation identified |
| 690 | // by a string key. |
Vladimir Marko | 4f99071 | 2021-07-14 12:45:13 +0100 | [diff] [blame] | 691 | using InstrumentationLevelTable = SafeMap<const char*, InstrumentationLevel>; |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 692 | InstrumentationLevelTable requested_instrumentation_levels_ GUARDED_BY(Locks::mutator_lock_); |
| 693 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 694 | // The event listeners, written to with the mutator_lock_ exclusively held. |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 695 | // Mutators must be able to iterate over these lists concurrently, that is, with listeners being |
| 696 | // added or removed while iterating. The modifying thread holds exclusive lock, |
| 697 | // so other threads cannot iterate (i.e. read the data of the list) at the same time but they |
| 698 | // do keep iterators that need to remain valid. This is the reason these listeners are std::list |
| 699 | // and not for example std::vector: the existing storage for a std::list does not move. |
| 700 | // Note that mutators cannot make a copy of these lists before iterating, as the instrumentation |
| 701 | // listeners can also be deleted concurrently. |
| 702 | // As a result, these lists are never trimmed. That's acceptable given the low number of |
| 703 | // listeners we have. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 704 | std::list<InstrumentationListener*> method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_); |
| 705 | std::list<InstrumentationListener*> method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_); |
| 706 | std::list<InstrumentationListener*> method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_); |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 707 | std::list<InstrumentationListener*> branch_listeners_ GUARDED_BY(Locks::mutator_lock_); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 708 | std::list<InstrumentationListener*> dex_pc_listeners_ GUARDED_BY(Locks::mutator_lock_); |
| 709 | std::list<InstrumentationListener*> field_read_listeners_ GUARDED_BY(Locks::mutator_lock_); |
| 710 | std::list<InstrumentationListener*> field_write_listeners_ GUARDED_BY(Locks::mutator_lock_); |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 711 | std::list<InstrumentationListener*> exception_thrown_listeners_ GUARDED_BY(Locks::mutator_lock_); |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 712 | std::list<InstrumentationListener*> watched_frame_pop_listeners_ GUARDED_BY(Locks::mutator_lock_); |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 713 | std::list<InstrumentationListener*> exception_handled_listeners_ GUARDED_BY(Locks::mutator_lock_); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 714 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 715 | // The set of methods being deoptimized (by the debugger) which must be executed with interpreter |
| 716 | // only. |
Andreas Gampe | 7e56a07 | 2018-11-29 10:40:06 -0800 | [diff] [blame] | 717 | mutable std::unique_ptr<ReaderWriterMutex> deoptimized_methods_lock_ BOTTOM_MUTEX_ACQUIRED_AFTER; |
| 718 | std::unordered_set<ArtMethod*> deoptimized_methods_ GUARDED_BY(GetDeoptimizedMethodsLock()); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 719 | |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 720 | // Current interpreter handler table. This is updated each time the thread state flags are |
| 721 | // modified. |
Sebastien Hertz | ee1997a | 2013-09-19 14:47:09 +0200 | [diff] [blame] | 722 | |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 723 | // Greater than 0 if quick alloc entry points instrumented. |
Mathieu Chartier | eebc3af | 2016-02-29 18:13:38 -0800 | [diff] [blame] | 724 | size_t quick_alloc_entry_points_instrumentation_counter_; |
Mathieu Chartier | 50e9331 | 2016-03-16 11:25:29 -0700 | [diff] [blame] | 725 | |
| 726 | // alloc_entrypoints_instrumented_ is only updated with all the threads suspended, this is done |
| 727 | // to prevent races with the GC where the GC relies on thread suspension only see |
| 728 | // alloc_entrypoints_instrumented_ change during suspend points. |
| 729 | bool alloc_entrypoints_instrumented_; |
| 730 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 731 | friend class InstrumentationTest; // For GetCurrentInstrumentationLevel and ConfigureStubs. |
Alex Light | 2c8206f | 2018-06-08 14:51:09 -0700 | [diff] [blame] | 732 | friend class InstrumentationStackPopper; // For popping instrumentation frames. |
Mythri Alle | 5097f83 | 2021-11-02 14:52:30 +0000 | [diff] [blame] | 733 | friend void InstrumentationInstallStack(Thread*, void*, bool); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 734 | |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 735 | DISALLOW_COPY_AND_ASSIGN(Instrumentation); |
| 736 | }; |
Vladimir Marko | 9974e3c | 2020-06-10 16:27:06 +0100 | [diff] [blame] | 737 | std::ostream& operator<<(std::ostream& os, Instrumentation::InstrumentationEvent rhs); |
| 738 | std::ostream& operator<<(std::ostream& os, Instrumentation::InstrumentationLevel rhs); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 739 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 740 | // An element in the instrumentation side stack maintained in art::Thread. |
| 741 | struct InstrumentationStackFrame { |
Mingyao Yang | 2ee1790 | 2017-08-30 11:37:08 -0700 | [diff] [blame] | 742 | InstrumentationStackFrame(mirror::Object* this_object, |
| 743 | ArtMethod* method, |
| 744 | uintptr_t return_pc, |
| 745 | size_t frame_id, |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 746 | bool interpreter_entry, |
| 747 | uint64_t force_deopt_id) |
Mingyao Yang | 2ee1790 | 2017-08-30 11:37:08 -0700 | [diff] [blame] | 748 | : this_object_(this_object), |
| 749 | method_(method), |
| 750 | return_pc_(return_pc), |
| 751 | frame_id_(frame_id), |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 752 | interpreter_entry_(interpreter_entry), |
| 753 | force_deopt_id_(force_deopt_id) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 754 | } |
| 755 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 756 | std::string Dump() const REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 757 | |
| 758 | mirror::Object* this_object_; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 759 | ArtMethod* method_; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 760 | uintptr_t return_pc_; |
| 761 | size_t frame_id_; |
| 762 | bool interpreter_entry_; |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 763 | uint64_t force_deopt_id_; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 764 | }; |
| 765 | |
| 766 | } // namespace instrumentation |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 767 | } // namespace art |
| 768 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 769 | #endif // ART_RUNTIME_INSTRUMENTATION_H_ |