Add decoding stack trace function names on Apple platforms
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "String.h"
|
||||
#include "StringView.h"
|
||||
#include "Engine/Core/Collections/Array.h"
|
||||
|
||||
/// <summary>
|
||||
@@ -138,6 +139,11 @@ public:
|
||||
_data.Add(*str, str.Length());
|
||||
return *this;
|
||||
}
|
||||
StringBuilder& Append(const StringView& str)
|
||||
{
|
||||
_data.Add(*str, str.Length());
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Append int to the string
|
||||
// @param val Value to append
|
||||
|
||||
@@ -43,6 +43,7 @@
|
||||
#include <dlfcn.h>
|
||||
#if CRASH_LOG_ENABLE
|
||||
#include <execinfo.h>
|
||||
#include <cxxabi.h>
|
||||
#endif
|
||||
|
||||
CPUInfo Cpu;
|
||||
@@ -502,20 +503,42 @@ Array<ApplePlatform::StackFrame> ApplePlatform::GetStackFrames(int32 skipCount,
|
||||
{
|
||||
char** names = backtrace_symbols(callstack + skipCount, useCount);
|
||||
result.Resize(useCount);
|
||||
Array<StringAnsi> parts;
|
||||
int32 len;
|
||||
#define COPY_STR(str, dst) \
|
||||
len = Math::Min<int32>(str.Length(), ARRAY_COUNT(frame.dst) - 1); \
|
||||
Platform::MemoryCopy(frame.dst, str.Get(), len); \
|
||||
frame.dst[len] = 0
|
||||
for (int32 i = 0; i < useCount; i++)
|
||||
{
|
||||
char* name = names[i];
|
||||
const StringAnsi name(names[i]);
|
||||
StackFrame& frame = result[i];
|
||||
frame.ProgramCounter = callstack[skipCount + i];
|
||||
frame.ModuleName[0] = 0;
|
||||
frame.FileName[0] = 0;
|
||||
frame.LineNumber = 0;
|
||||
int32 nameLen = Math::Min<int32>(StringUtils::Length(name), ARRAY_COUNT(frame.FunctionName) - 1);
|
||||
Platform::MemoryCopy(frame.FunctionName, name, nameLen);
|
||||
frame.FunctionName[nameLen] = 0;
|
||||
|
||||
|
||||
// Decode name
|
||||
parts.Clear();
|
||||
name.Split(' ', parts);
|
||||
if (parts.Count() == 6)
|
||||
{
|
||||
COPY_STR(parts[1], ModuleName);
|
||||
const StringAnsiView toDemangle(parts[3]);
|
||||
int status = 0;
|
||||
char* demangled = __cxxabiv1::__cxa_demangle(*toDemangle, 0, 0, &status);
|
||||
const StringAnsiView toCopy = demangled && status == 0 ? StringAnsiView(demangled) : StringAnsiView(toDemangle);
|
||||
COPY_STR(toCopy, FunctionName);
|
||||
if (demangled)
|
||||
free(demangled);
|
||||
}
|
||||
else
|
||||
{
|
||||
COPY_STR(name, FunctionName);
|
||||
}
|
||||
}
|
||||
free(names);
|
||||
#undef COPY_STR
|
||||
}
|
||||
#endif
|
||||
return result;
|
||||
|
||||
@@ -633,14 +633,21 @@ String PlatformBase::GetStackTrace(int32 skipCount, int32 maxDepth, void* contex
|
||||
for (const auto& frame : stackFrames)
|
||||
{
|
||||
StringAsUTF16<ARRAY_COUNT(StackFrame::FunctionName)> functionName(frame.FunctionName);
|
||||
const StringView functionNameStr(functionName.Get());
|
||||
if (StringUtils::Length(frame.FileName) != 0)
|
||||
{
|
||||
StringAsUTF16<ARRAY_COUNT(StackFrame::FileName)> fileName(frame.FileName);
|
||||
result.AppendFormat(TEXT(" at {0}() in {1}:line {2}\n"), functionName.Get(), fileName.Get(), frame.LineNumber);
|
||||
result.Append(TEXT(" at ")).Append(functionNameStr);
|
||||
if (!functionNameStr.EndsWith(')'))
|
||||
result.Append(TEXT("()"));
|
||||
result.AppendFormat(TEXT("in {0}:line {1}\n"),fileName.Get(), frame.LineNumber);
|
||||
}
|
||||
else if (StringUtils::Length(frame.FunctionName) != 0)
|
||||
{
|
||||
result.AppendFormat(TEXT(" at {0}()\n"), functionName.Get());
|
||||
result.Append(TEXT(" at ")).Append(functionNameStr);
|
||||
if (!functionNameStr.EndsWith(')'))
|
||||
result.Append(TEXT("()"));
|
||||
result.Append('\n');
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user