Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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_OPENJDKJVMTI_EVENTS_H_ |
| 18 | #define ART_RUNTIME_OPENJDKJVMTI_EVENTS_H_ |
| 19 | |
| 20 | #include <bitset> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include "base/logging.h" |
| 24 | #include "jvmti.h" |
| 25 | #include "thread.h" |
| 26 | |
| 27 | namespace openjdkjvmti { |
| 28 | |
| 29 | struct ArtJvmTiEnv; |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 30 | class JvmtiAllocationListener; |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 31 | class JvmtiGcPauseListener; |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 32 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame^] | 33 | // an enum for ArtEvents. |
| 34 | enum class ArtJvmtiEvent { |
| 35 | kMinEventTypeVal = JVMTI_MIN_EVENT_TYPE_VAL, |
| 36 | kVmInit = JVMTI_EVENT_VM_INIT, |
| 37 | kVmDeath = JVMTI_EVENT_VM_DEATH, |
| 38 | kThreadStart = JVMTI_EVENT_THREAD_START, |
| 39 | kThreadEnd = JVMTI_EVENT_THREAD_END, |
| 40 | kClassFileLoadHook = JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, |
| 41 | kClassLoad = JVMTI_EVENT_CLASS_LOAD, |
| 42 | kClassPrepare = JVMTI_EVENT_CLASS_PREPARE, |
| 43 | kVmStart = JVMTI_EVENT_VM_START, |
| 44 | kException = JVMTI_EVENT_EXCEPTION, |
| 45 | kExceptionCatch = JVMTI_EVENT_EXCEPTION_CATCH, |
| 46 | kSingleStep = JVMTI_EVENT_SINGLE_STEP, |
| 47 | kFramePop = JVMTI_EVENT_FRAME_POP, |
| 48 | kBreakpoint = JVMTI_EVENT_BREAKPOINT, |
| 49 | kFieldAccess = JVMTI_EVENT_FIELD_ACCESS, |
| 50 | kFieldModification = JVMTI_EVENT_FIELD_MODIFICATION, |
| 51 | kMethodEntry = JVMTI_EVENT_METHOD_ENTRY, |
| 52 | kMethodExit = JVMTI_EVENT_METHOD_EXIT, |
| 53 | kNativeMethodBind = JVMTI_EVENT_NATIVE_METHOD_BIND, |
| 54 | kCompiledMethodLoad = JVMTI_EVENT_COMPILED_METHOD_LOAD, |
| 55 | kCompiledMethodUnload = JVMTI_EVENT_COMPILED_METHOD_UNLOAD, |
| 56 | kDynamicCodeGenerated = JVMTI_EVENT_DYNAMIC_CODE_GENERATED, |
| 57 | kDataDumpRequest = JVMTI_EVENT_DATA_DUMP_REQUEST, |
| 58 | kMonitorWait = JVMTI_EVENT_MONITOR_WAIT, |
| 59 | kMonitorWaited = JVMTI_EVENT_MONITOR_WAITED, |
| 60 | kMonitorContendedEnter = JVMTI_EVENT_MONITOR_CONTENDED_ENTER, |
| 61 | kMonitorContendedEntered = JVMTI_EVENT_MONITOR_CONTENDED_ENTERED, |
| 62 | kResourceExhausted = JVMTI_EVENT_RESOURCE_EXHAUSTED, |
| 63 | kGarbageCollectionStart = JVMTI_EVENT_GARBAGE_COLLECTION_START, |
| 64 | kGarbageCollectionFinish = JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, |
| 65 | kObjectFree = JVMTI_EVENT_OBJECT_FREE, |
| 66 | kVmObjectAlloc = JVMTI_EVENT_VM_OBJECT_ALLOC, |
| 67 | kMaxEventTypeVal = JVMTI_MAX_EVENT_TYPE_VAL, |
| 68 | }; |
| 69 | |
| 70 | // Convert a jvmtiEvent into a ArtJvmtiEvent |
| 71 | ALWAYS_INLINE static inline ArtJvmtiEvent GetArtJvmtiEvent(ArtJvmTiEnv* env, jvmtiEvent e); |
| 72 | |
| 73 | ALWAYS_INLINE static inline jvmtiEvent GetJvmtiEvent(ArtJvmtiEvent e) { |
| 74 | return static_cast<jvmtiEvent>(e); |
| 75 | } |
| 76 | |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 77 | struct EventMask { |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame^] | 78 | static constexpr size_t kEventsSize = |
| 79 | static_cast<size_t>(ArtJvmtiEvent::kMaxEventTypeVal) - |
| 80 | static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal) + 1; |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 81 | std::bitset<kEventsSize> bit_set; |
| 82 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame^] | 83 | static bool EventIsInRange(ArtJvmtiEvent event) { |
| 84 | return event >= ArtJvmtiEvent::kMinEventTypeVal && event <= ArtJvmtiEvent::kMaxEventTypeVal; |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame^] | 87 | void Set(ArtJvmtiEvent event, bool value = true) { |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 88 | DCHECK(EventIsInRange(event)); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame^] | 89 | bit_set.set(static_cast<size_t>(event) - static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal), |
| 90 | value); |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame^] | 93 | bool Test(ArtJvmtiEvent event) const { |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 94 | DCHECK(EventIsInRange(event)); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame^] | 95 | return bit_set.test( |
| 96 | static_cast<size_t>(event) - static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal)); |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 97 | } |
| 98 | }; |
| 99 | |
| 100 | struct EventMasks { |
| 101 | // The globally enabled events. |
| 102 | EventMask global_event_mask; |
| 103 | |
| 104 | // The per-thread enabled events. |
| 105 | |
| 106 | // It is not enough to store a Thread pointer, as these may be reused. Use the pointer and the |
| 107 | // thread id. |
| 108 | // Note: We could just use the tid like tracing does. |
| 109 | using UniqueThread = std::pair<art::Thread*, uint32_t>; |
| 110 | // TODO: Native thread objects are immovable, so we can use them as keys in an (unordered) map, |
| 111 | // if necessary. |
| 112 | std::vector<std::pair<UniqueThread, EventMask>> thread_event_masks; |
| 113 | |
| 114 | // A union of the per-thread events, for fast-pathing. |
| 115 | EventMask unioned_thread_event_mask; |
| 116 | |
| 117 | EventMask& GetEventMask(art::Thread* thread); |
| 118 | EventMask* GetEventMaskOrNull(art::Thread* thread); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame^] | 119 | void EnableEvent(art::Thread* thread, ArtJvmtiEvent event); |
| 120 | void DisableEvent(art::Thread* thread, ArtJvmtiEvent event); |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | // Helper class for event handling. |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 124 | class EventHandler { |
| 125 | public: |
| 126 | EventHandler(); |
| 127 | ~EventHandler(); |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 128 | |
| 129 | // Register an env. It is assumed that this happens on env creation, that is, no events are |
| 130 | // enabled, yet. |
| 131 | void RegisterArtJvmTiEnv(ArtJvmTiEnv* env); |
| 132 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame^] | 133 | bool IsEventEnabledAnywhere(ArtJvmtiEvent event) const { |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 134 | if (!EventMask::EventIsInRange(event)) { |
| 135 | return false; |
| 136 | } |
| 137 | return global_mask.Test(event); |
| 138 | } |
| 139 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame^] | 140 | jvmtiError SetEvent(ArtJvmTiEnv* env, |
| 141 | art::Thread* thread, |
| 142 | ArtJvmtiEvent event, |
| 143 | jvmtiEventMode mode); |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 144 | |
| 145 | template <typename ...Args> |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame^] | 146 | ALWAYS_INLINE |
| 147 | inline void DispatchEvent(art::Thread* thread, ArtJvmtiEvent event, Args... args) const; |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 148 | |
| 149 | private: |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame^] | 150 | ALWAYS_INLINE |
| 151 | static inline bool ShouldDispatch(ArtJvmtiEvent event, ArtJvmTiEnv* env, art::Thread* thread); |
| 152 | |
| 153 | void HandleEventType(ArtJvmtiEvent event, bool enable); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 154 | |
| 155 | // List of all JvmTiEnv objects that have been created, in their creation order. |
| 156 | std::vector<ArtJvmTiEnv*> envs; |
| 157 | |
| 158 | // A union of all enabled events, anywhere. |
| 159 | EventMask global_mask; |
| 160 | |
| 161 | std::unique_ptr<JvmtiAllocationListener> alloc_listener_; |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 162 | std::unique_ptr<JvmtiGcPauseListener> gc_pause_listener_; |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | } // namespace openjdkjvmti |
| 166 | |
| 167 | #endif // ART_RUNTIME_OPENJDKJVMTI_EVENTS_H_ |