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