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