2008-02-15 Andreas Tobler <a.tobler@schweiz.org>
[official-gcc.git] / libjava / testsuite / libjava.jvmti / interp / natgetstacktrace.cc
blobcfd7c48c063be696b5ec05e0ca4d2a874be777c9
1 #include <jni.h>
3 #include <jvmti.h>
4 #include <stdio.h>
5 #include <stdlib.h>
7 #include <unistd.h>
9 #include "getstacktrace.h"
11 void
12 printStackTrace (jvmtiFrameInfo *frames, jint frame_cnt)
14 printf ("Thread has %d frames\n", static_cast<int> (frame_cnt));
16 for (int i = 0; i < frame_cnt; i++)
18 jmethodID method = frames[i].method;
19 jlocation location = frames[i].location;
21 if (location == -1)
23 printf ("Frame %d is native\n", i);
25 else
27 printf ("Frame %d is interpreted\n", i);
33 JNIEXPORT void JNICALL Java_getstacktrace_natPlaceholder (JNIEnv *env, jobject obj)
35 jclass klass = env->GetObjectClass (obj);
36 jfieldID done_id = env->GetFieldID (klass, "done", "Z");
37 jfieldID num_frames_id = env->GetFieldID (klass, "num_frames", "I");
38 jfieldID thread_num_id = env->GetFieldID (klass, "thread_num", "I");
40 // num_frames--
41 jint n_frames = env->GetIntField (obj, num_frames_id);
42 n_frames--;
43 env->SetIntField (obj, num_frames_id, n_frames);
45 jint t_num = env->GetIntField (obj, thread_num_id);
47 if (n_frames <= 1)
49 if (t_num % 2 == 1)
51 jmethodID natRunner_id = env->GetMethodID (klass, "natRunner", "()V");
52 env->CallVoidMethod (obj, natRunner_id);
54 else
56 jmethodID runner_id = env->GetMethodID (klass, "runner", "()V");
57 env->CallVoidMethod (obj, runner_id);
60 else
62 if (t_num % 2 == 0)
64 jmethodID natPlaceholder_id = env->GetMethodID (klass,
65 "natPlaceholder",
66 "()V");
67 env->CallVoidMethod (obj, natPlaceholder_id);
69 else
71 jmethodID placeholder_id = env->GetMethodID (klass, "placeholder",
72 "()V");
73 env->CallVoidMethod (obj, placeholder_id);
78 JNIEXPORT void JNICALL Java_getstacktrace_natRunner (JNIEnv *env, jobject obj)
80 jclass klass = env->GetObjectClass (obj);
81 jfieldID done_id = env->GetFieldID (klass, "done", "Z");
84 jboolean done;
85 done = true;
86 env->SetBooleanField (obj, done_id, done);
90 done = env->GetBooleanField (obj, done_id);
91 if (done == false)
92 break;
93 usleep (40);
95 while (done != false);
98 JNIEXPORT jint JNICALL Java_getstacktrace_do_1getstacktrace_1tests
99 (JNIEnv *env, jclass klass, jobjectArray thr_arr)
101 JavaVM *vm;
102 jint err = env->GetJavaVM (&vm);
103 if (err < 0)
105 fprintf (stderr, "error getting VM\n");
106 exit (1);
109 jvmtiEnv *jvmti = NULL;
110 vm->GetEnv ((void **) &jvmti, JVMTI_VERSION_1_0);
112 if (jvmti == NULL)
114 fprintf (stderr, "error getting jvmti environment\n");
115 exit (1);
118 jint frame_cnt;
119 jvmtiFrameInfo frames[30];
121 jvmtiError jerr;
122 jthread thr;
124 jsize num_threads = env->GetArrayLength (thr_arr);
126 for (int i = 0; i < num_threads; i++)
128 thr = reinterpret_cast<jthread>
129 (env->GetObjectArrayElement (thr_arr, static_cast<jsize> (i)));
130 fflush (stdout);
131 jerr = jvmti->GetStackTrace (thr, 0, 30, frames, &frame_cnt);
132 if (jerr != JVMTI_ERROR_NONE)
134 char *error_name;
135 jvmti->GetErrorName (jerr, &error_name);
136 fprintf (stderr, "JVMTI Error: %s\n", error_name);
137 jvmti->Deallocate (reinterpret_cast<unsigned char *> (error_name));
139 else
141 printStackTrace (frames, frame_cnt);