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 <string.h>
26 #include "jvmti.h"
27 #include "agent_common.h"
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
33 #ifndef JNI_ENV_ARG
34
35 #ifdef __cplusplus
36 #define JNI_ENV_ARG(x, y) y
37 #define JNI_ENV_ARG1(x)
38 #define JNI_ENV_PTR(x) x
39 #else
40 #define JNI_ENV_ARG(x,y) x, y
41 #define JNI_ENV_ARG1(x) x
42 #define JNI_ENV_PTR(x) (*x)
43 #endif
44
45 #endif
46
47 #define JVMTI_ENV_ARG JNI_ENV_ARG
48 #define JVMTI_ENV_ARG1 JNI_ENV_ARG1
49 #define JVMTI_ENV_PTR JNI_ENV_PTR
50
51 #define JVMTI_ERROR_CHECK(str,res) if ( res != JVMTI_ERROR_NONE) { printf(str); printf("%d\n",res); return res;}
52 #define JVMTI_ERROR_CHECK_VOID(str,res) if ( res != JVMTI_ERROR_NONE) { printf(str); printf("%d\n",res); iGlobalStatus = 2; }
53
54 #define THREADS_LIMIT 8
55
56 jrawMonitorID access_lock;
57 jvmtiEnv *jvmti;
58 jint iGlobalStatus = 0;
59 jthread susp_thrd[THREADS_LIMIT];
60 static jvmtiEventCallbacks callbacks;
61 static jvmtiCapabilities jvmti_caps;
62
63
64 int printdump = 0;
65 int gc_start_count =0;
66 int gc_finish_count =0;
67
68
69 void debug_printf(const char *fmt, ...) {
113 #ifdef STATIC_BUILD
114 JNIEXPORT jint JNICALL Agent_OnLoad_gc(JavaVM *jvm, char *options, void *reserved) {
115 return Agent_Initialize(jvm, options, reserved);
116 }
117 JNIEXPORT jint JNICALL Agent_OnAttach_gc(JavaVM *jvm, char *options, void *reserved) {
118 return Agent_Initialize(jvm, options, reserved);
119 }
120 JNIEXPORT jint JNI_OnLoad_gc(JavaVM *jvm, char *options, void *reserved) {
121 return JNI_VERSION_1_8;
122 }
123 #endif
124 jint Agent_Initialize(JavaVM * jvm, char *options, void *reserved) {
125 jint res;
126
127 if (options && strlen(options) > 0) {
128 if (strstr(options, "printdump")) {
129 printdump = 1;
130 }
131 }
132
133 res = JNI_ENV_PTR(jvm)->
134 GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti), JVMTI_VERSION_1_1);
135 if (res < 0) {
136 debug_printf("Wrong result of a valid call to GetEnv!\n");
137 return JNI_ERR;
138 }
139
140 /* Create data access lock */
141 res = JVMTI_ENV_PTR(jvmti)->CreateRawMonitor(JVMTI_ENV_ARG(jvmti,"_access_lock"),&access_lock);
142 JVMTI_ERROR_CHECK("RawMonitorEnter in monitor_contended_entered failed with error code ", res);
143
144
145 /* Add capabilities */
146 res = JVMTI_ENV_PTR(jvmti)->GetPotentialCapabilities(JVMTI_ENV_ARG(jvmti, &jvmti_caps));
147 JVMTI_ERROR_CHECK("SetEventCallbacks returned error", res);
148
149 res = JVMTI_ENV_PTR(jvmti)->AddCapabilities(JVMTI_ENV_ARG(jvmti, &jvmti_caps));
150 JVMTI_ERROR_CHECK("SetEventCallbacks returned error", res);
151
152 /* Enable events */
153 init_callbacks();
154 res = JVMTI_ENV_PTR(jvmti)->SetEventCallbacks(JVMTI_ENV_ARG(jvmti, &callbacks), sizeof(callbacks));
155 JVMTI_ERROR_CHECK("SetEventCallbacks returned error", res);
156
157 res = JVMTI_ENV_PTR(jvmti)->SetEventNotificationMode(JVMTI_ENV_ARG(jvmti,JVMTI_ENABLE),JVMTI_EVENT_VM_INIT,NULL);
158 JVMTI_ERROR_CHECK("SetEventNotificationMode for VM_INIT returned error", res);
159
160 res = JVMTI_ENV_PTR(jvmti)->SetEventNotificationMode(JVMTI_ENV_ARG(jvmti,JVMTI_ENABLE),JVMTI_EVENT_VM_DEATH,NULL);
161 JVMTI_ERROR_CHECK("SetEventNotificationMode for vm death event returned error", res);
162
163 res = JVMTI_ENV_PTR(jvmti)->SetEventNotificationMode(JVMTI_ENV_ARG(jvmti,JVMTI_ENABLE),JVMTI_EVENT_GARBAGE_COLLECTION_START,NULL);
164 JVMTI_ERROR_CHECK("SetEventNotificationMode for gc start returned error", res);
165
166 res = JVMTI_ENV_PTR(jvmti)->SetEventNotificationMode(JVMTI_ENV_ARG(jvmti,JVMTI_ENABLE),JVMTI_EVENT_GARBAGE_COLLECTION_FINISH,NULL);
167 JVMTI_ERROR_CHECK("SetEventNotificationMode for gc finish returned error", res);
168
169 return JNI_OK;
170 }
171
172
173
174
175 JNIEXPORT void JNICALL
176 Java_nsk_jvmti_unit_functions_ForceGarbageCollection_gc_checkGCStart(JNIEnv * env, jclass cls) {
177 if (gc_start_count == 0) {
178 printf("Error: JVMTI_EVENT_GARBAGE_COLLECTION_START count = 0\n");
179 iGlobalStatus = 2;
180 }
181 }
182
183 JNIEXPORT void JNICALL
184 Java_nsk_jvmti_unit_functions_ForceGarbageCollection_gc_checkGCFinish(JNIEnv * env, jclass cls) {
185 if (gc_finish_count == 0) {
186 printf("Error: JVMTI_EVENT_GARBAGE_COLLECTION_FINISH count = 0\n");
187 iGlobalStatus = 2;
188 }
189 }
190
191 JNIEXPORT jint JNICALL
192 Java_nsk_jvmti_unit_functions_ForceGarbageCollection_gc_GetResult(JNIEnv * env, jclass cls) {
193 return iGlobalStatus;
194 }
195
196
197 JNIEXPORT void JNICALL
198 Java_nsk_jvmti_unit_functions_ForceGarbageCollection_gc_jvmtiForceGC (JNIEnv * env, jclass cls) {
199 jvmtiError ret;
200
201 debug_printf("jvmti Force gc requested \n");
202 ret = JVMTI_ENV_PTR(jvmti)->ForceGarbageCollection(JVMTI_ENV_ARG1(jvmti));
203
204 if (ret != JVMTI_ERROR_NONE) {
205 printf("Error: ForceGarbageCollection %d \n", ret);
206 iGlobalStatus = 2;
207 }
208 }
209
210 #ifdef __cplusplus
211 }
212 #endif
|
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 <string.h>
26 #include "jvmti.h"
27 #include "agent_common.h"
28
29 extern "C" {
30
31
32 #define JVMTI_ERROR_CHECK(str,res) if ( res != JVMTI_ERROR_NONE) { printf(str); printf("%d\n",res); return res;}
33 #define JVMTI_ERROR_CHECK_VOID(str,res) if ( res != JVMTI_ERROR_NONE) { printf(str); printf("%d\n",res); iGlobalStatus = 2; }
34
35 #define THREADS_LIMIT 8
36
37 jrawMonitorID access_lock;
38 jvmtiEnv *jvmti;
39 jint iGlobalStatus = 0;
40 jthread susp_thrd[THREADS_LIMIT];
41 static jvmtiEventCallbacks callbacks;
42 static jvmtiCapabilities jvmti_caps;
43
44
45 int printdump = 0;
46 int gc_start_count =0;
47 int gc_finish_count =0;
48
49
50 void debug_printf(const char *fmt, ...) {
94 #ifdef STATIC_BUILD
95 JNIEXPORT jint JNICALL Agent_OnLoad_gc(JavaVM *jvm, char *options, void *reserved) {
96 return Agent_Initialize(jvm, options, reserved);
97 }
98 JNIEXPORT jint JNICALL Agent_OnAttach_gc(JavaVM *jvm, char *options, void *reserved) {
99 return Agent_Initialize(jvm, options, reserved);
100 }
101 JNIEXPORT jint JNI_OnLoad_gc(JavaVM *jvm, char *options, void *reserved) {
102 return JNI_VERSION_1_8;
103 }
104 #endif
105 jint Agent_Initialize(JavaVM * jvm, char *options, void *reserved) {
106 jint res;
107
108 if (options && strlen(options) > 0) {
109 if (strstr(options, "printdump")) {
110 printdump = 1;
111 }
112 }
113
114 res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);
115 if (res < 0) {
116 debug_printf("Wrong result of a valid call to GetEnv!\n");
117 return JNI_ERR;
118 }
119
120 /* Create data access lock */
121 res = jvmti->CreateRawMonitor("_access_lock", &access_lock);
122 JVMTI_ERROR_CHECK("RawMonitorEnter in monitor_contended_entered failed with error code ", res);
123
124
125 /* Add capabilities */
126 res = jvmti->GetPotentialCapabilities(&jvmti_caps);
127 JVMTI_ERROR_CHECK("SetEventCallbacks returned error", res);
128
129 res = jvmti->AddCapabilities(&jvmti_caps);
130 JVMTI_ERROR_CHECK("SetEventCallbacks returned error", res);
131
132 /* Enable events */
133 init_callbacks();
134 res = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
135 JVMTI_ERROR_CHECK("SetEventCallbacks returned error", res);
136
137 res = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_INIT, NULL);
138 JVMTI_ERROR_CHECK("SetEventNotificationMode for VM_INIT returned error", res);
139
140 res = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_DEATH, NULL);
141 JVMTI_ERROR_CHECK("SetEventNotificationMode for vm death event returned error", res);
142
143 res = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_GARBAGE_COLLECTION_START, NULL);
144 JVMTI_ERROR_CHECK("SetEventNotificationMode for gc start returned error", res);
145
146 res = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, NULL);
147 JVMTI_ERROR_CHECK("SetEventNotificationMode for gc finish returned error", res);
148
149 return JNI_OK;
150 }
151
152
153
154
155 JNIEXPORT void JNICALL
156 Java_nsk_jvmti_unit_functions_ForceGarbageCollection_gc_checkGCStart(JNIEnv * env, jclass cls) {
157 if (gc_start_count == 0) {
158 printf("Error: JVMTI_EVENT_GARBAGE_COLLECTION_START count = 0\n");
159 iGlobalStatus = 2;
160 }
161 }
162
163 JNIEXPORT void JNICALL
164 Java_nsk_jvmti_unit_functions_ForceGarbageCollection_gc_checkGCFinish(JNIEnv * env, jclass cls) {
165 if (gc_finish_count == 0) {
166 printf("Error: JVMTI_EVENT_GARBAGE_COLLECTION_FINISH count = 0\n");
167 iGlobalStatus = 2;
168 }
169 }
170
171 JNIEXPORT jint JNICALL
172 Java_nsk_jvmti_unit_functions_ForceGarbageCollection_gc_GetResult(JNIEnv * env, jclass cls) {
173 return iGlobalStatus;
174 }
175
176
177 JNIEXPORT void JNICALL
178 Java_nsk_jvmti_unit_functions_ForceGarbageCollection_gc_jvmtiForceGC (JNIEnv * env, jclass cls) {
179 jvmtiError ret;
180
181 debug_printf("jvmti Force gc requested \n");
182 ret = jvmti->ForceGarbageCollection();
183
184 if (ret != JVMTI_ERROR_NONE) {
185 printf("Error: ForceGarbageCollection %d \n", ret);
186 iGlobalStatus = 2;
187 }
188 }
189
190 }
|