|
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 */ |
|
23 |
|
24 #include <stdio.h> |
|
25 #include <stdlib.h> |
|
26 #include <string.h> |
|
27 #include "jvmti.h" |
|
28 |
|
29 #ifdef __cplusplus |
|
30 extern "C" { |
|
31 #endif |
|
32 |
|
33 #ifndef JNI_ENV_ARG |
|
34 |
|
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 |
|
42 |
|
43 #endif |
|
44 |
|
45 #define TranslateError(err) "JVMTI error" |
|
46 |
|
47 static jvmtiEnv *jvmti = NULL; |
|
48 |
|
49 static jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved); |
|
50 |
|
51 JNIEXPORT |
|
52 jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) { |
|
53 return Agent_Initialize(jvm, options, reserved); |
|
54 } |
|
55 |
|
56 JNIEXPORT |
|
57 jint JNICALL Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) { |
|
58 return Agent_Initialize(jvm, options, reserved); |
|
59 } |
|
60 |
|
61 JNIEXPORT |
|
62 jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) { |
|
63 return JNI_VERSION_1_8; |
|
64 } |
|
65 |
|
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); |
|
79 |
|
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 } |
|
88 |
|
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; |
|
95 |
|
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 } |
|
102 |
|
103 size = (jint)sizeof(callbacks); |
|
104 |
|
105 memset(&callbacks, 0, sizeof(callbacks)); |
|
106 callbacks.VMStart = Callback_VMStart; |
|
107 |
|
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 } |
|
113 |
|
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 } |
|
121 |
|
122 #ifdef __cplusplus |
|
123 } |
|
124 #endif |