From 389bf89e2a105e808178590c0212afe361ee3667 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Sun, 19 Nov 2023 11:07:40 +0100 Subject: [PATCH] Add `GetStackFrames` on Android --- .../Platform/Android/AndroidPlatform.cpp | 68 +++++++++++++++++++ .../Engine/Platform/Android/AndroidPlatform.h | 1 + 2 files changed, 69 insertions(+) diff --git a/Source/Engine/Platform/Android/AndroidPlatform.cpp b/Source/Engine/Platform/Android/AndroidPlatform.cpp index c844ab81d..e71d4534a 100644 --- a/Source/Engine/Platform/Android/AndroidPlatform.cpp +++ b/Source/Engine/Platform/Android/AndroidPlatform.cpp @@ -51,6 +51,33 @@ #include #include +#if CRASH_LOG_ENABLE + +#include +#include + +struct AndroidBacktraceState +{ + void** Current; + void** End; +}; + +_Unwind_Reason_Code AndroidUnwindCallback(struct _Unwind_Context* context, void* arg) +{ + AndroidBacktraceState* state = (AndroidBacktraceState*)arg; + uintptr_t pc = _Unwind_GetIP(context); + if (pc) + { + if (state->Current == state->End) + return _URC_END_OF_STACK; + else + *state->Current++ = reinterpret_cast(pc); + } + return _URC_NO_REASON; +} + +#endif + struct AndroidKeyEventType { uint32_t KeyCode; @@ -1066,4 +1093,45 @@ void* AndroidPlatform::GetProcAddress(void* handle, const char* symbol) return dlsym(handle, symbol); } +Array AndroidPlatform::GetStackFrames(int32 skipCount, int32 maxDepth, void* context) +{ + Array result; +#if CRASH_LOG_ENABLE + // Reference: https://stackoverflow.com/questions/8115192/android-ndk-getting-the-backtrace + void* callstack[120]; + skipCount = Math::Min(skipCount, ARRAY_COUNT(callstack)); + int32 maxCount = Math::Min(ARRAY_COUNT(callstack), skipCount + maxDepth); + AndroidBacktraceState state; + state.Current = callstack; + state.End = callstack + maxCount; + _Unwind_Backtrace(AndroidUnwindCallback, &state); + int32 count = (int32)(state.Current - callstack); + int32 useCount = count - skipCount; + if (useCount > 0) + { + result.Resize(useCount); + for (int32 i = 0; i < useCount; i++) + { + StackFrame& frame = result[i]; + frame.ProgramCounter = callstack[skipCount + i]; + frame.ModuleName[0] = 0; + frame.FileName[0] = 0; + frame.LineNumber = 0; + Dl_info info; + const char* symbol = ""; + if (dladdr(frame.ProgramCounter, &info) && info.dli_sname) + symbol = info.dli_sname; + int status = 0; + char* demangled = __cxxabiv1::__cxa_demangle(symbol, 0, 0, &status); + int32 nameLen = Math::Min(StringUtils::Length(demangled), ARRAY_COUNT(frame.FunctionName) - 1); + Platform::MemoryCopy(frame.FunctionName, demangled, nameLen); + frame.FunctionName[nameLen] = 0; + if (demangled) + free(demangled); + } + } +#endif + return result; +} + #endif diff --git a/Source/Engine/Platform/Android/AndroidPlatform.h b/Source/Engine/Platform/Android/AndroidPlatform.h index 07e72cac4..577da45de 100644 --- a/Source/Engine/Platform/Android/AndroidPlatform.h +++ b/Source/Engine/Platform/Android/AndroidPlatform.h @@ -132,6 +132,7 @@ public: static void* LoadLibrary(const Char* filename); static void FreeLibrary(void* handle); static void* GetProcAddress(void* handle, const char* symbol); + static Array GetStackFrames(int32 skipCount = 0, int32 maxDepth = 60, void* context = nullptr); }; #endif