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