blob: c1ba9643a79e4b880f5253bc3d6b35d51f1f9d59 [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
22#include "base/macros.h"
23#include "base/mutex.h"
Alex Lightb0f11922017-01-23 14:25:17 -080024#include "dex_file.h"
Andreas Gampe0f01b582017-01-18 15:22:37 -080025#include "handle.h"
Andreas Gampe04bbb5b2017-01-19 17:49:03 +000026
27namespace art {
28
Andreas Gampe0f01b582017-01-18 15:22:37 -080029namespace mirror {
30class Class;
Alex Lightb0f11922017-01-23 14:25:17 -080031class ClassLoader;
Alex Light77fee872017-09-05 14:51:49 -070032class Object;
Andreas Gampe0f01b582017-01-18 15:22:37 -080033} // namespace mirror
34
Alex Lightd78ddec2017-04-18 15:20:38 -070035class ArtMethod;
Andreas Gampe0f01b582017-01-18 15:22:37 -080036class ClassLoadCallback;
Andreas Gampe04bbb5b2017-01-19 17:49:03 +000037class Thread;
Alex Lightd78ddec2017-04-18 15:20:38 -070038class MethodCallback;
Alex Light77fee872017-09-05 14:51:49 -070039class Monitor;
Andreas Gampe04bbb5b2017-01-19 17:49:03 +000040class ThreadLifecycleCallback;
41
42// Note: RuntimeCallbacks uses the mutator lock to synchronize the callback lists. A thread must
43// hold the exclusive lock to add or remove a listener. A thread must hold the shared lock
44// to dispatch an event. This setup is chosen as some clients may want to suspend the
45// dispatching thread or all threads.
46//
47// To make this safe, the following restrictions apply:
48// * Only the owner of a listener may ever add or remove said listener.
49// * A listener must never add or remove itself or any other listener while running.
50// * It is the responsibility of the owner to not remove the listener while it is running
51// (and suspended).
52//
53// The simplest way to satisfy these restrictions is to never remove a listener, and to do
54// any state checking (is the listener enabled) in the listener itself. For an example, see
55// Dbg.
56
Andreas Gampea5814f92017-01-18 21:43:16 -080057class RuntimeSigQuitCallback {
58 public:
59 virtual ~RuntimeSigQuitCallback() {}
60
61 virtual void SigQuit() REQUIRES_SHARED(Locks::mutator_lock_) = 0;
62};
63
Andreas Gampe48864112017-01-19 17:23:17 -080064class RuntimePhaseCallback {
65 public:
66 enum RuntimePhase {
Andreas Gampe96eca782017-01-19 19:45:30 -080067 kInitialAgents, // Initial agent loading is done.
68 kStart, // The runtime is started.
69 kInit, // The runtime is initialized (and will run user code soon).
70 kDeath, // The runtime just died.
Andreas Gampe48864112017-01-19 17:23:17 -080071 };
72
73 virtual ~RuntimePhaseCallback() {}
74
75 virtual void NextRuntimePhase(RuntimePhase phase) REQUIRES_SHARED(Locks::mutator_lock_) = 0;
76};
77
Alex Light77fee872017-09-05 14:51:49 -070078class MonitorCallback {
79 public:
80 // Called just before the thread goes to sleep to wait for the monitor to become unlocked.
81 virtual void MonitorContendedLocking(Monitor* mon) REQUIRES_SHARED(Locks::mutator_lock_) = 0;
82 // Called just after the monitor has been successfully acquired when it was already locked.
83 virtual void MonitorContendedLocked(Monitor* mon) REQUIRES_SHARED(Locks::mutator_lock_) = 0;
84 // Called on entry to the Object#wait method regardless of whether or not the call is valid.
85 virtual void ObjectWaitStart(Handle<mirror::Object> obj, int64_t millis_timeout)
86 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
87
88 // Called just after the monitor has woken up from going to sleep for a wait(). At this point the
89 // thread does not possess a lock on the monitor. This will only be called for threads wait calls
90 // where the thread did (or at least could have) gone to sleep.
91 virtual void MonitorWaitFinished(Monitor* m, bool timed_out)
92 REQUIRES_SHARED(Locks::mutator_lock_) = 0;
93
94 virtual ~MonitorCallback() {}
95};
96
Alex Light21611932017-09-26 13:07:39 -070097// A callback to let parts of the runtime note that they are currently relying on a particular
98// method remaining in it's current state. Users should not rely on always being called. If multiple
99// callbacks are added the runtime will short-circuit when the first one returns 'true'.
100class MethodInspectionCallback {
101 public:
102 virtual ~MethodInspectionCallback() {}
103
104 // Returns true if the method is being inspected currently and the runtime should not modify it in
105 // potentially dangerous ways (i.e. replace with compiled version, JIT it, etc).
106 virtual bool IsMethodBeingInspected(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Alex Light0fa17862017-10-24 13:43:05 -0700107
108 // Returns true if the method is safe to Jit, false otherwise.
109 // Note that '!IsMethodSafeToJit(m) implies IsMethodBeingInspected(m)'. That is that if this
110 // method returns false IsMethodBeingInspected must return true.
111 virtual bool IsMethodSafeToJit(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) = 0;
Alex Light21611932017-09-26 13:07:39 -0700112};
113
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000114class RuntimeCallbacks {
115 public:
Andreas Gampe0f01b582017-01-18 15:22:37 -0800116 void AddThreadLifecycleCallback(ThreadLifecycleCallback* cb) REQUIRES(Locks::mutator_lock_);
117 void RemoveThreadLifecycleCallback(ThreadLifecycleCallback* cb) REQUIRES(Locks::mutator_lock_);
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000118
Andreas Gampe0f01b582017-01-18 15:22:37 -0800119 void ThreadStart(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
120 void ThreadDeath(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
121
122 void AddClassLoadCallback(ClassLoadCallback* cb) REQUIRES(Locks::mutator_lock_);
123 void RemoveClassLoadCallback(ClassLoadCallback* cb) REQUIRES(Locks::mutator_lock_);
124
125 void ClassLoad(Handle<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_);
126 void ClassPrepare(Handle<mirror::Class> temp_klass, Handle<mirror::Class> klass)
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000127 REQUIRES_SHARED(Locks::mutator_lock_);
128
Andreas Gampea5814f92017-01-18 21:43:16 -0800129 void AddRuntimeSigQuitCallback(RuntimeSigQuitCallback* cb)
130 REQUIRES(Locks::mutator_lock_);
131 void RemoveRuntimeSigQuitCallback(RuntimeSigQuitCallback* cb)
132 REQUIRES(Locks::mutator_lock_);
133
134 void SigQuit() REQUIRES_SHARED(Locks::mutator_lock_);
135
Andreas Gampe48864112017-01-19 17:23:17 -0800136 void AddRuntimePhaseCallback(RuntimePhaseCallback* cb)
137 REQUIRES(Locks::mutator_lock_);
138 void RemoveRuntimePhaseCallback(RuntimePhaseCallback* cb)
139 REQUIRES(Locks::mutator_lock_);
140
141 void NextRuntimePhase(RuntimePhaseCallback::RuntimePhase phase)
142 REQUIRES_SHARED(Locks::mutator_lock_);
143
Alex Lightb0f11922017-01-23 14:25:17 -0800144 void ClassPreDefine(const char* descriptor,
145 Handle<mirror::Class> temp_class,
146 Handle<mirror::ClassLoader> loader,
147 const DexFile& initial_dex_file,
148 const DexFile::ClassDef& initial_class_def,
149 /*out*/DexFile const** final_dex_file,
150 /*out*/DexFile::ClassDef const** final_class_def)
151 REQUIRES_SHARED(Locks::mutator_lock_);
152
Alex Lightd78ddec2017-04-18 15:20:38 -0700153 void AddMethodCallback(MethodCallback* cb) REQUIRES(Locks::mutator_lock_);
154 void RemoveMethodCallback(MethodCallback* cb) REQUIRES(Locks::mutator_lock_);
155
156 void RegisterNativeMethod(ArtMethod* method,
157 const void* original_implementation,
158 /*out*/void** new_implementation)
159 REQUIRES_SHARED(Locks::mutator_lock_);
160
Alex Light77fee872017-09-05 14:51:49 -0700161 void MonitorContendedLocking(Monitor* m) REQUIRES_SHARED(Locks::mutator_lock_);
162 void MonitorContendedLocked(Monitor* m) REQUIRES_SHARED(Locks::mutator_lock_);
163 void ObjectWaitStart(Handle<mirror::Object> m, int64_t timeout)
164 REQUIRES_SHARED(Locks::mutator_lock_);
165 void MonitorWaitFinished(Monitor* m, bool timed_out)
166 REQUIRES_SHARED(Locks::mutator_lock_);
167
168 void AddMonitorCallback(MonitorCallback* cb) REQUIRES_SHARED(Locks::mutator_lock_);
169 void RemoveMonitorCallback(MonitorCallback* cb) REQUIRES_SHARED(Locks::mutator_lock_);
170
Alex Light21611932017-09-26 13:07:39 -0700171 // Returns true if some MethodInspectionCallback indicates the method is being inspected/depended
172 // on by some code.
173 bool IsMethodBeingInspected(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
174
Alex Light0fa17862017-10-24 13:43:05 -0700175 // Returns false if some MethodInspectionCallback indicates the method cannot be safetly jitted
176 // (which implies that it is being Inspected). Returns true otherwise. If it returns false the
177 // entrypoint should not be changed to JITed code.
178 bool IsMethodSafeToJit(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
179
Alex Light21611932017-09-26 13:07:39 -0700180 void AddMethodInspectionCallback(MethodInspectionCallback* cb)
181 REQUIRES_SHARED(Locks::mutator_lock_);
182 void RemoveMethodInspectionCallback(MethodInspectionCallback* cb)
183 REQUIRES_SHARED(Locks::mutator_lock_);
184
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000185 private:
186 std::vector<ThreadLifecycleCallback*> thread_callbacks_
187 GUARDED_BY(Locks::mutator_lock_);
Andreas Gampe0f01b582017-01-18 15:22:37 -0800188 std::vector<ClassLoadCallback*> class_callbacks_
189 GUARDED_BY(Locks::mutator_lock_);
Andreas Gampea5814f92017-01-18 21:43:16 -0800190 std::vector<RuntimeSigQuitCallback*> sigquit_callbacks_
191 GUARDED_BY(Locks::mutator_lock_);
Andreas Gampe48864112017-01-19 17:23:17 -0800192 std::vector<RuntimePhaseCallback*> phase_callbacks_
Alex Lightd78ddec2017-04-18 15:20:38 -0700193 GUARDED_BY(Locks::mutator_lock_);
194 std::vector<MethodCallback*> method_callbacks_
195 GUARDED_BY(Locks::mutator_lock_);
Alex Light77fee872017-09-05 14:51:49 -0700196 std::vector<MonitorCallback*> monitor_callbacks_
197 GUARDED_BY(Locks::mutator_lock_);
Alex Light21611932017-09-26 13:07:39 -0700198 std::vector<MethodInspectionCallback*> method_inspection_callbacks_
199 GUARDED_BY(Locks::mutator_lock_);
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000200};
201
202} // namespace art
203
204#endif // ART_RUNTIME_RUNTIME_CALLBACKS_H_