zege_drive.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // Created by #Suyghur, on 4/6/21.
  3. //
  4. #include <logger.h>
  5. #include "includes/zege_drive.h"
  6. jvmtiEnv *getJvmtiEnv(JavaVM *vm) {
  7. return nullptr;
  8. }
  9. jvmtiEnv *getJvmtiEnvFromJNI(JNIEnv *env) {
  10. return nullptr;
  11. }
  12. JavaVM *getJavaVM(JNIEnv *env) {
  13. return nullptr;
  14. }
  15. /**
  16. * 获取指定对象的大小。对象的大小与JVM的具体实现相关,
  17. * 是该对象所占用存储空间的近似值,可能会包含某些或所有对象的开销,
  18. * 因此对象大小的比较,只在某个JVM实现内有意义,在不同JVM实现之间没有比较意思。
  19. * 对象的大小,在单次调用期间,也可能会发生变化。
  20. */
  21. jlong getObjectSize(JNIEnv *env, jclass jclz, jobject jobj) {
  22. return 0;
  23. }
  24. char *createStackInfo(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thread, int stack_depth) {
  25. char *result = nullptr;
  26. jvmtiFrameInfo frame_info[stack_depth];
  27. jint count;
  28. jvmtiError error;
  29. error = jvmti_env->GetStackTrace(thread, 0, stack_depth, frame_info, &count);
  30. if (error != JVMTI_ERROR_NONE) {
  31. LOGE("jvmti error on GetStackTrace: %i", error);
  32. return result;
  33. }
  34. if (count <= 0) {
  35. return result;
  36. }
  37. for (int i = 0; i < count; i++) {
  38. jvmtiFrameInfo info = frame_info[i];
  39. char *clz_signature = nullptr;
  40. char *method_name = nullptr;
  41. //获取方法名
  42. error = jvmti_env->GetMethodName(info.method, &method_name, nullptr, nullptr);
  43. if (error != JVMTI_ERROR_NONE) {
  44. LOGE("jvmti error on GetMethodName: %i", error);
  45. break;
  46. }
  47. //获取方法所在的类
  48. jclass declaring_clz;
  49. error = jvmti_env->GetMethodDeclaringClass(info.method, &declaring_clz);
  50. if (error != JVMTI_ERROR_NONE) {
  51. LOGE("jvmti error on GetMethodDeclaringClass: %i", error);
  52. break;
  53. }
  54. //获取方法所在类的签名
  55. error = jvmti_env->GetClassSignature(declaring_clz, &clz_signature, nullptr);
  56. if (error != JVMTI_ERROR_NONE) {
  57. LOGE("jvmti error on GetClassSignature: %i", error);
  58. break;
  59. }
  60. if (result == nullptr) {
  61. asprintf(&result, "%s%s%s", clz_signature, "^^^", method_name);
  62. } else {
  63. char *stack = nullptr;
  64. asprintf(&stack, "%s%s%s%s%s", result, ",,,", clz_signature, "^^^", method_name);
  65. free(result);
  66. result = stack;
  67. }
  68. jvmti_env->Deallocate(reinterpret_cast<unsigned char *>(clz_signature));
  69. jvmti_env->Deallocate(reinterpret_cast<unsigned char *>(method_name));
  70. }
  71. return result;
  72. }