test/runtime/8233197/libJvmtiAgent.c

Fri, 12 Jun 2020 02:59:56 +0100

author
jbachorik
date
Fri, 12 Jun 2020 02:59:56 +0100
changeset 9925
30fb8c8cceb9
permissions
-rw-r--r--

8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
8246703: [TESTBUG] Add test for JDK-8233197
Reviewed-by: aph, adinn, neugens

     1 /*
     2  * Copyright (c) 2017, 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  */
    24 #include <stdio.h>
    25 #include <stdlib.h>
    26 #include <string.h>
    27 #include "jvmti.h"
    29 #ifdef __cplusplus
    30 extern "C" {
    31 #endif
    33 #ifndef JNI_ENV_ARG
    35 #ifdef __cplusplus
    36 #define JNI_ENV_ARG(x, y) y
    37 #define JNI_ENV_PTR(x) x
    38 #else
    39 #define JNI_ENV_ARG(x,y) x, y
    40 #define JNI_ENV_PTR(x) (*x)
    41 #endif
    43 #endif
    45 #define TranslateError(err) "JVMTI error"
    47 static jvmtiEnv *jvmti = NULL;
    49 static jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved);
    51 JNIEXPORT
    52 jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
    53     return Agent_Initialize(jvm, options, reserved);
    54 }
    56 JNIEXPORT
    57 jint JNICALL Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) {
    58     return Agent_Initialize(jvm, options, reserved);
    59 }
    61 JNIEXPORT
    62 jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) {
    63     return JNI_VERSION_1_8;
    64 }
    66 static void JNICALL
    67 Callback_VMStart(jvmtiEnv *jvmti_env, JNIEnv *env) {
    68     printf("Localizing jdk.jfr.FlightRecorder\n");
    69     // without fix for 8233197 the process will crash at the following line
    70     jclass cls = (*env)->FindClass(env, "jdk/jfr/FlightRecorder");
    71     jmethodID mid = (*env)->GetStaticMethodID(env, cls, "getFlightRecorder", "()Ljdk/jfr/FlightRecorder;");
    72     if (mid == 0) {
    73         printf("Unable to localize jdk.jfr.FlightRecorder#getFlightRecorder() method\n");
    74         // crash the tested JVM to make the test fail
    75         exit(-1);
    76     }
    77     printf("Going to initialize JFR subsystem ...\n");
    78     jobject jfr = (*env)->CallStaticObjectMethod(env, cls, mid);
    80     if (!(*env)->ExceptionCheck(env)) {
    81         // crash the tested JVM to make the test fail
    82         printf("JFR subsystem is wrongly initialized too early\n");
    83         exit(-2);
    84     }
    85     // exit VM
    86     exit(0);
    87 }
    89 static
    90 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
    91     jint res, size;
    92     jvmtiCapabilities caps;
    93     jvmtiEventCallbacks callbacks;
    94     jvmtiError err;
    96     res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti),
    97         JVMTI_VERSION_1_2);
    98     if (res != JNI_OK || jvmti == NULL) {
    99         printf("    Error: wrong result of a valid call to GetEnv!\n");
   100         return JNI_ERR;
   101     }
   103     size = (jint)sizeof(callbacks);
   105     memset(&callbacks, 0, sizeof(callbacks));
   106     callbacks.VMStart = Callback_VMStart;
   108     err = (*jvmti)->SetEventCallbacks(jvmti, &callbacks, size);
   109     if (err != JVMTI_ERROR_NONE) {
   110         printf("    Error in SetEventCallbacks: %s (%d)\n", TranslateError(err), err);
   111         return JNI_ERR;
   112     }
   114     err = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE, JVMTI_EVENT_VM_START, (jthread)NULL);
   115     if (err != JVMTI_ERROR_NONE) {
   116         printf("    Error in SetEventNotificationMode: %s (%d)\n", TranslateError(err), err);
   117         return JNI_ERR;
   118     }
   119     return JNI_OK;
   120 }
   122 #ifdef __cplusplus
   123 }
   124 #endif

mercurial