blob: b49e80c46dd3edd2afff16f756a6dc9f348c6fa0 [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;
Andreas Gampe77708d92016-10-07 11:48:21 -070033
Alex Light73afd322017-01-18 11:17:47 -080034// an enum for ArtEvents. This differs from the JVMTI events only in that we distinguish between
35// retransformation capable and incapable loading
Alex Light40d87f42017-01-18 10:27:06 -080036enum class ArtJvmtiEvent {
37 kMinEventTypeVal = JVMTI_MIN_EVENT_TYPE_VAL,
38 kVmInit = JVMTI_EVENT_VM_INIT,
39 kVmDeath = JVMTI_EVENT_VM_DEATH,
40 kThreadStart = JVMTI_EVENT_THREAD_START,
41 kThreadEnd = JVMTI_EVENT_THREAD_END,
Alex Light73afd322017-01-18 11:17:47 -080042 kClassFileLoadHookNonRetransformable = JVMTI_EVENT_CLASS_FILE_LOAD_HOOK,
Alex Light40d87f42017-01-18 10:27:06 -080043 kClassLoad = JVMTI_EVENT_CLASS_LOAD,
44 kClassPrepare = JVMTI_EVENT_CLASS_PREPARE,
45 kVmStart = JVMTI_EVENT_VM_START,
46 kException = JVMTI_EVENT_EXCEPTION,
47 kExceptionCatch = JVMTI_EVENT_EXCEPTION_CATCH,
48 kSingleStep = JVMTI_EVENT_SINGLE_STEP,
49 kFramePop = JVMTI_EVENT_FRAME_POP,
50 kBreakpoint = JVMTI_EVENT_BREAKPOINT,
51 kFieldAccess = JVMTI_EVENT_FIELD_ACCESS,
52 kFieldModification = JVMTI_EVENT_FIELD_MODIFICATION,
53 kMethodEntry = JVMTI_EVENT_METHOD_ENTRY,
54 kMethodExit = JVMTI_EVENT_METHOD_EXIT,
55 kNativeMethodBind = JVMTI_EVENT_NATIVE_METHOD_BIND,
56 kCompiledMethodLoad = JVMTI_EVENT_COMPILED_METHOD_LOAD,
57 kCompiledMethodUnload = JVMTI_EVENT_COMPILED_METHOD_UNLOAD,
58 kDynamicCodeGenerated = JVMTI_EVENT_DYNAMIC_CODE_GENERATED,
59 kDataDumpRequest = JVMTI_EVENT_DATA_DUMP_REQUEST,
60 kMonitorWait = JVMTI_EVENT_MONITOR_WAIT,
61 kMonitorWaited = JVMTI_EVENT_MONITOR_WAITED,
62 kMonitorContendedEnter = JVMTI_EVENT_MONITOR_CONTENDED_ENTER,
63 kMonitorContendedEntered = JVMTI_EVENT_MONITOR_CONTENDED_ENTERED,
64 kResourceExhausted = JVMTI_EVENT_RESOURCE_EXHAUSTED,
65 kGarbageCollectionStart = JVMTI_EVENT_GARBAGE_COLLECTION_START,
66 kGarbageCollectionFinish = JVMTI_EVENT_GARBAGE_COLLECTION_FINISH,
67 kObjectFree = JVMTI_EVENT_OBJECT_FREE,
68 kVmObjectAlloc = JVMTI_EVENT_VM_OBJECT_ALLOC,
Alex Light73afd322017-01-18 11:17:47 -080069 kClassFileLoadHookRetransformable = JVMTI_MAX_EVENT_TYPE_VAL + 1,
70 kMaxEventTypeVal = kClassFileLoadHookRetransformable,
Alex Light40d87f42017-01-18 10:27:06 -080071};
72
73// Convert a jvmtiEvent into a ArtJvmtiEvent
74ALWAYS_INLINE static inline ArtJvmtiEvent GetArtJvmtiEvent(ArtJvmTiEnv* env, jvmtiEvent e);
75
Alex Light73afd322017-01-18 11:17:47 -080076static inline jvmtiEvent GetJvmtiEvent(ArtJvmtiEvent e) {
77 if (UNLIKELY(e == ArtJvmtiEvent::kClassFileLoadHookRetransformable)) {
78 return JVMTI_EVENT_CLASS_FILE_LOAD_HOOK;
79 } else {
80 return static_cast<jvmtiEvent>(e);
81 }
Alex Light40d87f42017-01-18 10:27:06 -080082}
83
Andreas Gampe77708d92016-10-07 11:48:21 -070084struct EventMask {
Alex Light40d87f42017-01-18 10:27:06 -080085 static constexpr size_t kEventsSize =
86 static_cast<size_t>(ArtJvmtiEvent::kMaxEventTypeVal) -
87 static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal) + 1;
Andreas Gampe77708d92016-10-07 11:48:21 -070088 std::bitset<kEventsSize> bit_set;
89
Alex Light40d87f42017-01-18 10:27:06 -080090 static bool EventIsInRange(ArtJvmtiEvent event) {
91 return event >= ArtJvmtiEvent::kMinEventTypeVal && event <= ArtJvmtiEvent::kMaxEventTypeVal;
Andreas Gampe77708d92016-10-07 11:48:21 -070092 }
93
Alex Light40d87f42017-01-18 10:27:06 -080094 void Set(ArtJvmtiEvent event, bool value = true) {
Andreas Gampe77708d92016-10-07 11:48:21 -070095 DCHECK(EventIsInRange(event));
Alex Light40d87f42017-01-18 10:27:06 -080096 bit_set.set(static_cast<size_t>(event) - static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal),
97 value);
Andreas Gampe77708d92016-10-07 11:48:21 -070098 }
99
Alex Light40d87f42017-01-18 10:27:06 -0800100 bool Test(ArtJvmtiEvent event) const {
Andreas Gampe77708d92016-10-07 11:48:21 -0700101 DCHECK(EventIsInRange(event));
Alex Light40d87f42017-01-18 10:27:06 -0800102 return bit_set.test(
103 static_cast<size_t>(event) - static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal));
Andreas Gampe77708d92016-10-07 11:48:21 -0700104 }
105};
106
107struct EventMasks {
108 // The globally enabled events.
109 EventMask global_event_mask;
110
111 // The per-thread enabled events.
112
113 // It is not enough to store a Thread pointer, as these may be reused. Use the pointer and the
114 // thread id.
115 // Note: We could just use the tid like tracing does.
116 using UniqueThread = std::pair<art::Thread*, uint32_t>;
117 // TODO: Native thread objects are immovable, so we can use them as keys in an (unordered) map,
118 // if necessary.
119 std::vector<std::pair<UniqueThread, EventMask>> thread_event_masks;
120
121 // A union of the per-thread events, for fast-pathing.
122 EventMask unioned_thread_event_mask;
123
124 EventMask& GetEventMask(art::Thread* thread);
125 EventMask* GetEventMaskOrNull(art::Thread* thread);
Alex Light40d87f42017-01-18 10:27:06 -0800126 void EnableEvent(art::Thread* thread, ArtJvmtiEvent event);
127 void DisableEvent(art::Thread* thread, ArtJvmtiEvent event);
Alex Light73afd322017-01-18 11:17:47 -0800128 bool IsEnabledAnywhere(ArtJvmtiEvent event);
129 // Make any changes to event masks needed for the given capability changes. If caps_added is true
130 // then caps is all the newly set capabilities of the jvmtiEnv. If it is false then caps is the
131 // set of all capabilities that were removed from the jvmtiEnv.
132 void HandleChangedCapabilities(const jvmtiCapabilities& caps, bool caps_added);
Andreas Gampe77708d92016-10-07 11:48:21 -0700133};
134
135// Helper class for event handling.
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700136class EventHandler {
137 public:
138 EventHandler();
139 ~EventHandler();
Andreas Gampe77708d92016-10-07 11:48:21 -0700140
Alex Lightb7edcda2017-04-27 13:20:31 -0700141 // do cleanup for the event handler.
142 void Shutdown();
143
Andreas Gampe77708d92016-10-07 11:48:21 -0700144 // Register an env. It is assumed that this happens on env creation, that is, no events are
145 // enabled, yet.
146 void RegisterArtJvmTiEnv(ArtJvmTiEnv* env);
147
Andreas Gampe3a7eb142017-01-19 21:59:22 -0800148 // Remove an env.
149 void RemoveArtJvmTiEnv(ArtJvmTiEnv* env);
150
Alex Light40d87f42017-01-18 10:27:06 -0800151 bool IsEventEnabledAnywhere(ArtJvmtiEvent event) const {
Andreas Gampe77708d92016-10-07 11:48:21 -0700152 if (!EventMask::EventIsInRange(event)) {
153 return false;
154 }
155 return global_mask.Test(event);
156 }
157
Alex Light40d87f42017-01-18 10:27:06 -0800158 jvmtiError SetEvent(ArtJvmTiEnv* env,
159 art::Thread* thread,
160 ArtJvmtiEvent event,
161 jvmtiEventMode mode);
Andreas Gampe77708d92016-10-07 11:48:21 -0700162
Andreas Gampea1705ea2017-03-28 20:12:13 -0700163 // Dispatch event to all registered environments.
Andreas Gampe983c1752017-01-23 19:46:56 -0800164 template <ArtJvmtiEvent kEvent, typename ...Args>
Alex Light40d87f42017-01-18 10:27:06 -0800165 ALWAYS_INLINE
Andreas Gampe983c1752017-01-23 19:46:56 -0800166 inline void DispatchEvent(art::Thread* thread, Args... args) const;
Alex Lightb7edcda2017-04-27 13:20:31 -0700167 // Dispatch event to all registered environments stashing exceptions as needed. This works since
168 // JNIEnv* is always the second argument if it is passed to an event. Needed since C++ does not
169 // allow partial template function specialization.
170 template <ArtJvmtiEvent kEvent, typename ...Args>
171 ALWAYS_INLINE
172 void DispatchEvent(art::Thread* thread, JNIEnv* jnienv, Args... args) const;
Andreas Gampea1705ea2017-03-28 20:12:13 -0700173 // Dispatch event to the given environment, only.
174 template <ArtJvmtiEvent kEvent, typename ...Args>
175 ALWAYS_INLINE
176 inline void DispatchEvent(ArtJvmTiEnv* env, art::Thread* thread, Args... args) const;
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700177
Alex Light73afd322017-01-18 11:17:47 -0800178 // Tell the event handler capabilities were added/lost so it can adjust the sent events.If
179 // caps_added is true then caps is all the newly set capabilities of the jvmtiEnv. If it is false
180 // then caps is the set of all capabilities that were removed from the jvmtiEnv.
181 ALWAYS_INLINE
182 inline void HandleChangedCapabilities(ArtJvmTiEnv* env,
183 const jvmtiCapabilities& caps,
184 bool added);
185
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700186 private:
Andreas Gampe983c1752017-01-23 19:46:56 -0800187 template <ArtJvmtiEvent kEvent>
Alex Light40d87f42017-01-18 10:27:06 -0800188 ALWAYS_INLINE
Andreas Gampe983c1752017-01-23 19:46:56 -0800189 static inline bool ShouldDispatch(ArtJvmTiEnv* env, art::Thread* thread);
Alex Light40d87f42017-01-18 10:27:06 -0800190
Alex Light73afd322017-01-18 11:17:47 -0800191 ALWAYS_INLINE
192 inline bool NeedsEventUpdate(ArtJvmTiEnv* env,
193 const jvmtiCapabilities& caps,
194 bool added);
195
196 // Recalculates the event mask for the given event.
197 ALWAYS_INLINE
198 inline void RecalculateGlobalEventMask(ArtJvmtiEvent event);
199
Andreas Gampe983c1752017-01-23 19:46:56 -0800200 template <ArtJvmtiEvent kEvent>
Alex Light6ac57502017-01-19 15:05:06 -0800201 ALWAYS_INLINE inline void DispatchClassFileLoadHookEvent(art::Thread* thread,
Andreas Gampe983c1752017-01-23 19:46:56 -0800202 JNIEnv* jnienv,
203 jclass class_being_redefined,
204 jobject loader,
205 const char* name,
206 jobject protection_domain,
207 jint class_data_len,
208 const unsigned char* class_data,
209 jint* new_class_data_len,
210 unsigned char** new_class_data) const;
Alex Light6ac57502017-01-19 15:05:06 -0800211
Alex Light40d87f42017-01-18 10:27:06 -0800212 void HandleEventType(ArtJvmtiEvent event, bool enable);
Alex Lightbebd7bd2017-07-25 14:05:52 -0700213 void HandleLocalAccessCapabilityAdded();
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700214
215 // List of all JvmTiEnv objects that have been created, in their creation order.
Alex Lightbb766462017-04-12 16:13:33 -0700216 // NB Some elements might be null representing envs that have been deleted. They should be skipped
217 // anytime this list is used.
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700218 std::vector<ArtJvmTiEnv*> envs;
219
220 // A union of all enabled events, anywhere.
221 EventMask global_mask;
222
223 std::unique_ptr<JvmtiAllocationListener> alloc_listener_;
Andreas Gampe9b8c5882016-10-21 15:27:46 -0700224 std::unique_ptr<JvmtiGcPauseListener> gc_pause_listener_;
Alex Lightb7edcda2017-04-27 13:20:31 -0700225 std::unique_ptr<JvmtiMethodTraceListener> method_trace_listener_;
Alex Lighte814f9d2017-07-31 16:14:39 -0700226
227 // True if frame pop has ever been enabled. Since we store pointers to stack frames we need to
228 // continue to listen to this event even if it has been disabled.
229 // TODO We could remove the listeners once all jvmtiEnvs have drained their shadow-frame vectors.
230 bool frame_pop_enabled;
Andreas Gampe77708d92016-10-07 11:48:21 -0700231};
232
233} // namespace openjdkjvmti
234
Andreas Gampe06c42a52017-07-26 14:17:14 -0700235#endif // ART_OPENJDKJVMTI_EVENTS_H_