Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/AttachOnDemand/attach020/attach020Agent00.cpp
40951 views
1
/*
2
* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
#include <stdio.h>
24
#include <stdlib.h>
25
#include <string.h>
26
#include <jni.h>
27
#include <jvmti.h>
28
#include <aod.h>
29
#include <jvmti_aod.h>
30
31
extern "C" {
32
33
/*
34
* Expected agent work scenario:
35
* - from Agent_OnAttach function start auxiliary thread waiting on 'gcFinishMonitor'
36
* - receive GarbageCollectionStart event
37
* - receive GarbageCollectionFinish event, notify 'gcFinishMonitor'
38
* - notified auxiliary thread calls function 'nsk_aod_agentFinished' and agent completes work
39
* (such schema is used because of agent can't call nsk_aod_agentFinished from GarbageCollectionFinish handler,
40
* nsk_aod_agentFinished calls JNI functions and it is prohibited in GarbageCollectionFinish handler)
41
*
42
*/
43
44
static Options* options = NULL;
45
static const char* agentName;
46
47
static jvmtiEvent testEvents[] = { JVMTI_EVENT_GARBAGE_COLLECTION_START, JVMTI_EVENT_GARBAGE_COLLECTION_FINISH };
48
static const int testEventsNumber = 2;
49
50
static jrawMonitorID gcFinishMonitor;
51
52
static volatile int gcStartEventReceived = 0;
53
54
static volatile int gcFinishEventReceived = 0;
55
56
static volatile int success = 1;
57
58
void JNICALL garbageCollectionStartHandler(jvmtiEnv *jvmti) {
59
NSK_DISPLAY1("%s: GC start event received\n", agentName);
60
61
gcStartEventReceived = 1;
62
}
63
64
void JNICALL garbageCollectionFinishHandler(jvmtiEnv *jvmti) {
65
int auxiliaryThreadNotified = 0;
66
67
NSK_DISPLAY1("%s: GC finish event received\n", agentName);
68
69
if (!gcStartEventReceived) {
70
NSK_COMPLAIN1("%s: GC start event wasn't received before GC finish event\n", agentName);
71
success = 0;
72
}
73
74
if (NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(gcFinishMonitor))) {
75
76
gcFinishEventReceived = 1;
77
78
if (NSK_JVMTI_VERIFY(jvmti->RawMonitorNotify(gcFinishMonitor))) {
79
auxiliaryThreadNotified = 1;
80
}
81
82
if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(gcFinishMonitor))) {
83
success = 0;
84
}
85
} else {
86
success = 0;
87
}
88
89
if (!auxiliaryThreadNotified) {
90
NSK_COMPLAIN1("%s: Error happenned during auxiliary thread notification, test may hang\n", agentName);
91
}
92
}
93
94
void JNICALL auxiliaryThreadFunction(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
95
NSK_DISPLAY1("%s: auxiliary thread is running\n", agentName);
96
97
if (NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(gcFinishMonitor))) {
98
99
if (!gcFinishEventReceived) {
100
if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorWait(gcFinishMonitor, 0))) {
101
success = 0;
102
}
103
}
104
105
if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(gcFinishMonitor))) {
106
success = 0;
107
}
108
} else {
109
success = 0;
110
}
111
112
nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, success, jvmti, jni);
113
}
114
115
int startAuxiliaryThread(jvmtiEnv* jvmti, JNIEnv* jni) {
116
jthread thread;
117
118
thread = nsk_jvmti_aod_createThread(jni);
119
if (!NSK_VERIFY(thread != NULL))
120
return NSK_FALSE;
121
122
if (!NSK_JVMTI_VERIFY(jvmti->RunAgentThread(thread, auxiliaryThreadFunction, NULL, JVMTI_THREAD_NORM_PRIORITY))) {
123
return NSK_FALSE;
124
}
125
126
NSK_DISPLAY1("%s: auxiliary thread was started\n", agentName);
127
128
return NSK_TRUE;
129
}
130
131
#ifdef STATIC_BUILD
132
JNIEXPORT jint JNI_OnLoad_attach020Agent00(JavaVM *jvm, char *options, void *reserved) {
133
return JNI_VERSION_1_8;
134
}
135
#endif
136
137
JNIEXPORT jint JNICALL
138
#ifdef STATIC_BUILD
139
Agent_OnAttach_attach020Agent00(JavaVM *vm, char *optionsString, void *reserved)
140
#else
141
Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)
142
#endif
143
{
144
jvmtiEnv* jvmti;
145
jvmtiEventCallbacks eventCallbacks;
146
jvmtiCapabilities caps;
147
JNIEnv* jni;
148
149
options = (Options*) nsk_aod_createOptions(optionsString);
150
if (!NSK_VERIFY(options != NULL))
151
return JNI_ERR;
152
153
agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);
154
155
jni = (JNIEnv*) nsk_aod_createJNIEnv(vm);
156
if (jni == NULL)
157
return JNI_ERR;
158
159
jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved);
160
if (!NSK_VERIFY(jvmti != NULL))
161
return JNI_ERR;
162
163
if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("GCFinishMonitor", &gcFinishMonitor))) {
164
return JNI_ERR;
165
}
166
167
if (!NSK_VERIFY(startAuxiliaryThread(jvmti, jni)))
168
return JNI_ERR;
169
170
memset(&caps, 0, sizeof(caps));
171
caps.can_generate_garbage_collection_events = 1;
172
if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {
173
return JNI_ERR;
174
}
175
176
memset(&eventCallbacks,0, sizeof(eventCallbacks));
177
eventCallbacks.GarbageCollectionStart = garbageCollectionStartHandler;
178
eventCallbacks.GarbageCollectionFinish = garbageCollectionFinishHandler;
179
if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {
180
return JNI_ERR;
181
}
182
183
if (!(nsk_jvmti_aod_enableEvents(jvmti, testEvents, testEventsNumber))) {
184
return JNI_ERR;
185
}
186
187
NSK_DISPLAY1("%s: initialization was done\n", agentName);
188
189
if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))
190
return JNI_ERR;
191
192
return JNI_OK;
193
}
194
}
195
196