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 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <jni.h>
27 #include <jvmti.h>
28 #include <aod.h>
29 #include <jvmti_aod.h>
30
31 extern "C" {
32
33 #define OBJECTS_FOR_ALLOCATION_TEST_CLASS_NAME "Lnsk/jvmti/AttachOnDemand/attach022/ClassForAllocationEventsTest;"
34
35 static jvmtiEnv* jvmti;
36 static Options* options = NULL;
37 static const char* agentName;
38
39 static jvmtiEvent testEvents[] = { JVMTI_EVENT_OBJECT_FREE, JVMTI_EVENT_VM_OBJECT_ALLOC };
40 static const int testEventsNumber = 2;
41
42 static volatile int taggedObjectsCounter = 0;
43 static volatile int freedObjectsCounter = 0;
44
45 static jrawMonitorID objectTagMonitor;
46 static jrawMonitorID objectFreeMonitor;
47
48 volatile int success = 1;
49
50 volatile int agentFinished;
51
52 void shutdownAgent(JNIEnv* jni) {
53 if (agentFinished)
54 return;
55
56 if (!nsk_jvmti_aod_disableEvents(jvmti, testEvents, testEventsNumber))
57 success = 0;
58
59 nsk_aod_agentFinished(jni, agentName, success);
60
61 agentFinished = 1;
62 }
63
64 JNIEXPORT jboolean JNICALL
65 Java_nsk_jvmti_AttachOnDemand_attach022_attach022Target_shutdownAgent(JNIEnv * jni,
66 jclass klass, jint expectedTaggedObjectsCounter) {
67
68 if (taggedObjectsCounter != expectedTaggedObjectsCounter) {
69 success = 0;
70 NSK_COMPLAIN2("ERROR: unexpected taggedObjectsCounter: %d (expected value is %d)\n", taggedObjectsCounter, expectedTaggedObjectsCounter);
71 }
72
73 if (taggedObjectsCounter != freedObjectsCounter) {
74 success = 0;
75 NSK_COMPLAIN2("ERROR: taggedObjectsCounter != freedObjectsCounter (taggedObjectsCounter: %d, freedObjectsCounter: %d)\n",
76 taggedObjectsCounter, freedObjectsCounter);
77 }
78
79 shutdownAgent(jni);
80
81 return success ? JNI_TRUE : JNI_FALSE;
82 }
83
84 void JNICALL objectFreeHandler(jvmtiEnv *jvmti, jlong tag) {
85 NSK_DISPLAY2("%s: ObjectFree event received (object tag: %ld)\n", agentName, tag);
86
87 if (NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(objectFreeMonitor))) {
88 freedObjectsCounter++;
89
90 if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(objectFreeMonitor))) {
91 success = 0;
92 }
93 } else {
94 success = 0;
95 }
96 }
97
98 #define ATTACH022_TARGET_APP_CLASS_NAME "nsk/jvmti/AttachOnDemand/attach022/attach022Target"
99
100 int registerNativeMethods(JNIEnv* jni) {
101 jclass appClass;
102 JNINativeMethod nativeMethods[] = {
103 { (char*)"shutdownAgent", (char*)"(I)Z",
104 (void*) Java_nsk_jvmti_AttachOnDemand_attach022_attach022Target_shutdownAgent } };
105 jint nativeMethodsNumber = 1;
106
107 appClass = jni->FindClass(ATTACH022_TARGET_APP_CLASS_NAME);
108 if (!NSK_JNI_VERIFY(jni, appClass != NULL)) {
109 return NSK_FALSE;
110 }
111
112 if (!NSK_JNI_VERIFY(jni,
113 (jni->RegisterNatives(appClass, nativeMethods, nativeMethodsNumber) == 0))) {
114 return NSK_FALSE;
115 }
116
117 return NSK_TRUE;
118 }
119
120 void JNICALL vmObjectAllocHandler(jvmtiEnv * jvmti,
121 JNIEnv * jni,
122 jthread thread,
123 jobject object,
124 jclass object_class,
125 jlong size) {
126 char className[MAX_STRING_LENGTH];
127
128 if (!nsk_jvmti_aod_getClassName(jvmti, object_class, className)) {
129 success = 0;
130 shutdownAgent(jni);
131 return;
132 }
133
134 NSK_DISPLAY2("%s: ObjectAlloc event received (object class: %s)\n", agentName, className);
135
136 if (!strcmp(className, OBJECTS_FOR_ALLOCATION_TEST_CLASS_NAME)) {
137 if (NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(objectTagMonitor))) {
173 #endif
174 {
175 jvmtiEventCallbacks eventCallbacks;
176 jvmtiCapabilities caps;
177 JNIEnv* jni;
178
179 options = (Options*) nsk_aod_createOptions(optionsString);
180 if (!NSK_VERIFY(options != NULL))
181 return JNI_ERR;
182
183 agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);
184
185 jni = (JNIEnv*) nsk_aod_createJNIEnv(vm);
186 if (jni == NULL)
187 return JNI_ERR;
188
189 jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved);
190 if (!NSK_VERIFY(jvmti != NULL))
191 return JNI_ERR;
192
193 if (!NSK_VERIFY(registerNativeMethods(jni))) {
194 return JNI_ERR;
195 }
196
197 if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("ObjectTagMonitor", &objectTagMonitor))) {
198 return JNI_ERR;
199 }
200
201 if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("ObjectFreeMonitor", &objectFreeMonitor))) {
202 return JNI_ERR;
203 }
204
205 memset(&caps, 0, sizeof(caps));
206 caps.can_tag_objects = 1;
207 caps.can_generate_object_free_events = 1;
208 caps.can_generate_vm_object_alloc_events = 1;
209 if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {
210 return JNI_ERR;
211 }
212
213 memset(&eventCallbacks,0, sizeof(eventCallbacks));
214 eventCallbacks.ObjectFree = objectFreeHandler;
215 eventCallbacks.VMObjectAlloc = vmObjectAllocHandler;
|
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 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <jni.h>
27 #include <jvmti.h>
28 #include <aod.h>
29 #include <jvmti_aod.h>
30 #include "ExceptionCheckingJniEnv.hpp"
31
32 extern "C" {
33
34 #define OBJECTS_FOR_ALLOCATION_TEST_CLASS_NAME "Lnsk/jvmti/AttachOnDemand/attach022/ClassForAllocationEventsTest;"
35
36 static jvmtiEnv* jvmti;
37 static Options* options = NULL;
38 static const char* agentName;
39
40 static jvmtiEvent testEvents[] = { JVMTI_EVENT_OBJECT_FREE, JVMTI_EVENT_VM_OBJECT_ALLOC };
41 static const int testEventsNumber = 2;
42
43 static volatile int taggedObjectsCounter = 0;
44 static volatile int freedObjectsCounter = 0;
45
46 static jrawMonitorID objectTagMonitor;
47 static jrawMonitorID objectFreeMonitor;
48
49 volatile int success = 1;
50
51 volatile int agentFinished;
52
53 void shutdownAgent(JNIEnv* jni) {
54 if (agentFinished)
55 return;
56
57 if (!nsk_jvmti_aod_disableEvents(jvmti, testEvents, testEventsNumber))
58 success = 0;
59
60 nsk_aod_agentFinished(jni, agentName, success);
61
62 agentFinished = 1;
63 }
64
65 JNIEXPORT jboolean JNICALL
66 Java_nsk_jvmti_AttachOnDemand_attach022_attach022Target_shutdownAgent(JNIEnv * jni,
67 jclass klass, jint expectedTaggedObjectsCounter) {
68 if (taggedObjectsCounter != expectedTaggedObjectsCounter) {
69 success = 0;
70 NSK_COMPLAIN2("ERROR: unexpected taggedObjectsCounter: %d (expected value is %d)\n", taggedObjectsCounter, expectedTaggedObjectsCounter);
71 }
72
73 if (taggedObjectsCounter != freedObjectsCounter) {
74 success = 0;
75 NSK_COMPLAIN2("ERROR: taggedObjectsCounter != freedObjectsCounter (taggedObjectsCounter: %d, freedObjectsCounter: %d)\n",
76 taggedObjectsCounter, freedObjectsCounter);
77 }
78
79 shutdownAgent(jni);
80
81 return success ? JNI_TRUE : JNI_FALSE;
82 }
83
84 void JNICALL objectFreeHandler(jvmtiEnv *jvmti, jlong tag) {
85 NSK_DISPLAY2("%s: ObjectFree event received (object tag: %ld)\n", agentName, tag);
86
87 if (NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(objectFreeMonitor))) {
88 freedObjectsCounter++;
89
90 if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(objectFreeMonitor))) {
91 success = 0;
92 }
93 } else {
94 success = 0;
95 }
96 }
97
98 #define ATTACH022_TARGET_APP_CLASS_NAME "nsk/jvmti/AttachOnDemand/attach022/attach022Target"
99
100 void registerNativeMethods(JNIEnv* jni_env) {
101 ExceptionCheckingJniEnvPtr jni(jni_env);
102 jclass appClass;
103 JNINativeMethod nativeMethods[] = {
104 { (char*)"shutdownAgent", (char*)"(I)Z",
105 (void*) Java_nsk_jvmti_AttachOnDemand_attach022_attach022Target_shutdownAgent } };
106 jint nativeMethodsNumber = 1;
107
108 appClass = jni->FindClass(ATTACH022_TARGET_APP_CLASS_NAME, TRACE_JNI_CALL);
109 jni->RegisterNatives(appClass, nativeMethods, nativeMethodsNumber, TRACE_JNI_CALL);
110 }
111
112 void JNICALL vmObjectAllocHandler(jvmtiEnv * jvmti,
113 JNIEnv * jni,
114 jthread thread,
115 jobject object,
116 jclass object_class,
117 jlong size) {
118 char className[MAX_STRING_LENGTH];
119
120 if (!nsk_jvmti_aod_getClassName(jvmti, object_class, className)) {
121 success = 0;
122 shutdownAgent(jni);
123 return;
124 }
125
126 NSK_DISPLAY2("%s: ObjectAlloc event received (object class: %s)\n", agentName, className);
127
128 if (!strcmp(className, OBJECTS_FOR_ALLOCATION_TEST_CLASS_NAME)) {
129 if (NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(objectTagMonitor))) {
165 #endif
166 {
167 jvmtiEventCallbacks eventCallbacks;
168 jvmtiCapabilities caps;
169 JNIEnv* jni;
170
171 options = (Options*) nsk_aod_createOptions(optionsString);
172 if (!NSK_VERIFY(options != NULL))
173 return JNI_ERR;
174
175 agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);
176
177 jni = (JNIEnv*) nsk_aod_createJNIEnv(vm);
178 if (jni == NULL)
179 return JNI_ERR;
180
181 jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved);
182 if (!NSK_VERIFY(jvmti != NULL))
183 return JNI_ERR;
184
185 registerNativeMethods(jni);
186
187 if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("ObjectTagMonitor", &objectTagMonitor))) {
188 return JNI_ERR;
189 }
190
191 if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("ObjectFreeMonitor", &objectFreeMonitor))) {
192 return JNI_ERR;
193 }
194
195 memset(&caps, 0, sizeof(caps));
196 caps.can_tag_objects = 1;
197 caps.can_generate_object_free_events = 1;
198 caps.can_generate_vm_object_alloc_events = 1;
199 if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {
200 return JNI_ERR;
201 }
202
203 memset(&eventCallbacks,0, sizeof(eventCallbacks));
204 eventCallbacks.ObjectFree = objectFreeHandler;
205 eventCallbacks.VMObjectAlloc = vmObjectAllocHandler;
|