Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/GetAllThreads/allthr001/allthr001.cpp
40955 views
1
/*
2
* Copyright (c) 2003, 2020, 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
24
#include <stdio.h>
25
#include <stdlib.h>
26
#include <string.h>
27
#include "jvmti.h"
28
#include "agent_common.h"
29
#include "JVMTITools.h"
30
31
extern "C" {
32
33
34
#define PASSED 0
35
#define STATUS_FAILED 2
36
37
typedef struct {
38
int cnt;
39
const char **thrNames;
40
} info;
41
42
static jvmtiEnv *jvmti;
43
static jrawMonitorID lock1;
44
static jrawMonitorID lock2;
45
static jboolean printdump = JNI_FALSE;
46
static jint result = PASSED;
47
static jvmtiThreadInfo inf;
48
static int sys_cnt;
49
static const char *names0[] = { "main" };
50
static const char *names1[] = { "main", "thread1" };
51
static const char *names2[] = { "main", "Thread-" };
52
static info thrInfo[] = {
53
{ 1, names0 }, { 1, names0 }, { 2, names1 }, { 1, names0 }, { 2, names2 }
54
};
55
56
jthread jthr(JNIEnv *env) {
57
jclass thrClass;
58
jmethodID cid;
59
jthread res;
60
thrClass = env->FindClass("java/lang/Thread");
61
cid = env->GetMethodID(thrClass, "<init>", "()V");
62
res = env->NewObject(thrClass, cid);
63
return res;
64
}
65
66
static void JNICALL
67
sys_thread(jvmtiEnv* jvmti, JNIEnv* jni, void *p) {
68
jvmtiError err;
69
70
err = jvmti->RawMonitorEnter(lock2);
71
if (err != JVMTI_ERROR_NONE) {
72
printf("Failed to enter raw monitor 2 (thread): %s (%d)\n",
73
TranslateError(err), err);
74
result = STATUS_FAILED;
75
}
76
77
/* allows the main thread to wait until the child thread is running */
78
err = jvmti->RawMonitorEnter(lock1);
79
if (err != JVMTI_ERROR_NONE) {
80
printf("Failed to enter raw monitor 1 (thread): %s (%d)\n",
81
TranslateError(err), err);
82
result = STATUS_FAILED;
83
}
84
err = jvmti->RawMonitorNotify(lock1);
85
if (err != JVMTI_ERROR_NONE) {
86
printf("Failed to notify raw monitor (thread): %s (%d)\n",
87
TranslateError(err), err);
88
result = STATUS_FAILED;
89
}
90
err = jvmti->RawMonitorExit(lock1);
91
if (err != JVMTI_ERROR_NONE) {
92
printf("Failed to exit raw monitor 1 (thread): %s (%d)\n",
93
TranslateError(err), err);
94
result = STATUS_FAILED;
95
}
96
97
/* keeps the child thread from exiting */
98
err = jvmti->RawMonitorWait(lock2, (jlong)0);
99
if (err != JVMTI_ERROR_NONE) {
100
printf("Failed to wait raw monitor (thread): %s (%d)\n",
101
TranslateError(err), err);
102
result = STATUS_FAILED;
103
}
104
err = jvmti->RawMonitorExit(lock2);
105
if (err != JVMTI_ERROR_NONE) {
106
printf("Failed to exit raw monitor 2 (thread): %s (%d)\n",
107
TranslateError(err), err);
108
result = STATUS_FAILED;
109
}
110
}
111
112
#ifdef STATIC_BUILD
113
JNIEXPORT jint JNICALL Agent_OnLoad_allthr001(JavaVM *jvm, char *options, void *reserved) {
114
return Agent_Initialize(jvm, options, reserved);
115
}
116
JNIEXPORT jint JNICALL Agent_OnAttach_allthr001(JavaVM *jvm, char *options, void *reserved) {
117
return Agent_Initialize(jvm, options, reserved);
118
}
119
JNIEXPORT jint JNI_OnLoad_allthr001(JavaVM *jvm, char *options, void *reserved) {
120
return JNI_VERSION_1_8;
121
}
122
#endif
123
jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
124
jint res;
125
jvmtiError err;
126
127
if (options != NULL && strcmp(options, "printdump") == 0) {
128
printdump = JNI_TRUE;
129
}
130
131
res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);
132
if (res != JNI_OK || jvmti == NULL) {
133
printf("Wrong result of a valid call to GetEnv !\n");
134
return JNI_ERR;
135
}
136
137
err = jvmti->CreateRawMonitor("_lock1", &lock1);
138
if (err != JVMTI_ERROR_NONE) {
139
printf("Failed to create raw monitor 1, err = %d\n", err);
140
return JNI_ERR;
141
}
142
143
err = jvmti->CreateRawMonitor("_lock2", &lock2);
144
if (err != JVMTI_ERROR_NONE) {
145
printf("Failed to create raw monitor 2, err = %d\n", err);
146
return JNI_ERR;
147
}
148
149
return JNI_OK;
150
}
151
152
JNIEXPORT void checkInfo(JNIEnv *env, int ind) {
153
jint threadsCount = -1;
154
jthread *threads;
155
int i, j, found;
156
jvmtiError err;
157
int num_unexpected = 0;
158
159
if (printdump == JNI_TRUE) {
160
printf(" >>> Check: %d\n", ind);
161
}
162
163
if (ind == 4) {
164
err = jvmti->RawMonitorEnter(lock1);
165
if (err != JVMTI_ERROR_NONE) {
166
printf("Failed to enter raw monitor (check): %s (%d)\n",
167
TranslateError(err), err);
168
result = STATUS_FAILED;
169
}
170
err = jvmti->RunAgentThread(jthr(env), sys_thread, NULL,
171
JVMTI_THREAD_NORM_PRIORITY);
172
if (err != JVMTI_ERROR_NONE) {
173
printf("Failed to start agent thread: %s (%d)\n",
174
TranslateError(err), err);
175
result = STATUS_FAILED;
176
}
177
err = jvmti->RawMonitorWait(lock1, (jlong)0);
178
if (err != JVMTI_ERROR_NONE) {
179
printf("Failed to wait raw monitor (check): %s (%d)\n",
180
TranslateError(err), err);
181
result = STATUS_FAILED;
182
}
183
err = jvmti->RawMonitorExit(lock1);
184
if (err != JVMTI_ERROR_NONE) {
185
printf("Failed to exit raw monitor (check): %s (%d)\n",
186
TranslateError(err), err);
187
result = STATUS_FAILED;
188
}
189
}
190
191
err = jvmti->GetAllThreads(&threadsCount, &threads);
192
if (err != JVMTI_ERROR_NONE) {
193
printf("Failed to get all threads (check): %s (%d)\n",
194
TranslateError(err), err);
195
result = STATUS_FAILED;
196
return;
197
}
198
199
for (i = 0; i < threadsCount; i++) {
200
if (!isThreadExpected(jvmti, threads[i])) {
201
num_unexpected++;
202
}
203
}
204
205
if (threadsCount - num_unexpected != thrInfo[ind].cnt + sys_cnt) {
206
printf("Point %d: number of threads expected: %d, got: %d\n",
207
ind, thrInfo[ind].cnt + sys_cnt, threadsCount - num_unexpected);
208
result = STATUS_FAILED;
209
return;
210
}
211
212
for (i = 0; i < thrInfo[ind].cnt; i++) {
213
for (j = 0, found = 0; j < threadsCount && !found; j++) {
214
err = jvmti->GetThreadInfo(threads[j], &inf);
215
if (err != JVMTI_ERROR_NONE) {
216
printf("Failed to get thread info: %s (%d)\n",
217
TranslateError(err), err);
218
result = STATUS_FAILED;
219
return;
220
}
221
if (printdump == JNI_TRUE) {
222
printf(" >>> %s", inf.name);
223
}
224
found = (inf.name != NULL &&
225
strstr(inf.name, thrInfo[ind].thrNames[i]) == inf.name &&
226
(ind == 4 || strlen(inf.name) ==
227
strlen(thrInfo[ind].thrNames[i])));
228
}
229
if (printdump == JNI_TRUE) {
230
printf("\n");
231
}
232
if (!found) {
233
printf("Point %d: thread %s not detected\n",
234
ind, thrInfo[ind].thrNames[i]);
235
result = STATUS_FAILED;
236
}
237
}
238
239
err = jvmti->Deallocate((unsigned char *)threads);
240
if (err != JVMTI_ERROR_NONE) {
241
printf("Failed to deallocate array: %s (%d)\n",
242
TranslateError(err), err);
243
result = STATUS_FAILED;
244
}
245
246
if (ind == 4) {
247
err = jvmti->RawMonitorEnter(lock2);
248
if (err != JVMTI_ERROR_NONE) {
249
printf("Failed to enter raw monitor (check): %s (%d)\n",
250
TranslateError(err), err);
251
result = STATUS_FAILED;
252
}
253
err = jvmti->RawMonitorNotify(lock2);
254
if (err != JVMTI_ERROR_NONE) {
255
printf("Failed to notify raw monitor (check): %s (%d)\n",
256
TranslateError(err), err);
257
result = STATUS_FAILED;
258
}
259
err = jvmti->RawMonitorExit(lock2);
260
if (err != JVMTI_ERROR_NONE) {
261
printf("Failed to exit raw monitor (check): %s (%d)\n",
262
TranslateError(err), err);
263
result = STATUS_FAILED;
264
}
265
}
266
}
267
268
JNIEXPORT void JNICALL Java_nsk_jvmti_GetAllThreads_allthr001_setSysCnt(JNIEnv *env, jclass cls) {
269
jint threadsCount = -1;
270
jthread *threads;
271
jvmtiError err;
272
int i;
273
274
err = jvmti->GetAllThreads(&threadsCount, &threads);
275
if (err != JVMTI_ERROR_NONE) {
276
printf("Failed to get all threads (count): %s (%d)\n",
277
TranslateError(err), err);
278
result = STATUS_FAILED;
279
return;
280
}
281
282
sys_cnt = threadsCount - 1;
283
284
for (i = 0; i < threadsCount; i++) {
285
if (!isThreadExpected(jvmti, threads[i])) {
286
sys_cnt--;
287
}
288
}
289
290
if (printdump == JNI_TRUE) {
291
printf(" >>> number of system threads: %d\n", sys_cnt);
292
}
293
}
294
295
JNIEXPORT void JNICALL
296
Java_nsk_jvmti_GetAllThreads_allthr001_checkInfo(JNIEnv *env, jclass cls, jint ind) {
297
checkInfo(env, ind);
298
}
299
300
JNIEXPORT jint JNICALL Java_nsk_jvmti_GetAllThreads_allthr001_getRes(JNIEnv *env, jclass cls) {
301
return result;
302
}
303
304
}
305
306