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 | #include "runtime_callbacks.h" |
| 18 | |
| 19 | #include <algorithm> |
| 20 | |
Alex Light | d78ddec | 2017-04-18 15:20:38 -0700 | [diff] [blame] | 21 | #include "art_method.h" |
Andreas Gampe | a5814f9 | 2017-01-18 21:43:16 -0800 | [diff] [blame] | 22 | #include "base/macros.h" |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 23 | #include "class_linker.h" |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 24 | #include "monitor.h" |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 25 | #include "thread.h" |
| 26 | |
| 27 | namespace art { |
| 28 | |
Andreas Gampe | a5814f9 | 2017-01-18 21:43:16 -0800 | [diff] [blame] | 29 | template <typename T> |
| 30 | ALWAYS_INLINE |
| 31 | static inline void Remove(T* cb, std::vector<T*>* data) { |
| 32 | auto it = std::find(data->begin(), data->end(), cb); |
| 33 | if (it != data->end()) { |
| 34 | data->erase(it); |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 35 | } |
| 36 | } |
| 37 | |
Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame^] | 38 | void RuntimeCallbacks::AddDdmCallback(DdmCallback* cb) { |
| 39 | ddm_callbacks_.push_back(cb); |
| 40 | } |
| 41 | |
| 42 | void RuntimeCallbacks::RemoveDdmCallback(DdmCallback* cb) { |
| 43 | Remove(cb, &ddm_callbacks_); |
| 44 | } |
| 45 | |
| 46 | void RuntimeCallbacks::DdmPublishChunk(uint32_t type, const ArrayRef<const uint8_t>& data) { |
| 47 | for (DdmCallback* cb : ddm_callbacks_) { |
| 48 | cb->DdmPublishChunk(type, data); |
| 49 | } |
| 50 | } |
| 51 | |
Alex Light | 2161193 | 2017-09-26 13:07:39 -0700 | [diff] [blame] | 52 | void RuntimeCallbacks::AddMethodInspectionCallback(MethodInspectionCallback* cb) { |
| 53 | method_inspection_callbacks_.push_back(cb); |
| 54 | } |
| 55 | |
| 56 | void RuntimeCallbacks::RemoveMethodInspectionCallback(MethodInspectionCallback* cb) { |
| 57 | Remove(cb, &method_inspection_callbacks_); |
| 58 | } |
| 59 | |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 60 | bool RuntimeCallbacks::IsMethodSafeToJit(ArtMethod* m) { |
| 61 | for (MethodInspectionCallback* cb : method_inspection_callbacks_) { |
| 62 | if (!cb->IsMethodSafeToJit(m)) { |
| 63 | DCHECK(cb->IsMethodBeingInspected(m)) |
| 64 | << "Contract requires that !IsMethodSafeToJit(m) -> IsMethodBeingInspected(m)"; |
| 65 | return false; |
| 66 | } |
| 67 | } |
| 68 | return true; |
| 69 | } |
| 70 | |
Alex Light | 2161193 | 2017-09-26 13:07:39 -0700 | [diff] [blame] | 71 | bool RuntimeCallbacks::IsMethodBeingInspected(ArtMethod* m) { |
| 72 | for (MethodInspectionCallback* cb : method_inspection_callbacks_) { |
| 73 | if (cb->IsMethodBeingInspected(m)) { |
| 74 | return true; |
| 75 | } |
| 76 | } |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | void RuntimeCallbacks::AddThreadLifecycleCallback(ThreadLifecycleCallback* cb) { |
| 81 | thread_callbacks_.push_back(cb); |
| 82 | } |
| 83 | |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 84 | void RuntimeCallbacks::MonitorContendedLocking(Monitor* m) { |
| 85 | for (MonitorCallback* cb : monitor_callbacks_) { |
| 86 | cb->MonitorContendedLocking(m); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void RuntimeCallbacks::MonitorContendedLocked(Monitor* m) { |
| 91 | for (MonitorCallback* cb : monitor_callbacks_) { |
| 92 | cb->MonitorContendedLocked(m); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | void RuntimeCallbacks::ObjectWaitStart(Handle<mirror::Object> m, int64_t timeout) { |
| 97 | for (MonitorCallback* cb : monitor_callbacks_) { |
| 98 | cb->ObjectWaitStart(m, timeout); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void RuntimeCallbacks::MonitorWaitFinished(Monitor* m, bool timeout) { |
| 103 | for (MonitorCallback* cb : monitor_callbacks_) { |
| 104 | cb->MonitorWaitFinished(m, timeout); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void RuntimeCallbacks::AddMonitorCallback(MonitorCallback* cb) { |
| 109 | monitor_callbacks_.push_back(cb); |
| 110 | } |
| 111 | |
| 112 | void RuntimeCallbacks::RemoveMonitorCallback(MonitorCallback* cb) { |
| 113 | Remove(cb, &monitor_callbacks_); |
| 114 | } |
| 115 | |
Andreas Gampe | a5814f9 | 2017-01-18 21:43:16 -0800 | [diff] [blame] | 116 | void RuntimeCallbacks::RemoveThreadLifecycleCallback(ThreadLifecycleCallback* cb) { |
| 117 | Remove(cb, &thread_callbacks_); |
| 118 | } |
| 119 | |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 120 | void RuntimeCallbacks::ThreadStart(Thread* self) { |
| 121 | for (ThreadLifecycleCallback* cb : thread_callbacks_) { |
| 122 | cb->ThreadStart(self); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | void RuntimeCallbacks::ThreadDeath(Thread* self) { |
| 127 | for (ThreadLifecycleCallback* cb : thread_callbacks_) { |
| 128 | cb->ThreadDeath(self); |
| 129 | } |
| 130 | } |
| 131 | |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 132 | void RuntimeCallbacks::AddClassLoadCallback(ClassLoadCallback* cb) { |
| 133 | class_callbacks_.push_back(cb); |
| 134 | } |
| 135 | |
| 136 | void RuntimeCallbacks::RemoveClassLoadCallback(ClassLoadCallback* cb) { |
Andreas Gampe | a5814f9 | 2017-01-18 21:43:16 -0800 | [diff] [blame] | 137 | Remove(cb, &class_callbacks_); |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | void RuntimeCallbacks::ClassLoad(Handle<mirror::Class> klass) { |
| 141 | for (ClassLoadCallback* cb : class_callbacks_) { |
| 142 | cb->ClassLoad(klass); |
| 143 | } |
| 144 | } |
| 145 | |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 146 | void RuntimeCallbacks::ClassPreDefine(const char* descriptor, |
| 147 | Handle<mirror::Class> temp_class, |
| 148 | Handle<mirror::ClassLoader> loader, |
| 149 | const DexFile& initial_dex_file, |
| 150 | const DexFile::ClassDef& initial_class_def, |
| 151 | /*out*/DexFile const** final_dex_file, |
| 152 | /*out*/DexFile::ClassDef const** final_class_def) { |
| 153 | DexFile const* current_dex_file = &initial_dex_file; |
| 154 | DexFile::ClassDef const* current_class_def = &initial_class_def; |
| 155 | for (ClassLoadCallback* cb : class_callbacks_) { |
| 156 | DexFile const* new_dex_file = nullptr; |
| 157 | DexFile::ClassDef const* new_class_def = nullptr; |
| 158 | cb->ClassPreDefine(descriptor, |
| 159 | temp_class, |
| 160 | loader, |
| 161 | *current_dex_file, |
| 162 | *current_class_def, |
| 163 | &new_dex_file, |
| 164 | &new_class_def); |
| 165 | if ((new_dex_file != nullptr && new_dex_file != current_dex_file) || |
| 166 | (new_class_def != nullptr && new_class_def != current_class_def)) { |
| 167 | DCHECK(new_dex_file != nullptr && new_class_def != nullptr); |
| 168 | current_dex_file = new_dex_file; |
| 169 | current_class_def = new_class_def; |
| 170 | } |
| 171 | } |
| 172 | *final_dex_file = current_dex_file; |
| 173 | *final_class_def = current_class_def; |
| 174 | } |
| 175 | |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 176 | void RuntimeCallbacks::ClassPrepare(Handle<mirror::Class> temp_klass, Handle<mirror::Class> klass) { |
| 177 | for (ClassLoadCallback* cb : class_callbacks_) { |
| 178 | cb->ClassPrepare(temp_klass, klass); |
| 179 | } |
| 180 | } |
| 181 | |
Andreas Gampe | a5814f9 | 2017-01-18 21:43:16 -0800 | [diff] [blame] | 182 | void RuntimeCallbacks::AddRuntimeSigQuitCallback(RuntimeSigQuitCallback* cb) { |
| 183 | sigquit_callbacks_.push_back(cb); |
| 184 | } |
| 185 | |
| 186 | void RuntimeCallbacks::RemoveRuntimeSigQuitCallback(RuntimeSigQuitCallback* cb) { |
| 187 | Remove(cb, &sigquit_callbacks_); |
| 188 | } |
| 189 | |
| 190 | void RuntimeCallbacks::SigQuit() { |
| 191 | for (RuntimeSigQuitCallback* cb : sigquit_callbacks_) { |
| 192 | cb->SigQuit(); |
| 193 | } |
| 194 | } |
| 195 | |
Andreas Gampe | 4886411 | 2017-01-19 17:23:17 -0800 | [diff] [blame] | 196 | void RuntimeCallbacks::AddRuntimePhaseCallback(RuntimePhaseCallback* cb) { |
| 197 | phase_callbacks_.push_back(cb); |
| 198 | } |
| 199 | |
| 200 | void RuntimeCallbacks::RemoveRuntimePhaseCallback(RuntimePhaseCallback* cb) { |
| 201 | Remove(cb, &phase_callbacks_); |
| 202 | } |
| 203 | |
| 204 | void RuntimeCallbacks::NextRuntimePhase(RuntimePhaseCallback::RuntimePhase phase) { |
| 205 | for (RuntimePhaseCallback* cb : phase_callbacks_) { |
| 206 | cb->NextRuntimePhase(phase); |
| 207 | } |
| 208 | } |
| 209 | |
Alex Light | d78ddec | 2017-04-18 15:20:38 -0700 | [diff] [blame] | 210 | void RuntimeCallbacks::AddMethodCallback(MethodCallback* cb) { |
| 211 | method_callbacks_.push_back(cb); |
| 212 | } |
| 213 | |
| 214 | void RuntimeCallbacks::RemoveMethodCallback(MethodCallback* cb) { |
| 215 | Remove(cb, &method_callbacks_); |
| 216 | } |
| 217 | |
| 218 | void RuntimeCallbacks::RegisterNativeMethod(ArtMethod* method, |
| 219 | const void* in_cur_method, |
| 220 | /*out*/void** new_method) { |
| 221 | void* cur_method = const_cast<void*>(in_cur_method); |
| 222 | *new_method = cur_method; |
| 223 | for (MethodCallback* cb : method_callbacks_) { |
| 224 | cb->RegisterNativeMethod(method, cur_method, new_method); |
| 225 | if (*new_method != nullptr) { |
| 226 | cur_method = *new_method; |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 231 | } // namespace art |