blob: baf941a8e1d72d28bda92ab15e6cc985bd0d4645 [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 Gampe04bbb5b2017-01-19 17:49:03 +000023#include "base/macros.h"
24#include "base/mutex.h"
Alex Lightb0f11922017-01-23 14:25:17 -080025#include "dex_file.h"
Andreas Gampe0f01b582017-01-18 15:22:37 -080026#include "handle.h"
Andreas Gampe04bbb5b2017-01-19 17:49:03 +000027
28namespace art {
29
Andreas Gampe0f01b582017-01-18 15:22:37 -080030namespace mirror {
31class Class;
Alex Lightb0f11922017-01-23 14:25:17 -080032class ClassLoader;
Alex Light77fee872017-09-05 14:51:49 -070033class Object;
Andreas Gampe0f01b582017-01-18 15:22:37 -080034} // namespace mirror
35
Alex Lightd78ddec2017-04-18 15:20:38 -070036class ArtMethod;
Andreas Gampe0f01b582017-01-18 15:22:37 -080037class ClassLoadCallback;
Andreas Gampe04bbb5b2017-01-19 17:49:03 +000038class Thread;
Alex Lightd78ddec2017-04-18 15:20:38 -070039class MethodCallback;
Alex Light77fee872017-09-05 14:51:49 -070040class Monitor;
Andreas Gampe04bbb5b2017-01-19 17:49:03 +000041class ThreadLifecycleCallback;
42
43// Note: RuntimeCallbacks uses the mutator lock to synchronize the callback lists. A thread must
44// hold the exclusive lock to add or remove a listener. A thread must hold the shared lock
45// to dispatch an event. This setup is chosen as some clients may want to suspend the
46// dispatching thread or all threads.
47//
48// To make this safe, the following restrictions apply:
49// * Only the owner of a listener may ever add or remove said listener.
50// * A listener must never add or remove itself or any other listener while running.
51// * It is the responsibility of the owner to not remove the listener while it is running
52// (and suspended).
53//
54// The simplest way to satisfy these restrictions is to never remove a listener, and to do
55// any state checking (is the listener enabled) in the listener itself. For an example, see
56// Dbg.
57
Alex Light8c2b9292017-11-09 13:21:01 -080058class DdmCallback {
59 public:
60 virtual ~DdmCallback() {}
61 virtual void DdmPublishChunk(uint32_t type, const ArrayRef<const uint8_t>& data)
62 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
63};
64
Andreas Gampea5814f92017-01-18 21:43:16 -080065class RuntimeSigQuitCallback {
66 public:
67 virtual ~RuntimeSigQuitCallback() {}
68
69 virtual void SigQuit() REQUIRES_SHARED(Locks::mutator_lock_) = 0;
70};
71
Andreas Gampe48864112017-01-19 17:23:17 -080072class RuntimePhaseCallback {
73 public:
74 enum RuntimePhase {
Andreas Gampe96eca782017-01-19 19:45:30 -080075 kInitialAgents, // Initial agent loading is done.
76 kStart, // The runtime is started.
77 kInit, // The runtime is initialized (and will run user code soon).
78 kDeath, // The runtime just died.
Andreas Gampe48864112017-01-19 17:23:17 -080079 };
80
81 virtual ~RuntimePhaseCallback() {}
82
83 virtual void NextRuntimePhase(RuntimePhase phase) REQUIRES_SHARED(Locks::mutator_lock_) = 0;
84};
85
Alex Light77fee872017-09-05 14:51:49 -070086class MonitorCallback {
87 public:
88 // Called just before the thread goes to sleep to wait for the monitor to become unlocked.
89 virtual void MonitorContendedLocking(Monitor* mon) REQUIRES_SHARED(Locks::mutator_lock_) = 0;
90 // Called just after the monitor has been successfully acquired when it was already locked.
91 virtual void MonitorContendedLocked(Monitor* mon) REQUIRES_SHARED(Locks::mutator_lock_) = 0;
92 // Called on entry to the Object#wait method regardless of whether or not the call is valid.
93 virtual void ObjectWaitStart(Handle<mirror::Object> obj, int64_t millis_timeout)
94 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
95
96 // Called just after the monitor has woken up from going to sleep for a wait(). At this point the
97 // thread does not possess a lock on the monitor. This will only be called for threads wait calls
98 // where the thread did (or at least could have) gone to sleep.
99 virtual void MonitorWaitFinished(Monitor* m, bool timed_out)
100 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
101
102 virtual ~MonitorCallback() {}
103};
104
Alex Light21611932017-09-26 13:07:39 -0700105// A callback to let parts of the runtime note that they are currently relying on a particular
106// method remaining in it's current state. Users should not rely on always being called. If multiple
107// callbacks are added the runtime will short-circuit when the first one returns 'true'.
108class MethodInspectionCallback {
109 public:
110 virtual ~MethodInspectionCallback() {}
111
112 // Returns true if the method is being inspected currently and the runtime should not modify it in
113 // potentially dangerous ways (i.e. replace with compiled version, JIT it, etc).
114 virtual bool IsMethodBeingInspected(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Alex Light0fa17862017-10-24 13:43:05 -0700115
116 // Returns true if the method is safe to Jit, false otherwise.
117 // Note that '!IsMethodSafeToJit(m) implies IsMethodBeingInspected(m)'. That is that if this
118 // method returns false IsMethodBeingInspected must return true.
119 virtual bool IsMethodSafeToJit(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Alex Light21611932017-09-26 13:07:39 -0700120};
121
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000122class RuntimeCallbacks {
123 public:
Andreas Gampe0f01b582017-01-18 15:22:37 -0800124 void AddThreadLifecycleCallback(ThreadLifecycleCallback* cb) REQUIRES(Locks::mutator_lock_);
125 void RemoveThreadLifecycleCallback(ThreadLifecycleCallback* cb) REQUIRES(Locks::mutator_lock_);
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000126
Andreas Gampe0f01b582017-01-18 15:22:37 -0800127 void ThreadStart(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
128 void ThreadDeath(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
129
130 void AddClassLoadCallback(ClassLoadCallback* cb) REQUIRES(Locks::mutator_lock_);
131 void RemoveClassLoadCallback(ClassLoadCallback* cb) REQUIRES(Locks::mutator_lock_);
132
133 void ClassLoad(Handle<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_);
134 void ClassPrepare(Handle<mirror::Class> temp_klass, Handle<mirror::Class> klass)
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000135 REQUIRES_SHARED(Locks::mutator_lock_);
136
Andreas Gampea5814f92017-01-18 21:43:16 -0800137 void AddRuntimeSigQuitCallback(RuntimeSigQuitCallback* cb)
138 REQUIRES(Locks::mutator_lock_);
139 void RemoveRuntimeSigQuitCallback(RuntimeSigQuitCallback* cb)
140 REQUIRES(Locks::mutator_lock_);
141
142 void SigQuit() REQUIRES_SHARED(Locks::mutator_lock_);
143
Andreas Gampe48864112017-01-19 17:23:17 -0800144 void AddRuntimePhaseCallback(RuntimePhaseCallback* cb)
145 REQUIRES(Locks::mutator_lock_);
146 void RemoveRuntimePhaseCallback(RuntimePhaseCallback* cb)
147 REQUIRES(Locks::mutator_lock_);
148
149 void NextRuntimePhase(RuntimePhaseCallback::RuntimePhase phase)
150 REQUIRES_SHARED(Locks::mutator_lock_);
151
Alex Lightb0f11922017-01-23 14:25:17 -0800152 void ClassPreDefine(const char* descriptor,
153 Handle<mirror::Class> temp_class,
154 Handle<mirror::ClassLoader> loader,
155 const DexFile& initial_dex_file,
156 const DexFile::ClassDef& initial_class_def,
157 /*out*/DexFile const** final_dex_file,
158 /*out*/DexFile::ClassDef const** final_class_def)
159 REQUIRES_SHARED(Locks::mutator_lock_);
160
Alex Lightd78ddec2017-04-18 15:20:38 -0700161 void AddMethodCallback(MethodCallback* cb) REQUIRES(Locks::mutator_lock_);
162 void RemoveMethodCallback(MethodCallback* cb) REQUIRES(Locks::mutator_lock_);
163
164 void RegisterNativeMethod(ArtMethod* method,
165 const void* original_implementation,
166 /*out*/void** new_implementation)
167 REQUIRES_SHARED(Locks::mutator_lock_);
168
Alex Light77fee872017-09-05 14:51:49 -0700169 void MonitorContendedLocking(Monitor* m) REQUIRES_SHARED(Locks::mutator_lock_);
170 void MonitorContendedLocked(Monitor* m) REQUIRES_SHARED(Locks::mutator_lock_);
171 void ObjectWaitStart(Handle<mirror::Object> m, int64_t timeout)
172 REQUIRES_SHARED(Locks::mutator_lock_);
173 void MonitorWaitFinished(Monitor* m, bool timed_out)
174 REQUIRES_SHARED(Locks::mutator_lock_);
175
176 void AddMonitorCallback(MonitorCallback* cb) REQUIRES_SHARED(Locks::mutator_lock_);
177 void RemoveMonitorCallback(MonitorCallback* cb) REQUIRES_SHARED(Locks::mutator_lock_);
178
Alex Light21611932017-09-26 13:07:39 -0700179 // Returns true if some MethodInspectionCallback indicates the method is being inspected/depended
180 // on by some code.
181 bool IsMethodBeingInspected(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
182
Alex Light0fa17862017-10-24 13:43:05 -0700183 // Returns false if some MethodInspectionCallback indicates the method cannot be safetly jitted
184 // (which implies that it is being Inspected). Returns true otherwise. If it returns false the
185 // entrypoint should not be changed to JITed code.
186 bool IsMethodSafeToJit(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
187
Alex Light21611932017-09-26 13:07:39 -0700188 void AddMethodInspectionCallback(MethodInspectionCallback* cb)
189 REQUIRES_SHARED(Locks::mutator_lock_);
190 void RemoveMethodInspectionCallback(MethodInspectionCallback* cb)
191 REQUIRES_SHARED(Locks::mutator_lock_);
192
Alex Light8c2b9292017-11-09 13:21:01 -0800193 // DDMS callbacks
194 void DdmPublishChunk(uint32_t type, const ArrayRef<const uint8_t>& data)
195 REQUIRES_SHARED(Locks::mutator_lock_);
196
197 void AddDdmCallback(DdmCallback* cb) REQUIRES_SHARED(Locks::mutator_lock_);
198 void RemoveDdmCallback(DdmCallback* cb) REQUIRES_SHARED(Locks::mutator_lock_);
199
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000200 private:
201 std::vector<ThreadLifecycleCallback*> thread_callbacks_
202 GUARDED_BY(Locks::mutator_lock_);
Andreas Gampe0f01b582017-01-18 15:22:37 -0800203 std::vector<ClassLoadCallback*> class_callbacks_
204 GUARDED_BY(Locks::mutator_lock_);
Andreas Gampea5814f92017-01-18 21:43:16 -0800205 std::vector<RuntimeSigQuitCallback*> sigquit_callbacks_
206 GUARDED_BY(Locks::mutator_lock_);
Andreas Gampe48864112017-01-19 17:23:17 -0800207 std::vector<RuntimePhaseCallback*> phase_callbacks_
Alex Lightd78ddec2017-04-18 15:20:38 -0700208 GUARDED_BY(Locks::mutator_lock_);
209 std::vector<MethodCallback*> method_callbacks_
210 GUARDED_BY(Locks::mutator_lock_);
Alex Light77fee872017-09-05 14:51:49 -0700211 std::vector<MonitorCallback*> monitor_callbacks_
212 GUARDED_BY(Locks::mutator_lock_);
Alex Light21611932017-09-26 13:07:39 -0700213 std::vector<MethodInspectionCallback*> method_inspection_callbacks_
214 GUARDED_BY(Locks::mutator_lock_);
Alex Light8c2b9292017-11-09 13:21:01 -0800215 std::vector<DdmCallback*> ddm_callbacks_
216 GUARDED_BY(Locks::mutator_lock_);
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000217};
218
219} // namespace art
220
221#endif // ART_RUNTIME_RUNTIME_CALLBACKS_H_