Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
| 17 | #ifndef ART_RUNTIME_RUNTIME_CALLBACKS_H_ |
| 18 | #define ART_RUNTIME_RUNTIME_CALLBACKS_H_ |
| 19 | |
| 20 | #include <vector> |
| 21 | |
| 22 | #include "base/macros.h" |
| 23 | #include "base/mutex.h" |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 24 | #include "dex_file.h" |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 25 | #include "handle.h" |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 26 | |
| 27 | namespace art { |
| 28 | |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 29 | namespace mirror { |
| 30 | class Class; |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 31 | class ClassLoader; |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 32 | class Object; |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 33 | } // namespace mirror |
| 34 | |
Alex Light | d78ddec | 2017-04-18 15:20:38 -0700 | [diff] [blame] | 35 | class ArtMethod; |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 36 | class ClassLoadCallback; |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 37 | class Thread; |
Alex Light | d78ddec | 2017-04-18 15:20:38 -0700 | [diff] [blame] | 38 | class MethodCallback; |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 39 | class Monitor; |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 40 | class ThreadLifecycleCallback; |
| 41 | |
| 42 | // Note: RuntimeCallbacks uses the mutator lock to synchronize the callback lists. A thread must |
| 43 | // hold the exclusive lock to add or remove a listener. A thread must hold the shared lock |
| 44 | // to dispatch an event. This setup is chosen as some clients may want to suspend the |
| 45 | // dispatching thread or all threads. |
| 46 | // |
| 47 | // To make this safe, the following restrictions apply: |
| 48 | // * Only the owner of a listener may ever add or remove said listener. |
| 49 | // * A listener must never add or remove itself or any other listener while running. |
| 50 | // * It is the responsibility of the owner to not remove the listener while it is running |
| 51 | // (and suspended). |
| 52 | // |
| 53 | // The simplest way to satisfy these restrictions is to never remove a listener, and to do |
| 54 | // any state checking (is the listener enabled) in the listener itself. For an example, see |
| 55 | // Dbg. |
| 56 | |
Andreas Gampe | a5814f9 | 2017-01-18 21:43:16 -0800 | [diff] [blame] | 57 | class RuntimeSigQuitCallback { |
| 58 | public: |
| 59 | virtual ~RuntimeSigQuitCallback() {} |
| 60 | |
| 61 | virtual void SigQuit() REQUIRES_SHARED(Locks::mutator_lock_) = 0; |
| 62 | }; |
| 63 | |
Andreas Gampe | 4886411 | 2017-01-19 17:23:17 -0800 | [diff] [blame] | 64 | class RuntimePhaseCallback { |
| 65 | public: |
| 66 | enum RuntimePhase { |
Andreas Gampe | 96eca78 | 2017-01-19 19:45:30 -0800 | [diff] [blame] | 67 | kInitialAgents, // Initial agent loading is done. |
| 68 | kStart, // The runtime is started. |
| 69 | kInit, // The runtime is initialized (and will run user code soon). |
| 70 | kDeath, // The runtime just died. |
Andreas Gampe | 4886411 | 2017-01-19 17:23:17 -0800 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | virtual ~RuntimePhaseCallback() {} |
| 74 | |
| 75 | virtual void NextRuntimePhase(RuntimePhase phase) REQUIRES_SHARED(Locks::mutator_lock_) = 0; |
| 76 | }; |
| 77 | |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 78 | class MonitorCallback { |
| 79 | public: |
| 80 | // Called just before the thread goes to sleep to wait for the monitor to become unlocked. |
| 81 | virtual void MonitorContendedLocking(Monitor* mon) REQUIRES_SHARED(Locks::mutator_lock_) = 0; |
| 82 | // Called just after the monitor has been successfully acquired when it was already locked. |
| 83 | virtual void MonitorContendedLocked(Monitor* mon) REQUIRES_SHARED(Locks::mutator_lock_) = 0; |
| 84 | // Called on entry to the Object#wait method regardless of whether or not the call is valid. |
| 85 | virtual void ObjectWaitStart(Handle<mirror::Object> obj, int64_t millis_timeout) |
| 86 | REQUIRES_SHARED(Locks::mutator_lock_) = 0; |
| 87 | |
| 88 | // Called just after the monitor has woken up from going to sleep for a wait(). At this point the |
| 89 | // thread does not possess a lock on the monitor. This will only be called for threads wait calls |
| 90 | // where the thread did (or at least could have) gone to sleep. |
| 91 | virtual void MonitorWaitFinished(Monitor* m, bool timed_out) |
| 92 | REQUIRES_SHARED(Locks::mutator_lock_) = 0; |
| 93 | |
| 94 | virtual ~MonitorCallback() {} |
| 95 | }; |
| 96 | |
Alex Light | 2161193 | 2017-09-26 13:07:39 -0700 | [diff] [blame^] | 97 | // A callback to let parts of the runtime note that they are currently relying on a particular |
| 98 | // method remaining in it's current state. Users should not rely on always being called. If multiple |
| 99 | // callbacks are added the runtime will short-circuit when the first one returns 'true'. |
| 100 | class MethodInspectionCallback { |
| 101 | public: |
| 102 | virtual ~MethodInspectionCallback() {} |
| 103 | |
| 104 | // Returns true if the method is being inspected currently and the runtime should not modify it in |
| 105 | // potentially dangerous ways (i.e. replace with compiled version, JIT it, etc). |
| 106 | virtual bool IsMethodBeingInspected(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) = 0; |
| 107 | }; |
| 108 | |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 109 | class RuntimeCallbacks { |
| 110 | public: |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 111 | void AddThreadLifecycleCallback(ThreadLifecycleCallback* cb) REQUIRES(Locks::mutator_lock_); |
| 112 | void RemoveThreadLifecycleCallback(ThreadLifecycleCallback* cb) REQUIRES(Locks::mutator_lock_); |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 113 | |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 114 | void ThreadStart(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_); |
| 115 | void ThreadDeath(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_); |
| 116 | |
| 117 | void AddClassLoadCallback(ClassLoadCallback* cb) REQUIRES(Locks::mutator_lock_); |
| 118 | void RemoveClassLoadCallback(ClassLoadCallback* cb) REQUIRES(Locks::mutator_lock_); |
| 119 | |
| 120 | void ClassLoad(Handle<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_); |
| 121 | void ClassPrepare(Handle<mirror::Class> temp_klass, Handle<mirror::Class> klass) |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 122 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 123 | |
Andreas Gampe | a5814f9 | 2017-01-18 21:43:16 -0800 | [diff] [blame] | 124 | void AddRuntimeSigQuitCallback(RuntimeSigQuitCallback* cb) |
| 125 | REQUIRES(Locks::mutator_lock_); |
| 126 | void RemoveRuntimeSigQuitCallback(RuntimeSigQuitCallback* cb) |
| 127 | REQUIRES(Locks::mutator_lock_); |
| 128 | |
| 129 | void SigQuit() REQUIRES_SHARED(Locks::mutator_lock_); |
| 130 | |
Andreas Gampe | 4886411 | 2017-01-19 17:23:17 -0800 | [diff] [blame] | 131 | void AddRuntimePhaseCallback(RuntimePhaseCallback* cb) |
| 132 | REQUIRES(Locks::mutator_lock_); |
| 133 | void RemoveRuntimePhaseCallback(RuntimePhaseCallback* cb) |
| 134 | REQUIRES(Locks::mutator_lock_); |
| 135 | |
| 136 | void NextRuntimePhase(RuntimePhaseCallback::RuntimePhase phase) |
| 137 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 138 | |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 139 | void ClassPreDefine(const char* descriptor, |
| 140 | Handle<mirror::Class> temp_class, |
| 141 | Handle<mirror::ClassLoader> loader, |
| 142 | const DexFile& initial_dex_file, |
| 143 | const DexFile::ClassDef& initial_class_def, |
| 144 | /*out*/DexFile const** final_dex_file, |
| 145 | /*out*/DexFile::ClassDef const** final_class_def) |
| 146 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 147 | |
Alex Light | d78ddec | 2017-04-18 15:20:38 -0700 | [diff] [blame] | 148 | void AddMethodCallback(MethodCallback* cb) REQUIRES(Locks::mutator_lock_); |
| 149 | void RemoveMethodCallback(MethodCallback* cb) REQUIRES(Locks::mutator_lock_); |
| 150 | |
| 151 | void RegisterNativeMethod(ArtMethod* method, |
| 152 | const void* original_implementation, |
| 153 | /*out*/void** new_implementation) |
| 154 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 155 | |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 156 | void MonitorContendedLocking(Monitor* m) REQUIRES_SHARED(Locks::mutator_lock_); |
| 157 | void MonitorContendedLocked(Monitor* m) REQUIRES_SHARED(Locks::mutator_lock_); |
| 158 | void ObjectWaitStart(Handle<mirror::Object> m, int64_t timeout) |
| 159 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 160 | void MonitorWaitFinished(Monitor* m, bool timed_out) |
| 161 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 162 | |
| 163 | void AddMonitorCallback(MonitorCallback* cb) REQUIRES_SHARED(Locks::mutator_lock_); |
| 164 | void RemoveMonitorCallback(MonitorCallback* cb) REQUIRES_SHARED(Locks::mutator_lock_); |
| 165 | |
Alex Light | 2161193 | 2017-09-26 13:07:39 -0700 | [diff] [blame^] | 166 | // Returns true if some MethodInspectionCallback indicates the method is being inspected/depended |
| 167 | // on by some code. |
| 168 | bool IsMethodBeingInspected(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_); |
| 169 | |
| 170 | void AddMethodInspectionCallback(MethodInspectionCallback* cb) |
| 171 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 172 | void RemoveMethodInspectionCallback(MethodInspectionCallback* cb) |
| 173 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 174 | |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 175 | private: |
| 176 | std::vector<ThreadLifecycleCallback*> thread_callbacks_ |
| 177 | GUARDED_BY(Locks::mutator_lock_); |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 178 | std::vector<ClassLoadCallback*> class_callbacks_ |
| 179 | GUARDED_BY(Locks::mutator_lock_); |
Andreas Gampe | a5814f9 | 2017-01-18 21:43:16 -0800 | [diff] [blame] | 180 | std::vector<RuntimeSigQuitCallback*> sigquit_callbacks_ |
| 181 | GUARDED_BY(Locks::mutator_lock_); |
Andreas Gampe | 4886411 | 2017-01-19 17:23:17 -0800 | [diff] [blame] | 182 | std::vector<RuntimePhaseCallback*> phase_callbacks_ |
Alex Light | d78ddec | 2017-04-18 15:20:38 -0700 | [diff] [blame] | 183 | GUARDED_BY(Locks::mutator_lock_); |
| 184 | std::vector<MethodCallback*> method_callbacks_ |
| 185 | GUARDED_BY(Locks::mutator_lock_); |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 186 | std::vector<MonitorCallback*> monitor_callbacks_ |
| 187 | GUARDED_BY(Locks::mutator_lock_); |
Alex Light | 2161193 | 2017-09-26 13:07:39 -0700 | [diff] [blame^] | 188 | std::vector<MethodInspectionCallback*> method_inspection_callbacks_ |
| 189 | GUARDED_BY(Locks::mutator_lock_); |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | } // namespace art |
| 193 | |
| 194 | #endif // ART_RUNTIME_RUNTIME_CALLBACKS_H_ |