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 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 20 | #include "atomic.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 21 | #include "base/macros.h" |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 22 | #include "base/mutex.h" |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 23 | #include "object_callbacks.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 24 | |
| 25 | #include <stdint.h> |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 26 | #include <set> |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 27 | #include <list> |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 28 | |
| 29 | namespace art { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 30 | namespace mirror { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 31 | class ArtMethod; |
| 32 | class Class; |
| 33 | class Object; |
| 34 | class Throwable; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 35 | } // namespace mirror |
| 36 | union JValue; |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 37 | class Thread; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 38 | class ThrowLocation; |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 39 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 40 | namespace instrumentation { |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 41 | |
Sebastien Hertz | ee1997a | 2013-09-19 14:47:09 +0200 | [diff] [blame] | 42 | // Interpreter handler tables. |
| 43 | enum InterpreterHandlerTable { |
| 44 | kMainHandlerTable = 0, // Main handler table: no suspend check, no instrumentation. |
| 45 | kAlternativeHandlerTable = 1, // Alternative handler table: suspend check and/or instrumentation |
| 46 | // enabled. |
| 47 | kNumHandlerTables |
| 48 | }; |
| 49 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 50 | // Instrumentation event listener API. Registered listeners will get the appropriate call back for |
| 51 | // the events they are listening for. The call backs supply the thread, method and dex_pc the event |
| 52 | // occurred upon. The thread may or may not be Thread::Current(). |
| 53 | struct InstrumentationListener { |
| 54 | InstrumentationListener() {} |
| 55 | virtual ~InstrumentationListener() {} |
| 56 | |
| 57 | // Call-back for when a method is entered. |
| 58 | virtual void MethodEntered(Thread* thread, mirror::Object* this_object, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 59 | mirror::ArtMethod* method, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 60 | uint32_t dex_pc) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0; |
| 61 | |
| 62 | // Call-back for when a method is exited. |
| 63 | // TODO: its likely passing the return value would be useful, however, we may need to get and |
| 64 | // parse the shorty to determine what kind of register holds the result. |
| 65 | virtual void MethodExited(Thread* thread, mirror::Object* this_object, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 66 | mirror::ArtMethod* method, uint32_t dex_pc, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 67 | const JValue& return_value) |
| 68 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0; |
| 69 | |
| 70 | // Call-back for when a method is popped due to an exception throw. A method will either cause a |
| 71 | // MethodExited call-back or a MethodUnwind call-back when its activation is removed. |
Sebastien Hertz | 51db44a | 2013-11-19 10:00:29 +0100 | [diff] [blame] | 72 | virtual void MethodUnwind(Thread* thread, mirror::Object* this_object, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 73 | mirror::ArtMethod* method, uint32_t dex_pc) |
Sebastien Hertz | 51db44a | 2013-11-19 10:00:29 +0100 | [diff] [blame] | 74 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 75 | |
| 76 | // Call-back for when the dex pc moves in a method. |
| 77 | virtual void DexPcMoved(Thread* thread, mirror::Object* this_object, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 78 | mirror::ArtMethod* method, uint32_t new_dex_pc) |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 79 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0; |
| 80 | |
| 81 | // Call-back when an exception is caught. |
| 82 | virtual void ExceptionCaught(Thread* thread, const ThrowLocation& throw_location, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 83 | mirror::ArtMethod* catch_method, uint32_t catch_dex_pc, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 84 | mirror::Throwable* exception_object) |
| 85 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0; |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 86 | }; |
| 87 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 88 | // Instrumentation is a catch-all for when extra information is required from the runtime. The |
| 89 | // typical use for instrumentation is for profiling and debugging. Instrumentation may add stubs |
| 90 | // to method entry and exit, it may also force execution to be switched to the interpreter and |
| 91 | // trigger deoptimization. |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 92 | class Instrumentation { |
| 93 | public: |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 94 | enum InstrumentationEvent { |
| 95 | kMethodEntered = 1, |
| 96 | kMethodExited = 2, |
| 97 | kMethodUnwind = 4, |
| 98 | kDexPcMoved = 8, |
| 99 | kExceptionCaught = 16 |
| 100 | }; |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 101 | |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 102 | Instrumentation(); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 103 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 104 | // Add a listener to be notified of the masked together sent of instrumentation events. This |
| 105 | // suspend the runtime to install stubs. You are expected to hold the mutator lock as a proxy |
| 106 | // for saying you should have suspended all threads (installing stubs while threads are running |
| 107 | // will break). |
| 108 | void AddListener(InstrumentationListener* listener, uint32_t events) |
| 109 | EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 110 | LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 111 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 112 | // Removes a listener possibly removing instrumentation stubs. |
| 113 | void RemoveListener(InstrumentationListener* listener, uint32_t events) |
| 114 | EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 115 | LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 116 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 117 | // Deoptimization. |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 118 | void EnableDeoptimization() |
| 119 | EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_) |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 120 | LOCKS_EXCLUDED(deoptimized_methods_lock_); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 121 | void DisableDeoptimization() |
| 122 | EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_) |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 123 | LOCKS_EXCLUDED(deoptimized_methods_lock_); |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 124 | bool AreAllMethodsDeoptimized() const { |
| 125 | return interpreter_stubs_installed_; |
| 126 | } |
Sebastien Hertz | 11d40c2 | 2014-02-19 18:00:17 +0100 | [diff] [blame] | 127 | bool ShouldNotifyMethodEnterExitEvents() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 128 | |
| 129 | // Executes everything with interpreter. |
| 130 | void DeoptimizeEverything() |
| 131 | EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 132 | LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_); |
| 133 | |
| 134 | // Executes everything with compiled code (or interpreter if there is no code). |
| 135 | void UndeoptimizeEverything() |
| 136 | EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 137 | LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_); |
| 138 | |
| 139 | // Deoptimize a method by forcing its execution with the interpreter. Nevertheless, a static |
| 140 | // method (except a class initializer) set to the resolution trampoline will be deoptimized only |
| 141 | // once its declaring class is initialized. |
| 142 | void Deoptimize(mirror::ArtMethod* method) |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 143 | LOCKS_EXCLUDED(Locks::thread_list_lock_, deoptimized_methods_lock_) |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 144 | EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 145 | |
| 146 | // Undeoptimze the method by restoring its entrypoints. Nevertheless, a static method |
| 147 | // (except a class initializer) set to the resolution trampoline will be updated only once its |
| 148 | // declaring class is initialized. |
| 149 | void Undeoptimize(mirror::ArtMethod* method) |
Sebastien Hertz | 4d25df3 | 2014-03-21 17:44:46 +0100 | [diff] [blame] | 150 | LOCKS_EXCLUDED(Locks::thread_list_lock_, deoptimized_methods_lock_) |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 151 | EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 152 | |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 153 | bool IsDeoptimized(mirror::ArtMethod* method) const LOCKS_EXCLUDED(deoptimized_methods_lock_); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 154 | |
| 155 | // Enable method tracing by installing instrumentation entry/exit stubs. |
| 156 | void EnableMethodTracing() |
| 157 | EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 158 | LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_); |
| 159 | |
| 160 | // Disable method tracing by uninstalling instrumentation entry/exit stubs. |
| 161 | void DisableMethodTracing() |
| 162 | EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 163 | LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_); |
| 164 | |
Sebastien Hertz | ee1997a | 2013-09-19 14:47:09 +0200 | [diff] [blame] | 165 | InterpreterHandlerTable GetInterpreterHandlerTable() const { |
| 166 | return interpreter_handler_table_; |
| 167 | } |
| 168 | |
Mathieu Chartier | d889178 | 2014-03-02 13:28:37 -0800 | [diff] [blame] | 169 | void InstrumentQuickAllocEntryPoints() LOCKS_EXCLUDED(Locks::thread_list_lock_, |
| 170 | Locks::runtime_shutdown_lock_); |
| 171 | void UninstrumentQuickAllocEntryPoints() LOCKS_EXCLUDED(Locks::thread_list_lock_, |
| 172 | Locks::runtime_shutdown_lock_); |
| 173 | void ResetQuickAllocEntryPoints() EXCLUSIVE_LOCKS_REQUIRED(Locks::runtime_shutdown_lock_); |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 174 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 175 | // Update the code of a method respecting any installed stubs. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 176 | void UpdateMethodsCode(mirror::ArtMethod* method, const void* quick_code, |
| 177 | const void* portable_code, bool have_portable_code) const |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 178 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 179 | |
| 180 | // Get the quick code for the given method. More efficient than asking the class linker as it |
| 181 | // will short-cut to GetCode if instrumentation and static method resolution stubs aren't |
| 182 | // installed. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 183 | const void* GetQuickCodeFor(mirror::ArtMethod* method) const |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 184 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 185 | |
| 186 | void ForceInterpretOnly() { |
| 187 | interpret_only_ = true; |
| 188 | forced_interpret_only_ = true; |
| 189 | } |
| 190 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 191 | // Called by ArtMethod::Invoke to determine dispatch mechanism. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 192 | bool InterpretOnly() const { |
| 193 | return interpret_only_; |
| 194 | } |
| 195 | |
Hiroshi Yamauchi | 563b47c | 2014-02-28 17:18:37 -0800 | [diff] [blame] | 196 | bool IsForcedInterpretOnly() const { |
| 197 | return forced_interpret_only_; |
| 198 | } |
| 199 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 200 | bool ShouldPortableCodeDeoptimize() const { |
| 201 | return instrumentation_stubs_installed_; |
| 202 | } |
| 203 | |
| 204 | bool AreExitStubsInstalled() const { |
| 205 | return instrumentation_stubs_installed_; |
| 206 | } |
| 207 | |
Sebastien Hertz | 74109f6 | 2013-06-07 17:40:09 +0200 | [diff] [blame] | 208 | bool HasMethodEntryListeners() const { |
| 209 | return have_method_entry_listeners_; |
| 210 | } |
| 211 | |
| 212 | bool HasMethodExitListeners() const { |
| 213 | return have_method_exit_listeners_; |
| 214 | } |
| 215 | |
| 216 | bool HasDexPcListeners() const { |
| 217 | return have_dex_pc_listeners_; |
| 218 | } |
| 219 | |
Sebastien Hertz | ee1997a | 2013-09-19 14:47:09 +0200 | [diff] [blame] | 220 | bool IsActive() const { |
| 221 | return have_dex_pc_listeners_ || have_method_entry_listeners_ || have_method_exit_listeners_ || |
| 222 | have_exception_caught_listeners_ || have_method_unwind_listeners_; |
| 223 | } |
| 224 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 225 | // Inform listeners that a method has been entered. A dex PC is provided as we may install |
| 226 | // listeners into executing code and get method enter events for methods already on the stack. |
| 227 | void MethodEnterEvent(Thread* thread, mirror::Object* this_object, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 228 | mirror::ArtMethod* method, uint32_t dex_pc) const |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 229 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | 74109f6 | 2013-06-07 17:40:09 +0200 | [diff] [blame] | 230 | if (UNLIKELY(HasMethodEntryListeners())) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 231 | MethodEnterEventImpl(thread, this_object, method, dex_pc); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | // Inform listeners that a method has been exited. |
| 236 | void MethodExitEvent(Thread* thread, mirror::Object* this_object, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 237 | mirror::ArtMethod* method, uint32_t dex_pc, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 238 | const JValue& return_value) const |
| 239 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | 74109f6 | 2013-06-07 17:40:09 +0200 | [diff] [blame] | 240 | if (UNLIKELY(HasMethodExitListeners())) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 241 | MethodExitEventImpl(thread, this_object, method, dex_pc, return_value); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | // Inform listeners that a method has been exited due to an exception. |
| 246 | void MethodUnwindEvent(Thread* thread, mirror::Object* this_object, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 247 | mirror::ArtMethod* method, uint32_t dex_pc) const |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 248 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 249 | |
| 250 | // Inform listeners that the dex pc has moved (only supported by the interpreter). |
| 251 | void DexPcMovedEvent(Thread* thread, mirror::Object* this_object, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 252 | mirror::ArtMethod* method, uint32_t dex_pc) const |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 253 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | 74109f6 | 2013-06-07 17:40:09 +0200 | [diff] [blame] | 254 | if (UNLIKELY(HasDexPcListeners())) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 255 | DexPcMovedEventImpl(thread, this_object, method, dex_pc); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | // Inform listeners that an exception was caught. |
| 260 | void ExceptionCaughtEvent(Thread* thread, const ThrowLocation& throw_location, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 261 | mirror::ArtMethod* catch_method, uint32_t catch_dex_pc, |
Sebastien Hertz | 947ff08 | 2013-09-17 14:10:13 +0200 | [diff] [blame] | 262 | mirror::Throwable* exception_object) const |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 263 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 264 | |
| 265 | // Called when an instrumented method is entered. The intended link register (lr) is saved so |
| 266 | // that returning causes a branch to the method exit stub. Generates method enter events. |
| 267 | void PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 268 | mirror::ArtMethod* method, uintptr_t lr, |
Jeff Hao | 9a916d3 | 2013-06-27 18:45:37 -0700 | [diff] [blame] | 269 | bool interpreter_entry) |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 270 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 271 | |
| 272 | // Called when an instrumented method is exited. Removes the pushed instrumentation frame |
| 273 | // returning the intended link register. Generates method exit events. |
| 274 | uint64_t PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc, uint64_t gpr_result, |
| 275 | uint64_t fpr_result) |
| 276 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 277 | |
| 278 | // Pops an instrumentation frame from the current thread and generate an unwind event. |
| 279 | void PopMethodForUnwind(Thread* self, bool is_deoptimization) const |
| 280 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 281 | |
| 282 | // Call back for configure stubs. |
| 283 | bool InstallStubsForClass(mirror::Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 284 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 285 | void InstallStubsForMethod(mirror::ArtMethod* method) |
| 286 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 287 | |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 288 | void VisitRoots(RootCallback* callback, void* arg) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 289 | LOCKS_EXCLUDED(deoptimized_methods_lock_); |
| 290 | |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 291 | private: |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 292 | // Does the job of installing or removing instrumentation code within methods. |
| 293 | void ConfigureStubs(bool require_entry_exit_stubs, bool require_interpreter) |
| 294 | EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_) |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 295 | LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::classlinker_classes_lock_, |
| 296 | deoptimized_methods_lock_); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 297 | |
Sebastien Hertz | ee1997a | 2013-09-19 14:47:09 +0200 | [diff] [blame] | 298 | void UpdateInterpreterHandlerTable() { |
| 299 | interpreter_handler_table_ = IsActive() ? kAlternativeHandlerTable : kMainHandlerTable; |
| 300 | } |
| 301 | |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 302 | // No thread safety analysis to get around SetQuickAllocEntryPointsInstrumented requiring |
| 303 | // exclusive access to mutator lock which you can't get if the runtime isn't started. |
| 304 | void SetEntrypointsInstrumented(bool instrumented) NO_THREAD_SAFETY_ANALYSIS; |
| 305 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 306 | void MethodEnterEventImpl(Thread* thread, mirror::Object* this_object, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 307 | mirror::ArtMethod* method, uint32_t dex_pc) const |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 308 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 309 | void MethodExitEventImpl(Thread* thread, mirror::Object* this_object, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 310 | mirror::ArtMethod* method, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 311 | uint32_t dex_pc, const JValue& return_value) const |
| 312 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 313 | void DexPcMovedEventImpl(Thread* thread, mirror::Object* this_object, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 314 | mirror::ArtMethod* method, uint32_t dex_pc) const |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 315 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 316 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 317 | // Have we hijacked ArtMethod::code_ so that it calls instrumentation/interpreter code? |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 318 | bool instrumentation_stubs_installed_; |
| 319 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 320 | // Have we hijacked ArtMethod::code_ to reference the enter/exit stubs? |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 321 | bool entry_exit_stubs_installed_; |
| 322 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 323 | // Have we hijacked ArtMethod::code_ to reference the enter interpreter stub? |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 324 | bool interpreter_stubs_installed_; |
| 325 | |
| 326 | // Do we need the fidelity of events that we only get from running within the interpreter? |
| 327 | bool interpret_only_; |
| 328 | |
| 329 | // Did the runtime request we only run in the interpreter? ie -Xint mode. |
| 330 | bool forced_interpret_only_; |
| 331 | |
| 332 | // Do we have any listeners for method entry events? Short-cut to avoid taking the |
| 333 | // instrumentation_lock_. |
| 334 | bool have_method_entry_listeners_; |
| 335 | |
| 336 | // Do we have any listeners for method exit events? Short-cut to avoid taking the |
| 337 | // instrumentation_lock_. |
| 338 | bool have_method_exit_listeners_; |
| 339 | |
| 340 | // Do we have any listeners for method unwind events? Short-cut to avoid taking the |
| 341 | // instrumentation_lock_. |
| 342 | bool have_method_unwind_listeners_; |
| 343 | |
| 344 | // Do we have any listeners for dex move events? Short-cut to avoid taking the |
| 345 | // instrumentation_lock_. |
| 346 | bool have_dex_pc_listeners_; |
| 347 | |
| 348 | // Do we have any exception caught listeners? Short-cut to avoid taking the instrumentation_lock_. |
| 349 | bool have_exception_caught_listeners_; |
| 350 | |
| 351 | // The event listeners, written to with the mutator_lock_ exclusively held. |
| 352 | std::list<InstrumentationListener*> method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_); |
| 353 | std::list<InstrumentationListener*> method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_); |
| 354 | std::list<InstrumentationListener*> method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_); |
| 355 | std::list<InstrumentationListener*> dex_pc_listeners_ GUARDED_BY(Locks::mutator_lock_); |
| 356 | std::list<InstrumentationListener*> exception_caught_listeners_ GUARDED_BY(Locks::mutator_lock_); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 357 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 358 | // The set of methods being deoptimized (by the debugger) which must be executed with interpreter |
| 359 | // only. |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 360 | mutable ReaderWriterMutex deoptimized_methods_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
| 361 | std::set<mirror::ArtMethod*> deoptimized_methods_ GUARDED_BY(deoptimized_methods_lock_); |
Sebastien Hertz | 11d40c2 | 2014-02-19 18:00:17 +0100 | [diff] [blame] | 362 | bool deoptimization_enabled_; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 363 | |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 364 | // Current interpreter handler table. This is updated each time the thread state flags are |
| 365 | // modified. |
Sebastien Hertz | ee1997a | 2013-09-19 14:47:09 +0200 | [diff] [blame] | 366 | InterpreterHandlerTable interpreter_handler_table_; |
| 367 | |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 368 | // Greater than 0 if quick alloc entry points instrumented. |
| 369 | // TODO: The access and changes to this is racy and should be guarded by a lock. |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 370 | AtomicInteger quick_alloc_entry_points_instrumentation_counter_; |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 371 | |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 372 | DISALLOW_COPY_AND_ASSIGN(Instrumentation); |
| 373 | }; |
| 374 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 375 | // An element in the instrumentation side stack maintained in art::Thread. |
| 376 | struct InstrumentationStackFrame { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 377 | InstrumentationStackFrame(mirror::Object* this_object, mirror::ArtMethod* method, |
Jeff Hao | 9a916d3 | 2013-06-27 18:45:37 -0700 | [diff] [blame] | 378 | uintptr_t return_pc, size_t frame_id, bool interpreter_entry) |
| 379 | : this_object_(this_object), method_(method), return_pc_(return_pc), frame_id_(frame_id), |
| 380 | interpreter_entry_(interpreter_entry) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | std::string Dump() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 384 | |
| 385 | mirror::Object* this_object_; |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 386 | mirror::ArtMethod* method_; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 387 | uintptr_t return_pc_; |
| 388 | size_t frame_id_; |
| 389 | bool interpreter_entry_; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 390 | }; |
| 391 | |
| 392 | } // namespace instrumentation |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 393 | } // namespace art |
| 394 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 395 | #endif // ART_RUNTIME_INSTRUMENTATION_H_ |