test/runtime/8233197/libJvmtiAgent.c

changeset 9925
30fb8c8cceb9
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/runtime/8233197/libJvmtiAgent.c	Fri Jun 12 02:59:56 2020 +0100
     1.3 @@ -0,0 +1,124 @@
     1.4 +/*
     1.5 + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +#include <stdio.h>
    1.28 +#include <stdlib.h>
    1.29 +#include <string.h>
    1.30 +#include "jvmti.h"
    1.31 +
    1.32 +#ifdef __cplusplus
    1.33 +extern "C" {
    1.34 +#endif
    1.35 +
    1.36 +#ifndef JNI_ENV_ARG
    1.37 +
    1.38 +#ifdef __cplusplus
    1.39 +#define JNI_ENV_ARG(x, y) y
    1.40 +#define JNI_ENV_PTR(x) x
    1.41 +#else
    1.42 +#define JNI_ENV_ARG(x,y) x, y
    1.43 +#define JNI_ENV_PTR(x) (*x)
    1.44 +#endif
    1.45 +
    1.46 +#endif
    1.47 +
    1.48 +#define TranslateError(err) "JVMTI error"
    1.49 +
    1.50 +static jvmtiEnv *jvmti = NULL;
    1.51 +
    1.52 +static jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved);
    1.53 +
    1.54 +JNIEXPORT
    1.55 +jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
    1.56 +    return Agent_Initialize(jvm, options, reserved);
    1.57 +}
    1.58 +
    1.59 +JNIEXPORT
    1.60 +jint JNICALL Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) {
    1.61 +    return Agent_Initialize(jvm, options, reserved);
    1.62 +}
    1.63 +
    1.64 +JNIEXPORT
    1.65 +jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) {
    1.66 +    return JNI_VERSION_1_8;
    1.67 +}
    1.68 +
    1.69 +static void JNICALL
    1.70 +Callback_VMStart(jvmtiEnv *jvmti_env, JNIEnv *env) {
    1.71 +    printf("Localizing jdk.jfr.FlightRecorder\n");
    1.72 +    // without fix for 8233197 the process will crash at the following line
    1.73 +    jclass cls = (*env)->FindClass(env, "jdk/jfr/FlightRecorder");
    1.74 +    jmethodID mid = (*env)->GetStaticMethodID(env, cls, "getFlightRecorder", "()Ljdk/jfr/FlightRecorder;");
    1.75 +    if (mid == 0) {
    1.76 +        printf("Unable to localize jdk.jfr.FlightRecorder#getFlightRecorder() method\n");
    1.77 +        // crash the tested JVM to make the test fail
    1.78 +        exit(-1);
    1.79 +    }
    1.80 +    printf("Going to initialize JFR subsystem ...\n");
    1.81 +    jobject jfr = (*env)->CallStaticObjectMethod(env, cls, mid);
    1.82 +
    1.83 +    if (!(*env)->ExceptionCheck(env)) {
    1.84 +        // crash the tested JVM to make the test fail
    1.85 +        printf("JFR subsystem is wrongly initialized too early\n");
    1.86 +        exit(-2);
    1.87 +    }
    1.88 +    // exit VM
    1.89 +    exit(0);
    1.90 +}
    1.91 +
    1.92 +static
    1.93 +jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
    1.94 +    jint res, size;
    1.95 +    jvmtiCapabilities caps;
    1.96 +    jvmtiEventCallbacks callbacks;
    1.97 +    jvmtiError err;
    1.98 +
    1.99 +    res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti),
   1.100 +        JVMTI_VERSION_1_2);
   1.101 +    if (res != JNI_OK || jvmti == NULL) {
   1.102 +        printf("    Error: wrong result of a valid call to GetEnv!\n");
   1.103 +        return JNI_ERR;
   1.104 +    }
   1.105 +
   1.106 +    size = (jint)sizeof(callbacks);
   1.107 +
   1.108 +    memset(&callbacks, 0, sizeof(callbacks));
   1.109 +    callbacks.VMStart = Callback_VMStart;
   1.110 +
   1.111 +    err = (*jvmti)->SetEventCallbacks(jvmti, &callbacks, size);
   1.112 +    if (err != JVMTI_ERROR_NONE) {
   1.113 +        printf("    Error in SetEventCallbacks: %s (%d)\n", TranslateError(err), err);
   1.114 +        return JNI_ERR;
   1.115 +    }
   1.116 +
   1.117 +    err = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE, JVMTI_EVENT_VM_START, (jthread)NULL);
   1.118 +    if (err != JVMTI_ERROR_NONE) {
   1.119 +        printf("    Error in SetEventNotificationMode: %s (%d)\n", TranslateError(err), err);
   1.120 +        return JNI_ERR;
   1.121 +    }
   1.122 +    return JNI_OK;
   1.123 +}
   1.124 +
   1.125 +#ifdef __cplusplus
   1.126 +}
   1.127 +#endif

mercurial