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> |
Ian Rogers | 576ca0c | 2014-06-06 15:58:22 -0700 | [diff] [blame] | 21 | #include <list> |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 22 | #include <unordered_set> |
Ian Rogers | 576ca0c | 2014-06-06 15:58:22 -0700 | [diff] [blame] | 23 | |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 24 | #include "arch/instruction_set.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 25 | #include "base/enums.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" |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 28 | #include "gc_root.h" |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 29 | #include "safe_map.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 30 | |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 31 | namespace art { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 32 | namespace mirror { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 33 | class Class; |
| 34 | class Object; |
| 35 | class Throwable; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 36 | } // namespace mirror |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 37 | class ArtField; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 38 | class ArtMethod; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 39 | union JValue; |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 40 | class Thread; |
| 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 | |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 52 | // Do we want to deoptimize for method entry and exit listeners or just try to intercept |
| 53 | // invocations? Deoptimization forces all code to run in the interpreter and considerably hurts the |
| 54 | // application's performance. |
| 55 | static constexpr bool kDeoptimizeForAccurateMethodEntryExitListeners = true; |
| 56 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 57 | // Instrumentation event listener API. Registered listeners will get the appropriate call back for |
| 58 | // the events they are listening for. The call backs supply the thread, method and dex_pc the event |
| 59 | // occurred upon. The thread may or may not be Thread::Current(). |
| 60 | struct InstrumentationListener { |
| 61 | InstrumentationListener() {} |
| 62 | virtual ~InstrumentationListener() {} |
| 63 | |
| 64 | // Call-back for when a method is entered. |
| 65 | virtual void MethodEntered(Thread* thread, mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 66 | ArtMethod* method, |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 67 | uint32_t dex_pc) REQUIRES_SHARED(Locks::mutator_lock_) = 0; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 68 | |
| 69 | // Call-back for when a method is exited. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 70 | virtual void MethodExited(Thread* thread, mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 71 | ArtMethod* method, uint32_t dex_pc, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 72 | const JValue& return_value) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 73 | REQUIRES_SHARED(Locks::mutator_lock_) = 0; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 74 | |
| 75 | // Call-back for when a method is popped due to an exception throw. A method will either cause a |
| 76 | // 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] | 77 | virtual void MethodUnwind(Thread* thread, mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 78 | ArtMethod* method, uint32_t dex_pc) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 79 | REQUIRES_SHARED(Locks::mutator_lock_) = 0; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 80 | |
| 81 | // Call-back for when the dex pc moves in a method. |
| 82 | virtual void DexPcMoved(Thread* thread, mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 83 | ArtMethod* method, uint32_t new_dex_pc) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 84 | REQUIRES_SHARED(Locks::mutator_lock_) = 0; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 85 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 86 | // Call-back for when we read from a field. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 87 | virtual void FieldRead(Thread* thread, mirror::Object* this_object, ArtMethod* method, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 88 | uint32_t dex_pc, ArtField* field) = 0; |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 89 | |
| 90 | // Call-back for when we write into a field. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 91 | virtual void FieldWritten(Thread* thread, mirror::Object* this_object, ArtMethod* method, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 92 | uint32_t dex_pc, ArtField* field, const JValue& field_value) = 0; |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 93 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 94 | // Call-back when an exception is caught. |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 95 | virtual void ExceptionCaught(Thread* thread, mirror::Throwable* exception_object) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 96 | REQUIRES_SHARED(Locks::mutator_lock_) = 0; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 97 | |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 98 | // Call-back for when we execute a branch. |
| 99 | virtual void Branch(Thread* thread, |
| 100 | ArtMethod* method, |
| 101 | uint32_t dex_pc, |
| 102 | int32_t dex_pc_offset) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 103 | REQUIRES_SHARED(Locks::mutator_lock_) = 0; |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 104 | |
| 105 | // Call-back for when we get an invokevirtual or an invokeinterface. |
| 106 | virtual void InvokeVirtualOrInterface(Thread* thread, |
| 107 | mirror::Object* this_object, |
| 108 | ArtMethod* caller, |
| 109 | uint32_t dex_pc, |
| 110 | ArtMethod* callee) |
Mathieu Chartier | 3fdb3fe | 2016-01-14 10:24:28 -0800 | [diff] [blame] | 111 | REQUIRES(Roles::uninterruptible_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 112 | REQUIRES_SHARED(Locks::mutator_lock_) = 0; |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 113 | }; |
| 114 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 115 | // Instrumentation is a catch-all for when extra information is required from the runtime. The |
| 116 | // typical use for instrumentation is for profiling and debugging. Instrumentation may add stubs |
| 117 | // to method entry and exit, it may also force execution to be switched to the interpreter and |
| 118 | // trigger deoptimization. |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 119 | class Instrumentation { |
| 120 | public: |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 121 | enum InstrumentationEvent { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 122 | kMethodEntered = 0x1, |
| 123 | kMethodExited = 0x2, |
| 124 | kMethodUnwind = 0x4, |
| 125 | kDexPcMoved = 0x8, |
| 126 | kFieldRead = 0x10, |
| 127 | kFieldWritten = 0x20, |
| 128 | kExceptionCaught = 0x40, |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 129 | kBranch = 0x80, |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 130 | kInvokeVirtualOrInterface = 0x100, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 131 | }; |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 132 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 133 | enum class InstrumentationLevel { |
| 134 | kInstrumentNothing, // execute without instrumentation |
| 135 | kInstrumentWithInstrumentationStubs, // execute with instrumentation entry/exit stubs |
| 136 | kInstrumentWithInterpreter // execute with interpreter |
| 137 | }; |
| 138 | |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 139 | Instrumentation(); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 140 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 141 | // Add a listener to be notified of the masked together sent of instrumentation events. This |
| 142 | // suspend the runtime to install stubs. You are expected to hold the mutator lock as a proxy |
| 143 | // for saying you should have suspended all threads (installing stubs while threads are running |
| 144 | // will break). |
| 145 | void AddListener(InstrumentationListener* listener, uint32_t events) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 146 | REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 147 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 148 | // Removes a listener possibly removing instrumentation stubs. |
| 149 | void RemoveListener(InstrumentationListener* listener, uint32_t events) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 150 | REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 151 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 152 | // Deoptimization. |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 153 | void EnableDeoptimization() |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 154 | REQUIRES(Locks::mutator_lock_) |
| 155 | REQUIRES(!deoptimized_methods_lock_); |
| 156 | // Calls UndeoptimizeEverything which may visit class linker classes through ConfigureStubs. |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 157 | void DisableDeoptimization(const char* key) |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 158 | REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_) |
| 159 | REQUIRES(!deoptimized_methods_lock_); |
| 160 | |
Sebastien Hertz | a76a6d4 | 2014-03-20 16:40:17 +0100 | [diff] [blame] | 161 | bool AreAllMethodsDeoptimized() const { |
| 162 | return interpreter_stubs_installed_; |
| 163 | } |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 164 | bool ShouldNotifyMethodEnterExitEvents() const REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 165 | |
| 166 | // Executes everything with interpreter. |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 167 | void DeoptimizeEverything(const char* key) |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 168 | REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_) |
| 169 | REQUIRES(!Locks::thread_list_lock_, |
| 170 | !Locks::classlinker_classes_lock_, |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 171 | !deoptimized_methods_lock_); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 172 | |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 173 | // Executes everything with compiled code (or interpreter if there is no code). May visit class |
| 174 | // linker classes through ConfigureStubs. |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 175 | void UndeoptimizeEverything(const char* key) |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 176 | REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_) |
| 177 | REQUIRES(!Locks::thread_list_lock_, |
| 178 | !Locks::classlinker_classes_lock_, |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 179 | !deoptimized_methods_lock_); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 180 | |
| 181 | // Deoptimize a method by forcing its execution with the interpreter. Nevertheless, a static |
| 182 | // method (except a class initializer) set to the resolution trampoline will be deoptimized only |
| 183 | // once its declaring class is initialized. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 184 | void Deoptimize(ArtMethod* method) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 185 | REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !deoptimized_methods_lock_); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 186 | |
| 187 | // Undeoptimze the method by restoring its entrypoints. Nevertheless, a static method |
| 188 | // (except a class initializer) set to the resolution trampoline will be updated only once its |
| 189 | // declaring class is initialized. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 190 | void Undeoptimize(ArtMethod* method) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 191 | REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !deoptimized_methods_lock_); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 192 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 193 | // 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] | 194 | bool IsDeoptimized(ArtMethod* method) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 195 | REQUIRES(!deoptimized_methods_lock_) REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 196 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 197 | // Enable method tracing by installing instrumentation entry/exit stubs or interpreter. |
| 198 | void EnableMethodTracing(const char* key, |
| 199 | bool needs_interpreter = kDeoptimizeForAccurateMethodEntryExitListeners) |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 200 | REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_) |
| 201 | REQUIRES(!Locks::thread_list_lock_, |
| 202 | !Locks::classlinker_classes_lock_, |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 203 | !deoptimized_methods_lock_); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 204 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 205 | // Disable method tracing by uninstalling instrumentation entry/exit stubs or interpreter. |
| 206 | void DisableMethodTracing(const char* key) |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 207 | REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_) |
| 208 | REQUIRES(!Locks::thread_list_lock_, |
| 209 | !Locks::classlinker_classes_lock_, |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 210 | !deoptimized_methods_lock_); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 211 | |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 212 | InterpreterHandlerTable GetInterpreterHandlerTable() const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 213 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | ee1997a | 2013-09-19 14:47:09 +0200 | [diff] [blame] | 214 | return interpreter_handler_table_; |
| 215 | } |
| 216 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 217 | void InstrumentQuickAllocEntryPoints() REQUIRES(!Locks::instrument_entrypoints_lock_); |
| 218 | void UninstrumentQuickAllocEntryPoints() REQUIRES(!Locks::instrument_entrypoints_lock_); |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 219 | void InstrumentQuickAllocEntryPointsLocked() |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 220 | REQUIRES(Locks::instrument_entrypoints_lock_, !Locks::thread_list_lock_, |
| 221 | !Locks::runtime_shutdown_lock_); |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 222 | void UninstrumentQuickAllocEntryPointsLocked() |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 223 | REQUIRES(Locks::instrument_entrypoints_lock_, !Locks::thread_list_lock_, |
| 224 | !Locks::runtime_shutdown_lock_); |
| 225 | void ResetQuickAllocEntryPoints() REQUIRES(Locks::runtime_shutdown_lock_); |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 226 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 227 | // Update the code of a method respecting any installed stubs. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 228 | void UpdateMethodsCode(ArtMethod* method, const void* quick_code) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 229 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 230 | |
Mingyao Yang | 3fd448a | 2016-05-10 14:30:41 -0700 | [diff] [blame] | 231 | // Update the code of a method respecting any installed stubs from debugger. |
Nicolas Geoffray | a0619e2 | 2016-12-20 13:57:43 +0000 | [diff] [blame] | 232 | void UpdateMethodsCodeForJavaDebuggable(ArtMethod* method, const void* quick_code) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 233 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_); |
Mingyao Yang | 3fd448a | 2016-05-10 14:30:41 -0700 | [diff] [blame] | 234 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 235 | // Get the quick code for the given method. More efficient than asking the class linker as it |
| 236 | // will short-cut to GetCode if instrumentation and static method resolution stubs aren't |
| 237 | // installed. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 238 | const void* GetQuickCodeFor(ArtMethod* method, PointerSize pointer_size) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 239 | REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 240 | |
| 241 | void ForceInterpretOnly() { |
| 242 | interpret_only_ = true; |
| 243 | forced_interpret_only_ = true; |
| 244 | } |
| 245 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 246 | // Called by ArtMethod::Invoke to determine dispatch mechanism. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 247 | bool InterpretOnly() const { |
| 248 | return interpret_only_; |
| 249 | } |
| 250 | |
Hiroshi Yamauchi | 563b47c | 2014-02-28 17:18:37 -0800 | [diff] [blame] | 251 | bool IsForcedInterpretOnly() const { |
| 252 | return forced_interpret_only_; |
| 253 | } |
| 254 | |
Mingyao Yang | 6ea1a0e | 2016-01-29 12:12:49 -0800 | [diff] [blame] | 255 | // Code is in boot image oat file which isn't compiled as debuggable. |
| 256 | // Need debug version (interpreter or jitted) if that's the case. |
Nicolas Geoffray | a0619e2 | 2016-12-20 13:57:43 +0000 | [diff] [blame] | 257 | bool NeedDebugVersionFor(ArtMethod* method) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 258 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mingyao Yang | 6ea1a0e | 2016-01-29 12:12:49 -0800 | [diff] [blame] | 259 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 260 | bool AreExitStubsInstalled() const { |
| 261 | return instrumentation_stubs_installed_; |
| 262 | } |
| 263 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 264 | bool HasMethodEntryListeners() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 74109f6 | 2013-06-07 17:40:09 +0200 | [diff] [blame] | 265 | return have_method_entry_listeners_; |
| 266 | } |
| 267 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 268 | bool HasMethodExitListeners() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 74109f6 | 2013-06-07 17:40:09 +0200 | [diff] [blame] | 269 | return have_method_exit_listeners_; |
| 270 | } |
| 271 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 272 | bool HasMethodUnwindListeners() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 273 | return have_method_unwind_listeners_; |
| 274 | } |
| 275 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 276 | bool HasDexPcListeners() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 74109f6 | 2013-06-07 17:40:09 +0200 | [diff] [blame] | 277 | return have_dex_pc_listeners_; |
| 278 | } |
| 279 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 280 | bool HasFieldReadListeners() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 281 | return have_field_read_listeners_; |
| 282 | } |
| 283 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 284 | bool HasFieldWriteListeners() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 285 | return have_field_write_listeners_; |
| 286 | } |
| 287 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 288 | bool HasExceptionCaughtListeners() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 9f10203 | 2014-05-23 08:59:42 +0200 | [diff] [blame] | 289 | return have_exception_caught_listeners_; |
| 290 | } |
| 291 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 292 | bool HasBranchListeners() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 293 | return have_branch_listeners_; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 294 | } |
| 295 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 296 | bool HasInvokeVirtualOrInterfaceListeners() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 297 | return have_invoke_virtual_or_interface_listeners_; |
| 298 | } |
| 299 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 300 | bool IsActive() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | ee1997a | 2013-09-19 14:47:09 +0200 | [diff] [blame] | 301 | 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] | 302 | have_field_read_listeners_ || have_field_write_listeners_ || |
Bill Buzbee | fd522f9 | 2016-02-11 22:37:42 +0000 | [diff] [blame] | 303 | have_exception_caught_listeners_ || have_method_unwind_listeners_ || |
| 304 | have_branch_listeners_ || have_invoke_virtual_or_interface_listeners_; |
| 305 | } |
| 306 | |
| 307 | // Any instrumentation *other* than what is needed for Jit profiling active? |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 308 | bool NonJitProfilingActive() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Bill Buzbee | fd522f9 | 2016-02-11 22:37:42 +0000 | [diff] [blame] | 309 | return have_dex_pc_listeners_ || have_method_exit_listeners_ || |
| 310 | have_field_read_listeners_ || have_field_write_listeners_ || |
Bill Buzbee | 1d011d9 | 2016-04-04 16:59:29 +0000 | [diff] [blame] | 311 | have_exception_caught_listeners_ || have_method_unwind_listeners_ || |
| 312 | have_branch_listeners_; |
Sebastien Hertz | ee1997a | 2013-09-19 14:47:09 +0200 | [diff] [blame] | 313 | } |
| 314 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 315 | // Inform listeners that a method has been entered. A dex PC is provided as we may install |
| 316 | // listeners into executing code and get method enter events for methods already on the stack. |
| 317 | void MethodEnterEvent(Thread* thread, mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 318 | ArtMethod* method, uint32_t dex_pc) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 319 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 74109f6 | 2013-06-07 17:40:09 +0200 | [diff] [blame] | 320 | if (UNLIKELY(HasMethodEntryListeners())) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 321 | MethodEnterEventImpl(thread, this_object, method, dex_pc); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | // Inform listeners that a method has been exited. |
| 326 | void MethodExitEvent(Thread* thread, mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 327 | ArtMethod* method, uint32_t dex_pc, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 328 | const JValue& return_value) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 329 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 74109f6 | 2013-06-07 17:40:09 +0200 | [diff] [blame] | 330 | if (UNLIKELY(HasMethodExitListeners())) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 331 | MethodExitEventImpl(thread, this_object, method, dex_pc, return_value); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | // Inform listeners that a method has been exited due to an exception. |
| 336 | void MethodUnwindEvent(Thread* thread, mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 337 | ArtMethod* method, uint32_t dex_pc) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 338 | REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 339 | |
| 340 | // Inform listeners that the dex pc has moved (only supported by the interpreter). |
| 341 | void DexPcMovedEvent(Thread* thread, mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 342 | ArtMethod* method, uint32_t dex_pc) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 343 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 74109f6 | 2013-06-07 17:40:09 +0200 | [diff] [blame] | 344 | if (UNLIKELY(HasDexPcListeners())) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 345 | DexPcMovedEventImpl(thread, this_object, method, dex_pc); |
| 346 | } |
| 347 | } |
| 348 | |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 349 | // Inform listeners that a branch has been taken (only supported by the interpreter). |
| 350 | 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] | 351 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 352 | if (UNLIKELY(HasBranchListeners())) { |
| 353 | BranchImpl(thread, method, dex_pc, offset); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 357 | // Inform listeners that we read a field (only supported by the interpreter). |
| 358 | void FieldReadEvent(Thread* thread, mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 359 | ArtMethod* method, uint32_t dex_pc, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 360 | ArtField* field) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 361 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 362 | if (UNLIKELY(HasFieldReadListeners())) { |
| 363 | FieldReadEventImpl(thread, this_object, method, dex_pc, field); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | // Inform listeners that we write a field (only supported by the interpreter). |
| 368 | void FieldWriteEvent(Thread* thread, mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 369 | ArtMethod* method, uint32_t dex_pc, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 370 | ArtField* field, const JValue& field_value) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 371 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 372 | if (UNLIKELY(HasFieldWriteListeners())) { |
| 373 | FieldWriteEventImpl(thread, this_object, method, dex_pc, field, field_value); |
| 374 | } |
| 375 | } |
| 376 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 377 | void InvokeVirtualOrInterface(Thread* thread, |
| 378 | mirror::Object* this_object, |
| 379 | ArtMethod* caller, |
| 380 | uint32_t dex_pc, |
| 381 | ArtMethod* callee) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 382 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 383 | if (UNLIKELY(HasInvokeVirtualOrInterfaceListeners())) { |
| 384 | InvokeVirtualOrInterfaceImpl(thread, this_object, caller, dex_pc, callee); |
| 385 | } |
| 386 | } |
| 387 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 388 | // Inform listeners that an exception was caught. |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 389 | void ExceptionCaughtEvent(Thread* thread, mirror::Throwable* exception_object) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 390 | REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 391 | |
| 392 | // Called when an instrumented method is entered. The intended link register (lr) is saved so |
| 393 | // that returning causes a branch to the method exit stub. Generates method enter events. |
| 394 | void PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 395 | ArtMethod* method, uintptr_t lr, |
Jeff Hao | 9a916d3 | 2013-06-27 18:45:37 -0700 | [diff] [blame] | 396 | bool interpreter_entry) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 397 | REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 398 | |
| 399 | // Called when an instrumented method is exited. Removes the pushed instrumentation frame |
| 400 | // returning the intended link register. Generates method exit events. |
Andreas Gampe | d58342c | 2014-06-05 14:18:08 -0700 | [diff] [blame] | 401 | TwoWordReturn PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc, |
| 402 | uint64_t gpr_result, uint64_t fpr_result) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 403 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 404 | |
| 405 | // Pops an instrumentation frame from the current thread and generate an unwind event. |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 406 | // Returns the return pc for the instrumentation frame that's popped. |
| 407 | uintptr_t PopMethodForUnwind(Thread* self, bool is_deoptimization) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 408 | REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 409 | |
| 410 | // Call back for configure stubs. |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 411 | void InstallStubsForClass(mirror::Class* klass) REQUIRES_SHARED(Locks::mutator_lock_) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 412 | REQUIRES(!deoptimized_methods_lock_); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 413 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 414 | void InstallStubsForMethod(ArtMethod* method) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 415 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 416 | |
Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 417 | // Install instrumentation exit stub on every method of the stack of the given thread. |
| 418 | // This is used by the debugger to cause a deoptimization of the thread's stack after updating |
| 419 | // local variable(s). |
| 420 | void InstrumentThreadStack(Thread* thread) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 421 | REQUIRES_SHARED(Locks::mutator_lock_) |
Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 422 | REQUIRES(!Locks::thread_list_lock_); |
| 423 | |
Sebastien Hertz | b2feaaf | 2015-10-12 13:40:10 +0000 | [diff] [blame] | 424 | static size_t ComputeFrameId(Thread* self, |
| 425 | size_t frame_depth, |
| 426 | size_t inlined_frames_before_frame) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 427 | REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | b2feaaf | 2015-10-12 13:40:10 +0000 | [diff] [blame] | 428 | |
Mathieu Chartier | eebc3af | 2016-02-29 18:13:38 -0800 | [diff] [blame] | 429 | // Does not hold lock, used to check if someone changed from not instrumented to instrumented |
| 430 | // during a GC suspend point. |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 431 | bool AllocEntrypointsInstrumented() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 50e9331 | 2016-03-16 11:25:29 -0700 | [diff] [blame] | 432 | return alloc_entrypoints_instrumented_; |
Mathieu Chartier | eebc3af | 2016-02-29 18:13:38 -0800 | [diff] [blame] | 433 | } |
| 434 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 435 | InstrumentationLevel GetCurrentInstrumentationLevel() const; |
| 436 | |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 437 | private: |
| 438 | // Returns true if moving to the given instrumentation level requires the installation of stubs. |
| 439 | // False otherwise. |
| 440 | bool RequiresInstrumentationInstallation(InstrumentationLevel new_level) const; |
| 441 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 442 | // Does the job of installing or removing instrumentation code within methods. |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 443 | // In order to support multiple clients using instrumentation at the same time, |
| 444 | // the caller must pass a unique key (a string) identifying it so we remind which |
| 445 | // instrumentation level it needs. Therefore the current instrumentation level |
| 446 | // becomes the highest instrumentation level required by a client. |
| 447 | void ConfigureStubs(const char* key, InstrumentationLevel desired_instrumentation_level) |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 448 | REQUIRES(Locks::mutator_lock_, Roles::uninterruptible_) |
| 449 | REQUIRES(!deoptimized_methods_lock_, |
| 450 | !Locks::thread_list_lock_, |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 451 | !Locks::classlinker_classes_lock_); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 452 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 453 | void UpdateInterpreterHandlerTable() REQUIRES(Locks::mutator_lock_) { |
buzbee | 1452bee | 2015-03-06 14:43:04 -0800 | [diff] [blame] | 454 | /* |
| 455 | * TUNING: Dalvik's mterp stashes the actual current handler table base in a |
| 456 | * tls field. For Arm, this enables all suspend, debug & tracing checks to be |
| 457 | * collapsed into a single conditionally-executed ldw instruction. |
| 458 | * Move to Dalvik-style handler-table management for both the goto interpreter and |
| 459 | * mterp. |
| 460 | */ |
Sebastien Hertz | ee1997a | 2013-09-19 14:47:09 +0200 | [diff] [blame] | 461 | interpreter_handler_table_ = IsActive() ? kAlternativeHandlerTable : kMainHandlerTable; |
| 462 | } |
| 463 | |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 464 | // No thread safety analysis to get around SetQuickAllocEntryPointsInstrumented requiring |
| 465 | // 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] | 466 | void SetEntrypointsInstrumented(bool instrumented) NO_THREAD_SAFETY_ANALYSIS; |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 467 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 468 | void MethodEnterEventImpl(Thread* thread, mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 469 | ArtMethod* method, uint32_t dex_pc) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 470 | REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 471 | void MethodExitEventImpl(Thread* thread, mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 472 | ArtMethod* method, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 473 | uint32_t dex_pc, const JValue& return_value) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 474 | REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 475 | void DexPcMovedEventImpl(Thread* thread, mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 476 | ArtMethod* method, uint32_t dex_pc) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 477 | REQUIRES_SHARED(Locks::mutator_lock_); |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 478 | 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] | 479 | REQUIRES_SHARED(Locks::mutator_lock_); |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 480 | void InvokeVirtualOrInterfaceImpl(Thread* thread, |
| 481 | mirror::Object* this_object, |
| 482 | ArtMethod* caller, |
| 483 | uint32_t dex_pc, |
| 484 | ArtMethod* callee) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 485 | REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 486 | void FieldReadEventImpl(Thread* thread, mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 487 | ArtMethod* method, uint32_t dex_pc, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 488 | ArtField* field) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 489 | REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 490 | void FieldWriteEventImpl(Thread* thread, mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 491 | ArtMethod* method, uint32_t dex_pc, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 492 | ArtField* field, const JValue& field_value) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 493 | REQUIRES_SHARED(Locks::mutator_lock_); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 494 | |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 495 | // Read barrier-aware utility functions for accessing deoptimized_methods_ |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 496 | bool AddDeoptimizedMethod(ArtMethod* method) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 497 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(deoptimized_methods_lock_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 498 | bool IsDeoptimizedMethod(ArtMethod* method) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 499 | REQUIRES_SHARED(Locks::mutator_lock_, deoptimized_methods_lock_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 500 | bool RemoveDeoptimizedMethod(ArtMethod* method) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 501 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(deoptimized_methods_lock_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 502 | ArtMethod* BeginDeoptimizedMethod() |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 503 | REQUIRES_SHARED(Locks::mutator_lock_, deoptimized_methods_lock_); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 504 | bool IsDeoptimizedMethodsEmpty() const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 505 | REQUIRES_SHARED(Locks::mutator_lock_, deoptimized_methods_lock_); |
Mingyao Yang | 3fd448a | 2016-05-10 14:30:41 -0700 | [diff] [blame] | 506 | void UpdateMethodsCodeImpl(ArtMethod* method, const void* quick_code) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 507 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!deoptimized_methods_lock_); |
Mingyao Yang | 3fd448a | 2016-05-10 14:30:41 -0700 | [diff] [blame] | 508 | |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 509 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 510 | // Have we hijacked ArtMethod::code_ so that it calls instrumentation/interpreter code? |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 511 | bool instrumentation_stubs_installed_; |
| 512 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 513 | // Have we hijacked ArtMethod::code_ to reference the enter/exit stubs? |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 514 | bool entry_exit_stubs_installed_; |
| 515 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 516 | // Have we hijacked ArtMethod::code_ to reference the enter interpreter stub? |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 517 | bool interpreter_stubs_installed_; |
| 518 | |
| 519 | // Do we need the fidelity of events that we only get from running within the interpreter? |
| 520 | bool interpret_only_; |
| 521 | |
| 522 | // Did the runtime request we only run in the interpreter? ie -Xint mode. |
| 523 | bool forced_interpret_only_; |
| 524 | |
| 525 | // Do we have any listeners for method entry events? Short-cut to avoid taking the |
| 526 | // instrumentation_lock_. |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 527 | bool have_method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 528 | |
| 529 | // Do we have any listeners for method exit events? Short-cut to avoid taking the |
| 530 | // instrumentation_lock_. |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 531 | bool have_method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 532 | |
| 533 | // Do we have any listeners for method unwind events? Short-cut to avoid taking the |
| 534 | // instrumentation_lock_. |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 535 | bool have_method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 536 | |
| 537 | // Do we have any listeners for dex move events? Short-cut to avoid taking the |
| 538 | // instrumentation_lock_. |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 539 | bool have_dex_pc_listeners_ GUARDED_BY(Locks::mutator_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 540 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 541 | // Do we have any listeners for field read events? Short-cut to avoid taking the |
| 542 | // instrumentation_lock_. |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 543 | bool have_field_read_listeners_ GUARDED_BY(Locks::mutator_lock_); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 544 | |
| 545 | // Do we have any listeners for field write events? Short-cut to avoid taking the |
| 546 | // instrumentation_lock_. |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 547 | bool have_field_write_listeners_ GUARDED_BY(Locks::mutator_lock_); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 548 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 549 | // Do we have any exception caught listeners? Short-cut to avoid taking the instrumentation_lock_. |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 550 | bool have_exception_caught_listeners_ GUARDED_BY(Locks::mutator_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 551 | |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 552 | // Do we have any branch listeners? Short-cut to avoid taking the instrumentation_lock_. |
| 553 | bool have_branch_listeners_ GUARDED_BY(Locks::mutator_lock_); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 554 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 555 | // Do we have any invoke listeners? Short-cut to avoid taking the instrumentation_lock_. |
| 556 | bool have_invoke_virtual_or_interface_listeners_ GUARDED_BY(Locks::mutator_lock_); |
| 557 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 558 | // Contains the instrumentation level required by each client of the instrumentation identified |
| 559 | // by a string key. |
| 560 | typedef SafeMap<const char*, InstrumentationLevel> InstrumentationLevelTable; |
| 561 | InstrumentationLevelTable requested_instrumentation_levels_ GUARDED_BY(Locks::mutator_lock_); |
| 562 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 563 | // The event listeners, written to with the mutator_lock_ exclusively held. |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 564 | // Mutators must be able to iterate over these lists concurrently, that is, with listeners being |
| 565 | // added or removed while iterating. The modifying thread holds exclusive lock, |
| 566 | // so other threads cannot iterate (i.e. read the data of the list) at the same time but they |
| 567 | // do keep iterators that need to remain valid. This is the reason these listeners are std::list |
| 568 | // and not for example std::vector: the existing storage for a std::list does not move. |
| 569 | // Note that mutators cannot make a copy of these lists before iterating, as the instrumentation |
| 570 | // listeners can also be deleted concurrently. |
| 571 | // As a result, these lists are never trimmed. That's acceptable given the low number of |
| 572 | // listeners we have. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 573 | std::list<InstrumentationListener*> method_entry_listeners_ GUARDED_BY(Locks::mutator_lock_); |
| 574 | std::list<InstrumentationListener*> method_exit_listeners_ GUARDED_BY(Locks::mutator_lock_); |
| 575 | std::list<InstrumentationListener*> method_unwind_listeners_ GUARDED_BY(Locks::mutator_lock_); |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 576 | std::list<InstrumentationListener*> branch_listeners_ GUARDED_BY(Locks::mutator_lock_); |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 577 | std::list<InstrumentationListener*> invoke_virtual_or_interface_listeners_ |
| 578 | GUARDED_BY(Locks::mutator_lock_); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 579 | std::list<InstrumentationListener*> dex_pc_listeners_ GUARDED_BY(Locks::mutator_lock_); |
| 580 | std::list<InstrumentationListener*> field_read_listeners_ GUARDED_BY(Locks::mutator_lock_); |
| 581 | std::list<InstrumentationListener*> field_write_listeners_ GUARDED_BY(Locks::mutator_lock_); |
| 582 | std::list<InstrumentationListener*> exception_caught_listeners_ GUARDED_BY(Locks::mutator_lock_); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 583 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 584 | // The set of methods being deoptimized (by the debugger) which must be executed with interpreter |
| 585 | // only. |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 586 | mutable ReaderWriterMutex deoptimized_methods_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 587 | std::unordered_set<ArtMethod*> deoptimized_methods_ GUARDED_BY(deoptimized_methods_lock_); |
Sebastien Hertz | 11d40c2 | 2014-02-19 18:00:17 +0100 | [diff] [blame] | 588 | bool deoptimization_enabled_; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 589 | |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 590 | // Current interpreter handler table. This is updated each time the thread state flags are |
| 591 | // modified. |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 592 | InterpreterHandlerTable interpreter_handler_table_ GUARDED_BY(Locks::mutator_lock_); |
Sebastien Hertz | ee1997a | 2013-09-19 14:47:09 +0200 | [diff] [blame] | 593 | |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 594 | // Greater than 0 if quick alloc entry points instrumented. |
Mathieu Chartier | eebc3af | 2016-02-29 18:13:38 -0800 | [diff] [blame] | 595 | size_t quick_alloc_entry_points_instrumentation_counter_; |
Mathieu Chartier | 50e9331 | 2016-03-16 11:25:29 -0700 | [diff] [blame] | 596 | |
| 597 | // alloc_entrypoints_instrumented_ is only updated with all the threads suspended, this is done |
| 598 | // to prevent races with the GC where the GC relies on thread suspension only see |
| 599 | // alloc_entrypoints_instrumented_ change during suspend points. |
| 600 | bool alloc_entrypoints_instrumented_; |
| 601 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 602 | friend class InstrumentationTest; // For GetCurrentInstrumentationLevel and ConfigureStubs. |
| 603 | |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 604 | DISALLOW_COPY_AND_ASSIGN(Instrumentation); |
| 605 | }; |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 606 | std::ostream& operator<<(std::ostream& os, const Instrumentation::InstrumentationEvent& rhs); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 607 | std::ostream& operator<<(std::ostream& os, const Instrumentation::InstrumentationLevel& rhs); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 608 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 609 | // An element in the instrumentation side stack maintained in art::Thread. |
| 610 | struct InstrumentationStackFrame { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 611 | InstrumentationStackFrame(mirror::Object* this_object, ArtMethod* method, |
Jeff Hao | 9a916d3 | 2013-06-27 18:45:37 -0700 | [diff] [blame] | 612 | uintptr_t return_pc, size_t frame_id, bool interpreter_entry) |
| 613 | : this_object_(this_object), method_(method), return_pc_(return_pc), frame_id_(frame_id), |
| 614 | interpreter_entry_(interpreter_entry) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 615 | } |
| 616 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 617 | std::string Dump() const REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 618 | |
| 619 | mirror::Object* this_object_; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 620 | ArtMethod* method_; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 621 | uintptr_t return_pc_; |
| 622 | size_t frame_id_; |
| 623 | bool interpreter_entry_; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 624 | }; |
| 625 | |
| 626 | } // namespace instrumentation |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 627 | } // namespace art |
| 628 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 629 | #endif // ART_RUNTIME_INSTRUMENTATION_H_ |