/
allthr001.cpp
312 lines (279 loc) · 10.1 KB
/
allthr001.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "jvmti.h"
#include "agent_common.h"
#include "JVMTITools.h"
extern "C" {
#define PASSED 0
#define STATUS_FAILED 2
typedef struct {
int cnt;
const char **thrNames;
} info;
typedef struct {
info expected;
info unexpected;
} threadInfo;
static jvmtiEnv *jvmti;
static jrawMonitorID lock1;
static jrawMonitorID lock2;
static jboolean printdump = JNI_FALSE;
static jint result = PASSED;
static const char main_name[] = "main";
static const char thread1_name[] = "thread1";
static const char sys_thread_name[] = "SysThread";
static const char *main_only[] = { main_name };
static const char *thr1_only[] = { thread1_name };
static const char *sys_only[] = { sys_thread_name };
static const char *main_thr1[] = { main_name, thread1_name };
static const char *main_sys[] = { main_name, sys_thread_name };
static const char *thr1_sys[] = { thread1_name, sys_thread_name };
static threadInfo thrInfo[] = {
{{1, main_only}, {2, thr1_sys}},
{{1, main_only}, {2, thr1_sys}},
{{2, main_thr1}, {1, sys_only}},
{{1, main_only}, {2, thr1_sys}},
{{2, main_sys}, {1, thr1_only}}
};
jthread jthr(JNIEnv *env) {
jclass thrClass;
jmethodID cid;
jthread res;
thrClass = env->FindClass("java/lang/Thread");
cid = env->GetMethodID(thrClass, "<init>", "(Ljava/lang/String;)V");
jstring thread_name = env->NewStringUTF(sys_thread_name);
res = env->NewObject(thrClass, cid, thread_name);
env->DeleteLocalRef(thread_name);
return res;
}
static void JNICALL
sys_thread(jvmtiEnv* jvmti, JNIEnv* jni, void *p) {
jvmtiError err;
err = jvmti->RawMonitorEnter(lock2);
if (err != JVMTI_ERROR_NONE) {
printf("Failed to enter raw monitor 2 (thread): %s (%d)\n",
TranslateError(err), err);
result = STATUS_FAILED;
}
/* allows the main thread to wait until the child thread is running */
err = jvmti->RawMonitorEnter(lock1);
if (err != JVMTI_ERROR_NONE) {
printf("Failed to enter raw monitor 1 (thread): %s (%d)\n",
TranslateError(err), err);
result = STATUS_FAILED;
}
err = jvmti->RawMonitorNotify(lock1);
if (err != JVMTI_ERROR_NONE) {
printf("Failed to notify raw monitor (thread): %s (%d)\n",
TranslateError(err), err);
result = STATUS_FAILED;
}
err = jvmti->RawMonitorExit(lock1);
if (err != JVMTI_ERROR_NONE) {
printf("Failed to exit raw monitor 1 (thread): %s (%d)\n",
TranslateError(err), err);
result = STATUS_FAILED;
}
/* keeps the child thread from exiting */
err = jvmti->RawMonitorWait(lock2, (jlong)0);
if (err != JVMTI_ERROR_NONE) {
printf("Failed to wait raw monitor (thread): %s (%d)\n",
TranslateError(err), err);
result = STATUS_FAILED;
}
err = jvmti->RawMonitorExit(lock2);
if (err != JVMTI_ERROR_NONE) {
printf("Failed to exit raw monitor 2 (thread): %s (%d)\n",
TranslateError(err), err);
result = STATUS_FAILED;
}
}
#ifdef STATIC_BUILD
JNIEXPORT jint JNICALL Agent_OnLoad_allthr001(JavaVM *jvm, char *options, void *reserved) {
return Agent_Initialize(jvm, options, reserved);
}
JNIEXPORT jint JNICALL Agent_OnAttach_allthr001(JavaVM *jvm, char *options, void *reserved) {
return Agent_Initialize(jvm, options, reserved);
}
JNIEXPORT jint JNI_OnLoad_allthr001(JavaVM *jvm, char *options, void *reserved) {
return JNI_VERSION_1_8;
}
#endif
jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
jint res;
jvmtiError err;
if (options != NULL && strcmp(options, "printdump") == 0) {
printdump = JNI_TRUE;
}
res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);
if (res != JNI_OK || jvmti == NULL) {
printf("Wrong result of a valid call to GetEnv !\n");
return JNI_ERR;
}
err = jvmti->CreateRawMonitor("_lock1", &lock1);
if (err != JVMTI_ERROR_NONE) {
printf("Failed to create raw monitor 1, err = %d\n", err);
return JNI_ERR;
}
err = jvmti->CreateRawMonitor("_lock2", &lock2);
if (err != JVMTI_ERROR_NONE) {
printf("Failed to create raw monitor 2, err = %d\n", err);
return JNI_ERR;
}
return JNI_OK;
}
void releaseThreadInfo(JNIEnv *env, jvmtiThreadInfo *info) {
jvmti->Deallocate((unsigned char *)info->name);
if (info->thread_group != NULL) {
env->DeleteLocalRef(info->thread_group);
}
if (info->context_class_loader != NULL) {
env->DeleteLocalRef(info->context_class_loader);
}
}
JNIEXPORT void checkInfo(JNIEnv *env, int ind) {
jint threadsCount = -1;
jthread *threads;
jvmtiError err;
int expected = 0;
jvmtiThreadInfo inf;
if (printdump == JNI_TRUE) {
printf(" >>> Check: %d\n", ind);
}
if (ind == 4) {
err = jvmti->RawMonitorEnter(lock1);
if (err != JVMTI_ERROR_NONE) {
printf("Failed to enter raw monitor (check): %s (%d)\n",
TranslateError(err), err);
result = STATUS_FAILED;
}
err = jvmti->RunAgentThread(jthr(env), sys_thread, NULL,
JVMTI_THREAD_NORM_PRIORITY);
if (err != JVMTI_ERROR_NONE) {
printf("Failed to start agent thread: %s (%d)\n",
TranslateError(err), err);
result = STATUS_FAILED;
}
err = jvmti->RawMonitorWait(lock1, (jlong)0);
if (err != JVMTI_ERROR_NONE) {
printf("Failed to wait raw monitor (check): %s (%d)\n",
TranslateError(err), err);
result = STATUS_FAILED;
}
err = jvmti->RawMonitorExit(lock1);
if (err != JVMTI_ERROR_NONE) {
printf("Failed to exit raw monitor (check): %s (%d)\n",
TranslateError(err), err);
result = STATUS_FAILED;
}
}
err = jvmti->GetAllThreads(&threadsCount, &threads);
if (err != JVMTI_ERROR_NONE) {
printf("Failed to get all threads (check): %s (%d)\n",
TranslateError(err), err);
result = STATUS_FAILED;
return;
}
// check unexpected threads
for (int i = 0; i < threadsCount; i++) {
err = jvmti->GetThreadInfo(threads[i], &inf);
if (err != JVMTI_ERROR_NONE) {
printf("Failed to get thread info: %s (%d)\n",
TranslateError(err), err);
result = STATUS_FAILED;
return;
}
if (printdump == JNI_TRUE) {
printf(" >>> %s", inf.name);
}
bool found = false;
for (int j = 0; j < thrInfo[ind].unexpected.cnt && !found; j++) {
found = (inf.name != NULL && strcmp(inf.name, thrInfo[ind].unexpected.thrNames[j]) == 0);
}
if (found) {
printf("Point %d: detected unexpected thread %s\n", ind, inf.name);
result = STATUS_FAILED;
}
releaseThreadInfo(env, &inf);
}
if (printdump == JNI_TRUE) {
printf("\n");
}
// verify all expected threads are present
for (int i = 0; i < thrInfo[ind].expected.cnt; i++) {
bool found = false;
for (int j = 0; j < threadsCount && !found; j++) {
err = jvmti->GetThreadInfo(threads[j], &inf);
if (err != JVMTI_ERROR_NONE) {
printf("Failed to get thread info: %s (%d)\n",
TranslateError(err), err);
result = STATUS_FAILED;
return;
}
found = (inf.name != NULL && strcmp(inf.name, thrInfo[ind].expected.thrNames[j]) == 0);
releaseThreadInfo(env, &inf);
}
if (!found) {
printf("Point %d: thread %s not detected\n",
ind, thrInfo[ind].expected.thrNames[i]);
result = STATUS_FAILED;
}
}
err = jvmti->Deallocate((unsigned char *)threads);
if (err != JVMTI_ERROR_NONE) {
printf("Failed to deallocate array: %s (%d)\n",
TranslateError(err), err);
result = STATUS_FAILED;
}
if (ind == 4) {
err = jvmti->RawMonitorEnter(lock2);
if (err != JVMTI_ERROR_NONE) {
printf("Failed to enter raw monitor (check): %s (%d)\n",
TranslateError(err), err);
result = STATUS_FAILED;
}
err = jvmti->RawMonitorNotify(lock2);
if (err != JVMTI_ERROR_NONE) {
printf("Failed to notify raw monitor (check): %s (%d)\n",
TranslateError(err), err);
result = STATUS_FAILED;
}
err = jvmti->RawMonitorExit(lock2);
if (err != JVMTI_ERROR_NONE) {
printf("Failed to exit raw monitor (check): %s (%d)\n",
TranslateError(err), err);
result = STATUS_FAILED;
}
}
}
JNIEXPORT void JNICALL
Java_nsk_jvmti_GetAllThreads_allthr001_checkInfo(JNIEnv *env, jclass cls, jint ind) {
checkInfo(env, ind);
}
JNIEXPORT jint JNICALL Java_nsk_jvmti_GetAllThreads_allthr001_getRes(JNIEnv *env, jclass cls) {
return result;
}
}