|
1 /* |
|
2 * Copyright (c) 2003, 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 <stdio.h> |
|
25 #include <stdlib.h> |
|
26 #include <string.h> |
|
27 #include <jvmti.h> |
|
28 #include "agent_common.h" |
|
29 |
|
30 #include "nsk_tools.h" |
|
31 #include "JVMTITools.h" |
|
32 #include "jvmti_tools.h" |
|
33 |
|
34 #ifdef __cplusplus |
|
35 extern "C" { |
|
36 #endif |
|
37 |
|
38 #define STATUS_FAILED 2 |
|
39 #define PASSED 0 |
|
40 |
|
41 #define MEM_SIZE 1024 |
|
42 |
|
43 static jvmtiEnv *jvmti = NULL; |
|
44 static jvmtiEventCallbacks callbacks; |
|
45 static jvmtiCapabilities caps; |
|
46 |
|
47 static volatile jint result = PASSED; |
|
48 static volatile int gcstart = 0; |
|
49 unsigned char *mem; |
|
50 |
|
51 static void rawMonitorFunc(jvmtiEnv *jvmti_env, const char *msg) { |
|
52 jrawMonitorID _lock; |
|
53 |
|
54 NSK_DISPLAY1("%s: creating a raw monitor ...\n", |
|
55 msg); |
|
56 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(CreateRawMonitor, |
|
57 jvmti_env, "_lock", &_lock))) { |
|
58 result = STATUS_FAILED; |
|
59 NSK_COMPLAIN1("TEST FAILED: %s: unable to create a raw monitor\n\n", |
|
60 msg); |
|
61 return; |
|
62 } |
|
63 NSK_DISPLAY1("CHECK PASSED: %s: raw monitor created\n", |
|
64 msg); |
|
65 |
|
66 NSK_DISPLAY1("%s: entering the raw monitor ...\n", |
|
67 msg); |
|
68 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorEnter, |
|
69 jvmti_env, _lock))) { |
|
70 result = STATUS_FAILED; |
|
71 NSK_COMPLAIN1("TEST FAILED: %s: unable to enter the raw monitor\n\n", |
|
72 msg); |
|
73 } |
|
74 else { |
|
75 NSK_DISPLAY1("CHECK PASSED: %s: the raw monitor entered\n", |
|
76 msg); |
|
77 |
|
78 NSK_DISPLAY1("%s: waiting the raw monitor ...\n", |
|
79 msg); |
|
80 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(RawMonitorWait, |
|
81 jvmti_env, _lock, (jlong)10))) { |
|
82 result = STATUS_FAILED; |
|
83 NSK_COMPLAIN1("TEST FAILED: %s: unable to wait the raw monitor\n\n", |
|
84 msg); |
|
85 } |
|
86 NSK_DISPLAY1("CHECK PASSED: %s: the raw monitor waited\n", |
|
87 msg); |
|
88 |
|
89 |
|
90 NSK_DISPLAY1("%s: notifying a single thread waiting on the raw monitor ...\n", |
|
91 msg); |
|
92 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorNotify, |
|
93 jvmti_env, _lock))) { |
|
94 result = STATUS_FAILED; |
|
95 NSK_COMPLAIN1("TEST FAILED: %s: unable to notify single thread\n\n", |
|
96 msg); |
|
97 } |
|
98 NSK_DISPLAY1("CHECK PASSED: %s: single thread notified\n", |
|
99 msg); |
|
100 |
|
101 |
|
102 NSK_DISPLAY1("%s: notifying all threads waiting on the raw monitor ...\n", |
|
103 msg); |
|
104 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorNotifyAll, |
|
105 jvmti_env, _lock))) { |
|
106 result = STATUS_FAILED; |
|
107 NSK_COMPLAIN1("TEST FAILED: %s: unable to notify all threads\n\n", |
|
108 msg); |
|
109 } |
|
110 NSK_DISPLAY1("CHECK PASSED: %s: all threads notified\n", |
|
111 msg); |
|
112 |
|
113 |
|
114 NSK_DISPLAY1("%s: exiting the raw monitor ...\n", |
|
115 msg); |
|
116 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorExit, |
|
117 jvmti_env, _lock))) { |
|
118 result = STATUS_FAILED; |
|
119 NSK_COMPLAIN1("TEST FAILED: %s: unable to exit the raw monitor\n\n", |
|
120 msg); |
|
121 } |
|
122 NSK_DISPLAY1("CHECK PASSED: %s: the raw monitor exited\n", |
|
123 msg); |
|
124 } |
|
125 |
|
126 NSK_DISPLAY1("%s: destroying the raw monitor ...\n", |
|
127 msg); |
|
128 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(DestroyRawMonitor, |
|
129 jvmti_env, _lock))) { |
|
130 result = STATUS_FAILED; |
|
131 NSK_COMPLAIN1("TEST FAILED: %s: unable to destroy a raw monitor\n", |
|
132 msg); |
|
133 return; |
|
134 } |
|
135 NSK_DISPLAY1("CHECK PASSED: %s: the raw monitor destroyed\n", |
|
136 msg); |
|
137 } |
|
138 |
|
139 static void memoryFunc(jvmtiEnv *jvmti_env, const char *msg) { |
|
140 NSK_DISPLAY1("%s: allocating memory ...\n", |
|
141 msg); |
|
142 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(Allocate, |
|
143 jvmti_env, MEM_SIZE, &mem))) { |
|
144 result = STATUS_FAILED; |
|
145 NSK_COMPLAIN1("TEST FAILED: %s: unable to allocate memory\n\n", |
|
146 msg); |
|
147 return; |
|
148 } |
|
149 else |
|
150 NSK_DISPLAY1("CHECK PASSED: %s: memory has been allocated successfully\n", |
|
151 msg); |
|
152 |
|
153 NSK_DISPLAY1("%s: deallocating memory ...\n", |
|
154 msg); |
|
155 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(Deallocate, |
|
156 jvmti_env, mem))) { |
|
157 result = STATUS_FAILED; |
|
158 NSK_COMPLAIN1("TEST FAILED: %s: unable to deallocate memory\n\n", |
|
159 msg); |
|
160 } |
|
161 else |
|
162 NSK_DISPLAY1("CHECK PASSED: %s: memory has been deallocated successfully\n\n", |
|
163 msg); |
|
164 } |
|
165 |
|
166 /** callback functions **/ |
|
167 void JNICALL |
|
168 GarbageCollectionFinish(jvmtiEnv *jvmti_env) { |
|
169 gcstart++; |
|
170 NSK_DISPLAY1(">>>> GarbageCollectionFinish event #%d received\n", |
|
171 gcstart); |
|
172 |
|
173 rawMonitorFunc(jvmti_env, "GarbageCollectionFinish"); |
|
174 |
|
175 memoryFunc(jvmti_env, "GarbageCollectionFinish"); |
|
176 |
|
177 NSK_DISPLAY0("<<<<\n\n"); |
|
178 } |
|
179 |
|
180 void JNICALL |
|
181 VMDeath(jvmtiEnv *jvmti_env, JNIEnv *env) { |
|
182 NSK_DISPLAY0("VMDeath event received\n"); |
|
183 |
|
184 if (result == STATUS_FAILED) |
|
185 exit(95 + STATUS_FAILED); |
|
186 } |
|
187 |
|
188 /************************/ |
|
189 |
|
190 #ifdef STATIC_BUILD |
|
191 JNIEXPORT jint JNICALL Agent_OnLoad_gcfinish001(JavaVM *jvm, char *options, void *reserved) { |
|
192 return Agent_Initialize(jvm, options, reserved); |
|
193 } |
|
194 JNIEXPORT jint JNICALL Agent_OnAttach_gcfinish001(JavaVM *jvm, char *options, void *reserved) { |
|
195 return Agent_Initialize(jvm, options, reserved); |
|
196 } |
|
197 JNIEXPORT jint JNI_OnLoad_gcfinish001(JavaVM *jvm, char *options, void *reserved) { |
|
198 return JNI_VERSION_1_8; |
|
199 } |
|
200 #endif |
|
201 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { |
|
202 /* init framework and parse options */ |
|
203 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) |
|
204 return JNI_ERR; |
|
205 |
|
206 /* create JVMTI environment */ |
|
207 if (!NSK_VERIFY((jvmti = |
|
208 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL)) |
|
209 return JNI_ERR; |
|
210 |
|
211 /* add capability to generate compiled method events */ |
|
212 memset(&caps, 0, sizeof(jvmtiCapabilities)); |
|
213 caps.can_generate_garbage_collection_events = 1; |
|
214 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(AddCapabilities, |
|
215 jvmti, &caps))) |
|
216 return JNI_ERR; |
|
217 |
|
218 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(GetCapabilities, |
|
219 jvmti, &caps))) |
|
220 return JNI_ERR; |
|
221 |
|
222 if (!caps.can_generate_garbage_collection_events) |
|
223 NSK_DISPLAY0("Warning: generation of garbage collection events is not implemented\n"); |
|
224 |
|
225 /* set event callback */ |
|
226 NSK_DISPLAY0("setting event callbacks ...\n"); |
|
227 (void) memset(&callbacks, 0, sizeof(callbacks)); |
|
228 callbacks.VMDeath = &VMDeath; |
|
229 callbacks.GarbageCollectionFinish = &GarbageCollectionFinish; |
|
230 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(SetEventCallbacks, |
|
231 jvmti, &callbacks, sizeof(callbacks)))) |
|
232 return JNI_ERR; |
|
233 |
|
234 NSK_DISPLAY0("setting event callbacks done\nenabling JVMTI events ...\n"); |
|
235 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode, |
|
236 jvmti, JVMTI_ENABLE, JVMTI_EVENT_VM_DEATH, NULL))) |
|
237 return JNI_ERR; |
|
238 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode, |
|
239 jvmti, JVMTI_ENABLE, JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, NULL))) |
|
240 return JNI_ERR; |
|
241 NSK_DISPLAY0("enabling the events done\n\n"); |
|
242 |
|
243 return JNI_OK; |
|
244 } |
|
245 |
|
246 #ifdef __cplusplus |
|
247 } |
|
248 #endif |