blob: fb39db5da950860ee1c77cc262c2314a699e27e1 [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_INL_H_
18#define ART_RUNTIME_OPENJDKJVMTI_EVENTS_INL_H_
19
20#include "events.h"
21
22#include "art_jvmti.h"
23
24namespace openjdkjvmti {
25
Alex Light40d87f42017-01-18 10:27:06 -080026static inline ArtJvmtiEvent GetArtJvmtiEvent(ArtJvmTiEnv* env ATTRIBUTE_UNUSED,
27 jvmtiEvent e) {
28 return static_cast<ArtJvmtiEvent>(e);
29}
30
Andreas Gampe77708d92016-10-07 11:48:21 -070031template <typename FnType>
Alex Light40d87f42017-01-18 10:27:06 -080032ALWAYS_INLINE static inline FnType* GetCallback(ArtJvmTiEnv* env, ArtJvmtiEvent event) {
Andreas Gampe77708d92016-10-07 11:48:21 -070033 if (env->event_callbacks == nullptr) {
34 return nullptr;
35 }
36
Andreas Gampe27fa96c2016-10-07 15:05:24 -070037 // TODO: Add a type check. Can be done, for example, by an explicitly instantiated template
38 // function.
39
Andreas Gampe77708d92016-10-07 11:48:21 -070040 switch (event) {
Alex Light40d87f42017-01-18 10:27:06 -080041 case ArtJvmtiEvent::kVmInit:
Andreas Gampe77708d92016-10-07 11:48:21 -070042 return reinterpret_cast<FnType*>(env->event_callbacks->VMInit);
Alex Light40d87f42017-01-18 10:27:06 -080043 case ArtJvmtiEvent::kVmDeath:
Andreas Gampe77708d92016-10-07 11:48:21 -070044 return reinterpret_cast<FnType*>(env->event_callbacks->VMDeath);
Alex Light40d87f42017-01-18 10:27:06 -080045 case ArtJvmtiEvent::kThreadStart:
Andreas Gampe77708d92016-10-07 11:48:21 -070046 return reinterpret_cast<FnType*>(env->event_callbacks->ThreadStart);
Alex Light40d87f42017-01-18 10:27:06 -080047 case ArtJvmtiEvent::kThreadEnd:
Andreas Gampe77708d92016-10-07 11:48:21 -070048 return reinterpret_cast<FnType*>(env->event_callbacks->ThreadEnd);
Alex Light40d87f42017-01-18 10:27:06 -080049 case ArtJvmtiEvent::kClassFileLoadHook:
Andreas Gampe77708d92016-10-07 11:48:21 -070050 return reinterpret_cast<FnType*>(env->event_callbacks->ClassFileLoadHook);
Alex Light40d87f42017-01-18 10:27:06 -080051 case ArtJvmtiEvent::kClassLoad:
Andreas Gampe77708d92016-10-07 11:48:21 -070052 return reinterpret_cast<FnType*>(env->event_callbacks->ClassLoad);
Alex Light40d87f42017-01-18 10:27:06 -080053 case ArtJvmtiEvent::kClassPrepare:
Andreas Gampe77708d92016-10-07 11:48:21 -070054 return reinterpret_cast<FnType*>(env->event_callbacks->ClassPrepare);
Alex Light40d87f42017-01-18 10:27:06 -080055 case ArtJvmtiEvent::kVmStart:
Andreas Gampe77708d92016-10-07 11:48:21 -070056 return reinterpret_cast<FnType*>(env->event_callbacks->VMStart);
Alex Light40d87f42017-01-18 10:27:06 -080057 case ArtJvmtiEvent::kException:
Andreas Gampe77708d92016-10-07 11:48:21 -070058 return reinterpret_cast<FnType*>(env->event_callbacks->Exception);
Alex Light40d87f42017-01-18 10:27:06 -080059 case ArtJvmtiEvent::kExceptionCatch:
Andreas Gampe77708d92016-10-07 11:48:21 -070060 return reinterpret_cast<FnType*>(env->event_callbacks->ExceptionCatch);
Alex Light40d87f42017-01-18 10:27:06 -080061 case ArtJvmtiEvent::kSingleStep:
Andreas Gampe77708d92016-10-07 11:48:21 -070062 return reinterpret_cast<FnType*>(env->event_callbacks->SingleStep);
Alex Light40d87f42017-01-18 10:27:06 -080063 case ArtJvmtiEvent::kFramePop:
Andreas Gampe77708d92016-10-07 11:48:21 -070064 return reinterpret_cast<FnType*>(env->event_callbacks->FramePop);
Alex Light40d87f42017-01-18 10:27:06 -080065 case ArtJvmtiEvent::kBreakpoint:
Andreas Gampe77708d92016-10-07 11:48:21 -070066 return reinterpret_cast<FnType*>(env->event_callbacks->Breakpoint);
Alex Light40d87f42017-01-18 10:27:06 -080067 case ArtJvmtiEvent::kFieldAccess:
Andreas Gampe77708d92016-10-07 11:48:21 -070068 return reinterpret_cast<FnType*>(env->event_callbacks->FieldAccess);
Alex Light40d87f42017-01-18 10:27:06 -080069 case ArtJvmtiEvent::kFieldModification:
Andreas Gampe77708d92016-10-07 11:48:21 -070070 return reinterpret_cast<FnType*>(env->event_callbacks->FieldModification);
Alex Light40d87f42017-01-18 10:27:06 -080071 case ArtJvmtiEvent::kMethodEntry:
Andreas Gampe77708d92016-10-07 11:48:21 -070072 return reinterpret_cast<FnType*>(env->event_callbacks->MethodEntry);
Alex Light40d87f42017-01-18 10:27:06 -080073 case ArtJvmtiEvent::kMethodExit:
Andreas Gampe77708d92016-10-07 11:48:21 -070074 return reinterpret_cast<FnType*>(env->event_callbacks->MethodExit);
Alex Light40d87f42017-01-18 10:27:06 -080075 case ArtJvmtiEvent::kNativeMethodBind:
Andreas Gampe77708d92016-10-07 11:48:21 -070076 return reinterpret_cast<FnType*>(env->event_callbacks->NativeMethodBind);
Alex Light40d87f42017-01-18 10:27:06 -080077 case ArtJvmtiEvent::kCompiledMethodLoad:
Andreas Gampe77708d92016-10-07 11:48:21 -070078 return reinterpret_cast<FnType*>(env->event_callbacks->CompiledMethodLoad);
Alex Light40d87f42017-01-18 10:27:06 -080079 case ArtJvmtiEvent::kCompiledMethodUnload:
Andreas Gampe77708d92016-10-07 11:48:21 -070080 return reinterpret_cast<FnType*>(env->event_callbacks->CompiledMethodUnload);
Alex Light40d87f42017-01-18 10:27:06 -080081 case ArtJvmtiEvent::kDynamicCodeGenerated:
Andreas Gampe77708d92016-10-07 11:48:21 -070082 return reinterpret_cast<FnType*>(env->event_callbacks->DynamicCodeGenerated);
Alex Light40d87f42017-01-18 10:27:06 -080083 case ArtJvmtiEvent::kDataDumpRequest:
Andreas Gampe77708d92016-10-07 11:48:21 -070084 return reinterpret_cast<FnType*>(env->event_callbacks->DataDumpRequest);
Alex Light40d87f42017-01-18 10:27:06 -080085 case ArtJvmtiEvent::kMonitorWait:
Andreas Gampe77708d92016-10-07 11:48:21 -070086 return reinterpret_cast<FnType*>(env->event_callbacks->MonitorWait);
Alex Light40d87f42017-01-18 10:27:06 -080087 case ArtJvmtiEvent::kMonitorWaited:
Andreas Gampe77708d92016-10-07 11:48:21 -070088 return reinterpret_cast<FnType*>(env->event_callbacks->MonitorWaited);
Alex Light40d87f42017-01-18 10:27:06 -080089 case ArtJvmtiEvent::kMonitorContendedEnter:
Andreas Gampe77708d92016-10-07 11:48:21 -070090 return reinterpret_cast<FnType*>(env->event_callbacks->MonitorContendedEnter);
Alex Light40d87f42017-01-18 10:27:06 -080091 case ArtJvmtiEvent::kMonitorContendedEntered:
Andreas Gampe77708d92016-10-07 11:48:21 -070092 return reinterpret_cast<FnType*>(env->event_callbacks->MonitorContendedEntered);
Alex Light40d87f42017-01-18 10:27:06 -080093 case ArtJvmtiEvent::kResourceExhausted:
Andreas Gampe77708d92016-10-07 11:48:21 -070094 return reinterpret_cast<FnType*>(env->event_callbacks->ResourceExhausted);
Alex Light40d87f42017-01-18 10:27:06 -080095 case ArtJvmtiEvent::kGarbageCollectionStart:
Andreas Gampe77708d92016-10-07 11:48:21 -070096 return reinterpret_cast<FnType*>(env->event_callbacks->GarbageCollectionStart);
Alex Light40d87f42017-01-18 10:27:06 -080097 case ArtJvmtiEvent::kGarbageCollectionFinish:
Andreas Gampe77708d92016-10-07 11:48:21 -070098 return reinterpret_cast<FnType*>(env->event_callbacks->GarbageCollectionFinish);
Alex Light40d87f42017-01-18 10:27:06 -080099 case ArtJvmtiEvent::kObjectFree:
Andreas Gampe77708d92016-10-07 11:48:21 -0700100 return reinterpret_cast<FnType*>(env->event_callbacks->ObjectFree);
Alex Light40d87f42017-01-18 10:27:06 -0800101 case ArtJvmtiEvent::kVmObjectAlloc:
Andreas Gampe77708d92016-10-07 11:48:21 -0700102 return reinterpret_cast<FnType*>(env->event_callbacks->VMObjectAlloc);
103 }
104 return nullptr;
105}
106
107template <typename ...Args>
Alex Light40d87f42017-01-18 10:27:06 -0800108inline void EventHandler::DispatchEvent(art::Thread* thread,
109 ArtJvmtiEvent event,
110 Args... args) const {
Andreas Gampe77708d92016-10-07 11:48:21 -0700111 using FnType = void(jvmtiEnv*, Args...);
112 for (ArtJvmTiEnv* env : envs) {
Alex Light40d87f42017-01-18 10:27:06 -0800113 if (ShouldDispatch(event, env, thread)) {
Andreas Gampe77708d92016-10-07 11:48:21 -0700114 FnType* callback = GetCallback<FnType>(env, event);
115 if (callback != nullptr) {
116 (*callback)(env, args...);
117 }
118 }
119 }
120}
121
Alex Light40d87f42017-01-18 10:27:06 -0800122inline bool EventHandler::ShouldDispatch(ArtJvmtiEvent event,
123 ArtJvmTiEnv* env,
124 art::Thread* thread) {
125 bool dispatch = env->event_masks.global_event_mask.Test(event);
126
127 if (!dispatch && thread != nullptr && env->event_masks.unioned_thread_event_mask.Test(event)) {
128 EventMask* mask = env->event_masks.GetEventMaskOrNull(thread);
129 dispatch = mask != nullptr && mask->Test(event);
130 }
131 return dispatch;
132}
133
Andreas Gampe77708d92016-10-07 11:48:21 -0700134} // namespace openjdkjvmti
135
136#endif // ART_RUNTIME_OPENJDKJVMTI_EVENTS_INL_H_