blob: 0d36c46fa1094f9168be7a7a9d8511fb990b8efa [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
Andreas Gampe06c42a52017-07-26 14:17:14 -070017#ifndef ART_OPENJDKJVMTI_EVENTS_H_
18#define ART_OPENJDKJVMTI_EVENTS_H_
Andreas Gampe77708d92016-10-07 11:48:21 -070019
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;
Alex Lightb7edcda2017-04-27 13:20:31 -070032class JvmtiMethodTraceListener;
Alex Light77fee872017-09-05 14:51:49 -070033class JvmtiMonitorListener;
Andreas Gampe77708d92016-10-07 11:48:21 -070034
Alex Light73afd322017-01-18 11:17:47 -080035// an enum for ArtEvents. This differs from the JVMTI events only in that we distinguish between
36// retransformation capable and incapable loading
Alex Light40d87f42017-01-18 10:27:06 -080037enum class ArtJvmtiEvent {
38 kMinEventTypeVal = JVMTI_MIN_EVENT_TYPE_VAL,
39 kVmInit = JVMTI_EVENT_VM_INIT,
40 kVmDeath = JVMTI_EVENT_VM_DEATH,
41 kThreadStart = JVMTI_EVENT_THREAD_START,
42 kThreadEnd = JVMTI_EVENT_THREAD_END,
Alex Light73afd322017-01-18 11:17:47 -080043 kClassFileLoadHookNonRetransformable = JVMTI_EVENT_CLASS_FILE_LOAD_HOOK,
Alex Light40d87f42017-01-18 10:27:06 -080044 kClassLoad = JVMTI_EVENT_CLASS_LOAD,
45 kClassPrepare = JVMTI_EVENT_CLASS_PREPARE,
46 kVmStart = JVMTI_EVENT_VM_START,
47 kException = JVMTI_EVENT_EXCEPTION,
48 kExceptionCatch = JVMTI_EVENT_EXCEPTION_CATCH,
49 kSingleStep = JVMTI_EVENT_SINGLE_STEP,
50 kFramePop = JVMTI_EVENT_FRAME_POP,
51 kBreakpoint = JVMTI_EVENT_BREAKPOINT,
52 kFieldAccess = JVMTI_EVENT_FIELD_ACCESS,
53 kFieldModification = JVMTI_EVENT_FIELD_MODIFICATION,
54 kMethodEntry = JVMTI_EVENT_METHOD_ENTRY,
55 kMethodExit = JVMTI_EVENT_METHOD_EXIT,
56 kNativeMethodBind = JVMTI_EVENT_NATIVE_METHOD_BIND,
57 kCompiledMethodLoad = JVMTI_EVENT_COMPILED_METHOD_LOAD,
58 kCompiledMethodUnload = JVMTI_EVENT_COMPILED_METHOD_UNLOAD,
59 kDynamicCodeGenerated = JVMTI_EVENT_DYNAMIC_CODE_GENERATED,
60 kDataDumpRequest = JVMTI_EVENT_DATA_DUMP_REQUEST,
61 kMonitorWait = JVMTI_EVENT_MONITOR_WAIT,
62 kMonitorWaited = JVMTI_EVENT_MONITOR_WAITED,
63 kMonitorContendedEnter = JVMTI_EVENT_MONITOR_CONTENDED_ENTER,
64 kMonitorContendedEntered = JVMTI_EVENT_MONITOR_CONTENDED_ENTERED,
65 kResourceExhausted = JVMTI_EVENT_RESOURCE_EXHAUSTED,
66 kGarbageCollectionStart = JVMTI_EVENT_GARBAGE_COLLECTION_START,
67 kGarbageCollectionFinish = JVMTI_EVENT_GARBAGE_COLLECTION_FINISH,
68 kObjectFree = JVMTI_EVENT_OBJECT_FREE,
69 kVmObjectAlloc = JVMTI_EVENT_VM_OBJECT_ALLOC,
Alex Light73afd322017-01-18 11:17:47 -080070 kClassFileLoadHookRetransformable = JVMTI_MAX_EVENT_TYPE_VAL + 1,
71 kMaxEventTypeVal = kClassFileLoadHookRetransformable,
Alex Light40d87f42017-01-18 10:27:06 -080072};
73
74// Convert a jvmtiEvent into a ArtJvmtiEvent
75ALWAYS_INLINE static inline ArtJvmtiEvent GetArtJvmtiEvent(ArtJvmTiEnv* env, jvmtiEvent e);
76
Alex Light73afd322017-01-18 11:17:47 -080077static inline jvmtiEvent GetJvmtiEvent(ArtJvmtiEvent e) {
78 if (UNLIKELY(e == ArtJvmtiEvent::kClassFileLoadHookRetransformable)) {
79 return JVMTI_EVENT_CLASS_FILE_LOAD_HOOK;
80 } else {
81 return static_cast<jvmtiEvent>(e);
82 }
Alex Light40d87f42017-01-18 10:27:06 -080083}
84
Andreas Gampe77708d92016-10-07 11:48:21 -070085struct EventMask {
Alex Light40d87f42017-01-18 10:27:06 -080086 static constexpr size_t kEventsSize =
87 static_cast<size_t>(ArtJvmtiEvent::kMaxEventTypeVal) -
88 static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal) + 1;
Andreas Gampe77708d92016-10-07 11:48:21 -070089 std::bitset<kEventsSize> bit_set;
90
Alex Light40d87f42017-01-18 10:27:06 -080091 static bool EventIsInRange(ArtJvmtiEvent event) {
92 return event >= ArtJvmtiEvent::kMinEventTypeVal && event <= ArtJvmtiEvent::kMaxEventTypeVal;
Andreas Gampe77708d92016-10-07 11:48:21 -070093 }
94
Alex Light40d87f42017-01-18 10:27:06 -080095 void Set(ArtJvmtiEvent event, bool value = true) {
Andreas Gampe77708d92016-10-07 11:48:21 -070096 DCHECK(EventIsInRange(event));
Alex Light40d87f42017-01-18 10:27:06 -080097 bit_set.set(static_cast<size_t>(event) - static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal),
98 value);
Andreas Gampe77708d92016-10-07 11:48:21 -070099 }
100
Alex Light40d87f42017-01-18 10:27:06 -0800101 bool Test(ArtJvmtiEvent event) const {
Andreas Gampe77708d92016-10-07 11:48:21 -0700102 DCHECK(EventIsInRange(event));
Alex Light40d87f42017-01-18 10:27:06 -0800103 return bit_set.test(
104 static_cast<size_t>(event) - static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal));
Andreas Gampe77708d92016-10-07 11:48:21 -0700105 }
106};
107
108struct EventMasks {
109 // The globally enabled events.
110 EventMask global_event_mask;
111
112 // The per-thread enabled events.
113
114 // It is not enough to store a Thread pointer, as these may be reused. Use the pointer and the
115 // thread id.
116 // Note: We could just use the tid like tracing does.
117 using UniqueThread = std::pair<art::Thread*, uint32_t>;
118 // TODO: Native thread objects are immovable, so we can use them as keys in an (unordered) map,
119 // if necessary.
120 std::vector<std::pair<UniqueThread, EventMask>> thread_event_masks;
121
122 // A union of the per-thread events, for fast-pathing.
123 EventMask unioned_thread_event_mask;
124
125 EventMask& GetEventMask(art::Thread* thread);
126 EventMask* GetEventMaskOrNull(art::Thread* thread);
Alex Light40d87f42017-01-18 10:27:06 -0800127 void EnableEvent(art::Thread* thread, ArtJvmtiEvent event);
128 void DisableEvent(art::Thread* thread, ArtJvmtiEvent event);
Alex Light73afd322017-01-18 11:17:47 -0800129 bool IsEnabledAnywhere(ArtJvmtiEvent event);
130 // Make any changes to event masks needed for the given capability changes. If caps_added is true
131 // then caps is all the newly set capabilities of the jvmtiEnv. If it is false then caps is the
132 // set of all capabilities that were removed from the jvmtiEnv.
133 void HandleChangedCapabilities(const jvmtiCapabilities& caps, bool caps_added);
Andreas Gampe77708d92016-10-07 11:48:21 -0700134};
135
136// Helper class for event handling.
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700137class EventHandler {
138 public:
139 EventHandler();
140 ~EventHandler();
Andreas Gampe77708d92016-10-07 11:48:21 -0700141
Alex Lightb7edcda2017-04-27 13:20:31 -0700142 // do cleanup for the event handler.
143 void Shutdown();
144
Andreas Gampe77708d92016-10-07 11:48:21 -0700145 // Register an env. It is assumed that this happens on env creation, that is, no events are
146 // enabled, yet.
147 void RegisterArtJvmTiEnv(ArtJvmTiEnv* env);
148
Andreas Gampe3a7eb142017-01-19 21:59:22 -0800149 // Remove an env.
150 void RemoveArtJvmTiEnv(ArtJvmTiEnv* env);
151
Alex Light40d87f42017-01-18 10:27:06 -0800152 bool IsEventEnabledAnywhere(ArtJvmtiEvent event) const {
Andreas Gampe77708d92016-10-07 11:48:21 -0700153 if (!EventMask::EventIsInRange(event)) {
154 return false;
155 }
156 return global_mask.Test(event);
157 }
158
Alex Light40d87f42017-01-18 10:27:06 -0800159 jvmtiError SetEvent(ArtJvmTiEnv* env,
160 art::Thread* thread,
161 ArtJvmtiEvent event,
162 jvmtiEventMode mode);
Andreas Gampe77708d92016-10-07 11:48:21 -0700163
Andreas Gampea1705ea2017-03-28 20:12:13 -0700164 // Dispatch event to all registered environments.
Andreas Gampe983c1752017-01-23 19:46:56 -0800165 template <ArtJvmtiEvent kEvent, typename ...Args>
Alex Light40d87f42017-01-18 10:27:06 -0800166 ALWAYS_INLINE
Andreas Gampe983c1752017-01-23 19:46:56 -0800167 inline void DispatchEvent(art::Thread* thread, Args... args) const;
Alex Lightb7edcda2017-04-27 13:20:31 -0700168 // Dispatch event to all registered environments stashing exceptions as needed. This works since
169 // JNIEnv* is always the second argument if it is passed to an event. Needed since C++ does not
170 // allow partial template function specialization.
171 template <ArtJvmtiEvent kEvent, typename ...Args>
172 ALWAYS_INLINE
173 void DispatchEvent(art::Thread* thread, JNIEnv* jnienv, Args... args) const;
Andreas Gampea1705ea2017-03-28 20:12:13 -0700174 // Dispatch event to the given environment, only.
175 template <ArtJvmtiEvent kEvent, typename ...Args>
176 ALWAYS_INLINE
177 inline void DispatchEvent(ArtJvmTiEnv* env, art::Thread* thread, Args... args) const;
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700178
Alex Light73afd322017-01-18 11:17:47 -0800179 // Tell the event handler capabilities were added/lost so it can adjust the sent events.If
180 // caps_added is true then caps is all the newly set capabilities of the jvmtiEnv. If it is false
181 // then caps is the set of all capabilities that were removed from the jvmtiEnv.
182 ALWAYS_INLINE
183 inline void HandleChangedCapabilities(ArtJvmTiEnv* env,
184 const jvmtiCapabilities& caps,
185 bool added);
186
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700187 private:
Andreas Gampe983c1752017-01-23 19:46:56 -0800188 template <ArtJvmtiEvent kEvent>
Alex Light40d87f42017-01-18 10:27:06 -0800189 ALWAYS_INLINE
Andreas Gampe983c1752017-01-23 19:46:56 -0800190 static inline bool ShouldDispatch(ArtJvmTiEnv* env, art::Thread* thread);
Alex Light40d87f42017-01-18 10:27:06 -0800191
Alex Light73afd322017-01-18 11:17:47 -0800192 ALWAYS_INLINE
193 inline bool NeedsEventUpdate(ArtJvmTiEnv* env,
194 const jvmtiCapabilities& caps,
195 bool added);
196
197 // Recalculates the event mask for the given event.
198 ALWAYS_INLINE
199 inline void RecalculateGlobalEventMask(ArtJvmtiEvent event);
200
Andreas Gampe983c1752017-01-23 19:46:56 -0800201 template <ArtJvmtiEvent kEvent>
Alex Light6ac57502017-01-19 15:05:06 -0800202 ALWAYS_INLINE inline void DispatchClassFileLoadHookEvent(art::Thread* thread,
Andreas Gampe983c1752017-01-23 19:46:56 -0800203 JNIEnv* jnienv,
204 jclass class_being_redefined,
205 jobject loader,
206 const char* name,
207 jobject protection_domain,
208 jint class_data_len,
209 const unsigned char* class_data,
210 jint* new_class_data_len,
211 unsigned char** new_class_data) const;
Alex Light6ac57502017-01-19 15:05:06 -0800212
Alex Light40d87f42017-01-18 10:27:06 -0800213 void HandleEventType(ArtJvmtiEvent event, bool enable);
Alex Lightbebd7bd2017-07-25 14:05:52 -0700214 void HandleLocalAccessCapabilityAdded();
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700215
Alex Light77fee872017-09-05 14:51:49 -0700216 bool OtherMonitorEventsEnabledAnywhere(ArtJvmtiEvent event);
217
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700218 // List of all JvmTiEnv objects that have been created, in their creation order.
Alex Lightbb766462017-04-12 16:13:33 -0700219 // NB Some elements might be null representing envs that have been deleted. They should be skipped
220 // anytime this list is used.
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700221 std::vector<ArtJvmTiEnv*> envs;
222
223 // A union of all enabled events, anywhere.
224 EventMask global_mask;
225
226 std::unique_ptr<JvmtiAllocationListener> alloc_listener_;
Andreas Gampe9b8c5882016-10-21 15:27:46 -0700227 std::unique_ptr<JvmtiGcPauseListener> gc_pause_listener_;
Alex Lightb7edcda2017-04-27 13:20:31 -0700228 std::unique_ptr<JvmtiMethodTraceListener> method_trace_listener_;
Alex Light77fee872017-09-05 14:51:49 -0700229 std::unique_ptr<JvmtiMonitorListener> monitor_listener_;
Alex Lighte814f9d2017-07-31 16:14:39 -0700230
231 // True if frame pop has ever been enabled. Since we store pointers to stack frames we need to
232 // continue to listen to this event even if it has been disabled.
233 // TODO We could remove the listeners once all jvmtiEnvs have drained their shadow-frame vectors.
234 bool frame_pop_enabled;
Andreas Gampe77708d92016-10-07 11:48:21 -0700235};
236
237} // namespace openjdkjvmti
238
Andreas Gampe06c42a52017-07-26 14:17:14 -0700239#endif // ART_OPENJDKJVMTI_EVENTS_H_