blob: cd3c0b7c88fd882897a693a987539bc0eb88f4ec [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"
Andreas Gampe0f01b582017-01-18 15:22:37 -080023#include "class_linker.h"
Alex Light77fee872017-09-05 14:51:49 -070024#include "monitor.h"
Andreas Gampe04bbb5b2017-01-19 17:49:03 +000025#include "thread.h"
26
27namespace art {
28
Andreas Gampea5814f92017-01-18 21:43:16 -080029template <typename T>
30ALWAYS_INLINE
31static 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 Gampe04bbb5b2017-01-19 17:49:03 +000035 }
36}
37
Alex Light8c2b9292017-11-09 13:21:01 -080038void RuntimeCallbacks::AddDdmCallback(DdmCallback* cb) {
39 ddm_callbacks_.push_back(cb);
40}
41
42void RuntimeCallbacks::RemoveDdmCallback(DdmCallback* cb) {
43 Remove(cb, &ddm_callbacks_);
44}
45
46void 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 Light40320712017-12-14 11:52:04 -080052void RuntimeCallbacks::AddDebuggerControlCallback(DebuggerControlCallback* cb) {
53 debugger_control_callbacks_.push_back(cb);
54}
55
56void RuntimeCallbacks::RemoveDebuggerControlCallback(DebuggerControlCallback* cb) {
57 Remove(cb, &debugger_control_callbacks_);
58}
59
60bool RuntimeCallbacks::IsDebuggerConfigured() {
61 for (DebuggerControlCallback* cb : debugger_control_callbacks_) {
62 if (cb->IsDebuggerConfigured()) {
63 return true;
64 }
65 }
66 return false;
67}
68
69void RuntimeCallbacks::StartDebugger() {
70 for (DebuggerControlCallback* cb : debugger_control_callbacks_) {
71 cb->StartDebugger();
72 }
73}
74
75void RuntimeCallbacks::StopDebugger() {
76 for (DebuggerControlCallback* cb : debugger_control_callbacks_) {
77 cb->StopDebugger();
78 }
79}
80
Alex Light21611932017-09-26 13:07:39 -070081void RuntimeCallbacks::AddMethodInspectionCallback(MethodInspectionCallback* cb) {
82 method_inspection_callbacks_.push_back(cb);
83}
84
85void RuntimeCallbacks::RemoveMethodInspectionCallback(MethodInspectionCallback* cb) {
86 Remove(cb, &method_inspection_callbacks_);
87}
88
Alex Light0fa17862017-10-24 13:43:05 -070089bool RuntimeCallbacks::IsMethodSafeToJit(ArtMethod* m) {
90 for (MethodInspectionCallback* cb : method_inspection_callbacks_) {
91 if (!cb->IsMethodSafeToJit(m)) {
92 DCHECK(cb->IsMethodBeingInspected(m))
93 << "Contract requires that !IsMethodSafeToJit(m) -> IsMethodBeingInspected(m)";
94 return false;
95 }
96 }
97 return true;
98}
99
Alex Light21611932017-09-26 13:07:39 -0700100bool RuntimeCallbacks::IsMethodBeingInspected(ArtMethod* m) {
101 for (MethodInspectionCallback* cb : method_inspection_callbacks_) {
102 if (cb->IsMethodBeingInspected(m)) {
103 return true;
104 }
105 }
106 return false;
107}
108
109void RuntimeCallbacks::AddThreadLifecycleCallback(ThreadLifecycleCallback* cb) {
110 thread_callbacks_.push_back(cb);
111}
112
Alex Light77fee872017-09-05 14:51:49 -0700113void RuntimeCallbacks::MonitorContendedLocking(Monitor* m) {
114 for (MonitorCallback* cb : monitor_callbacks_) {
115 cb->MonitorContendedLocking(m);
116 }
117}
118
119void RuntimeCallbacks::MonitorContendedLocked(Monitor* m) {
120 for (MonitorCallback* cb : monitor_callbacks_) {
121 cb->MonitorContendedLocked(m);
122 }
123}
124
125void RuntimeCallbacks::ObjectWaitStart(Handle<mirror::Object> m, int64_t timeout) {
126 for (MonitorCallback* cb : monitor_callbacks_) {
127 cb->ObjectWaitStart(m, timeout);
128 }
129}
130
131void RuntimeCallbacks::MonitorWaitFinished(Monitor* m, bool timeout) {
132 for (MonitorCallback* cb : monitor_callbacks_) {
133 cb->MonitorWaitFinished(m, timeout);
134 }
135}
136
137void RuntimeCallbacks::AddMonitorCallback(MonitorCallback* cb) {
138 monitor_callbacks_.push_back(cb);
139}
140
141void RuntimeCallbacks::RemoveMonitorCallback(MonitorCallback* cb) {
142 Remove(cb, &monitor_callbacks_);
143}
144
Andreas Gampea5814f92017-01-18 21:43:16 -0800145void RuntimeCallbacks::RemoveThreadLifecycleCallback(ThreadLifecycleCallback* cb) {
146 Remove(cb, &thread_callbacks_);
147}
148
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000149void RuntimeCallbacks::ThreadStart(Thread* self) {
150 for (ThreadLifecycleCallback* cb : thread_callbacks_) {
151 cb->ThreadStart(self);
152 }
153}
154
155void RuntimeCallbacks::ThreadDeath(Thread* self) {
156 for (ThreadLifecycleCallback* cb : thread_callbacks_) {
157 cb->ThreadDeath(self);
158 }
159}
160
Andreas Gampe0f01b582017-01-18 15:22:37 -0800161void RuntimeCallbacks::AddClassLoadCallback(ClassLoadCallback* cb) {
162 class_callbacks_.push_back(cb);
163}
164
165void RuntimeCallbacks::RemoveClassLoadCallback(ClassLoadCallback* cb) {
Andreas Gampea5814f92017-01-18 21:43:16 -0800166 Remove(cb, &class_callbacks_);
Andreas Gampe0f01b582017-01-18 15:22:37 -0800167}
168
169void RuntimeCallbacks::ClassLoad(Handle<mirror::Class> klass) {
170 for (ClassLoadCallback* cb : class_callbacks_) {
171 cb->ClassLoad(klass);
172 }
173}
174
Alex Lightb0f11922017-01-23 14:25:17 -0800175void RuntimeCallbacks::ClassPreDefine(const char* descriptor,
176 Handle<mirror::Class> temp_class,
177 Handle<mirror::ClassLoader> loader,
178 const DexFile& initial_dex_file,
179 const DexFile::ClassDef& initial_class_def,
180 /*out*/DexFile const** final_dex_file,
181 /*out*/DexFile::ClassDef const** final_class_def) {
182 DexFile const* current_dex_file = &initial_dex_file;
183 DexFile::ClassDef const* current_class_def = &initial_class_def;
184 for (ClassLoadCallback* cb : class_callbacks_) {
185 DexFile const* new_dex_file = nullptr;
186 DexFile::ClassDef const* new_class_def = nullptr;
187 cb->ClassPreDefine(descriptor,
188 temp_class,
189 loader,
190 *current_dex_file,
191 *current_class_def,
192 &new_dex_file,
193 &new_class_def);
194 if ((new_dex_file != nullptr && new_dex_file != current_dex_file) ||
195 (new_class_def != nullptr && new_class_def != current_class_def)) {
196 DCHECK(new_dex_file != nullptr && new_class_def != nullptr);
197 current_dex_file = new_dex_file;
198 current_class_def = new_class_def;
199 }
200 }
201 *final_dex_file = current_dex_file;
202 *final_class_def = current_class_def;
203}
204
Andreas Gampe0f01b582017-01-18 15:22:37 -0800205void RuntimeCallbacks::ClassPrepare(Handle<mirror::Class> temp_klass, Handle<mirror::Class> klass) {
206 for (ClassLoadCallback* cb : class_callbacks_) {
207 cb->ClassPrepare(temp_klass, klass);
208 }
209}
210
Andreas Gampea5814f92017-01-18 21:43:16 -0800211void RuntimeCallbacks::AddRuntimeSigQuitCallback(RuntimeSigQuitCallback* cb) {
212 sigquit_callbacks_.push_back(cb);
213}
214
215void RuntimeCallbacks::RemoveRuntimeSigQuitCallback(RuntimeSigQuitCallback* cb) {
216 Remove(cb, &sigquit_callbacks_);
217}
218
219void RuntimeCallbacks::SigQuit() {
220 for (RuntimeSigQuitCallback* cb : sigquit_callbacks_) {
221 cb->SigQuit();
222 }
223}
224
Andreas Gampe48864112017-01-19 17:23:17 -0800225void RuntimeCallbacks::AddRuntimePhaseCallback(RuntimePhaseCallback* cb) {
226 phase_callbacks_.push_back(cb);
227}
228
229void RuntimeCallbacks::RemoveRuntimePhaseCallback(RuntimePhaseCallback* cb) {
230 Remove(cb, &phase_callbacks_);
231}
232
233void RuntimeCallbacks::NextRuntimePhase(RuntimePhaseCallback::RuntimePhase phase) {
234 for (RuntimePhaseCallback* cb : phase_callbacks_) {
235 cb->NextRuntimePhase(phase);
236 }
237}
238
Alex Lightd78ddec2017-04-18 15:20:38 -0700239void RuntimeCallbacks::AddMethodCallback(MethodCallback* cb) {
240 method_callbacks_.push_back(cb);
241}
242
243void RuntimeCallbacks::RemoveMethodCallback(MethodCallback* cb) {
244 Remove(cb, &method_callbacks_);
245}
246
247void RuntimeCallbacks::RegisterNativeMethod(ArtMethod* method,
248 const void* in_cur_method,
249 /*out*/void** new_method) {
250 void* cur_method = const_cast<void*>(in_cur_method);
251 *new_method = cur_method;
252 for (MethodCallback* cb : method_callbacks_) {
253 cb->RegisterNativeMethod(method, cur_method, new_method);
254 if (*new_method != nullptr) {
255 cur_method = *new_method;
256 }
257 }
258}
259
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000260} // namespace art