#ifndef MRT_STACK_METADATA_H
#define MRT_STACK_METADATA_H
#include "Base/Macros.h"
#include "Base/Types.h"
#include "Common/TypeDef.h"
#include "MangleNameHelper.h"
#include "ObjectModel/MFuncdesc.h"
namespace MapleRuntime {
class StackMetadataHelper {
public:
explicit StackMetadataHelper(const uint32_t* ip, const uint32_t* startPC, uint64_t* funcDesc)
: funcPC(ip), funcStartAddress(reinterpret_cast<uintptr_t>(startPC)), funcDesc(funcDesc)
{
mangleNameHelper = new (std::nothrow)
MangleNameHelper(reinterpret_cast<FuncDescRef>(funcDesc)->GetFuncName(),
StackTraceFormatFlag(reinterpret_cast<FuncDescRef>(funcDesc)->GetStackTraceFormat()));
}
explicit StackMetadataHelper(const FrameInfo& frameInfo)
: funcPC(frameInfo.mFrame.GetIP()), funcStartAddress(reinterpret_cast<uintptr_t>(frameInfo.GetFuncStartPC()))
{
#ifdef __APPLE__
FuncDescRef tmpFuncDesc = MFuncDesc::GetFuncDesc(frameInfo.mFrame.GetFA());
#else
FuncDescRef tmpFuncDesc = MFuncDesc::GetFuncDesc(reinterpret_cast<Uptr>(frameInfo.GetFuncStartPC()));
#endif
mangleNameHelper = new (std::nothrow)
MangleNameHelper(tmpFuncDesc->GetFuncName(), StackTraceFormatFlag(tmpFuncDesc->GetStackTraceFormat()));
CHECK_DETAIL(mangleNameHelper != nullptr, "new mangleNameHelper failed when create StackMetadataHelper.");
funcDesc = reinterpret_cast<uint64_t*>(tmpFuncDesc);
}
StackMetadataHelper() = delete;
~StackMetadataHelper()
{
if (mangleNameHelper != nullptr) {
delete mangleNameHelper;
mangleNameHelper = nullptr;
}
};
uint32_t GetLineNumber() const;
CString GetFilePath() const { return reinterpret_cast<FuncDescRef>(funcDesc)->GetFuncDir(); }
CString GetFileName() const { return reinterpret_cast<FuncDescRef>(funcDesc)->GetFuncFilename(); }
CString GetFilePathAndName() const
{
CString filePath = GetFilePath();
CString fileName = GetFileName();
#ifdef _WIN64
CString slash = "\\";
#else
CString slash = "/";
#endif
return filePath.IsEmpty() ? fileName : filePath + slash + fileName;
}
MangleNameHelper* GetMangleNameHelper() const { return mangleNameHelper; }
CString GetMangleName() const { return mangleNameHelper->GetMangleName(); }
CString GetDemangleName() const { return mangleNameHelper->GetDemangleName(); }
CString GetDemangleMethodName() const { return mangleNameHelper->GetMethodName(); }
CString GetDemangleClassName() const { return mangleNameHelper->GetClassName(); }
bool IsNeedFiltExceptionCreationLayer() const { return mangleNameHelper->IsNeedFilt(); }
private:
const uint32_t* funcPC;
MangleNameHelper* mangleNameHelper = nullptr;
uintptr_t funcStartAddress = 0;
DISABLE_CLASS_COPY_AND_ASSIGN(StackMetadataHelper);
uint64_t* funcDesc;
};
}
#endif