blob: 40d7889565912a2db46288d749ff9e8f7677a79f [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 Light21611932017-09-26 13:07:39 -070052void RuntimeCallbacks::AddMethodInspectionCallback(MethodInspectionCallback* cb) {
53 method_inspection_callbacks_.push_back(cb);
54}
55
56void RuntimeCallbacks::RemoveMethodInspectionCallback(MethodInspectionCallback* cb) {
57 Remove(cb, &method_inspection_callbacks_);
58}
59
Alex Light0fa17862017-10-24 13:43:05 -070060bool 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 Light21611932017-09-26 13:07:39 -070071bool 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
80void RuntimeCallbacks::AddThreadLifecycleCallback(ThreadLifecycleCallback* cb) {
81 thread_callbacks_.push_back(cb);
82}
83
Alex Light77fee872017-09-05 14:51:49 -070084void RuntimeCallbacks::MonitorContendedLocking(Monitor* m) {
85 for (MonitorCallback* cb : monitor_callbacks_) {
86 cb->MonitorContendedLocking(m);
87 }
88}
89
90void RuntimeCallbacks::MonitorContendedLocked(Monitor* m) {
91 for (MonitorCallback* cb : monitor_callbacks_) {
92 cb->MonitorContendedLocked(m);
93 }
94}
95
96void RuntimeCallbacks::ObjectWaitStart(Handle<mirror::Object> m, int64_t timeout) {
97 for (MonitorCallback* cb : monitor_callbacks_) {
98 cb->ObjectWaitStart(m, timeout);
99 }
100}
101
102void RuntimeCallbacks::MonitorWaitFinished(Monitor* m, bool timeout) {
103 for (MonitorCallback* cb : monitor_callbacks_) {
104 cb->MonitorWaitFinished(m, timeout);
105 }
106}
107
108void RuntimeCallbacks::AddMonitorCallback(MonitorCallback* cb) {
109 monitor_callbacks_.push_back(cb);
110}
111
112void RuntimeCallbacks::RemoveMonitorCallback(MonitorCallback* cb) {
113 Remove(cb, &monitor_callbacks_);
114}
115
Andreas Gampea5814f92017-01-18 21:43:16 -0800116void RuntimeCallbacks::RemoveThreadLifecycleCallback(ThreadLifecycleCallback* cb) {
117 Remove(cb, &thread_callbacks_);
118}
119
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000120void RuntimeCallbacks::ThreadStart(Thread* self) {
121 for (ThreadLifecycleCallback* cb : thread_callbacks_) {
122 cb->ThreadStart(self);
123 }
124}
125
126void RuntimeCallbacks::ThreadDeath(Thread* self) {
127 for (ThreadLifecycleCallback* cb : thread_callbacks_) {
128 cb->ThreadDeath(self);
129 }
130}
131
Andreas Gampe0f01b582017-01-18 15:22:37 -0800132void RuntimeCallbacks::AddClassLoadCallback(ClassLoadCallback* cb) {
133 class_callbacks_.push_back(cb);
134}
135
136void RuntimeCallbacks::RemoveClassLoadCallback(ClassLoadCallback* cb) {
Andreas Gampea5814f92017-01-18 21:43:16 -0800137 Remove(cb, &class_callbacks_);
Andreas Gampe0f01b582017-01-18 15:22:37 -0800138}
139
140void RuntimeCallbacks::ClassLoad(Handle<mirror::Class> klass) {
141 for (ClassLoadCallback* cb : class_callbacks_) {
142 cb->ClassLoad(klass);
143 }
144}
145
Alex Lightb0f11922017-01-23 14:25:17 -0800146void 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 Gampe0f01b582017-01-18 15:22:37 -0800176void 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 Gampea5814f92017-01-18 21:43:16 -0800182void RuntimeCallbacks::AddRuntimeSigQuitCallback(RuntimeSigQuitCallback* cb) {
183 sigquit_callbacks_.push_back(cb);
184}
185
186void RuntimeCallbacks::RemoveRuntimeSigQuitCallback(RuntimeSigQuitCallback* cb) {
187 Remove(cb, &sigquit_callbacks_);
188}
189
190void RuntimeCallbacks::SigQuit() {
191 for (RuntimeSigQuitCallback* cb : sigquit_callbacks_) {
192 cb->SigQuit();
193 }
194}
195
Andreas Gampe48864112017-01-19 17:23:17 -0800196void RuntimeCallbacks::AddRuntimePhaseCallback(RuntimePhaseCallback* cb) {
197 phase_callbacks_.push_back(cb);
198}
199
200void RuntimeCallbacks::RemoveRuntimePhaseCallback(RuntimePhaseCallback* cb) {
201 Remove(cb, &phase_callbacks_);
202}
203
204void RuntimeCallbacks::NextRuntimePhase(RuntimePhaseCallback::RuntimePhase phase) {
205 for (RuntimePhaseCallback* cb : phase_callbacks_) {
206 cb->NextRuntimePhase(phase);
207 }
208}
209
Alex Lightd78ddec2017-04-18 15:20:38 -0700210void RuntimeCallbacks::AddMethodCallback(MethodCallback* cb) {
211 method_callbacks_.push_back(cb);
212}
213
214void RuntimeCallbacks::RemoveMethodCallback(MethodCallback* cb) {
215 Remove(cb, &method_callbacks_);
216}
217
218void 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 Gampe04bbb5b2017-01-19 17:49:03 +0000231} // namespace art