blob: 08a87659c7821441ad7c99101a423c0ea3d492d2 [file] [log] [blame]
Andreas Gampe77708d92016-10-07 11:48:21 -07001/*
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
27namespace openjdkjvmti {
28
29struct ArtJvmTiEnv;
Andreas Gampe27fa96c2016-10-07 15:05:24 -070030class JvmtiAllocationListener;
Andreas Gampe9b8c5882016-10-21 15:27:46 -070031class JvmtiGcPauseListener;
Andreas Gampe77708d92016-10-07 11:48:21 -070032
Alex Light73afd322017-01-18 11:17:47 -080033// an enum for ArtEvents. This differs from the JVMTI events only in that we distinguish between
34// retransformation capable and incapable loading
Alex Light40d87f42017-01-18 10:27:06 -080035enum class ArtJvmtiEvent {
36 kMinEventTypeVal = JVMTI_MIN_EVENT_TYPE_VAL,
37 kVmInit = JVMTI_EVENT_VM_INIT,
38 kVmDeath = JVMTI_EVENT_VM_DEATH,
39 kThreadStart = JVMTI_EVENT_THREAD_START,
40 kThreadEnd = JVMTI_EVENT_THREAD_END,
Alex Light73afd322017-01-18 11:17:47 -080041 kClassFileLoadHookNonRetransformable = JVMTI_EVENT_CLASS_FILE_LOAD_HOOK,
Alex Light40d87f42017-01-18 10:27:06 -080042 kClassLoad = JVMTI_EVENT_CLASS_LOAD,
43 kClassPrepare = JVMTI_EVENT_CLASS_PREPARE,
44 kVmStart = JVMTI_EVENT_VM_START,
45 kException = JVMTI_EVENT_EXCEPTION,
46 kExceptionCatch = JVMTI_EVENT_EXCEPTION_CATCH,
47 kSingleStep = JVMTI_EVENT_SINGLE_STEP,
48 kFramePop = JVMTI_EVENT_FRAME_POP,
49 kBreakpoint = JVMTI_EVENT_BREAKPOINT,
50 kFieldAccess = JVMTI_EVENT_FIELD_ACCESS,
51 kFieldModification = JVMTI_EVENT_FIELD_MODIFICATION,
52 kMethodEntry = JVMTI_EVENT_METHOD_ENTRY,
53 kMethodExit = JVMTI_EVENT_METHOD_EXIT,
54 kNativeMethodBind = JVMTI_EVENT_NATIVE_METHOD_BIND,
55 kCompiledMethodLoad = JVMTI_EVENT_COMPILED_METHOD_LOAD,
56 kCompiledMethodUnload = JVMTI_EVENT_COMPILED_METHOD_UNLOAD,
57 kDynamicCodeGenerated = JVMTI_EVENT_DYNAMIC_CODE_GENERATED,
58 kDataDumpRequest = JVMTI_EVENT_DATA_DUMP_REQUEST,
59 kMonitorWait = JVMTI_EVENT_MONITOR_WAIT,
60 kMonitorWaited = JVMTI_EVENT_MONITOR_WAITED,
61 kMonitorContendedEnter = JVMTI_EVENT_MONITOR_CONTENDED_ENTER,
62 kMonitorContendedEntered = JVMTI_EVENT_MONITOR_CONTENDED_ENTERED,
63 kResourceExhausted = JVMTI_EVENT_RESOURCE_EXHAUSTED,
64 kGarbageCollectionStart = JVMTI_EVENT_GARBAGE_COLLECTION_START,
65 kGarbageCollectionFinish = JVMTI_EVENT_GARBAGE_COLLECTION_FINISH,
66 kObjectFree = JVMTI_EVENT_OBJECT_FREE,
67 kVmObjectAlloc = JVMTI_EVENT_VM_OBJECT_ALLOC,
Alex Light73afd322017-01-18 11:17:47 -080068 kClassFileLoadHookRetransformable = JVMTI_MAX_EVENT_TYPE_VAL + 1,
69 kMaxEventTypeVal = kClassFileLoadHookRetransformable,
Alex Light40d87f42017-01-18 10:27:06 -080070};
71
72// Convert a jvmtiEvent into a ArtJvmtiEvent
73ALWAYS_INLINE static inline ArtJvmtiEvent GetArtJvmtiEvent(ArtJvmTiEnv* env, jvmtiEvent e);
74
Alex Light73afd322017-01-18 11:17:47 -080075static inline jvmtiEvent GetJvmtiEvent(ArtJvmtiEvent e) {
76 if (UNLIKELY(e == ArtJvmtiEvent::kClassFileLoadHookRetransformable)) {
77 return JVMTI_EVENT_CLASS_FILE_LOAD_HOOK;
78 } else {
79 return static_cast<jvmtiEvent>(e);
80 }
Alex Light40d87f42017-01-18 10:27:06 -080081}
82
Andreas Gampe77708d92016-10-07 11:48:21 -070083struct EventMask {
Alex Light40d87f42017-01-18 10:27:06 -080084 static constexpr size_t kEventsSize =
85 static_cast<size_t>(ArtJvmtiEvent::kMaxEventTypeVal) -
86 static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal) + 1;
Andreas Gampe77708d92016-10-07 11:48:21 -070087 std::bitset<kEventsSize> bit_set;
88
Alex Light40d87f42017-01-18 10:27:06 -080089 static bool EventIsInRange(ArtJvmtiEvent event) {
90 return event >= ArtJvmtiEvent::kMinEventTypeVal && event <= ArtJvmtiEvent::kMaxEventTypeVal;
Andreas Gampe77708d92016-10-07 11:48:21 -070091 }
92
Alex Light40d87f42017-01-18 10:27:06 -080093 void Set(ArtJvmtiEvent event, bool value = true) {
Andreas Gampe77708d92016-10-07 11:48:21 -070094 DCHECK(EventIsInRange(event));
Alex Light40d87f42017-01-18 10:27:06 -080095 bit_set.set(static_cast<size_t>(event) - static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal),
96 value);
Andreas Gampe77708d92016-10-07 11:48:21 -070097 }
98
Alex Light40d87f42017-01-18 10:27:06 -080099 bool Test(ArtJvmtiEvent event) const {
Andreas Gampe77708d92016-10-07 11:48:21 -0700100 DCHECK(EventIsInRange(event));
Alex Light40d87f42017-01-18 10:27:06 -0800101 return bit_set.test(
102 static_cast<size_t>(event) - static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal));
Andreas Gampe77708d92016-10-07 11:48:21 -0700103 }
104};
105
106struct EventMasks {
107 // The globally enabled events.
108 EventMask global_event_mask;
109
110 // The per-thread enabled events.
111
112 // It is not enough to store a Thread pointer, as these may be reused. Use the pointer and the
113 // thread id.
114 // Note: We could just use the tid like tracing does.
115 using UniqueThread = std::pair<art::Thread*, uint32_t>;
116 // TODO: Native thread objects are immovable, so we can use them as keys in an (unordered) map,
117 // if necessary.
118 std::vector<std::pair<UniqueThread, EventMask>> thread_event_masks;
119
120 // A union of the per-thread events, for fast-pathing.
121 EventMask unioned_thread_event_mask;
122
123 EventMask& GetEventMask(art::Thread* thread);
124 EventMask* GetEventMaskOrNull(art::Thread* thread);
Alex Light40d87f42017-01-18 10:27:06 -0800125 void EnableEvent(art::Thread* thread, ArtJvmtiEvent event);
126 void DisableEvent(art::Thread* thread, ArtJvmtiEvent event);
Alex Light73afd322017-01-18 11:17:47 -0800127 bool IsEnabledAnywhere(ArtJvmtiEvent event);
128 // Make any changes to event masks needed for the given capability changes. If caps_added is true
129 // then caps is all the newly set capabilities of the jvmtiEnv. If it is false then caps is the
130 // set of all capabilities that were removed from the jvmtiEnv.
131 void HandleChangedCapabilities(const jvmtiCapabilities& caps, bool caps_added);
Andreas Gampe77708d92016-10-07 11:48:21 -0700132};
133
134// Helper class for event handling.
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700135class EventHandler {
136 public:
137 EventHandler();
138 ~EventHandler();
Andreas Gampe77708d92016-10-07 11:48:21 -0700139
140 // Register an env. It is assumed that this happens on env creation, that is, no events are
141 // enabled, yet.
142 void RegisterArtJvmTiEnv(ArtJvmTiEnv* env);
143
Alex Light40d87f42017-01-18 10:27:06 -0800144 bool IsEventEnabledAnywhere(ArtJvmtiEvent event) const {
Andreas Gampe77708d92016-10-07 11:48:21 -0700145 if (!EventMask::EventIsInRange(event)) {
146 return false;
147 }
148 return global_mask.Test(event);
149 }
150
Alex Light40d87f42017-01-18 10:27:06 -0800151 jvmtiError SetEvent(ArtJvmTiEnv* env,
152 art::Thread* thread,
153 ArtJvmtiEvent event,
154 jvmtiEventMode mode);
Andreas Gampe77708d92016-10-07 11:48:21 -0700155
156 template <typename ...Args>
Alex Light40d87f42017-01-18 10:27:06 -0800157 ALWAYS_INLINE
158 inline void DispatchEvent(art::Thread* thread, ArtJvmtiEvent event, Args... args) const;
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700159
Alex Light73afd322017-01-18 11:17:47 -0800160 // Tell the event handler capabilities were added/lost so it can adjust the sent events.If
161 // caps_added is true then caps is all the newly set capabilities of the jvmtiEnv. If it is false
162 // then caps is the set of all capabilities that were removed from the jvmtiEnv.
163 ALWAYS_INLINE
164 inline void HandleChangedCapabilities(ArtJvmTiEnv* env,
165 const jvmtiCapabilities& caps,
166 bool added);
167
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700168 private:
Alex Light40d87f42017-01-18 10:27:06 -0800169 ALWAYS_INLINE
170 static inline bool ShouldDispatch(ArtJvmtiEvent event, ArtJvmTiEnv* env, art::Thread* thread);
171
Alex Light73afd322017-01-18 11:17:47 -0800172 ALWAYS_INLINE
173 inline bool NeedsEventUpdate(ArtJvmTiEnv* env,
174 const jvmtiCapabilities& caps,
175 bool added);
176
177 // Recalculates the event mask for the given event.
178 ALWAYS_INLINE
179 inline void RecalculateGlobalEventMask(ArtJvmtiEvent event);
180
Alex Light6ac57502017-01-19 15:05:06 -0800181 template <typename ...Args>
182 ALWAYS_INLINE inline void GenericDispatchEvent(art::Thread* thread,
183 ArtJvmtiEvent event,
184 Args... args) const;
185 template <typename ...Args>
186 ALWAYS_INLINE inline void DispatchClassFileLoadHookEvent(art::Thread* thread,
187 ArtJvmtiEvent event,
188 Args... args) const;
189
Alex Light40d87f42017-01-18 10:27:06 -0800190 void HandleEventType(ArtJvmtiEvent event, bool enable);
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700191
192 // List of all JvmTiEnv objects that have been created, in their creation order.
193 std::vector<ArtJvmTiEnv*> envs;
194
195 // A union of all enabled events, anywhere.
196 EventMask global_mask;
197
198 std::unique_ptr<JvmtiAllocationListener> alloc_listener_;
Andreas Gampe9b8c5882016-10-21 15:27:46 -0700199 std::unique_ptr<JvmtiGcPauseListener> gc_pause_listener_;
Andreas Gampe77708d92016-10-07 11:48:21 -0700200};
201
202} // namespace openjdkjvmti
203
204#endif // ART_RUNTIME_OPENJDKJVMTI_EVENTS_H_