blob: 41d552ae44cd29baac1dabc00949c51e27cb5e7b [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#ifndef ART_RUNTIME_RUNTIME_CALLBACKS_H_
18#define ART_RUNTIME_RUNTIME_CALLBACKS_H_
19
20#include <vector>
21
Alex Light8c2b9292017-11-09 13:21:01 -080022#include "base/array_ref.h"
Andreas Gampe7fbc4a52018-11-28 08:26:47 -080023#include "base/locks.h"
Andreas Gampe04bbb5b2017-01-19 17:49:03 +000024#include "base/macros.h"
Andreas Gampe0f01b582017-01-18 15:22:37 -080025#include "handle.h"
Andreas Gampe04bbb5b2017-01-19 17:49:03 +000026
27namespace art {
28
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080029namespace dex {
30struct ClassDef;
31} // namespace dex
32
Andreas Gampe0f01b582017-01-18 15:22:37 -080033namespace mirror {
34class Class;
Alex Lightb0f11922017-01-23 14:25:17 -080035class ClassLoader;
Alex Light77fee872017-09-05 14:51:49 -070036class Object;
Andreas Gampe0f01b582017-01-18 15:22:37 -080037} // namespace mirror
38
Alex Lightd78ddec2017-04-18 15:20:38 -070039class ArtMethod;
Andreas Gampe0f01b582017-01-18 15:22:37 -080040class ClassLoadCallback;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080041class DexFile;
Andreas Gampe04bbb5b2017-01-19 17:49:03 +000042class Thread;
Alex Lightd78ddec2017-04-18 15:20:38 -070043class MethodCallback;
Alex Light77fee872017-09-05 14:51:49 -070044class Monitor;
Andreas Gampe04bbb5b2017-01-19 17:49:03 +000045class ThreadLifecycleCallback;
46
47// Note: RuntimeCallbacks uses the mutator lock to synchronize the callback lists. A thread must
48// hold the exclusive lock to add or remove a listener. A thread must hold the shared lock
49// to dispatch an event. This setup is chosen as some clients may want to suspend the
50// dispatching thread or all threads.
51//
52// To make this safe, the following restrictions apply:
53// * Only the owner of a listener may ever add or remove said listener.
54// * A listener must never add or remove itself or any other listener while running.
55// * It is the responsibility of the owner to not remove the listener while it is running
56// (and suspended).
57//
58// The simplest way to satisfy these restrictions is to never remove a listener, and to do
59// any state checking (is the listener enabled) in the listener itself. For an example, see
60// Dbg.
61
Alex Light8c2b9292017-11-09 13:21:01 -080062class DdmCallback {
63 public:
64 virtual ~DdmCallback() {}
65 virtual void DdmPublishChunk(uint32_t type, const ArrayRef<const uint8_t>& data)
66 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
67};
68
Alex Light40320712017-12-14 11:52:04 -080069class DebuggerControlCallback {
70 public:
71 virtual ~DebuggerControlCallback() {}
72
73 // Begin running the debugger.
74 virtual void StartDebugger() = 0;
75 // The debugger should begin shutting down since the runtime is ending. This is just advisory
76 virtual void StopDebugger() = 0;
77
78 // This allows the debugger to tell the runtime if it is configured.
79 virtual bool IsDebuggerConfigured() = 0;
80};
81
Andreas Gampea5814f92017-01-18 21:43:16 -080082class RuntimeSigQuitCallback {
83 public:
84 virtual ~RuntimeSigQuitCallback() {}
85
86 virtual void SigQuit() REQUIRES_SHARED(Locks::mutator_lock_) = 0;
87};
88
Andreas Gampe48864112017-01-19 17:23:17 -080089class RuntimePhaseCallback {
90 public:
91 enum RuntimePhase {
Andreas Gampe96eca782017-01-19 19:45:30 -080092 kInitialAgents, // Initial agent loading is done.
93 kStart, // The runtime is started.
94 kInit, // The runtime is initialized (and will run user code soon).
95 kDeath, // The runtime just died.
Andreas Gampe48864112017-01-19 17:23:17 -080096 };
97
98 virtual ~RuntimePhaseCallback() {}
99
100 virtual void NextRuntimePhase(RuntimePhase phase) REQUIRES_SHARED(Locks::mutator_lock_) = 0;
101};
102
Alex Light77fee872017-09-05 14:51:49 -0700103class MonitorCallback {
104 public:
105 // Called just before the thread goes to sleep to wait for the monitor to become unlocked.
106 virtual void MonitorContendedLocking(Monitor* mon) REQUIRES_SHARED(Locks::mutator_lock_) = 0;
107 // Called just after the monitor has been successfully acquired when it was already locked.
108 virtual void MonitorContendedLocked(Monitor* mon) REQUIRES_SHARED(Locks::mutator_lock_) = 0;
109 // Called on entry to the Object#wait method regardless of whether or not the call is valid.
110 virtual void ObjectWaitStart(Handle<mirror::Object> obj, int64_t millis_timeout)
111 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
112
113 // Called just after the monitor has woken up from going to sleep for a wait(). At this point the
114 // thread does not possess a lock on the monitor. This will only be called for threads wait calls
115 // where the thread did (or at least could have) gone to sleep.
116 virtual void MonitorWaitFinished(Monitor* m, bool timed_out)
117 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
118
119 virtual ~MonitorCallback() {}
120};
121
Charles Munger5cc0e752018-11-09 12:30:46 -0800122class ParkCallback {
123 public:
124 // Called on entry to the Unsafe.#park method
125 virtual void ThreadParkStart(bool is_absolute, int64_t millis_timeout)
126 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
127
128 // Called just after the thread has woken up from going to sleep for a park(). This will only be
129 // called for Unsafe.park() calls where the thread did (or at least could have) gone to sleep.
130 virtual void ThreadParkFinished(bool timed_out) REQUIRES_SHARED(Locks::mutator_lock_) = 0;
131
132 virtual ~ParkCallback() {}
133};
134
Alex Light21611932017-09-26 13:07:39 -0700135// A callback to let parts of the runtime note that they are currently relying on a particular
136// method remaining in it's current state. Users should not rely on always being called. If multiple
137// callbacks are added the runtime will short-circuit when the first one returns 'true'.
138class MethodInspectionCallback {
139 public:
140 virtual ~MethodInspectionCallback() {}
141
142 // Returns true if the method is being inspected currently and the runtime should not modify it in
143 // potentially dangerous ways (i.e. replace with compiled version, JIT it, etc).
144 virtual bool IsMethodBeingInspected(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Alex Light0fa17862017-10-24 13:43:05 -0700145
146 // Returns true if the method is safe to Jit, false otherwise.
147 // Note that '!IsMethodSafeToJit(m) implies IsMethodBeingInspected(m)'. That is that if this
148 // method returns false IsMethodBeingInspected must return true.
149 virtual bool IsMethodSafeToJit(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Alex Lightf2858632018-04-02 11:28:50 -0700150
151 // Returns true if we expect the method to be debuggable but are not doing anything unusual with
152 // it currently.
153 virtual bool MethodNeedsDebugVersion(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Alex Light21611932017-09-26 13:07:39 -0700154};
155
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000156class RuntimeCallbacks {
157 public:
Andreas Gampe0f01b582017-01-18 15:22:37 -0800158 void AddThreadLifecycleCallback(ThreadLifecycleCallback* cb) REQUIRES(Locks::mutator_lock_);
159 void RemoveThreadLifecycleCallback(ThreadLifecycleCallback* cb) REQUIRES(Locks::mutator_lock_);
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000160
Andreas Gampe0f01b582017-01-18 15:22:37 -0800161 void ThreadStart(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
162 void ThreadDeath(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
163
164 void AddClassLoadCallback(ClassLoadCallback* cb) REQUIRES(Locks::mutator_lock_);
165 void RemoveClassLoadCallback(ClassLoadCallback* cb) REQUIRES(Locks::mutator_lock_);
166
167 void ClassLoad(Handle<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_);
168 void ClassPrepare(Handle<mirror::Class> temp_klass, Handle<mirror::Class> klass)
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000169 REQUIRES_SHARED(Locks::mutator_lock_);
170
Andreas Gampea5814f92017-01-18 21:43:16 -0800171 void AddRuntimeSigQuitCallback(RuntimeSigQuitCallback* cb)
172 REQUIRES(Locks::mutator_lock_);
173 void RemoveRuntimeSigQuitCallback(RuntimeSigQuitCallback* cb)
174 REQUIRES(Locks::mutator_lock_);
175
176 void SigQuit() REQUIRES_SHARED(Locks::mutator_lock_);
177
Andreas Gampe48864112017-01-19 17:23:17 -0800178 void AddRuntimePhaseCallback(RuntimePhaseCallback* cb)
179 REQUIRES(Locks::mutator_lock_);
180 void RemoveRuntimePhaseCallback(RuntimePhaseCallback* cb)
181 REQUIRES(Locks::mutator_lock_);
182
183 void NextRuntimePhase(RuntimePhaseCallback::RuntimePhase phase)
184 REQUIRES_SHARED(Locks::mutator_lock_);
185
Alex Lightb0f11922017-01-23 14:25:17 -0800186 void ClassPreDefine(const char* descriptor,
187 Handle<mirror::Class> temp_class,
188 Handle<mirror::ClassLoader> loader,
189 const DexFile& initial_dex_file,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800190 const dex::ClassDef& initial_class_def,
Alex Lightb0f11922017-01-23 14:25:17 -0800191 /*out*/DexFile const** final_dex_file,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800192 /*out*/dex::ClassDef const** final_class_def)
Alex Lightb0f11922017-01-23 14:25:17 -0800193 REQUIRES_SHARED(Locks::mutator_lock_);
194
Alex Lightd78ddec2017-04-18 15:20:38 -0700195 void AddMethodCallback(MethodCallback* cb) REQUIRES(Locks::mutator_lock_);
196 void RemoveMethodCallback(MethodCallback* cb) REQUIRES(Locks::mutator_lock_);
197
198 void RegisterNativeMethod(ArtMethod* method,
199 const void* original_implementation,
200 /*out*/void** new_implementation)
201 REQUIRES_SHARED(Locks::mutator_lock_);
202
Alex Light77fee872017-09-05 14:51:49 -0700203 void MonitorContendedLocking(Monitor* m) REQUIRES_SHARED(Locks::mutator_lock_);
204 void MonitorContendedLocked(Monitor* m) REQUIRES_SHARED(Locks::mutator_lock_);
205 void ObjectWaitStart(Handle<mirror::Object> m, int64_t timeout)
206 REQUIRES_SHARED(Locks::mutator_lock_);
207 void MonitorWaitFinished(Monitor* m, bool timed_out)
208 REQUIRES_SHARED(Locks::mutator_lock_);
209
210 void AddMonitorCallback(MonitorCallback* cb) REQUIRES_SHARED(Locks::mutator_lock_);
211 void RemoveMonitorCallback(MonitorCallback* cb) REQUIRES_SHARED(Locks::mutator_lock_);
212
Charles Munger5cc0e752018-11-09 12:30:46 -0800213 void ThreadParkStart(bool is_absolute, int64_t timeout) REQUIRES_SHARED(Locks::mutator_lock_);
214 void ThreadParkFinished(bool timed_out) REQUIRES_SHARED(Locks::mutator_lock_);
215 void AddParkCallback(ParkCallback* cb) REQUIRES_SHARED(Locks::mutator_lock_);
216 void RemoveParkCallback(ParkCallback* cb) REQUIRES_SHARED(Locks::mutator_lock_);
217
Alex Light21611932017-09-26 13:07:39 -0700218 // Returns true if some MethodInspectionCallback indicates the method is being inspected/depended
219 // on by some code.
220 bool IsMethodBeingInspected(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
221
Alex Light0fa17862017-10-24 13:43:05 -0700222 // Returns false if some MethodInspectionCallback indicates the method cannot be safetly jitted
223 // (which implies that it is being Inspected). Returns true otherwise. If it returns false the
224 // entrypoint should not be changed to JITed code.
225 bool IsMethodSafeToJit(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
226
Alex Lightf2858632018-04-02 11:28:50 -0700227 // Returns true if some MethodInspectionCallback indicates the method needs to use a debug
228 // version. This allows later code to set breakpoints or perform other actions that could be
229 // broken by some optimizations.
230 bool MethodNeedsDebugVersion(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
231
Alex Light21611932017-09-26 13:07:39 -0700232 void AddMethodInspectionCallback(MethodInspectionCallback* cb)
233 REQUIRES_SHARED(Locks::mutator_lock_);
234 void RemoveMethodInspectionCallback(MethodInspectionCallback* cb)
235 REQUIRES_SHARED(Locks::mutator_lock_);
236
Alex Light8c2b9292017-11-09 13:21:01 -0800237 // DDMS callbacks
238 void DdmPublishChunk(uint32_t type, const ArrayRef<const uint8_t>& data)
239 REQUIRES_SHARED(Locks::mutator_lock_);
240
241 void AddDdmCallback(DdmCallback* cb) REQUIRES_SHARED(Locks::mutator_lock_);
242 void RemoveDdmCallback(DdmCallback* cb) REQUIRES_SHARED(Locks::mutator_lock_);
243
Alex Light40320712017-12-14 11:52:04 -0800244 void StartDebugger() REQUIRES_SHARED(Locks::mutator_lock_);
245 // NO_THREAD_SAFETY_ANALYSIS since this is only called when we are in the middle of shutting down
246 // and the mutator_lock_ is no longer acquirable.
247 void StopDebugger() NO_THREAD_SAFETY_ANALYSIS;
248 bool IsDebuggerConfigured() REQUIRES_SHARED(Locks::mutator_lock_);
249
250 void AddDebuggerControlCallback(DebuggerControlCallback* cb)
251 REQUIRES_SHARED(Locks::mutator_lock_);
252 void RemoveDebuggerControlCallback(DebuggerControlCallback* cb)
253 REQUIRES_SHARED(Locks::mutator_lock_);
254
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000255 private:
256 std::vector<ThreadLifecycleCallback*> thread_callbacks_
257 GUARDED_BY(Locks::mutator_lock_);
Andreas Gampe0f01b582017-01-18 15:22:37 -0800258 std::vector<ClassLoadCallback*> class_callbacks_
259 GUARDED_BY(Locks::mutator_lock_);
Andreas Gampea5814f92017-01-18 21:43:16 -0800260 std::vector<RuntimeSigQuitCallback*> sigquit_callbacks_
261 GUARDED_BY(Locks::mutator_lock_);
Andreas Gampe48864112017-01-19 17:23:17 -0800262 std::vector<RuntimePhaseCallback*> phase_callbacks_
Alex Lightd78ddec2017-04-18 15:20:38 -0700263 GUARDED_BY(Locks::mutator_lock_);
264 std::vector<MethodCallback*> method_callbacks_
265 GUARDED_BY(Locks::mutator_lock_);
Alex Light77fee872017-09-05 14:51:49 -0700266 std::vector<MonitorCallback*> monitor_callbacks_
267 GUARDED_BY(Locks::mutator_lock_);
Charles Munger5cc0e752018-11-09 12:30:46 -0800268 std::vector<ParkCallback*> park_callbacks_
269 GUARDED_BY(Locks::mutator_lock_);
Alex Light21611932017-09-26 13:07:39 -0700270 std::vector<MethodInspectionCallback*> method_inspection_callbacks_
271 GUARDED_BY(Locks::mutator_lock_);
Alex Light8c2b9292017-11-09 13:21:01 -0800272 std::vector<DdmCallback*> ddm_callbacks_
273 GUARDED_BY(Locks::mutator_lock_);
Alex Light40320712017-12-14 11:52:04 -0800274 std::vector<DebuggerControlCallback*> debugger_control_callbacks_
275 GUARDED_BY(Locks::mutator_lock_);
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000276};
277
278} // namespace art
279
280#endif // ART_RUNTIME_RUNTIME_CALLBACKS_H_