|
1 /* |
|
2 * Copyright (c) 2004, 2018, 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 <stdlib.h> |
|
25 #include <string.h> |
|
26 #include "jni_tools.h" |
|
27 #include "agent_common.h" |
|
28 #include "jvmti_tools.h" |
|
29 |
|
30 #define PASSED 0 |
|
31 #define STATUS_FAILED 2 |
|
32 |
|
33 #ifdef __cplusplus |
|
34 extern "C" { |
|
35 #endif |
|
36 |
|
37 /* ========================================================================== */ |
|
38 |
|
39 /* scaffold objects */ |
|
40 static jlong timeout = 0; |
|
41 |
|
42 /* test objects */ |
|
43 static int VMInitEventsCount = 0; |
|
44 static int VMInitEventsExpected = 1; |
|
45 static int VMDeathEventsCount = 0; |
|
46 static int VMDeathEventsExpected = 1; |
|
47 |
|
48 /* ========================================================================== */ |
|
49 |
|
50 /** callback functions **/ |
|
51 |
|
52 void JNICALL VMInit(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thr) { |
|
53 NSK_DISPLAY0("VMInit event\n"); |
|
54 VMInitEventsCount++; |
|
55 } |
|
56 |
|
57 void JNICALL |
|
58 VMDeath(jvmtiEnv *jvmti_env, JNIEnv *env) { |
|
59 NSK_DISPLAY0("VMDeath event\n"); |
|
60 VMDeathEventsCount++; |
|
61 } |
|
62 |
|
63 /* ========================================================================== */ |
|
64 |
|
65 /** Agent algorithm. */ |
|
66 static void JNICALL |
|
67 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) { |
|
68 if (!nsk_jvmti_waitForSync(timeout)) |
|
69 return; |
|
70 |
|
71 if (VMInitEventsCount != VMInitEventsExpected) { |
|
72 NSK_COMPLAIN2("Wrong number of VM init events: %d, expected: %d\n", |
|
73 VMInitEventsCount, VMInitEventsExpected); |
|
74 nsk_jvmti_setFailStatus(); |
|
75 } |
|
76 |
|
77 if (!nsk_jvmti_resumeSync()) |
|
78 return; |
|
79 } |
|
80 |
|
81 /* ========================================================================== */ |
|
82 |
|
83 /** Agent library initialization. */ |
|
84 #ifdef STATIC_BUILD |
|
85 JNIEXPORT jint JNICALL Agent_OnLoad_ma02t001(JavaVM *jvm, char *options, void *reserved) { |
|
86 return Agent_Initialize(jvm, options, reserved); |
|
87 } |
|
88 JNIEXPORT jint JNICALL Agent_OnAttach_ma02t001(JavaVM *jvm, char *options, void *reserved) { |
|
89 return Agent_Initialize(jvm, options, reserved); |
|
90 } |
|
91 JNIEXPORT jint JNI_OnLoad_ma02t001(JavaVM *jvm, char *options, void *reserved) { |
|
92 return JNI_VERSION_1_8; |
|
93 } |
|
94 #endif |
|
95 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { |
|
96 jvmtiEnv* jvmti = NULL; |
|
97 jvmtiEventCallbacks callbacks; |
|
98 |
|
99 NSK_DISPLAY0("Agent_OnLoad\n"); |
|
100 |
|
101 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) |
|
102 return JNI_ERR; |
|
103 |
|
104 timeout = nsk_jvmti_getWaitTime() * 60 * 1000; |
|
105 |
|
106 if (!NSK_VERIFY((jvmti = |
|
107 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL)) |
|
108 return JNI_ERR; |
|
109 |
|
110 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL))) |
|
111 return JNI_ERR; |
|
112 |
|
113 memset(&callbacks, 0, sizeof(callbacks)); |
|
114 callbacks.VMInit = &VMInit; |
|
115 callbacks.VMDeath = &VMDeath; |
|
116 if (!NSK_VERIFY(nsk_jvmti_init_MA(&callbacks))) |
|
117 return JNI_ERR; |
|
118 |
|
119 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode, |
|
120 jvmti, JVMTI_ENABLE, JVMTI_EVENT_VM_INIT, NULL))) |
|
121 return JNI_ERR; |
|
122 |
|
123 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode, |
|
124 jvmti, JVMTI_ENABLE, JVMTI_EVENT_VM_DEATH, NULL))) |
|
125 return JNI_ERR; |
|
126 |
|
127 return JNI_OK; |
|
128 } |
|
129 |
|
130 /* ========================================================================== */ |
|
131 |
|
132 /* agent library shutdown */ |
|
133 JNIEXPORT void JNICALL |
|
134 #ifdef STATIC_BUILD |
|
135 Agent_OnUnload_ma02t001(JavaVM *jvm) |
|
136 #else |
|
137 Agent_OnUnload(JavaVM *jvm) |
|
138 #endif |
|
139 { |
|
140 const char* KEY_PHRASE = "Agent_OnUnload() of the 1st agent"; |
|
141 |
|
142 fprintf(stdout, "%s\n", KEY_PHRASE); |
|
143 fflush(stdout); |
|
144 |
|
145 if (VMDeathEventsCount != VMDeathEventsExpected) { |
|
146 NSK_COMPLAIN2("Wrong number of VM death events: %d, expected: %d\n", |
|
147 VMDeathEventsCount, VMDeathEventsExpected); |
|
148 exit(97); |
|
149 } |
|
150 } |
|
151 |
|
152 /* ========================================================================== */ |
|
153 |
|
154 #ifdef __cplusplus |
|
155 } |
|
156 #endif |