File: thrstat001.c

package info (click to toggle)
openjdk-11-jre-dcevm 11.0.12%2B7-1~deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 221,040 kB
  • sloc: java: 1,360,115; cpp: 832,709; ansic: 209,376; xml: 62,473; sh: 8,122; asm: 3,279; python: 1,661; javascript: 942; makefile: 382; sed: 172; perl: 114
file content (442 lines) | stat: -rw-r--r-- 14,944 bytes parent folder | download | duplicates (4)
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*
 * Copyright (c) 2004, 2018, 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 <string.h>
#include "jvmti.h"
#include "agent_common.h"
#include "JVMTITools.h"

#ifdef __cplusplus
extern "C" {
#endif

#ifndef JNI_ENV_ARG

#ifdef __cplusplus
#define JNI_ENV_ARG(x, y) y
#define JNI_ENV_PTR(x) x
#else
#define JNI_ENV_ARG(x,y) x, y
#define JNI_ENV_PTR(x) (*x)
#endif

#endif

#define PASSED  0
#define STATUS_FAILED  2
#define WAIT_START 100
#define WAIT_TIME (2*60*1000)

static jvmtiEnv *jvmti = NULL;
static jvmtiCapabilities caps;
static jvmtiEventCallbacks callbacks;
static jrawMonitorID access_lock;
static jint result = PASSED;
static jboolean printdump = JNI_FALSE;
static jthread thr_ptr = NULL;
static jint state[] = {
    JVMTI_THREAD_STATE_RUNNABLE,
    JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER,
    JVMTI_THREAD_STATE_IN_OBJECT_WAIT
};

static int entry_count = 0;
static int entry_error_count = 0;
static int exit_count = 0;
static int exit_error_count = 0;

void JNICALL VMInit(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thr) {
    jvmtiError err;

    err = (*jvmti_env)->SetEventNotificationMode(jvmti_env, JVMTI_ENABLE,
        JVMTI_EVENT_THREAD_START, NULL);
    if (err != JVMTI_ERROR_NONE) {
        printf("Failed to enable THREAD_START event: %s (%d)\n",
               TranslateError(err), err);
        result = STATUS_FAILED;
    }

    if (caps.can_generate_method_entry_events) {
        err = (*jvmti_env)->SetEventNotificationMode(jvmti_env, JVMTI_ENABLE,
            JVMTI_EVENT_METHOD_ENTRY, NULL);
        if (err != JVMTI_ERROR_NONE) {
            printf("Failed to enable METHOD_ENTRY event: %s (%d)\n",
                   TranslateError(err), err);
            result = STATUS_FAILED;
        }
    }

    if (caps.can_generate_method_exit_events) {
        err = (*jvmti_env)->SetEventNotificationMode(jvmti_env, JVMTI_ENABLE,
            JVMTI_EVENT_METHOD_EXIT, NULL);
        if (err != JVMTI_ERROR_NONE) {
            printf("Failed to enable METHOD_EXIT event: %s (%d)\n",
                   TranslateError(err), err);
            result = STATUS_FAILED;
        }
    }
}

void JNICALL
ThreadStart(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thread) {
    jvmtiError err;
    jvmtiThreadInfo thrInfo;

    err = (*jvmti_env)->RawMonitorEnter(jvmti_env, access_lock);
    if (err != JVMTI_ERROR_NONE) {
        printf("(RawMonitorEnter#TS) unexpected error: %s (%d)\n",
               TranslateError(err), err);
        result = STATUS_FAILED;
    }

    err = (*jvmti_env)->GetThreadInfo(jvmti_env, thread, &thrInfo);
    if (err != JVMTI_ERROR_NONE) {
        printf("(GetThreadInfo#TS) unexpected error: %s (%d)\n",
               TranslateError(err), err);
        result = STATUS_FAILED;
    }
    if (thrInfo.name != NULL && strcmp(thrInfo.name, "thr1") == 0) {
        thr_ptr = JNI_ENV_PTR(env)->NewGlobalRef(JNI_ENV_ARG((JNIEnv *)env, thread));
        if (printdump == JNI_TRUE) {
            printf(">>> ThreadStart: \"%s\", 0x%p\n", thrInfo.name, thr_ptr);
        }
    }

    err = (*jvmti_env)->RawMonitorExit(jvmti_env, access_lock);
    if (err != JVMTI_ERROR_NONE) {
        printf("(RawMonitorExit#TS) unexpected error: %s (%d)\n",
               TranslateError(err), err);
        result = STATUS_FAILED;
    }
}

void JNICALL MethodEntry(jvmtiEnv *jvmti_env, JNIEnv *env,
        jthread thread, jmethodID mid) {
    jvmtiError err;
    jvmtiThreadInfo thrInfo;
    jint thrState;

    err = (*jvmti_env)->RawMonitorEnter(jvmti_env, access_lock);
    if (err != JVMTI_ERROR_NONE) {
        printf("(RawMonitorEnter#ME) unexpected error: %s (%d)\n",
               TranslateError(err), err);
        result = STATUS_FAILED;
    }

    entry_count++;
    err = (*jvmti_env)->GetThreadState(jvmti_env, thread, &thrState);
    if (err != JVMTI_ERROR_NONE) {
        printf("(GetThreadState#ME) unexpected error: %s (%d)\n",
            TranslateError(err), err);
        result = STATUS_FAILED;
    }
    if ((thrState & JVMTI_THREAD_STATE_RUNNABLE) == 0) {
        if (entry_error_count == 0) {
            err = (*jvmti_env)->GetThreadInfo(jvmti_env, thread, &thrInfo);
            if (err != JVMTI_ERROR_NONE) {
                printf("(GetThreadInfo#ME) unexpected error: %s (%d)\n",
                       TranslateError(err), err);
                result = STATUS_FAILED;
            }
            printf("Wrong thread \"%s\" state on MethodEntry event:\n",
                   thrInfo.name);
            printf("    expected: JVMTI_THREAD_STATE_RUNNABLE\n");
            printf("    got: %s (%d)\n",
                   TranslateState(thrState), thrState);
        }
        entry_error_count++;
        result = STATUS_FAILED;
    }

    err = (*jvmti_env)->RawMonitorExit(jvmti_env, access_lock);
    if (err != JVMTI_ERROR_NONE) {
        printf("(RawMonitorExit#ME) unexpected error: %s (%d)\n",
               TranslateError(err), err);
        result = STATUS_FAILED;
    }

}

void JNICALL MethodExit(jvmtiEnv *jvmti_env, JNIEnv *env,
        jthread thread, jmethodID mid,
        jboolean was_poped_by_exception, jvalue return_value) {
    jvmtiError err;
    jvmtiThreadInfo thrInfo;
    jint thrState;

    err = (*jvmti_env)->RawMonitorEnter(jvmti_env, access_lock);
    if (err != JVMTI_ERROR_NONE) {
        printf("(RawMonitorEnter#MX) unexpected error: %s (%d)\n",
               TranslateError(err), err);
        result = STATUS_FAILED;
    }

    exit_count++;
    err = (*jvmti_env)->GetThreadState(jvmti_env, thread, &thrState);
    if (err != JVMTI_ERROR_NONE) {
        printf("(GetThreadState#MX) unexpected error: %s (%d)\n",
            TranslateError(err), err);
        result = STATUS_FAILED;
    }
    if ((thrState & JVMTI_THREAD_STATE_RUNNABLE) == 0) {
        if (exit_error_count == 0) {
            err = (*jvmti_env)->GetThreadInfo(jvmti_env, thread, &thrInfo);
            if (err != JVMTI_ERROR_NONE) {
                printf("(GetThreadInfo#MX) unexpected error: %s (%d)\n",
                       TranslateError(err), err);
                result = STATUS_FAILED;
            }
            printf("Wrong thread \"%s\" state on MethodExit event:\n",
                   thrInfo.name);
            printf("    expected: JVMTI_THREAD_STATE_RUNNABLE\n");
            printf("    got: %s (%d)\n",
                   TranslateState(thrState), thrState);
        }
        exit_error_count++;
        result = STATUS_FAILED;
    }

    err = (*jvmti_env)->RawMonitorExit(jvmti_env, access_lock);
    if (err != JVMTI_ERROR_NONE) {
        printf("(RawMonitorExit#MX) unexpected error: %s (%d)\n",
               TranslateError(err), err);
        result = STATUS_FAILED;
    }
}

#ifdef STATIC_BUILD
JNIEXPORT jint JNICALL Agent_OnLoad_thrstat001(JavaVM *jvm, char *options, void *reserved) {
    return Agent_Initialize(jvm, options, reserved);
}
JNIEXPORT jint JNICALL Agent_OnAttach_thrstat001(JavaVM *jvm, char *options, void *reserved) {
    return Agent_Initialize(jvm, options, reserved);
}
JNIEXPORT jint JNI_OnLoad_thrstat001(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 = JNI_ENV_PTR(jvm)->
        GetEnv(JNI_ENV_ARG(jvm, (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)->GetPotentialCapabilities(jvmti, &caps);
    if (err != JVMTI_ERROR_NONE) {
        printf("(GetPotentialCapabilities) unexpected error: %s (%d)\n",
               TranslateError(err), err);
        return JNI_ERR;
    }

    err = (*jvmti)->AddCapabilities(jvmti, &caps);
    if (err != JVMTI_ERROR_NONE) {
        printf("(AddCapabilities) unexpected error: %s (%d)\n",
               TranslateError(err), err);
        return JNI_ERR;
    }

    err = (*jvmti)->GetCapabilities(jvmti, &caps);
    if (err != JVMTI_ERROR_NONE) {
        printf("(GetCapabilities) unexpected error: %s (%d)\n",
               TranslateError(err), err);
        return JNI_ERR;
    }

    err = (*jvmti)->CreateRawMonitor(jvmti, "_access_lock", &access_lock);
    if (err != JVMTI_ERROR_NONE) {
        printf("(CreateRawMonitor) unexpected error: %s (%d)\n",
               TranslateError(err), err);
        return JNI_ERR;
    }

    callbacks.VMInit = &VMInit;
    callbacks.ThreadStart = &ThreadStart;
    if (caps.can_generate_method_entry_events) {
        callbacks.MethodEntry = &MethodEntry;
    } else {
        printf("Warning: MethodEntry event is not implemented\n");
    }
    if (caps.can_generate_method_exit_events) {
        callbacks.MethodExit = &MethodExit;
    } else {
        printf("Warning: MethodExit event is not implemented\n");
    }
    err = (*jvmti)->SetEventCallbacks(jvmti, &callbacks, sizeof(callbacks));
    if (err != JVMTI_ERROR_NONE) {
        printf("(SetEventCallbacks) unexpected error: %s (%d)\n",
               TranslateError(err), err);
        return JNI_ERR;
    }

    err = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
        JVMTI_EVENT_VM_INIT, NULL);
    if (err != JVMTI_ERROR_NONE) {
        printf("Failed to enable VM_INIT event: %s (%d)\n",
               TranslateError(err), err);
        result = STATUS_FAILED;
    }

    return JNI_OK;
}

JNIEXPORT void JNICALL
Java_nsk_jvmti_GetThreadState_thrstat001_checkStatus(JNIEnv *env,
        jclass cls, jint statInd) {
    jvmtiError err;
    jrawMonitorID wait_lock;
    jint thrState;
    jint millis;

    if (jvmti == NULL) {
        printf("JVMTI client was not properly loaded!\n");
        result = STATUS_FAILED;
        return;
    }

    if (thr_ptr == NULL) {
        printf("Missing thread \"thr1\" start event\n");
        result = STATUS_FAILED;
        return;
    }

    /* wait until thread gets an expected state */
    err = (*jvmti)->CreateRawMonitor(jvmti, "_wait_lock", &wait_lock);
    if (err != JVMTI_ERROR_NONE) {
        printf("(CreateRawMonitor) unexpected error: %s (%d)\n",
               TranslateError(err), err);
        result = STATUS_FAILED;
    }
    for (millis = WAIT_START; millis < WAIT_TIME; millis <<= 1) {
        err = (*jvmti)->GetThreadState(jvmti, thr_ptr, &thrState);
        if (err != JVMTI_ERROR_NONE) {
            printf("(GetThreadState#%d) unexpected error: %s (%d)\n",
                statInd, TranslateError(err), err);
            result = STATUS_FAILED;
        }
        if ((thrState & state[statInd]) != 0) {
            break;
        }
        err = (*jvmti)->RawMonitorEnter(jvmti, wait_lock);
        if (err != JVMTI_ERROR_NONE) {
            printf("(RawMonitorEnter) unexpected error: %s (%d)\n",
                   TranslateError(err), err);
            result = STATUS_FAILED;
        }
        err = (*jvmti)->RawMonitorWait(jvmti, wait_lock, (jlong)millis);
        if (err != JVMTI_ERROR_NONE) {
            printf("(RawMonitorWait) unexpected error: %s (%d)\n",
                   TranslateError(err), err);
            result = STATUS_FAILED;
        }
        err = (*jvmti)->RawMonitorExit(jvmti, wait_lock);
        if (err != JVMTI_ERROR_NONE) {
            printf("(RawMonitorExit) unexpected error: %s (%d)\n",
                   TranslateError(err), err);
            result = STATUS_FAILED;
        }
    }
    err = (*jvmti)->DestroyRawMonitor(jvmti, wait_lock);
    if (err != JVMTI_ERROR_NONE) {
        printf("(DestroyRawMonitor) unexpected error: %s (%d)\n",
               TranslateError(err), err);
        result = STATUS_FAILED;
    }

    if (printdump == JNI_TRUE) {
        printf(">>> thread \"thr1\" (0x%p) state: %s (%d)\n",
            thr_ptr, TranslateState(thrState), thrState);
    }

    if ((thrState & state[statInd]) == 0) {
        printf("Wrong thread \"thr1\" (0x%p) state:\n", thr_ptr);
        printf("    expected: %s (%d)\n",
            TranslateState(state[statInd]), state[statInd]);
        printf("      actual: %s (%d)\n",
            TranslateState(thrState), thrState);
        result = STATUS_FAILED;
    }
}

JNIEXPORT jint JNICALL
Java_nsk_jvmti_GetThreadState_thrstat001_getRes(JNIEnv *env, jclass cls) {
    jvmtiError err;

    err = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_DISABLE,
        JVMTI_EVENT_THREAD_START, NULL);
    if (err != JVMTI_ERROR_NONE) {
        printf("Failed to disable THREAD_START event: %s (%d)\n",
               TranslateError(err), err);
        result = STATUS_FAILED;
    }

    if (caps.can_generate_method_entry_events) {
        err = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_DISABLE,
            JVMTI_EVENT_METHOD_ENTRY, NULL);
        if (err != JVMTI_ERROR_NONE) {
            printf("Failed to disable METHOD_ENTRY event: %s (%d)\n",
                   TranslateError(err), err);
            result = STATUS_FAILED;
        }
    }

    if (caps.can_generate_method_exit_events) {
        err = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_DISABLE,
            JVMTI_EVENT_METHOD_EXIT, NULL);
        if (err != JVMTI_ERROR_NONE) {
            printf("Failed to disable METHOD_EXIT event: %s (%d)\n",
                   TranslateError(err), err);
            result = STATUS_FAILED;
        }
    }

    if (printdump == JNI_TRUE) {
        printf(">>> total number of method entry events = %d\n", entry_count);
        printf(">>> total number of method exit events = %d\n", exit_count);
    }

    if (entry_error_count != 0) {
        printf("Total number of errors on METHOD_ENTRY: %d of %d events\n",
               entry_error_count, entry_count);
    }

    if (exit_error_count != 0) {
        printf("Total number of errors on METHOD_EXIT: %d of %d events\n",
               exit_error_count, exit_count);
    }

    return result;
}

#ifdef __cplusplus
}
#endif