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
|
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 int 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 if (appClass == NULL) {
110 return NSK_FALSE;
111 }
112
113 if (jni->RegisterNatives(appClass, nativeMethods, nativeMethodsNumber, TRACE_JNI_CALL) != 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
|