blob: 40976c23aef06321d1cd618cf906ce9d0d00d7b7 [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
Alex Light0fa17862017-10-24 13:43:05 -0700108bool RuntimeCallbacks::IsMethodSafeToJit(ArtMethod* m) {
Alex Light51d5a302019-04-09 11:22:17 -0700109 for (MethodInspectionCallback* cb : COPY(method_inspection_callbacks_)) {
Alex Light0fa17862017-10-24 13:43:05 -0700110 if (!cb->IsMethodSafeToJit(m)) {
111 DCHECK(cb->IsMethodBeingInspected(m))
112 << "Contract requires that !IsMethodSafeToJit(m) -> IsMethodBeingInspected(m)";
113 return false;
114 }
115 }
116 return true;
117}
118
Alex Light21611932017-09-26 13:07:39 -0700119bool RuntimeCallbacks::IsMethodBeingInspected(ArtMethod* m) {
Alex Light51d5a302019-04-09 11:22:17 -0700120 for (MethodInspectionCallback* cb : COPY(method_inspection_callbacks_)) {
Alex Light21611932017-09-26 13:07:39 -0700121 if (cb->IsMethodBeingInspected(m)) {
122 return true;
123 }
124 }
125 return false;
126}
127
Alex Lightf2858632018-04-02 11:28:50 -0700128bool RuntimeCallbacks::MethodNeedsDebugVersion(ArtMethod* m) {
Alex Light51d5a302019-04-09 11:22:17 -0700129 for (MethodInspectionCallback* cb : COPY(method_inspection_callbacks_)) {
Alex Lightf2858632018-04-02 11:28:50 -0700130 if (cb->MethodNeedsDebugVersion(m)) {
131 return true;
132 }
133 }
134 return false;
135}
136
Alex Light21611932017-09-26 13:07:39 -0700137void RuntimeCallbacks::AddThreadLifecycleCallback(ThreadLifecycleCallback* cb) {
Alex Light51d5a302019-04-09 11:22:17 -0700138 WriterMutexLock mu(Thread::Current(), *callback_lock_);
Alex Light21611932017-09-26 13:07:39 -0700139 thread_callbacks_.push_back(cb);
140}
141
Alex Light77fee872017-09-05 14:51:49 -0700142void RuntimeCallbacks::MonitorContendedLocking(Monitor* m) {
Alex Light51d5a302019-04-09 11:22:17 -0700143 for (MonitorCallback* cb : COPY(monitor_callbacks_)) {
Alex Light77fee872017-09-05 14:51:49 -0700144 cb->MonitorContendedLocking(m);
145 }
146}
147
148void RuntimeCallbacks::MonitorContendedLocked(Monitor* m) {
Alex Light51d5a302019-04-09 11:22:17 -0700149 for (MonitorCallback* cb : COPY(monitor_callbacks_)) {
Alex Light77fee872017-09-05 14:51:49 -0700150 cb->MonitorContendedLocked(m);
151 }
152}
153
154void RuntimeCallbacks::ObjectWaitStart(Handle<mirror::Object> m, int64_t timeout) {
Alex Light51d5a302019-04-09 11:22:17 -0700155 for (MonitorCallback* cb : COPY(monitor_callbacks_)) {
Alex Light77fee872017-09-05 14:51:49 -0700156 cb->ObjectWaitStart(m, timeout);
157 }
158}
159
160void RuntimeCallbacks::MonitorWaitFinished(Monitor* m, bool timeout) {
Alex Light51d5a302019-04-09 11:22:17 -0700161 for (MonitorCallback* cb : COPY(monitor_callbacks_)) {
Alex Light77fee872017-09-05 14:51:49 -0700162 cb->MonitorWaitFinished(m, timeout);
163 }
164}
165
166void RuntimeCallbacks::AddMonitorCallback(MonitorCallback* cb) {
Alex Light51d5a302019-04-09 11:22:17 -0700167 WriterMutexLock mu(Thread::Current(), *callback_lock_);
Alex Light77fee872017-09-05 14:51:49 -0700168 monitor_callbacks_.push_back(cb);
169}
170
171void RuntimeCallbacks::RemoveMonitorCallback(MonitorCallback* cb) {
Alex Light51d5a302019-04-09 11:22:17 -0700172 WriterMutexLock mu(Thread::Current(), *callback_lock_);
Alex Light77fee872017-09-05 14:51:49 -0700173 Remove(cb, &monitor_callbacks_);
174}
175
Charles Munger5cc0e752018-11-09 12:30:46 -0800176void RuntimeCallbacks::ThreadParkStart(bool is_absolute, int64_t timeout) {
Alex Light51d5a302019-04-09 11:22:17 -0700177 for (ParkCallback * cb : COPY(park_callbacks_)) {
Charles Munger5cc0e752018-11-09 12:30:46 -0800178 cb->ThreadParkStart(is_absolute, timeout);
179 }
180}
181
182void RuntimeCallbacks::ThreadParkFinished(bool timeout) {
Alex Light51d5a302019-04-09 11:22:17 -0700183 for (ParkCallback * cb : COPY(park_callbacks_)) {
Charles Munger5cc0e752018-11-09 12:30:46 -0800184 cb->ThreadParkFinished(timeout);
185 }
186}
187
188void RuntimeCallbacks::AddParkCallback(ParkCallback* cb) {
Alex Light51d5a302019-04-09 11:22:17 -0700189 WriterMutexLock mu(Thread::Current(), *callback_lock_);
Charles Munger5cc0e752018-11-09 12:30:46 -0800190 park_callbacks_.push_back(cb);
191}
192
193void RuntimeCallbacks::RemoveParkCallback(ParkCallback* cb) {
Alex Light51d5a302019-04-09 11:22:17 -0700194 WriterMutexLock mu(Thread::Current(), *callback_lock_);
Charles Munger5cc0e752018-11-09 12:30:46 -0800195 Remove(cb, &park_callbacks_);
196}
197
Andreas Gampea5814f92017-01-18 21:43:16 -0800198void RuntimeCallbacks::RemoveThreadLifecycleCallback(ThreadLifecycleCallback* cb) {
Alex Light51d5a302019-04-09 11:22:17 -0700199 WriterMutexLock mu(Thread::Current(), *callback_lock_);
Andreas Gampea5814f92017-01-18 21:43:16 -0800200 Remove(cb, &thread_callbacks_);
201}
202
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000203void RuntimeCallbacks::ThreadStart(Thread* self) {
Alex Light51d5a302019-04-09 11:22:17 -0700204 for (ThreadLifecycleCallback* cb : COPY(thread_callbacks_)) {
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000205 cb->ThreadStart(self);
206 }
207}
208
209void RuntimeCallbacks::ThreadDeath(Thread* self) {
Alex Light51d5a302019-04-09 11:22:17 -0700210 for (ThreadLifecycleCallback* cb : COPY(thread_callbacks_)) {
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000211 cb->ThreadDeath(self);
212 }
213}
214
Andreas Gampe0f01b582017-01-18 15:22:37 -0800215void RuntimeCallbacks::AddClassLoadCallback(ClassLoadCallback* cb) {
Alex Light51d5a302019-04-09 11:22:17 -0700216 WriterMutexLock mu(Thread::Current(), *callback_lock_);
Andreas Gampe0f01b582017-01-18 15:22:37 -0800217 class_callbacks_.push_back(cb);
218}
219
220void RuntimeCallbacks::RemoveClassLoadCallback(ClassLoadCallback* cb) {
Alex Light51d5a302019-04-09 11:22:17 -0700221 WriterMutexLock mu(Thread::Current(), *callback_lock_);
Andreas Gampea5814f92017-01-18 21:43:16 -0800222 Remove(cb, &class_callbacks_);
Andreas Gampe0f01b582017-01-18 15:22:37 -0800223}
224
225void RuntimeCallbacks::ClassLoad(Handle<mirror::Class> klass) {
Alex Light51d5a302019-04-09 11:22:17 -0700226 for (ClassLoadCallback* cb : COPY(class_callbacks_)) {
Andreas Gampe0f01b582017-01-18 15:22:37 -0800227 cb->ClassLoad(klass);
228 }
229}
230
Alex Lightb0f11922017-01-23 14:25:17 -0800231void RuntimeCallbacks::ClassPreDefine(const char* descriptor,
232 Handle<mirror::Class> temp_class,
233 Handle<mirror::ClassLoader> loader,
234 const DexFile& initial_dex_file,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800235 const dex::ClassDef& initial_class_def,
Alex Lightb0f11922017-01-23 14:25:17 -0800236 /*out*/DexFile const** final_dex_file,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800237 /*out*/dex::ClassDef const** final_class_def) {
Alex Lightb0f11922017-01-23 14:25:17 -0800238 DexFile const* current_dex_file = &initial_dex_file;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800239 dex::ClassDef const* current_class_def = &initial_class_def;
Alex Light51d5a302019-04-09 11:22:17 -0700240 for (ClassLoadCallback* cb : COPY(class_callbacks_)) {
Alex Lightb0f11922017-01-23 14:25:17 -0800241 DexFile const* new_dex_file = nullptr;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800242 dex::ClassDef const* new_class_def = nullptr;
Alex Lightb0f11922017-01-23 14:25:17 -0800243 cb->ClassPreDefine(descriptor,
244 temp_class,
245 loader,
246 *current_dex_file,
247 *current_class_def,
248 &new_dex_file,
249 &new_class_def);
250 if ((new_dex_file != nullptr && new_dex_file != current_dex_file) ||
251 (new_class_def != nullptr && new_class_def != current_class_def)) {
252 DCHECK(new_dex_file != nullptr && new_class_def != nullptr);
253 current_dex_file = new_dex_file;
254 current_class_def = new_class_def;
255 }
256 }
257 *final_dex_file = current_dex_file;
258 *final_class_def = current_class_def;
259}
260
Andreas Gampe0f01b582017-01-18 15:22:37 -0800261void RuntimeCallbacks::ClassPrepare(Handle<mirror::Class> temp_klass, Handle<mirror::Class> klass) {
Alex Light51d5a302019-04-09 11:22:17 -0700262 for (ClassLoadCallback* cb : COPY(class_callbacks_)) {
Andreas Gampe0f01b582017-01-18 15:22:37 -0800263 cb->ClassPrepare(temp_klass, klass);
264 }
265}
266
Andreas Gampea5814f92017-01-18 21:43:16 -0800267void RuntimeCallbacks::AddRuntimeSigQuitCallback(RuntimeSigQuitCallback* cb) {
Alex Light51d5a302019-04-09 11:22:17 -0700268 WriterMutexLock mu(Thread::Current(), *callback_lock_);
Andreas Gampea5814f92017-01-18 21:43:16 -0800269 sigquit_callbacks_.push_back(cb);
270}
271
272void RuntimeCallbacks::RemoveRuntimeSigQuitCallback(RuntimeSigQuitCallback* cb) {
Alex Light51d5a302019-04-09 11:22:17 -0700273 WriterMutexLock mu(Thread::Current(), *callback_lock_);
Andreas Gampea5814f92017-01-18 21:43:16 -0800274 Remove(cb, &sigquit_callbacks_);
275}
276
277void RuntimeCallbacks::SigQuit() {
Alex Light51d5a302019-04-09 11:22:17 -0700278 for (RuntimeSigQuitCallback* cb : COPY(sigquit_callbacks_)) {
Andreas Gampea5814f92017-01-18 21:43:16 -0800279 cb->SigQuit();
280 }
281}
282
Andreas Gampe48864112017-01-19 17:23:17 -0800283void RuntimeCallbacks::AddRuntimePhaseCallback(RuntimePhaseCallback* cb) {
Alex Light51d5a302019-04-09 11:22:17 -0700284 WriterMutexLock mu(Thread::Current(), *callback_lock_);
Andreas Gampe48864112017-01-19 17:23:17 -0800285 phase_callbacks_.push_back(cb);
286}
287
288void RuntimeCallbacks::RemoveRuntimePhaseCallback(RuntimePhaseCallback* cb) {
Alex Light51d5a302019-04-09 11:22:17 -0700289 WriterMutexLock mu(Thread::Current(), *callback_lock_);
Andreas Gampe48864112017-01-19 17:23:17 -0800290 Remove(cb, &phase_callbacks_);
291}
292
293void RuntimeCallbacks::NextRuntimePhase(RuntimePhaseCallback::RuntimePhase phase) {
Alex Light51d5a302019-04-09 11:22:17 -0700294 for (RuntimePhaseCallback* cb : COPY(phase_callbacks_)) {
Andreas Gampe48864112017-01-19 17:23:17 -0800295 cb->NextRuntimePhase(phase);
296 }
297}
298
Alex Lightd78ddec2017-04-18 15:20:38 -0700299void RuntimeCallbacks::AddMethodCallback(MethodCallback* cb) {
Alex Light51d5a302019-04-09 11:22:17 -0700300 WriterMutexLock mu(Thread::Current(), *callback_lock_);
Alex Lightd78ddec2017-04-18 15:20:38 -0700301 method_callbacks_.push_back(cb);
302}
303
304void RuntimeCallbacks::RemoveMethodCallback(MethodCallback* cb) {
Alex Light51d5a302019-04-09 11:22:17 -0700305 WriterMutexLock mu(Thread::Current(), *callback_lock_);
Alex Lightd78ddec2017-04-18 15:20:38 -0700306 Remove(cb, &method_callbacks_);
307}
308
309void RuntimeCallbacks::RegisterNativeMethod(ArtMethod* method,
310 const void* in_cur_method,
311 /*out*/void** new_method) {
312 void* cur_method = const_cast<void*>(in_cur_method);
313 *new_method = cur_method;
Alex Light51d5a302019-04-09 11:22:17 -0700314 for (MethodCallback* cb : COPY(method_callbacks_)) {
Alex Lightd78ddec2017-04-18 15:20:38 -0700315 cb->RegisterNativeMethod(method, cur_method, new_method);
316 if (*new_method != nullptr) {
317 cur_method = *new_method;
318 }
319 }
320}
321
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000322} // namespace art