blob: 753ac280e3ac12cad4c101ff299366e70c2e4990 [file] [log] [blame]
Andreas Gampe04bbb5b2017-01-19 17:49:03 +00001/*
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 Lightd78ddec2017-04-18 15:20:38 -070021#include "art_method.h"
Andreas Gampea5814f92017-01-18 21:43:16 -080022#include "base/macros.h"
Alex Light51d5a302019-04-09 11:22:17 -070023#include "base/mutex-inl.h"
Andreas Gampe0f01b582017-01-18 15:22:37 -080024#include "class_linker.h"
Alex Light77fee872017-09-05 14:51:49 -070025#include "monitor.h"
Alex Light51d5a302019-04-09 11:22:17 -070026#include "thread-current-inl.h"
Andreas Gampe04bbb5b2017-01-19 17:49:03 +000027
28namespace art {
29
Alex Light51d5a302019-04-09 11:22:17 -070030RuntimeCallbacks::RuntimeCallbacks()
31 : callback_lock_(new ReaderWriterMutex("Runtime callbacks lock",
32 LockLevel::kGenericBottomLock)) {}
33
34// We don't want to be holding any locks when the actual event is called so we use this to define a
35// helper that gets a copy of the current event list and returns it.
36#define COPY(T) \
37 ([this]() -> decltype(this->T) { \
38 ReaderMutexLock mu(Thread::Current(), *this->callback_lock_); \
39 return std::vector<decltype(this->T)::value_type>(this->T); \
40 })()
41
Andreas Gampea5814f92017-01-18 21:43:16 -080042template <typename T>
43ALWAYS_INLINE
44static inline void Remove(T* cb, std::vector<T*>* data) {
45 auto it = std::find(data->begin(), data->end(), cb);
46 if (it != data->end()) {
47 data->erase(it);
Andreas Gampe04bbb5b2017-01-19 17:49:03 +000048 }
49}
50
Alex Light8c2b9292017-11-09 13:21:01 -080051void RuntimeCallbacks::AddDdmCallback(DdmCallback* cb) {
Alex Light51d5a302019-04-09 11:22:17 -070052 WriterMutexLock mu(Thread::Current(), *callback_lock_);
Alex Light8c2b9292017-11-09 13:21:01 -080053 ddm_callbacks_.push_back(cb);
54}
55
56void RuntimeCallbacks::RemoveDdmCallback(DdmCallback* cb) {
Alex Light51d5a302019-04-09 11:22:17 -070057 WriterMutexLock mu(Thread::Current(), *callback_lock_);
Alex Light8c2b9292017-11-09 13:21:01 -080058 Remove(cb, &ddm_callbacks_);
59}
60
61void RuntimeCallbacks::DdmPublishChunk(uint32_t type, const ArrayRef<const uint8_t>& data) {
Alex Light51d5a302019-04-09 11:22:17 -070062 for (DdmCallback* cb : COPY(ddm_callbacks_)) {
Alex Light8c2b9292017-11-09 13:21:01 -080063 cb->DdmPublishChunk(type, data);
64 }
65}
66
Alex Light40320712017-12-14 11:52:04 -080067void RuntimeCallbacks::AddDebuggerControlCallback(DebuggerControlCallback* cb) {
Alex Light51d5a302019-04-09 11:22:17 -070068 WriterMutexLock mu(Thread::Current(), *callback_lock_);
Alex Light40320712017-12-14 11:52:04 -080069 debugger_control_callbacks_.push_back(cb);
70}
71
72void RuntimeCallbacks::RemoveDebuggerControlCallback(DebuggerControlCallback* cb) {
Alex Light51d5a302019-04-09 11:22:17 -070073 WriterMutexLock mu(Thread::Current(), *callback_lock_);
Alex Light40320712017-12-14 11:52:04 -080074 Remove(cb, &debugger_control_callbacks_);
75}
76
77bool RuntimeCallbacks::IsDebuggerConfigured() {
Alex Light51d5a302019-04-09 11:22:17 -070078 for (DebuggerControlCallback* cb : COPY(debugger_control_callbacks_)) {
Alex Light40320712017-12-14 11:52:04 -080079 if (cb->IsDebuggerConfigured()) {
80 return true;
81 }
82 }
83 return false;
84}
85
86void RuntimeCallbacks::StartDebugger() {
Alex Light51d5a302019-04-09 11:22:17 -070087 for (DebuggerControlCallback* cb : COPY(debugger_control_callbacks_)) {
Alex Light40320712017-12-14 11:52:04 -080088 cb->StartDebugger();
89 }
90}
91
92void RuntimeCallbacks::StopDebugger() {
Alex Light51d5a302019-04-09 11:22:17 -070093 for (DebuggerControlCallback* cb : COPY(debugger_control_callbacks_)) {
Alex Light40320712017-12-14 11:52:04 -080094 cb->StopDebugger();
95 }
96}
97
Alex Light21611932017-09-26 13:07:39 -070098void RuntimeCallbacks::AddMethodInspectionCallback(MethodInspectionCallback* cb) {
Alex Light51d5a302019-04-09 11:22:17 -070099 WriterMutexLock mu(Thread::Current(), *callback_lock_);
Alex Light21611932017-09-26 13:07:39 -0700100 method_inspection_callbacks_.push_back(cb);
101}
102
103void RuntimeCallbacks::RemoveMethodInspectionCallback(MethodInspectionCallback* cb) {
Alex Light51d5a302019-04-09 11:22:17 -0700104 WriterMutexLock mu(Thread::Current(), *callback_lock_);
Alex Light21611932017-09-26 13:07:39 -0700105 Remove(cb, &method_inspection_callbacks_);
106}
107
108bool RuntimeCallbacks::IsMethodBeingInspected(ArtMethod* m) {
Alex Light51d5a302019-04-09 11:22:17 -0700109 for (MethodInspectionCallback* cb : COPY(method_inspection_callbacks_)) {
Alex Light21611932017-09-26 13:07:39 -0700110 if (cb->IsMethodBeingInspected(m)) {
111 return true;
112 }
113 }
114 return false;
115}
116
117void RuntimeCallbacks::AddThreadLifecycleCallback(ThreadLifecycleCallback* cb) {
Alex Light51d5a302019-04-09 11:22:17 -0700118 WriterMutexLock mu(Thread::Current(), *callback_lock_);
Alex Light21611932017-09-26 13:07:39 -0700119 thread_callbacks_.push_back(cb);
120}
121
Alex Light77fee872017-09-05 14:51:49 -0700122void RuntimeCallbacks::MonitorContendedLocking(Monitor* m) {
Alex Light51d5a302019-04-09 11:22:17 -0700123 for (MonitorCallback* cb : COPY(monitor_callbacks_)) {
Alex Light77fee872017-09-05 14:51:49 -0700124 cb->MonitorContendedLocking(m);
125 }
126}
127
128void RuntimeCallbacks::MonitorContendedLocked(Monitor* m) {
Alex Light51d5a302019-04-09 11:22:17 -0700129 for (MonitorCallback* cb : COPY(monitor_callbacks_)) {
Alex Light77fee872017-09-05 14:51:49 -0700130 cb->MonitorContendedLocked(m);
131 }
132}
133
134void RuntimeCallbacks::ObjectWaitStart(Handle<mirror::Object> m, int64_t timeout) {
Alex Light51d5a302019-04-09 11:22:17 -0700135 for (MonitorCallback* cb : COPY(monitor_callbacks_)) {
Alex Light77fee872017-09-05 14:51:49 -0700136 cb->ObjectWaitStart(m, timeout);
137 }
138}
139
140void RuntimeCallbacks::MonitorWaitFinished(Monitor* m, bool timeout) {
Alex Light51d5a302019-04-09 11:22:17 -0700141 for (MonitorCallback* cb : COPY(monitor_callbacks_)) {
Alex Light77fee872017-09-05 14:51:49 -0700142 cb->MonitorWaitFinished(m, timeout);
143 }
144}
145
146void RuntimeCallbacks::AddMonitorCallback(MonitorCallback* cb) {
Alex Light51d5a302019-04-09 11:22:17 -0700147 WriterMutexLock mu(Thread::Current(), *callback_lock_);
Alex Light77fee872017-09-05 14:51:49 -0700148 monitor_callbacks_.push_back(cb);
149}
150
151void RuntimeCallbacks::RemoveMonitorCallback(MonitorCallback* cb) {
Alex Light51d5a302019-04-09 11:22:17 -0700152 WriterMutexLock mu(Thread::Current(), *callback_lock_);
Alex Light77fee872017-09-05 14:51:49 -0700153 Remove(cb, &monitor_callbacks_);
154}
155
Charles Munger5cc0e752018-11-09 12:30:46 -0800156void RuntimeCallbacks::ThreadParkStart(bool is_absolute, int64_t timeout) {
Alex Light51d5a302019-04-09 11:22:17 -0700157 for (ParkCallback * cb : COPY(park_callbacks_)) {
Charles Munger5cc0e752018-11-09 12:30:46 -0800158 cb->ThreadParkStart(is_absolute, timeout);
159 }
160}
161
162void RuntimeCallbacks::ThreadParkFinished(bool timeout) {
Alex Light51d5a302019-04-09 11:22:17 -0700163 for (ParkCallback * cb : COPY(park_callbacks_)) {
Charles Munger5cc0e752018-11-09 12:30:46 -0800164 cb->ThreadParkFinished(timeout);
165 }
166}
167
168void RuntimeCallbacks::AddParkCallback(ParkCallback* cb) {
Alex Light51d5a302019-04-09 11:22:17 -0700169 WriterMutexLock mu(Thread::Current(), *callback_lock_);
Charles Munger5cc0e752018-11-09 12:30:46 -0800170 park_callbacks_.push_back(cb);
171}
172
173void RuntimeCallbacks::RemoveParkCallback(ParkCallback* cb) {
Alex Light51d5a302019-04-09 11:22:17 -0700174 WriterMutexLock mu(Thread::Current(), *callback_lock_);
Charles Munger5cc0e752018-11-09 12:30:46 -0800175 Remove(cb, &park_callbacks_);
176}
177
Andreas Gampea5814f92017-01-18 21:43:16 -0800178void RuntimeCallbacks::RemoveThreadLifecycleCallback(ThreadLifecycleCallback* cb) {
Alex Light51d5a302019-04-09 11:22:17 -0700179 WriterMutexLock mu(Thread::Current(), *callback_lock_);
Andreas Gampea5814f92017-01-18 21:43:16 -0800180 Remove(cb, &thread_callbacks_);
181}
182
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000183void RuntimeCallbacks::ThreadStart(Thread* self) {
Alex Light51d5a302019-04-09 11:22:17 -0700184 for (ThreadLifecycleCallback* cb : COPY(thread_callbacks_)) {
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000185 cb->ThreadStart(self);
186 }
187}
188
189void RuntimeCallbacks::ThreadDeath(Thread* self) {
Alex Light51d5a302019-04-09 11:22:17 -0700190 for (ThreadLifecycleCallback* cb : COPY(thread_callbacks_)) {
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000191 cb->ThreadDeath(self);
192 }
193}
194
Andreas Gampe0f01b582017-01-18 15:22:37 -0800195void RuntimeCallbacks::AddClassLoadCallback(ClassLoadCallback* cb) {
Alex Light51d5a302019-04-09 11:22:17 -0700196 WriterMutexLock mu(Thread::Current(), *callback_lock_);
Andreas Gampe0f01b582017-01-18 15:22:37 -0800197 class_callbacks_.push_back(cb);
198}
199
200void RuntimeCallbacks::RemoveClassLoadCallback(ClassLoadCallback* cb) {
Alex Light51d5a302019-04-09 11:22:17 -0700201 WriterMutexLock mu(Thread::Current(), *callback_lock_);
Andreas Gampea5814f92017-01-18 21:43:16 -0800202 Remove(cb, &class_callbacks_);
Andreas Gampe0f01b582017-01-18 15:22:37 -0800203}
204
205void RuntimeCallbacks::ClassLoad(Handle<mirror::Class> klass) {
Alex Light51d5a302019-04-09 11:22:17 -0700206 for (ClassLoadCallback* cb : COPY(class_callbacks_)) {
Andreas Gampe0f01b582017-01-18 15:22:37 -0800207 cb->ClassLoad(klass);
208 }
209}
210
Alex Light270db1c2019-12-03 12:20:01 +0000211void RuntimeCallbacks::EndDefineClass() {
212 for (ClassLoadCallback* cb : COPY(class_callbacks_)) {
213 cb->EndDefineClass();
214 }
215}
216
217void RuntimeCallbacks::BeginDefineClass() {
218 for (ClassLoadCallback* cb : COPY(class_callbacks_)) {
219 cb->BeginDefineClass();
220 }
221}
222
223
Alex Lightb0f11922017-01-23 14:25:17 -0800224void RuntimeCallbacks::ClassPreDefine(const char* descriptor,
225 Handle<mirror::Class> temp_class,
226 Handle<mirror::ClassLoader> loader,
227 const DexFile& initial_dex_file,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800228 const dex::ClassDef& initial_class_def,
Alex Lightb0f11922017-01-23 14:25:17 -0800229 /*out*/DexFile const** final_dex_file,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800230 /*out*/dex::ClassDef const** final_class_def) {
Alex Lightb0f11922017-01-23 14:25:17 -0800231 DexFile const* current_dex_file = &initial_dex_file;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800232 dex::ClassDef const* current_class_def = &initial_class_def;
Alex Light51d5a302019-04-09 11:22:17 -0700233 for (ClassLoadCallback* cb : COPY(class_callbacks_)) {
Alex Lightb0f11922017-01-23 14:25:17 -0800234 DexFile const* new_dex_file = nullptr;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800235 dex::ClassDef const* new_class_def = nullptr;
Alex Lightb0f11922017-01-23 14:25:17 -0800236 cb->ClassPreDefine(descriptor,
237 temp_class,
238 loader,
239 *current_dex_file,
240 *current_class_def,
241 &new_dex_file,
242 &new_class_def);
243 if ((new_dex_file != nullptr && new_dex_file != current_dex_file) ||
244 (new_class_def != nullptr && new_class_def != current_class_def)) {
245 DCHECK(new_dex_file != nullptr && new_class_def != nullptr);
246 current_dex_file = new_dex_file;
247 current_class_def = new_class_def;
248 }
249 }
250 *final_dex_file = current_dex_file;
251 *final_class_def = current_class_def;
252}
253
Andreas Gampe0f01b582017-01-18 15:22:37 -0800254void RuntimeCallbacks::ClassPrepare(Handle<mirror::Class> temp_klass, Handle<mirror::Class> klass) {
Alex Light51d5a302019-04-09 11:22:17 -0700255 for (ClassLoadCallback* cb : COPY(class_callbacks_)) {
Andreas Gampe0f01b582017-01-18 15:22:37 -0800256 cb->ClassPrepare(temp_klass, klass);
257 }
258}
259
Andreas Gampea5814f92017-01-18 21:43:16 -0800260void RuntimeCallbacks::AddRuntimeSigQuitCallback(RuntimeSigQuitCallback* cb) {
Alex Light51d5a302019-04-09 11:22:17 -0700261 WriterMutexLock mu(Thread::Current(), *callback_lock_);
Andreas Gampea5814f92017-01-18 21:43:16 -0800262 sigquit_callbacks_.push_back(cb);
263}
264
265void RuntimeCallbacks::RemoveRuntimeSigQuitCallback(RuntimeSigQuitCallback* cb) {
Alex Light51d5a302019-04-09 11:22:17 -0700266 WriterMutexLock mu(Thread::Current(), *callback_lock_);
Andreas Gampea5814f92017-01-18 21:43:16 -0800267 Remove(cb, &sigquit_callbacks_);
268}
269
270void RuntimeCallbacks::SigQuit() {
Alex Light51d5a302019-04-09 11:22:17 -0700271 for (RuntimeSigQuitCallback* cb : COPY(sigquit_callbacks_)) {
Andreas Gampea5814f92017-01-18 21:43:16 -0800272 cb->SigQuit();
273 }
274}
275
Andreas Gampe48864112017-01-19 17:23:17 -0800276void RuntimeCallbacks::AddRuntimePhaseCallback(RuntimePhaseCallback* cb) {
Alex Light51d5a302019-04-09 11:22:17 -0700277 WriterMutexLock mu(Thread::Current(), *callback_lock_);
Andreas Gampe48864112017-01-19 17:23:17 -0800278 phase_callbacks_.push_back(cb);
279}
280
281void RuntimeCallbacks::RemoveRuntimePhaseCallback(RuntimePhaseCallback* cb) {
Alex Light51d5a302019-04-09 11:22:17 -0700282 WriterMutexLock mu(Thread::Current(), *callback_lock_);
Andreas Gampe48864112017-01-19 17:23:17 -0800283 Remove(cb, &phase_callbacks_);
284}
285
286void RuntimeCallbacks::NextRuntimePhase(RuntimePhaseCallback::RuntimePhase phase) {
Alex Light51d5a302019-04-09 11:22:17 -0700287 for (RuntimePhaseCallback* cb : COPY(phase_callbacks_)) {
Andreas Gampe48864112017-01-19 17:23:17 -0800288 cb->NextRuntimePhase(phase);
289 }
290}
291
Alex Lightd78ddec2017-04-18 15:20:38 -0700292void RuntimeCallbacks::AddMethodCallback(MethodCallback* cb) {
Alex Light51d5a302019-04-09 11:22:17 -0700293 WriterMutexLock mu(Thread::Current(), *callback_lock_);
Alex Lightd78ddec2017-04-18 15:20:38 -0700294 method_callbacks_.push_back(cb);
295}
296
297void RuntimeCallbacks::RemoveMethodCallback(MethodCallback* cb) {
Alex Light51d5a302019-04-09 11:22:17 -0700298 WriterMutexLock mu(Thread::Current(), *callback_lock_);
Alex Lightd78ddec2017-04-18 15:20:38 -0700299 Remove(cb, &method_callbacks_);
300}
301
302void RuntimeCallbacks::RegisterNativeMethod(ArtMethod* method,
303 const void* in_cur_method,
304 /*out*/void** new_method) {
305 void* cur_method = const_cast<void*>(in_cur_method);
306 *new_method = cur_method;
Alex Light51d5a302019-04-09 11:22:17 -0700307 for (MethodCallback* cb : COPY(method_callbacks_)) {
Alex Lightd78ddec2017-04-18 15:20:38 -0700308 cb->RegisterNativeMethod(method, cur_method, new_method);
309 if (*new_method != nullptr) {
310 cur_method = *new_method;
311 }
312 }
313}
314
Alex Lightc18eba32019-09-24 14:36:27 -0700315void RuntimeCallbacks::AddReflectiveValueVisitCallback(ReflectiveValueVisitCallback *cb) {
316 WriterMutexLock mu(Thread::Current(), *callback_lock_);
317 reflective_value_visit_callbacks_.push_back(cb);
318}
319
320void RuntimeCallbacks::RemoveReflectiveValueVisitCallback(ReflectiveValueVisitCallback *cb) {
321 WriterMutexLock mu(Thread::Current(), *callback_lock_);
322 Remove(cb, &reflective_value_visit_callbacks_);
323}
324
325void RuntimeCallbacks::VisitReflectiveTargets(ReflectiveValueVisitor *visitor) {
326 for (ReflectiveValueVisitCallback* cb : COPY(reflective_value_visit_callbacks_)) {
327 cb->VisitReflectiveTargets(visitor);
328 }
329}
330
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000331} // namespace art