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 /*
34 * Expected agent work scenario:
35 * - during initialization agent registers native methods used be target application and enables ObjectFree events
36 * - target application using native method and agent's jvmti environment tags object and provokes collection
37 * of this object
38 * - agent receives ObjectFree event for tagged object
39 * - target application using native method calls nsk_aod_agentFinished and agent finishes work
40 * (agent can't call nsk_aod_agentFinished from ObjectFree handler, nsk_aod_agentFinished calls
41 * JNI functions and it is prohibited in ObjectFree handler)
42 *
43 */
44
45 #define TAG_VALUE (jlong)777
46 #define ATTACH021_TARGET_APP_CLASS_NAME "nsk/jvmti/AttachOnDemand/attach021/attach021Target"
47
48 static jvmtiEnv* jvmti;
49
70 jclass klass) {
71 nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_OBJECT_FREE, success, jvmti, jni);
72 }
73
74 void JNICALL objectFreeHandler(jvmtiEnv *jvmti, jlong tag) {
75 NSK_DISPLAY2("%s: object free event for object %ld\n", agentName, tag);
76
77 if (tag != TAG_VALUE) {
78 success = 0;
79 NSK_COMPLAIN2("%s: unexpected tag value, expected is %ld\n", agentName, TAG_VALUE);
80 } else {
81 success = 1;
82 }
83
84 /*
85 * Can't use JNI functions from ObjectFree event handler, in this test target application calls
86 * function nsk_aod_agentFinished
87 */
88 }
89
90 int registerNativeMethods(JNIEnv* jni) {
91 jclass appClass;
92 JNINativeMethod nativeMethods[] = {
93 { (char*) "setTagFor", (char*) "(Ljava/lang/Object;)Z", (void*) Java_nsk_jvmti_AttachOnDemand_attach021_attach021Target_setTagFor },
94 { (char*) "shutdownAgent", (char*) "()V", (void*) Java_nsk_jvmti_AttachOnDemand_attach021_attach021Target_shutdownAgent } };
95 jint nativeMethodsNumber = 2;
96
97 appClass = jni->FindClass(ATTACH021_TARGET_APP_CLASS_NAME);
98 if (!NSK_JNI_VERIFY(jni, appClass != NULL)) {
99 return NSK_FALSE;
100 }
101
102 if (!NSK_JNI_VERIFY(jni,
103 (jni->RegisterNatives(appClass, nativeMethods, nativeMethodsNumber) == 0))) {
104 return NSK_FALSE;
105 }
106
107 return NSK_TRUE;
108 }
109
110 #ifdef STATIC_BUILD
111 JNIEXPORT jint JNI_OnLoad_attach021Agent00(JavaVM *jvm, char *options, void *reserved) {
112 return JNI_VERSION_1_8;
113 }
114 #endif
115
116 JNIEXPORT jint JNICALL
117 #ifdef STATIC_BUILD
118 Agent_OnAttach_attach021Agent00(JavaVM *vm, char *optionsString, void *reserved)
119 #else
120 Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)
121 #endif
122 {
123 jvmtiEventCallbacks eventCallbacks;
124 jvmtiCapabilities caps;
125 JNIEnv* jni;
126
127 options = (Options*) nsk_aod_createOptions(optionsString);
128 if (!NSK_VERIFY(options != NULL))
129 return JNI_ERR;
130
131 agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);
132
133 jni = (JNIEnv*) nsk_aod_createJNIEnv(vm);
134 if (jni == NULL)
135 return JNI_ERR;
136
137 jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved);
138 if (!NSK_VERIFY(jvmti != NULL))
139 return JNI_ERR;
140
141 if (!NSK_VERIFY(registerNativeMethods(jni))) {
142 return JNI_ERR;
143 }
144
145 memset(&caps, 0, sizeof(caps));
146 caps.can_tag_objects = 1;
147 caps.can_generate_object_free_events = 1;
148 if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {
149 return JNI_ERR;
150 }
151
152 memset(&eventCallbacks,0, sizeof(eventCallbacks));
153 eventCallbacks.ObjectFree = objectFreeHandler;
154 if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {
155 return JNI_ERR;
156 }
157
158 if (!(nsk_jvmti_aod_enableEvent(jvmti, JVMTI_EVENT_OBJECT_FREE))) {
159 return JNI_ERR;
160 }
161
162 NSK_DISPLAY1("%s: initialization was done\n", agentName);
163
|
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 /*
35 * Expected agent work scenario:
36 * - during initialization agent registers native methods used be target application and enables ObjectFree events
37 * - target application using native method and agent's jvmti environment tags object and provokes collection
38 * of this object
39 * - agent receives ObjectFree event for tagged object
40 * - target application using native method calls nsk_aod_agentFinished and agent finishes work
41 * (agent can't call nsk_aod_agentFinished from ObjectFree handler, nsk_aod_agentFinished calls
42 * JNI functions and it is prohibited in ObjectFree handler)
43 *
44 */
45
46 #define TAG_VALUE (jlong)777
47 #define ATTACH021_TARGET_APP_CLASS_NAME "nsk/jvmti/AttachOnDemand/attach021/attach021Target"
48
49 static jvmtiEnv* jvmti;
50
71 jclass klass) {
72 nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_OBJECT_FREE, success, jvmti, jni);
73 }
74
75 void JNICALL objectFreeHandler(jvmtiEnv *jvmti, jlong tag) {
76 NSK_DISPLAY2("%s: object free event for object %ld\n", agentName, tag);
77
78 if (tag != TAG_VALUE) {
79 success = 0;
80 NSK_COMPLAIN2("%s: unexpected tag value, expected is %ld\n", agentName, TAG_VALUE);
81 } else {
82 success = 1;
83 }
84
85 /*
86 * Can't use JNI functions from ObjectFree event handler, in this test target application calls
87 * function nsk_aod_agentFinished
88 */
89 }
90
91 void registerNativeMethods(JNIEnv* jni_env) {
92 ExceptionCheckingJniEnvPtr jni(jni_env);
93 jclass appClass;
94 JNINativeMethod nativeMethods[] = {
95 { (char*) "setTagFor", (char*) "(Ljava/lang/Object;)Z", (void*) Java_nsk_jvmti_AttachOnDemand_attach021_attach021Target_setTagFor },
96 { (char*) "shutdownAgent", (char*) "()V", (void*) Java_nsk_jvmti_AttachOnDemand_attach021_attach021Target_shutdownAgent } };
97 jint nativeMethodsNumber = 2;
98
99 appClass = jni->FindClass(ATTACH021_TARGET_APP_CLASS_NAME, TRACE_JNI_CALL);
100 jni->RegisterNatives(appClass, nativeMethods, nativeMethodsNumber, TRACE_JNI_CALL);
101 }
102
103 #ifdef STATIC_BUILD
104 JNIEXPORT jint JNI_OnLoad_attach021Agent00(JavaVM *jvm, char *options, void *reserved) {
105 return JNI_VERSION_1_8;
106 }
107 #endif
108
109 JNIEXPORT jint JNICALL
110 #ifdef STATIC_BUILD
111 Agent_OnAttach_attach021Agent00(JavaVM *vm, char *optionsString, void *reserved)
112 #else
113 Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)
114 #endif
115 {
116 jvmtiEventCallbacks eventCallbacks;
117 jvmtiCapabilities caps;
118 JNIEnv* jni;
119
120 options = (Options*) nsk_aod_createOptions(optionsString);
121 if (!NSK_VERIFY(options != NULL))
122 return JNI_ERR;
123
124 agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);
125
126 jni = (JNIEnv*) nsk_aod_createJNIEnv(vm);
127 if (jni == NULL)
128 return JNI_ERR;
129
130 jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved);
131 if (!NSK_VERIFY(jvmti != NULL))
132 return JNI_ERR;
133
134 registerNativeMethods(jni);
135
136 memset(&caps, 0, sizeof(caps));
137 caps.can_tag_objects = 1;
138 caps.can_generate_object_free_events = 1;
139 if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {
140 return JNI_ERR;
141 }
142
143 memset(&eventCallbacks,0, sizeof(eventCallbacks));
144 eventCallbacks.ObjectFree = objectFreeHandler;
145 if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {
146 return JNI_ERR;
147 }
148
149 if (!(nsk_jvmti_aod_enableEvent(jvmti, JVMTI_EVENT_OBJECT_FREE))) {
150 return JNI_ERR;
151 }
152
153 NSK_DISPLAY1("%s: initialization was done\n", agentName);
154
|