Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 1 | /* |
| 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 Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 24 | #include "dex_file.h" |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 25 | #include "handle.h" |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 26 | |
| 27 | namespace art { |
| 28 | |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 29 | namespace mirror { |
| 30 | class Class; |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 31 | class ClassLoader; |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 32 | } // namespace mirror |
| 33 | |
Alex Light | d78ddec | 2017-04-18 15:20:38 -0700 | [diff] [blame] | 34 | class ArtMethod; |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 35 | class ClassLoadCallback; |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 36 | class Thread; |
Alex Light | d78ddec | 2017-04-18 15:20:38 -0700 | [diff] [blame] | 37 | class MethodCallback; |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 38 | class ThreadLifecycleCallback; |
| 39 | |
| 40 | // Note: RuntimeCallbacks uses the mutator lock to synchronize the callback lists. A thread must |
| 41 | // hold the exclusive lock to add or remove a listener. A thread must hold the shared lock |
| 42 | // to dispatch an event. This setup is chosen as some clients may want to suspend the |
| 43 | // dispatching thread or all threads. |
| 44 | // |
| 45 | // To make this safe, the following restrictions apply: |
| 46 | // * Only the owner of a listener may ever add or remove said listener. |
| 47 | // * A listener must never add or remove itself or any other listener while running. |
| 48 | // * It is the responsibility of the owner to not remove the listener while it is running |
| 49 | // (and suspended). |
| 50 | // |
| 51 | // The simplest way to satisfy these restrictions is to never remove a listener, and to do |
| 52 | // any state checking (is the listener enabled) in the listener itself. For an example, see |
| 53 | // Dbg. |
| 54 | |
Andreas Gampe | a5814f9 | 2017-01-18 21:43:16 -0800 | [diff] [blame] | 55 | class RuntimeSigQuitCallback { |
| 56 | public: |
| 57 | virtual ~RuntimeSigQuitCallback() {} |
| 58 | |
| 59 | virtual void SigQuit() REQUIRES_SHARED(Locks::mutator_lock_) = 0; |
| 60 | }; |
| 61 | |
Andreas Gampe | 4886411 | 2017-01-19 17:23:17 -0800 | [diff] [blame] | 62 | class RuntimePhaseCallback { |
| 63 | public: |
| 64 | enum RuntimePhase { |
Andreas Gampe | 96eca78 | 2017-01-19 19:45:30 -0800 | [diff] [blame] | 65 | kInitialAgents, // Initial agent loading is done. |
| 66 | kStart, // The runtime is started. |
| 67 | kInit, // The runtime is initialized (and will run user code soon). |
| 68 | kDeath, // The runtime just died. |
Andreas Gampe | 4886411 | 2017-01-19 17:23:17 -0800 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | virtual ~RuntimePhaseCallback() {} |
| 72 | |
| 73 | virtual void NextRuntimePhase(RuntimePhase phase) REQUIRES_SHARED(Locks::mutator_lock_) = 0; |
| 74 | }; |
| 75 | |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 76 | class RuntimeCallbacks { |
| 77 | public: |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 78 | void AddThreadLifecycleCallback(ThreadLifecycleCallback* cb) REQUIRES(Locks::mutator_lock_); |
| 79 | void RemoveThreadLifecycleCallback(ThreadLifecycleCallback* cb) REQUIRES(Locks::mutator_lock_); |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 80 | |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 81 | void ThreadStart(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_); |
| 82 | void ThreadDeath(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_); |
| 83 | |
| 84 | void AddClassLoadCallback(ClassLoadCallback* cb) REQUIRES(Locks::mutator_lock_); |
| 85 | void RemoveClassLoadCallback(ClassLoadCallback* cb) REQUIRES(Locks::mutator_lock_); |
| 86 | |
| 87 | void ClassLoad(Handle<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_); |
| 88 | void ClassPrepare(Handle<mirror::Class> temp_klass, Handle<mirror::Class> klass) |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 89 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 90 | |
Andreas Gampe | a5814f9 | 2017-01-18 21:43:16 -0800 | [diff] [blame] | 91 | void AddRuntimeSigQuitCallback(RuntimeSigQuitCallback* cb) |
| 92 | REQUIRES(Locks::mutator_lock_); |
| 93 | void RemoveRuntimeSigQuitCallback(RuntimeSigQuitCallback* cb) |
| 94 | REQUIRES(Locks::mutator_lock_); |
| 95 | |
| 96 | void SigQuit() REQUIRES_SHARED(Locks::mutator_lock_); |
| 97 | |
Andreas Gampe | 4886411 | 2017-01-19 17:23:17 -0800 | [diff] [blame] | 98 | void AddRuntimePhaseCallback(RuntimePhaseCallback* cb) |
| 99 | REQUIRES(Locks::mutator_lock_); |
| 100 | void RemoveRuntimePhaseCallback(RuntimePhaseCallback* cb) |
| 101 | REQUIRES(Locks::mutator_lock_); |
| 102 | |
| 103 | void NextRuntimePhase(RuntimePhaseCallback::RuntimePhase phase) |
| 104 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 105 | |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 106 | void ClassPreDefine(const char* descriptor, |
| 107 | Handle<mirror::Class> temp_class, |
| 108 | Handle<mirror::ClassLoader> loader, |
| 109 | const DexFile& initial_dex_file, |
| 110 | const DexFile::ClassDef& initial_class_def, |
| 111 | /*out*/DexFile const** final_dex_file, |
| 112 | /*out*/DexFile::ClassDef const** final_class_def) |
| 113 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 114 | |
Alex Light | d78ddec | 2017-04-18 15:20:38 -0700 | [diff] [blame] | 115 | void AddMethodCallback(MethodCallback* cb) REQUIRES(Locks::mutator_lock_); |
| 116 | void RemoveMethodCallback(MethodCallback* cb) REQUIRES(Locks::mutator_lock_); |
| 117 | |
| 118 | void RegisterNativeMethod(ArtMethod* method, |
| 119 | const void* original_implementation, |
| 120 | /*out*/void** new_implementation) |
| 121 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 122 | |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 123 | private: |
| 124 | std::vector<ThreadLifecycleCallback*> thread_callbacks_ |
| 125 | GUARDED_BY(Locks::mutator_lock_); |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 126 | std::vector<ClassLoadCallback*> class_callbacks_ |
| 127 | GUARDED_BY(Locks::mutator_lock_); |
Andreas Gampe | a5814f9 | 2017-01-18 21:43:16 -0800 | [diff] [blame] | 128 | std::vector<RuntimeSigQuitCallback*> sigquit_callbacks_ |
| 129 | GUARDED_BY(Locks::mutator_lock_); |
Andreas Gampe | 4886411 | 2017-01-19 17:23:17 -0800 | [diff] [blame] | 130 | std::vector<RuntimePhaseCallback*> phase_callbacks_ |
Alex Light | d78ddec | 2017-04-18 15:20:38 -0700 | [diff] [blame] | 131 | GUARDED_BY(Locks::mutator_lock_); |
| 132 | std::vector<MethodCallback*> method_callbacks_ |
| 133 | GUARDED_BY(Locks::mutator_lock_); |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | } // namespace art |
| 137 | |
| 138 | #endif // ART_RUNTIME_RUNTIME_CALLBACKS_H_ |