blob: 8f5614521758a87e349c6e65ad473063d9e502d2 [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 Light40d87f42017-01-18 10:27:06 -080033// an enum for ArtEvents.
34enum 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
71ALWAYS_INLINE static inline ArtJvmtiEvent GetArtJvmtiEvent(ArtJvmTiEnv* env, jvmtiEvent e);
72
73ALWAYS_INLINE static inline jvmtiEvent GetJvmtiEvent(ArtJvmtiEvent e) {
74 return static_cast<jvmtiEvent>(e);
75}
76
Andreas Gampe77708d92016-10-07 11:48:21 -070077struct EventMask {
Alex Light40d87f42017-01-18 10:27:06 -080078 static constexpr size_t kEventsSize =
79 static_cast<size_t>(ArtJvmtiEvent::kMaxEventTypeVal) -
80 static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal) + 1;
Andreas Gampe77708d92016-10-07 11:48:21 -070081 std::bitset<kEventsSize> bit_set;
82
Alex Light40d87f42017-01-18 10:27:06 -080083 static bool EventIsInRange(ArtJvmtiEvent event) {
84 return event >= ArtJvmtiEvent::kMinEventTypeVal && event <= ArtJvmtiEvent::kMaxEventTypeVal;
Andreas Gampe77708d92016-10-07 11:48:21 -070085 }
86
Alex Light40d87f42017-01-18 10:27:06 -080087 void Set(ArtJvmtiEvent event, bool value = true) {
Andreas Gampe77708d92016-10-07 11:48:21 -070088 DCHECK(EventIsInRange(event));
Alex Light40d87f42017-01-18 10:27:06 -080089 bit_set.set(static_cast<size_t>(event) - static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal),
90 value);
Andreas Gampe77708d92016-10-07 11:48:21 -070091 }
92
Alex Light40d87f42017-01-18 10:27:06 -080093 bool Test(ArtJvmtiEvent event) const {
Andreas Gampe77708d92016-10-07 11:48:21 -070094 DCHECK(EventIsInRange(event));
Alex Light40d87f42017-01-18 10:27:06 -080095 return bit_set.test(
96 static_cast<size_t>(event) - static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal));
Andreas Gampe77708d92016-10-07 11:48:21 -070097 }
98};
99
100struct 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 Light40d87f42017-01-18 10:27:06 -0800119 void EnableEvent(art::Thread* thread, ArtJvmtiEvent event);
120 void DisableEvent(art::Thread* thread, ArtJvmtiEvent event);
Andreas Gampe77708d92016-10-07 11:48:21 -0700121};
122
123// Helper class for event handling.
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700124class EventHandler {
125 public:
126 EventHandler();
127 ~EventHandler();
Andreas Gampe77708d92016-10-07 11:48:21 -0700128
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 Light40d87f42017-01-18 10:27:06 -0800133 bool IsEventEnabledAnywhere(ArtJvmtiEvent event) const {
Andreas Gampe77708d92016-10-07 11:48:21 -0700134 if (!EventMask::EventIsInRange(event)) {
135 return false;
136 }
137 return global_mask.Test(event);
138 }
139
Alex Light40d87f42017-01-18 10:27:06 -0800140 jvmtiError SetEvent(ArtJvmTiEnv* env,
141 art::Thread* thread,
142 ArtJvmtiEvent event,
143 jvmtiEventMode mode);
Andreas Gampe77708d92016-10-07 11:48:21 -0700144
145 template <typename ...Args>
Alex Light40d87f42017-01-18 10:27:06 -0800146 ALWAYS_INLINE
147 inline void DispatchEvent(art::Thread* thread, ArtJvmtiEvent event, Args... args) const;
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700148
149 private:
Alex Light40d87f42017-01-18 10:27:06 -0800150 ALWAYS_INLINE
151 static inline bool ShouldDispatch(ArtJvmtiEvent event, ArtJvmTiEnv* env, art::Thread* thread);
152
153 void HandleEventType(ArtJvmtiEvent event, bool enable);
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700154
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 Gampe9b8c5882016-10-21 15:27:46 -0700162 std::unique_ptr<JvmtiGcPauseListener> gc_pause_listener_;
Andreas Gampe77708d92016-10-07 11:48:21 -0700163};
164
165} // namespace openjdkjvmti
166
167#endif // ART_RUNTIME_OPENJDKJVMTI_EVENTS_H_