blob: c9360491bbd01cd6be6c2ba3b4ccfcfff9fc4489 [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;
107};
108
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000109class RuntimeCallbacks {
110 public:
Andreas Gampe0f01b582017-01-18 15:22:37 -0800111 void AddThreadLifecycleCallback(ThreadLifecycleCallback* cb) REQUIRES(Locks::mutator_lock_);
112 void RemoveThreadLifecycleCallback(ThreadLifecycleCallback* cb) REQUIRES(Locks::mutator_lock_);
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000113
Andreas Gampe0f01b582017-01-18 15:22:37 -0800114 void ThreadStart(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
115 void ThreadDeath(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
116
117 void AddClassLoadCallback(ClassLoadCallback* cb) REQUIRES(Locks::mutator_lock_);
118 void RemoveClassLoadCallback(ClassLoadCallback* cb) REQUIRES(Locks::mutator_lock_);
119
120 void ClassLoad(Handle<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_);
121 void ClassPrepare(Handle<mirror::Class> temp_klass, Handle<mirror::Class> klass)
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000122 REQUIRES_SHARED(Locks::mutator_lock_);
123
Andreas Gampea5814f92017-01-18 21:43:16 -0800124 void AddRuntimeSigQuitCallback(RuntimeSigQuitCallback* cb)
125 REQUIRES(Locks::mutator_lock_);
126 void RemoveRuntimeSigQuitCallback(RuntimeSigQuitCallback* cb)
127 REQUIRES(Locks::mutator_lock_);
128
129 void SigQuit() REQUIRES_SHARED(Locks::mutator_lock_);
130
Andreas Gampe48864112017-01-19 17:23:17 -0800131 void AddRuntimePhaseCallback(RuntimePhaseCallback* cb)
132 REQUIRES(Locks::mutator_lock_);
133 void RemoveRuntimePhaseCallback(RuntimePhaseCallback* cb)
134 REQUIRES(Locks::mutator_lock_);
135
136 void NextRuntimePhase(RuntimePhaseCallback::RuntimePhase phase)
137 REQUIRES_SHARED(Locks::mutator_lock_);
138
Alex Lightb0f11922017-01-23 14:25:17 -0800139 void ClassPreDefine(const char* descriptor,
140 Handle<mirror::Class> temp_class,
141 Handle<mirror::ClassLoader> loader,
142 const DexFile& initial_dex_file,
143 const DexFile::ClassDef& initial_class_def,
144 /*out*/DexFile const** final_dex_file,
145 /*out*/DexFile::ClassDef const** final_class_def)
146 REQUIRES_SHARED(Locks::mutator_lock_);
147
Alex Lightd78ddec2017-04-18 15:20:38 -0700148 void AddMethodCallback(MethodCallback* cb) REQUIRES(Locks::mutator_lock_);
149 void RemoveMethodCallback(MethodCallback* cb) REQUIRES(Locks::mutator_lock_);
150
151 void RegisterNativeMethod(ArtMethod* method,
152 const void* original_implementation,
153 /*out*/void** new_implementation)
154 REQUIRES_SHARED(Locks::mutator_lock_);
155
Alex Light77fee872017-09-05 14:51:49 -0700156 void MonitorContendedLocking(Monitor* m) REQUIRES_SHARED(Locks::mutator_lock_);
157 void MonitorContendedLocked(Monitor* m) REQUIRES_SHARED(Locks::mutator_lock_);
158 void ObjectWaitStart(Handle<mirror::Object> m, int64_t timeout)
159 REQUIRES_SHARED(Locks::mutator_lock_);
160 void MonitorWaitFinished(Monitor* m, bool timed_out)
161 REQUIRES_SHARED(Locks::mutator_lock_);
162
163 void AddMonitorCallback(MonitorCallback* cb) REQUIRES_SHARED(Locks::mutator_lock_);
164 void RemoveMonitorCallback(MonitorCallback* cb) REQUIRES_SHARED(Locks::mutator_lock_);
165
Alex Light21611932017-09-26 13:07:39 -0700166 // Returns true if some MethodInspectionCallback indicates the method is being inspected/depended
167 // on by some code.
168 bool IsMethodBeingInspected(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
169
170 void AddMethodInspectionCallback(MethodInspectionCallback* cb)
171 REQUIRES_SHARED(Locks::mutator_lock_);
172 void RemoveMethodInspectionCallback(MethodInspectionCallback* cb)
173 REQUIRES_SHARED(Locks::mutator_lock_);
174
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000175 private:
176 std::vector<ThreadLifecycleCallback*> thread_callbacks_
177 GUARDED_BY(Locks::mutator_lock_);
Andreas Gampe0f01b582017-01-18 15:22:37 -0800178 std::vector<ClassLoadCallback*> class_callbacks_
179 GUARDED_BY(Locks::mutator_lock_);
Andreas Gampea5814f92017-01-18 21:43:16 -0800180 std::vector<RuntimeSigQuitCallback*> sigquit_callbacks_
181 GUARDED_BY(Locks::mutator_lock_);
Andreas Gampe48864112017-01-19 17:23:17 -0800182 std::vector<RuntimePhaseCallback*> phase_callbacks_
Alex Lightd78ddec2017-04-18 15:20:38 -0700183 GUARDED_BY(Locks::mutator_lock_);
184 std::vector<MethodCallback*> method_callbacks_
185 GUARDED_BY(Locks::mutator_lock_);
Alex Light77fee872017-09-05 14:51:49 -0700186 std::vector<MonitorCallback*> monitor_callbacks_
187 GUARDED_BY(Locks::mutator_lock_);
Alex Light21611932017-09-26 13:07:39 -0700188 std::vector<MethodInspectionCallback*> method_inspection_callbacks_
189 GUARDED_BY(Locks::mutator_lock_);
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000190};
191
192} // namespace art
193
194#endif // ART_RUNTIME_RUNTIME_CALLBACKS_H_