StackTrace.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include "il2cpp-config.h"
  2. #if IL2CPP_TARGET_ANDROID
  3. #include "os/StackTrace.h"
  4. #include "os/Image.h"
  5. #include <unwind.h>
  6. #include <dlfcn.h>
  7. #include <pthread.h>
  8. #include <string.h>
  9. namespace il2cpp
  10. {
  11. namespace os
  12. {
  13. const int kMaxStackFrames = 128;
  14. namespace
  15. {
  16. extern "C" char end;
  17. uintptr_t s_BaseAddress;
  18. uintptr_t s_EndAddress;
  19. pthread_once_t s_InitKnownSymbolInfoOnceFlag = PTHREAD_ONCE_INIT;
  20. static void InitKnownSymbolInfo()
  21. {
  22. s_BaseAddress = reinterpret_cast<uintptr_t>(os::Image::GetImageBase());
  23. s_EndAddress = reinterpret_cast<uintptr_t>(&end);
  24. }
  25. static bool KnownSymbol(const uintptr_t addr)
  26. {
  27. pthread_once(&s_InitKnownSymbolInfoOnceFlag, &InitKnownSymbolInfo);
  28. if (addr >= s_BaseAddress && addr <= s_EndAddress)
  29. return true;
  30. Dl_info info;
  31. if (!dladdr(reinterpret_cast<void*>(addr), &info))
  32. return false;
  33. // dli_name can have different values depending on Android OS:
  34. // Google Pixel 2 Android 10, dli_name will be "/data/app/com.unity.stopaskingforpackagename-uRHSDLXYA4cnHxyTNT30-g==/lib/arm/libunity.so"
  35. // Samsung GT-I9505 Android 5, dli_name will be "libunity.so"
  36. return info.dli_fname != NULL && strstr(info.dli_fname, "libunity.so") != NULL;
  37. }
  38. struct AndroidStackTrace
  39. {
  40. size_t size;
  41. Il2CppMethodPointer addrs[kMaxStackFrames];
  42. bool PushStackFrameAddress(const uintptr_t addr)
  43. {
  44. if (size >= kMaxStackFrames)
  45. return false;
  46. addrs[size++] = reinterpret_cast<Il2CppMethodPointer>(addr);
  47. return true;
  48. }
  49. static _Unwind_Reason_Code Callback(struct _Unwind_Context* context, void* self)
  50. {
  51. const uintptr_t addr = _Unwind_GetIP(context);
  52. // Workaround to avoid crash when generating stack trace in some third-party libraries
  53. if (!KnownSymbol(addr))
  54. return _URC_END_OF_STACK;
  55. if (static_cast<AndroidStackTrace*>(self)->PushStackFrameAddress(addr))
  56. return _URC_NO_REASON;
  57. else
  58. return _URC_END_OF_STACK;
  59. }
  60. };
  61. }
  62. void StackTrace::WalkStackNative(WalkStackCallback callback, void* context, WalkOrder walkOrder)
  63. {
  64. AndroidStackTrace callstack = {};
  65. _Unwind_Backtrace(AndroidStackTrace::Callback, &callstack);
  66. for (size_t i = 0; i < callstack.size; ++i)
  67. {
  68. const size_t index = (walkOrder == kFirstCalledToLastCalled) ? (callstack.size - i - 1) : i;
  69. if (!callback(callstack.addrs[index], context))
  70. break;
  71. }
  72. }
  73. std::string StackTrace::NativeStackTrace()
  74. {
  75. return std::string();
  76. }
  77. const void* StackTrace::GetStackPointer()
  78. {
  79. return __builtin_frame_address(0);
  80. }
  81. }
  82. }
  83. #endif