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

jbachorik@9925 1 /*
jbachorik@9925 2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
jbachorik@9925 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jbachorik@9925 4 *
jbachorik@9925 5 * This code is free software; you can redistribute it and/or modify it
jbachorik@9925 6 * under the terms of the GNU General Public License version 2 only, as
jbachorik@9925 7 * published by the Free Software Foundation.
jbachorik@9925 8 *
jbachorik@9925 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jbachorik@9925 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jbachorik@9925 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jbachorik@9925 12 * version 2 for more details (a copy is included in the LICENSE file that
jbachorik@9925 13 * accompanied this code).
jbachorik@9925 14 *
jbachorik@9925 15 * You should have received a copy of the GNU General Public License version
jbachorik@9925 16 * 2 along with this work; if not, write to the Free Software Foundation,
jbachorik@9925 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jbachorik@9925 18 *
jbachorik@9925 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jbachorik@9925 20 * or visit www.oracle.com if you need additional information or have any
jbachorik@9925 21 * questions.
jbachorik@9925 22 */
jbachorik@9925 23
jbachorik@9925 24 #include <stdio.h>
jbachorik@9925 25 #include <stdlib.h>
jbachorik@9925 26 #include <string.h>
jbachorik@9925 27 #include "jvmti.h"
jbachorik@9925 28
jbachorik@9925 29 #ifdef __cplusplus
jbachorik@9925 30 extern "C" {
jbachorik@9925 31 #endif
jbachorik@9925 32
jbachorik@9925 33 #ifndef JNI_ENV_ARG
jbachorik@9925 34
jbachorik@9925 35 #ifdef __cplusplus
jbachorik@9925 36 #define JNI_ENV_ARG(x, y) y
jbachorik@9925 37 #define JNI_ENV_PTR(x) x
jbachorik@9925 38 #else
jbachorik@9925 39 #define JNI_ENV_ARG(x,y) x, y
jbachorik@9925 40 #define JNI_ENV_PTR(x) (*x)
jbachorik@9925 41 #endif
jbachorik@9925 42
jbachorik@9925 43 #endif
jbachorik@9925 44
jbachorik@9925 45 #define TranslateError(err) "JVMTI error"
jbachorik@9925 46
jbachorik@9925 47 static jvmtiEnv *jvmti = NULL;
jbachorik@9925 48
jbachorik@9925 49 static jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved);
jbachorik@9925 50
jbachorik@9925 51 JNIEXPORT
jbachorik@9925 52 jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
jbachorik@9925 53 return Agent_Initialize(jvm, options, reserved);
jbachorik@9925 54 }
jbachorik@9925 55
jbachorik@9925 56 JNIEXPORT
jbachorik@9925 57 jint JNICALL Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) {
jbachorik@9925 58 return Agent_Initialize(jvm, options, reserved);
jbachorik@9925 59 }
jbachorik@9925 60
jbachorik@9925 61 JNIEXPORT
jbachorik@9925 62 jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) {
jbachorik@9925 63 return JNI_VERSION_1_8;
jbachorik@9925 64 }
jbachorik@9925 65
jbachorik@9925 66 static void JNICALL
jbachorik@9925 67 Callback_VMStart(jvmtiEnv *jvmti_env, JNIEnv *env) {
jbachorik@9925 68 printf("Localizing jdk.jfr.FlightRecorder\n");
jbachorik@9925 69 // without fix for 8233197 the process will crash at the following line
jbachorik@9925 70 jclass cls = (*env)->FindClass(env, "jdk/jfr/FlightRecorder");
jbachorik@9925 71 jmethodID mid = (*env)->GetStaticMethodID(env, cls, "getFlightRecorder", "()Ljdk/jfr/FlightRecorder;");
jbachorik@9925 72 if (mid == 0) {
jbachorik@9925 73 printf("Unable to localize jdk.jfr.FlightRecorder#getFlightRecorder() method\n");
jbachorik@9925 74 // crash the tested JVM to make the test fail
jbachorik@9925 75 exit(-1);
jbachorik@9925 76 }
jbachorik@9925 77 printf("Going to initialize JFR subsystem ...\n");
jbachorik@9925 78 jobject jfr = (*env)->CallStaticObjectMethod(env, cls, mid);
jbachorik@9925 79
jbachorik@9925 80 if (!(*env)->ExceptionCheck(env)) {
jbachorik@9925 81 // crash the tested JVM to make the test fail
jbachorik@9925 82 printf("JFR subsystem is wrongly initialized too early\n");
jbachorik@9925 83 exit(-2);
jbachorik@9925 84 }
jbachorik@9925 85 // exit VM
jbachorik@9925 86 exit(0);
jbachorik@9925 87 }
jbachorik@9925 88
jbachorik@9925 89 static
jbachorik@9925 90 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
jbachorik@9925 91 jint res, size;
jbachorik@9925 92 jvmtiCapabilities caps;
jbachorik@9925 93 jvmtiEventCallbacks callbacks;
jbachorik@9925 94 jvmtiError err;
jbachorik@9925 95
jbachorik@9925 96 res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti),
jbachorik@9925 97 JVMTI_VERSION_1_2);
jbachorik@9925 98 if (res != JNI_OK || jvmti == NULL) {
jbachorik@9925 99 printf(" Error: wrong result of a valid call to GetEnv!\n");
jbachorik@9925 100 return JNI_ERR;
jbachorik@9925 101 }
jbachorik@9925 102
jbachorik@9925 103 size = (jint)sizeof(callbacks);
jbachorik@9925 104
jbachorik@9925 105 memset(&callbacks, 0, sizeof(callbacks));
jbachorik@9925 106 callbacks.VMStart = Callback_VMStart;
jbachorik@9925 107
jbachorik@9925 108 err = (*jvmti)->SetEventCallbacks(jvmti, &callbacks, size);
jbachorik@9925 109 if (err != JVMTI_ERROR_NONE) {
jbachorik@9925 110 printf(" Error in SetEventCallbacks: %s (%d)\n", TranslateError(err), err);
jbachorik@9925 111 return JNI_ERR;
jbachorik@9925 112 }
jbachorik@9925 113
jbachorik@9925 114 err = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE, JVMTI_EVENT_VM_START, (jthread)NULL);
jbachorik@9925 115 if (err != JVMTI_ERROR_NONE) {
jbachorik@9925 116 printf(" Error in SetEventNotificationMode: %s (%d)\n", TranslateError(err), err);
jbachorik@9925 117 return JNI_ERR;
jbachorik@9925 118 }
jbachorik@9925 119 return JNI_OK;
jbachorik@9925 120 }
jbachorik@9925 121
jbachorik@9925 122 #ifdef __cplusplus
jbachorik@9925 123 }
jbachorik@9925 124 #endif

mercurial