blob: fa686d30e14e869f588bc99a1f514f4358c666ae [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
Andreas Gampe04bbb5b2017-01-19 17:49:03 +000097class RuntimeCallbacks {
98 public:
Andreas Gampe0f01b582017-01-18 15:22:37 -080099 void AddThreadLifecycleCallback(ThreadLifecycleCallback* cb) REQUIRES(Locks::mutator_lock_);
100 void RemoveThreadLifecycleCallback(ThreadLifecycleCallback* cb) REQUIRES(Locks::mutator_lock_);
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000101
Andreas Gampe0f01b582017-01-18 15:22:37 -0800102 void ThreadStart(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
103 void ThreadDeath(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
104
105 void AddClassLoadCallback(ClassLoadCallback* cb) REQUIRES(Locks::mutator_lock_);
106 void RemoveClassLoadCallback(ClassLoadCallback* cb) REQUIRES(Locks::mutator_lock_);
107
108 void ClassLoad(Handle<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_);
109 void ClassPrepare(Handle<mirror::Class> temp_klass, Handle<mirror::Class> klass)
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000110 REQUIRES_SHARED(Locks::mutator_lock_);
111
Andreas Gampea5814f92017-01-18 21:43:16 -0800112 void AddRuntimeSigQuitCallback(RuntimeSigQuitCallback* cb)
113 REQUIRES(Locks::mutator_lock_);
114 void RemoveRuntimeSigQuitCallback(RuntimeSigQuitCallback* cb)
115 REQUIRES(Locks::mutator_lock_);
116
117 void SigQuit() REQUIRES_SHARED(Locks::mutator_lock_);
118
Andreas Gampe48864112017-01-19 17:23:17 -0800119 void AddRuntimePhaseCallback(RuntimePhaseCallback* cb)
120 REQUIRES(Locks::mutator_lock_);
121 void RemoveRuntimePhaseCallback(RuntimePhaseCallback* cb)
122 REQUIRES(Locks::mutator_lock_);
123
124 void NextRuntimePhase(RuntimePhaseCallback::RuntimePhase phase)
125 REQUIRES_SHARED(Locks::mutator_lock_);
126
Alex Lightb0f11922017-01-23 14:25:17 -0800127 void ClassPreDefine(const char* descriptor,
128 Handle<mirror::Class> temp_class,
129 Handle<mirror::ClassLoader> loader,
130 const DexFile& initial_dex_file,
131 const DexFile::ClassDef& initial_class_def,
132 /*out*/DexFile const** final_dex_file,
133 /*out*/DexFile::ClassDef const** final_class_def)
134 REQUIRES_SHARED(Locks::mutator_lock_);
135
Alex Lightd78ddec2017-04-18 15:20:38 -0700136 void AddMethodCallback(MethodCallback* cb) REQUIRES(Locks::mutator_lock_);
137 void RemoveMethodCallback(MethodCallback* cb) REQUIRES(Locks::mutator_lock_);
138
139 void RegisterNativeMethod(ArtMethod* method,
140 const void* original_implementation,
141 /*out*/void** new_implementation)
142 REQUIRES_SHARED(Locks::mutator_lock_);
143
Alex Light77fee872017-09-05 14:51:49 -0700144 void MonitorContendedLocking(Monitor* m) REQUIRES_SHARED(Locks::mutator_lock_);
145 void MonitorContendedLocked(Monitor* m) REQUIRES_SHARED(Locks::mutator_lock_);
146 void ObjectWaitStart(Handle<mirror::Object> m, int64_t timeout)
147 REQUIRES_SHARED(Locks::mutator_lock_);
148 void MonitorWaitFinished(Monitor* m, bool timed_out)
149 REQUIRES_SHARED(Locks::mutator_lock_);
150
151 void AddMonitorCallback(MonitorCallback* cb) REQUIRES_SHARED(Locks::mutator_lock_);
152 void RemoveMonitorCallback(MonitorCallback* cb) REQUIRES_SHARED(Locks::mutator_lock_);
153
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000154 private:
155 std::vector<ThreadLifecycleCallback*> thread_callbacks_
156 GUARDED_BY(Locks::mutator_lock_);
Andreas Gampe0f01b582017-01-18 15:22:37 -0800157 std::vector<ClassLoadCallback*> class_callbacks_
158 GUARDED_BY(Locks::mutator_lock_);
Andreas Gampea5814f92017-01-18 21:43:16 -0800159 std::vector<RuntimeSigQuitCallback*> sigquit_callbacks_
160 GUARDED_BY(Locks::mutator_lock_);
Andreas Gampe48864112017-01-19 17:23:17 -0800161 std::vector<RuntimePhaseCallback*> phase_callbacks_
Alex Lightd78ddec2017-04-18 15:20:38 -0700162 GUARDED_BY(Locks::mutator_lock_);
163 std::vector<MethodCallback*> method_callbacks_
164 GUARDED_BY(Locks::mutator_lock_);
Alex Light77fee872017-09-05 14:51:49 -0700165 std::vector<MonitorCallback*> monitor_callbacks_
166 GUARDED_BY(Locks::mutator_lock_);
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000167};
168
169} // namespace art
170
171#endif // ART_RUNTIME_RUNTIME_CALLBACKS_H_