natgetstacktrace.cc 3.5 KB

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