1 #include <jni.h>
2 
3 #include <jvmti.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 
7 #include <unistd.h>
8 
9 #include "getstacktrace.h"
10 
11 void
printStackTrace(jvmtiFrameInfo * frames,jint frame_cnt)12 printStackTrace (jvmtiFrameInfo *frames, jint frame_cnt)
13 {
14   printf ("Thread has %d frames\n", static_cast<int> (frame_cnt));
15 
16   for (int i = 0; i < frame_cnt; i++)
17     {
18       jmethodID method = frames[i].method;
19       jlocation location = frames[i].location;
20 
21       if (location == -1)
22         {
23           printf ("Frame %d is native\n", i);
24         }
25       else
26         {
27           printf ("Frame %d is interpreted\n", i);
28         }
29     }
30 }
31 
32 
Java_getstacktrace_natPlaceholder(JNIEnv * env,jobject obj)33 JNIEXPORT void JNICALL Java_getstacktrace_natPlaceholder (JNIEnv *env, jobject obj)
34 {
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");
39 
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);
44 
45   jint t_num = env->GetIntField (obj, thread_num_id);
46 
47   if (n_frames <= 1)
48     {
49       if (t_num % 2 == 1)
50         {
51           jmethodID natRunner_id = env->GetMethodID (klass, "natRunner", "()V");
52           env->CallVoidMethod (obj, natRunner_id);
53         }
54       else
55         {
56           jmethodID runner_id = env->GetMethodID (klass, "runner", "()V");
57           env->CallVoidMethod (obj, runner_id);
58         }
59     }
60   else
61     {
62       if (t_num % 2 == 0)
63         {
64           jmethodID natPlaceholder_id = env->GetMethodID (klass,
65                                         "natPlaceholder",
66                                         "()V");
67           env->CallVoidMethod (obj, natPlaceholder_id);
68         }
69       else
70         {
71           jmethodID placeholder_id = env->GetMethodID (klass, "placeholder",
72                                      "()V");
73           env->CallVoidMethod (obj, placeholder_id);
74         }
75     }
76 }
77 
Java_getstacktrace_natRunner(JNIEnv * env,jobject obj)78 JNIEXPORT void JNICALL Java_getstacktrace_natRunner (JNIEnv *env, jobject obj)
79 {
80   jclass klass = env->GetObjectClass (obj);
81   jfieldID done_id = env->GetFieldID (klass, "done", "Z");
82 
83 
84   jboolean done;
85   done = true;
86   env->SetBooleanField (obj, done_id, done);
87 
88   do
89     {
90       done = env->GetBooleanField (obj, done_id);
91       if (done == false)
92         break;
93       usleep (40);
94     }
95   while (done != false);
96 }
97 
Java_getstacktrace_do_1getstacktrace_1tests(JNIEnv * env,jclass klass,jobjectArray thr_arr)98 JNIEXPORT jint JNICALL Java_getstacktrace_do_1getstacktrace_1tests
99 (JNIEnv *env, jclass klass, jobjectArray thr_arr)
100 {
101   JavaVM *vm;
102   jint err = env->GetJavaVM (&vm);
103   if (err < 0)
104     {
105       fprintf (stderr, "error getting VM\n");
106       exit (1);
107     }
108 
109   jvmtiEnv *jvmti = NULL;
110   vm->GetEnv ((void **) &jvmti, JVMTI_VERSION_1_0);
111 
112   if (jvmti == NULL)
113     {
114       fprintf (stderr, "error getting jvmti environment\n");
115       exit (1);
116     }
117 
118   jint frame_cnt;
119   jvmtiFrameInfo frames[30];
120 
121   jvmtiError jerr;
122   jthread thr;
123 
124   jsize num_threads = env->GetArrayLength (thr_arr);
125 
126   for (int i = 0; i < num_threads; i++)
127     {
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)
133         {
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));
138         }
139       else
140         {
141           printStackTrace (frames, frame_cnt);
142         }
143     }
144 }
145