StackTrace.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "il2cpp-config.h"
  2. #if IL2CPP_TARGET_POSIX && !IL2CPP_TARGET_JAVASCRIPT && !IL2CPP_TARGET_ANDROID && !IL2CPP_TARGET_QNX && !IL2CPP_USE_PLATFORM_SPECIFIC_STACKTRACE
  3. #include "os/StackTrace.h"
  4. #include <execinfo.h>
  5. #include <cxxabi.h>
  6. #include <cstdlib>
  7. namespace il2cpp
  8. {
  9. namespace os
  10. {
  11. const int kMaxStackFrames = 128;
  12. void StackTrace::WalkStackNative(WalkStackCallback callback, void* context, WalkOrder walkOrder)
  13. {
  14. void* callstack[kMaxStackFrames];
  15. int frames = backtrace(callstack, kMaxStackFrames);
  16. if (walkOrder == kFirstCalledToLastCalled)
  17. {
  18. for (int i = frames - 1; i >= 0; i--)
  19. {
  20. if (!callback(reinterpret_cast<Il2CppMethodPointer>(callstack[i]), context))
  21. break;
  22. }
  23. }
  24. else
  25. {
  26. for (int i = 0; i < frames; i++)
  27. {
  28. if (!callback(reinterpret_cast<Il2CppMethodPointer>(callstack[i]), context))
  29. break;
  30. }
  31. }
  32. }
  33. std::string StackTrace::NativeStackTrace()
  34. {
  35. void* callstack[kMaxStackFrames];
  36. int frames = backtrace(callstack, kMaxStackFrames);
  37. char **symbols = backtrace_symbols(callstack, frames);
  38. std::string stackTrace;
  39. if (symbols != NULL)
  40. {
  41. for (int i = 0; i < frames; ++i)
  42. {
  43. stackTrace += symbols[i];
  44. stackTrace += "\n";
  45. }
  46. free(symbols);
  47. }
  48. return stackTrace;
  49. }
  50. const void* StackTrace::GetStackPointer()
  51. {
  52. // TODO implement to avoid extra WalkStack calls
  53. return nullptr;
  54. }
  55. }
  56. }
  57. #endif