blob: 5716d0399ae13bfa3e33d40415fe65a07027423f [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;
30
31struct EventMask {
32 static constexpr size_t kEventsSize = JVMTI_MAX_EVENT_TYPE_VAL - JVMTI_MIN_EVENT_TYPE_VAL + 1;
33 std::bitset<kEventsSize> bit_set;
34
35 static bool EventIsInRange(jvmtiEvent event) {
36 return event >= JVMTI_MIN_EVENT_TYPE_VAL && event <= JVMTI_MAX_EVENT_TYPE_VAL;
37 }
38
39 void Set(jvmtiEvent event, bool value = true) {
40 DCHECK(EventIsInRange(event));
41 bit_set.set(event - JVMTI_MIN_EVENT_TYPE_VAL, value);
42 }
43
44 bool Test(jvmtiEvent event) const {
45 DCHECK(EventIsInRange(event));
46 return bit_set.test(event - JVMTI_MIN_EVENT_TYPE_VAL);
47 }
48};
49
50struct EventMasks {
51 // The globally enabled events.
52 EventMask global_event_mask;
53
54 // The per-thread enabled events.
55
56 // It is not enough to store a Thread pointer, as these may be reused. Use the pointer and the
57 // thread id.
58 // Note: We could just use the tid like tracing does.
59 using UniqueThread = std::pair<art::Thread*, uint32_t>;
60 // TODO: Native thread objects are immovable, so we can use them as keys in an (unordered) map,
61 // if necessary.
62 std::vector<std::pair<UniqueThread, EventMask>> thread_event_masks;
63
64 // A union of the per-thread events, for fast-pathing.
65 EventMask unioned_thread_event_mask;
66
67 EventMask& GetEventMask(art::Thread* thread);
68 EventMask* GetEventMaskOrNull(art::Thread* thread);
69 void EnableEvent(art::Thread* thread, jvmtiEvent event);
70 void DisableEvent(art::Thread* thread, jvmtiEvent event);
71};
72
73// Helper class for event handling.
74struct EventHandler {
75 // List of all JvmTiEnv objects that have been created, in their creation order.
76 std::vector<ArtJvmTiEnv*> envs;
77
78 // A union of all enabled events, anywhere.
79 EventMask global_mask;
80
81 // Register an env. It is assumed that this happens on env creation, that is, no events are
82 // enabled, yet.
83 void RegisterArtJvmTiEnv(ArtJvmTiEnv* env);
84
85 bool IsEventEnabledAnywhere(jvmtiEvent event) {
86 if (!EventMask::EventIsInRange(event)) {
87 return false;
88 }
89 return global_mask.Test(event);
90 }
91
92 jvmtiError SetEvent(ArtJvmTiEnv* env, art::Thread* thread, jvmtiEvent event, jvmtiEventMode mode);
93
94 template <typename ...Args>
95 ALWAYS_INLINE inline void DispatchEvent(art::Thread* thread, jvmtiEvent event, Args... args);
96};
97
98} // namespace openjdkjvmti
99
100#endif // ART_RUNTIME_OPENJDKJVMTI_EVENTS_H_