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