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 "jni_tools.h"
28 #include "agent_common.h"
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 #ifndef JNI_ENV_ARG
35
36 #ifdef __cplusplus
37 #define JNI_ENV_ARG(x, y) y
38 #define JNI_ENV_ARG1(x)
39 #define JNI_ENV_PTR(x) x
40 #else
41 #define JNI_ENV_ARG(x,y) x, y
42 #define JNI_ENV_ARG1(x) x
43 #define JNI_ENV_PTR(x) (*x)
44 #endif
45
46 #endif
47
48 #define JVMTI_ENV_ARG JNI_ENV_ARG
49 #define JVMTI_ENV_ARG1 JNI_ENV_ARG1
50 #define JVMTI_ENV_PTR JNI_ENV_PTR
51
52 #define STATUS_FAILED 2
53 #define PASSED 0
54
55 #define JVMTI_ERROR_CHECK(str,res) \
56 if ( res != JVMTI_ERROR_NONE) { \
57 printf("%s %d\n" ,str, res); \
58 return res; \
59 }
60
61 #define JVMTI_ERROR_CHECK_EXPECTED_ERROR(str,res,err) \
62 if ( res != err) { \
63 printf("%s unexpected error %d\n", str, res); \
64 return res; \
65 }
66
67 #define JVMTI_ERROR_CHECK_VOID(str,res) \
68 if (res != JVMTI_ERROR_NONE) { \
69 printf("%s %d\n" ,str, res); \
70 iGlobalStatus = STATUS_FAILED; \
71 }
72
73 #define JVMTI_ERROR_CHECK_EXPECTED_ERROR_VOID(str,res,err) \
74 if (res != err) { \
75 printf("%s unexpected error %d\n",str, res); \
76 iGlobalStatus = STATUS_FAILED; \
77 }
78
79
80 static jvmtiEnv *jvmti;
81 static jint iGlobalStatus = PASSED;
82 static jvmtiCapabilities jvmti_caps;
88 static jvmtiStackInfo *stack_buf2 = NULL;
89 static jthread *thread_list = NULL;
90 static jvmtiThreadInfo *thread_info = NULL;
91 static jint threads_count = 0;
92
93
94 #ifdef STATIC_BUILD
95 JNIEXPORT jint JNICALL Agent_OnLoad_getallstktr001(JavaVM *jvm, char *options, void *reserved) {
96 return Agent_Initialize(jvm, options, reserved);
97 }
98 JNIEXPORT jint JNICALL Agent_OnAttach_getallstktr001(JavaVM *jvm, char *options, void *reserved) {
99 return Agent_Initialize(jvm, options, reserved);
100 }
101 JNIEXPORT jint JNI_OnLoad_getallstktr001(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 res = JNI_ENV_PTR(jvm)->
109 GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti), JVMTI_VERSION_1_1);
110 if (res < 0) {
111 printf("Wrong result of a valid call to GetEnv!\n");
112 return JNI_ERR;
113 }
114
115 /* Add capabilities */
116 res = JVMTI_ENV_PTR(jvmti)->GetPotentialCapabilities(JVMTI_ENV_ARG(jvmti, &jvmti_caps));
117 JVMTI_ERROR_CHECK("GetPotentialCapabilities returned error", res);
118
119 res = JVMTI_ENV_PTR(jvmti)->AddCapabilities(JVMTI_ENV_ARG(jvmti, &jvmti_caps));
120 JVMTI_ERROR_CHECK("GetPotentialCapabilities returned error", res);
121
122 return JNI_OK;
123 }
124
125
126 JNIEXPORT jint JNICALL
127 Java_nsk_jvmti_unit_GetAllStackTraces_getallstktr001_GetResult(
128 JNIEnv * env, jclass cls)
129 {
130 return iGlobalStatus;
131 }
132
133
134 JNIEXPORT void JNICALL
135 Java_nsk_jvmti_unit_GetAllStackTraces_getallstktr001_CreateRawMonitor(
136 JNIEnv * env, jclass cls)
137 {
138 jvmtiError ret;
139 char sz[128];
140
141 sprintf(sz, "Raw-monitor");
142 ret = JVMTI_ENV_PTR(jvmti)->CreateRawMonitor(JVMTI_ENV_ARG(jvmti, sz),
143 &jraw_monitor);
144
145 if (ret != JVMTI_ERROR_NONE) {
146 printf("Error: Raw monitor create %d \n", ret);
147 iGlobalStatus = STATUS_FAILED;
148 }
149 }
150
151 JNIEXPORT void JNICALL
152 Java_nsk_jvmti_unit_GetAllStackTraces_getallstktr001_RawMonitorEnter(
153 JNIEnv * env, jclass cls)
154 {
155 jvmtiError ret;
156
157 ret = JVMTI_ENV_PTR(jvmti)->RawMonitorEnter(JVMTI_ENV_ARG(jvmti, jraw_monitor));
158
159 if (ret != JVMTI_ERROR_NONE) {
160 printf("Error: Raw monitor enter %d \n", ret);
161 iGlobalStatus = STATUS_FAILED;
162 }
163 }
164
165 JNIEXPORT void JNICALL
166 Java_nsk_jvmti_unit_GetAllStackTraces_getallstktr001_RawMonitorExit(
167 JNIEnv * env, jclass cls)
168 {
169 jvmtiError ret;
170
171 ret = JVMTI_ENV_PTR(jvmti)->RawMonitorExit(JVMTI_ENV_ARG(jvmti, jraw_monitor));
172
173 if (ret != JVMTI_ERROR_NONE) {
174 printf("Error: RawMonitorExit %d \n", ret);
175 iGlobalStatus = STATUS_FAILED;
176 }
177 }
178
179 void compare_all_frames(int ti, int frames_count,
180 jvmtiFrameInfo *fr_buf1,
181 jvmtiFrameInfo *fr_buf2)
182 {
183 int fi;
184 jvmtiFrameInfo *fr1, *fr2;
185
186 for (fi = 0; fi < frames_count; fi++) {
187 fr1 = &fr_buf1[fi];
188 fr2 = &fr_buf2[fi];
189 if (fr1->method != fr2->method) {
190 printf("FAILED: compare frame: thread %d: frame %d: "
191 "different methods", ti, fi);
256
257 void compare_all_stack_traces(int thr_count,
258 jvmtiStackInfo *stk_buf1,
259 jvmtiStackInfo *stk_buf2,
260 jvmtiThreadInfo *thr_info)
261 {
262 int ti;
263 for (ti = 0; ti < thr_count; ti++) {
264 compare_one_stack_trace(ti, &stk_buf1[ti], &stk_buf2[ti], &thr_info[ti]);
265 }
266 }
267
268
269 JNIEXPORT void JNICALL
270 Java_nsk_jvmti_unit_GetAllStackTraces_getallstktr001_GetAllStackTraces(
271 JNIEnv * env, jclass cls)
272 {
273 jvmtiError ret;
274 int ti;
275
276 ret = JVMTI_ENV_PTR(jvmti)->GetAllStackTraces(JVMTI_ENV_ARG(jvmti, MAX_FRAMES_CNT),
277 &stack_buf1,
278 &threads_count);
279 if (ret != JVMTI_ERROR_NONE) {
280 printf("Error: GetAllStackTraces %d \n", ret);
281 iGlobalStatus = STATUS_FAILED;
282 }
283
284 ret = JVMTI_ENV_PTR(jvmti)->Allocate(JVMTI_ENV_ARG(jvmti, sizeof(jthread) * threads_count),
285 (unsigned char**)&thread_list);
286 if (ret != JVMTI_ERROR_NONE) {
287 printf("Error: Allocate failed with %d \n", ret);
288 iGlobalStatus = STATUS_FAILED;
289 }
290
291 for (ti = 0; ti < threads_count; ti++) {
292 thread_list[ti] =
293 (jthread)JNI_ENV_PTR(env)->NewGlobalRef(
294 JNI_ENV_ARG(env, stack_buf1[ti].thread));
295 }
296 }
297
298 JNIEXPORT void JNICALL
299 Java_nsk_jvmti_unit_GetAllStackTraces_getallstktr001_GetThreadsInfo(
300 JNIEnv * env, jclass cls)
301 {
302 jvmtiError ret;
303 int ti;
304
305 ret = JVMTI_ENV_PTR(jvmti)->Allocate(JVMTI_ENV_ARG(jvmti, sizeof(jvmtiThreadInfo) * threads_count),
306 (unsigned char**)&thread_info);
307 if (ret != JVMTI_ERROR_NONE) {
308 printf("Error: Allocate failed with %d \n", ret);
309 iGlobalStatus = STATUS_FAILED;
310 }
311
312 for (ti = 0; ti < threads_count; ti++) {
313 ret = JVMTI_ENV_PTR(jvmti)->GetThreadInfo(JVMTI_ENV_ARG(jvmti, thread_list[ti]),
314 &thread_info[ti]);
315 if (ret != JVMTI_ERROR_NONE) {
316 printf("Error: GetThreadInfo %d \n", ret);
317 iGlobalStatus = STATUS_FAILED;
318 }
319 printf("GetThreadInfo %d: thread: %s\n", ti, thread_info[ti].name);
320 fflush(0);
321 }
322 }
323
324 JNIEXPORT void JNICALL
325 Java_nsk_jvmti_unit_GetAllStackTraces_getallstktr001_GetThreadListStackTraces(
326 JNIEnv * env, jclass cls)
327 {
328 jvmtiError ret;
329
330 ret = JVMTI_ENV_PTR(jvmti)->GetThreadListStackTraces(
331 JVMTI_ENV_ARG(jvmti, threads_count),
332 thread_list,
333 MAX_FRAMES_CNT,
334 &stack_buf2);
335 if (ret != JVMTI_ERROR_NONE) {
336 printf("Error: GetThreadListStackTraces %d \n", ret);
337 iGlobalStatus = STATUS_FAILED;
338 }
339
340 }
341
342 JNIEXPORT void JNICALL
343 Java_nsk_jvmti_unit_GetAllStackTraces_getallstktr001_ForceGC(
344 JNIEnv * env, jclass cls)
345 {
346 jvmtiError ret;
347 ret = JVMTI_ENV_PTR(jvmti)->ForceGarbageCollection(JVMTI_ENV_ARG1(jvmti));
348
349 if (ret != JVMTI_ERROR_NONE) {
350 printf("Error: ForceGarbageCollection %d \n", ret);
351 iGlobalStatus = STATUS_FAILED;
352 }
353 }
354
355 JNIEXPORT void JNICALL
356 Java_nsk_jvmti_unit_GetAllStackTraces_getallstktr001_CompareStackTraces(
357 JNIEnv * env, jclass cls)
358 {
359 compare_all_stack_traces(threads_count, stack_buf1, stack_buf2, thread_info);
360 }
361
362 JNIEXPORT void JNICALL
363 Java_nsk_jvmti_unit_GetAllStackTraces_getallstktr001_DeallocateBuffers(
364 JNIEnv * env, jclass cls)
365 {
366 jvmtiError ret;
367
368 ret = JVMTI_ENV_PTR(jvmti)->Deallocate(JVMTI_ENV_ARG(jvmti, (unsigned char *)stack_buf1));
369 if (ret != JVMTI_ERROR_NONE) {
370 printf("Error: Deallocate stack_buf1 failed with %d \n", ret);
371 iGlobalStatus = STATUS_FAILED;
372 }
373
374 ret = JVMTI_ENV_PTR(jvmti)->Deallocate(JVMTI_ENV_ARG(jvmti, (unsigned char *)stack_buf2));
375 if (ret != JVMTI_ERROR_NONE) {
376 printf("Error: Deallocate stack_buf2 failed with %d \n", ret);
377 iGlobalStatus = STATUS_FAILED;
378 }
379
380 ret = JVMTI_ENV_PTR(jvmti)->Deallocate(JVMTI_ENV_ARG(jvmti, (unsigned char *)thread_info));
381 if (ret != JVMTI_ERROR_NONE) {
382 printf("Error: Deallocate thread_info failed with %d \n", ret);
383 iGlobalStatus = STATUS_FAILED;
384 }
385 ret = JVMTI_ENV_PTR(jvmti)->Deallocate(JVMTI_ENV_ARG(jvmti, (unsigned char *)thread_list));
386 if (ret != JVMTI_ERROR_NONE) {
387 printf("Error: Deallocate thread_list failed with %d \n", ret);
388 iGlobalStatus = STATUS_FAILED;
389 }
390 }
391
392 #ifdef __cplusplus
393 }
394 #endif
|
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 "jni_tools.h"
28 #include "agent_common.h"
29
30 extern "C" {
31
32 #define STATUS_FAILED 2
33 #define PASSED 0
34
35 #define JVMTI_ERROR_CHECK(str,res) \
36 if (res != JVMTI_ERROR_NONE) { \
37 printf("%s %d\n" ,str, res); \
38 return res; \
39 }
40
41 #define JVMTI_ERROR_CHECK_EXPECTED_ERROR(str,res,err) \
42 if (res != err) { \
43 printf("%s unexpected error %d\n", str, res); \
44 return res; \
45 }
46
47 #define JVMTI_ERROR_CHECK_VOID(str,res) \
48 if (res != JVMTI_ERROR_NONE) { \
49 printf("%s %d\n" ,str, res); \
50 iGlobalStatus = STATUS_FAILED; \
51 }
52
53 #define JVMTI_ERROR_CHECK_EXPECTED_ERROR_VOID(str,res,err) \
54 if (res != err) { \
55 printf("%s unexpected error %d\n",str, res); \
56 iGlobalStatus = STATUS_FAILED; \
57 }
58
59
60 static jvmtiEnv *jvmti;
61 static jint iGlobalStatus = PASSED;
62 static jvmtiCapabilities jvmti_caps;
68 static jvmtiStackInfo *stack_buf2 = NULL;
69 static jthread *thread_list = NULL;
70 static jvmtiThreadInfo *thread_info = NULL;
71 static jint threads_count = 0;
72
73
74 #ifdef STATIC_BUILD
75 JNIEXPORT jint JNICALL Agent_OnLoad_getallstktr001(JavaVM *jvm, char *options, void *reserved) {
76 return Agent_Initialize(jvm, options, reserved);
77 }
78 JNIEXPORT jint JNICALL Agent_OnAttach_getallstktr001(JavaVM *jvm, char *options, void *reserved) {
79 return Agent_Initialize(jvm, options, reserved);
80 }
81 JNIEXPORT jint JNI_OnLoad_getallstktr001(JavaVM *jvm, char *options, void *reserved) {
82 return JNI_VERSION_1_8;
83 }
84 #endif
85 jint Agent_Initialize(JavaVM * jvm, char *options, void *reserved) {
86 jint res;
87
88 res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);
89 if (res < 0) {
90 printf("Wrong result of a valid call to GetEnv!\n");
91 return JNI_ERR;
92 }
93
94 /* Add capabilities */
95 res = jvmti->GetPotentialCapabilities(&jvmti_caps);
96 JVMTI_ERROR_CHECK("GetPotentialCapabilities returned error", res);
97
98 res = jvmti->AddCapabilities(&jvmti_caps);
99 JVMTI_ERROR_CHECK("GetPotentialCapabilities returned error", res);
100
101 return JNI_OK;
102 }
103
104
105 JNIEXPORT jint JNICALL
106 Java_nsk_jvmti_unit_GetAllStackTraces_getallstktr001_GetResult(
107 JNIEnv * env, jclass cls)
108 {
109 return iGlobalStatus;
110 }
111
112
113 JNIEXPORT void JNICALL
114 Java_nsk_jvmti_unit_GetAllStackTraces_getallstktr001_CreateRawMonitor(
115 JNIEnv * env, jclass cls)
116 {
117 jvmtiError ret;
118 char sz[128];
119
120 sprintf(sz, "Raw-monitor");
121 ret = jvmti->CreateRawMonitor(sz, &jraw_monitor);
122
123 if (ret != JVMTI_ERROR_NONE) {
124 printf("Error: Raw monitor create %d \n", ret);
125 iGlobalStatus = STATUS_FAILED;
126 }
127 }
128
129 JNIEXPORT void JNICALL
130 Java_nsk_jvmti_unit_GetAllStackTraces_getallstktr001_RawMonitorEnter(
131 JNIEnv * env, jclass cls)
132 {
133 jvmtiError ret;
134
135 ret = jvmti->RawMonitorEnter(jraw_monitor);
136
137 if (ret != JVMTI_ERROR_NONE) {
138 printf("Error: Raw monitor enter %d \n", ret);
139 iGlobalStatus = STATUS_FAILED;
140 }
141 }
142
143 JNIEXPORT void JNICALL
144 Java_nsk_jvmti_unit_GetAllStackTraces_getallstktr001_RawMonitorExit(
145 JNIEnv * env, jclass cls)
146 {
147 jvmtiError ret;
148
149 ret = jvmti->RawMonitorExit(jraw_monitor);
150
151 if (ret != JVMTI_ERROR_NONE) {
152 printf("Error: RawMonitorExit %d \n", ret);
153 iGlobalStatus = STATUS_FAILED;
154 }
155 }
156
157 void compare_all_frames(int ti, int frames_count,
158 jvmtiFrameInfo *fr_buf1,
159 jvmtiFrameInfo *fr_buf2)
160 {
161 int fi;
162 jvmtiFrameInfo *fr1, *fr2;
163
164 for (fi = 0; fi < frames_count; fi++) {
165 fr1 = &fr_buf1[fi];
166 fr2 = &fr_buf2[fi];
167 if (fr1->method != fr2->method) {
168 printf("FAILED: compare frame: thread %d: frame %d: "
169 "different methods", ti, fi);
234
235 void compare_all_stack_traces(int thr_count,
236 jvmtiStackInfo *stk_buf1,
237 jvmtiStackInfo *stk_buf2,
238 jvmtiThreadInfo *thr_info)
239 {
240 int ti;
241 for (ti = 0; ti < thr_count; ti++) {
242 compare_one_stack_trace(ti, &stk_buf1[ti], &stk_buf2[ti], &thr_info[ti]);
243 }
244 }
245
246
247 JNIEXPORT void JNICALL
248 Java_nsk_jvmti_unit_GetAllStackTraces_getallstktr001_GetAllStackTraces(
249 JNIEnv * env, jclass cls)
250 {
251 jvmtiError ret;
252 int ti;
253
254 ret = jvmti->GetAllStackTraces(MAX_FRAMES_CNT, &stack_buf1, &threads_count);
255 if (ret != JVMTI_ERROR_NONE) {
256 printf("Error: GetAllStackTraces %d \n", ret);
257 iGlobalStatus = STATUS_FAILED;
258 }
259
260 ret = jvmti->Allocate(sizeof(jthread) * threads_count,
261 (unsigned char**) &thread_list);
262 if (ret != JVMTI_ERROR_NONE) {
263 printf("Error: Allocate failed with %d \n", ret);
264 iGlobalStatus = STATUS_FAILED;
265 }
266
267 for (ti = 0; ti < threads_count; ti++) {
268 thread_list[ti] =
269 (jthread)env->NewGlobalRef(stack_buf1[ti].thread);
270 }
271 }
272
273 JNIEXPORT void JNICALL
274 Java_nsk_jvmti_unit_GetAllStackTraces_getallstktr001_GetThreadsInfo(
275 JNIEnv * env, jclass cls)
276 {
277 jvmtiError ret;
278 int ti;
279
280 ret = jvmti->Allocate(sizeof(jvmtiThreadInfo) * threads_count,
281 (unsigned char**)&thread_info);
282 if (ret != JVMTI_ERROR_NONE) {
283 printf("Error: Allocate failed with %d \n", ret);
284 iGlobalStatus = STATUS_FAILED;
285 }
286
287 for (ti = 0; ti < threads_count; ti++) {
288 ret = jvmti->GetThreadInfo(thread_list[ti], &thread_info[ti]);
289 if (ret != JVMTI_ERROR_NONE) {
290 printf("Error: GetThreadInfo %d \n", ret);
291 iGlobalStatus = STATUS_FAILED;
292 }
293 printf("GetThreadInfo %d: thread: %s\n", ti, thread_info[ti].name);
294 fflush(0);
295 }
296 }
297
298 JNIEXPORT void JNICALL
299 Java_nsk_jvmti_unit_GetAllStackTraces_getallstktr001_GetThreadListStackTraces(
300 JNIEnv * env, jclass cls)
301 {
302 jvmtiError ret;
303
304 ret = jvmti->GetThreadListStackTraces(
305 threads_count, thread_list, MAX_FRAMES_CNT, &stack_buf2);
306 if (ret != JVMTI_ERROR_NONE) {
307 printf("Error: GetThreadListStackTraces %d \n", ret);
308 iGlobalStatus = STATUS_FAILED;
309 }
310
311 }
312
313 JNIEXPORT void JNICALL
314 Java_nsk_jvmti_unit_GetAllStackTraces_getallstktr001_ForceGC(
315 JNIEnv * env, jclass cls)
316 {
317 jvmtiError ret;
318 ret = jvmti->ForceGarbageCollection();
319
320 if (ret != JVMTI_ERROR_NONE) {
321 printf("Error: ForceGarbageCollection %d \n", ret);
322 iGlobalStatus = STATUS_FAILED;
323 }
324 }
325
326 JNIEXPORT void JNICALL
327 Java_nsk_jvmti_unit_GetAllStackTraces_getallstktr001_CompareStackTraces(
328 JNIEnv * env, jclass cls)
329 {
330 compare_all_stack_traces(threads_count, stack_buf1, stack_buf2, thread_info);
331 }
332
333 JNIEXPORT void JNICALL
334 Java_nsk_jvmti_unit_GetAllStackTraces_getallstktr001_DeallocateBuffers(
335 JNIEnv * env, jclass cls)
336 {
337 jvmtiError ret;
338
339 ret = jvmti->Deallocate((unsigned char *) stack_buf1);
340 if (ret != JVMTI_ERROR_NONE) {
341 printf("Error: Deallocate stack_buf1 failed with %d \n", ret);
342 iGlobalStatus = STATUS_FAILED;
343 }
344
345 ret = jvmti->Deallocate((unsigned char *) stack_buf2);
346 if (ret != JVMTI_ERROR_NONE) {
347 printf("Error: Deallocate stack_buf2 failed with %d \n", ret);
348 iGlobalStatus = STATUS_FAILED;
349 }
350
351 ret = jvmti->Deallocate((unsigned char *) thread_info);
352 if (ret != JVMTI_ERROR_NONE) {
353 printf("Error: Deallocate thread_info failed with %d \n", ret);
354 iGlobalStatus = STATUS_FAILED;
355 }
356 ret = jvmti->Deallocate((unsigned char *) thread_list);
357 if (ret != JVMTI_ERROR_NONE) {
358 printf("Error: Deallocate thread_list failed with %d \n", ret);
359 iGlobalStatus = STATUS_FAILED;
360 }
361 }
362
363 }
|