Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
alabuzhev committed Feb 3, 2025
1 parent 27adc78 commit 02fc7de
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 44 deletions.
25 changes: 5 additions & 20 deletions far/exception_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1315,27 +1315,12 @@ static string ExtractObjectType(EXCEPTION_RECORD const& xr)
if (Iterator != CatchableTypesEnumerator.cend())
return encoding::utf8::get_chars(*Iterator);

#if IS_MICROSOFT_SDK()
return {};
#else
const auto TypeInfo = abi::__cxa_current_exception_type();
if (!TypeInfo)
return {};

const auto Name = TypeInfo->name();
auto Status = -1;

struct free_deleter
{
void operator()(void* Ptr) const
{
free(Ptr);
}
};

std::unique_ptr<char, free_deleter> const DemangledName(abi::__cxa_demangle(Name, {}, {}, &Status));
return encoding::utf8::get_chars(DemangledName.get());
#if !IS_MICROSOFT_SDK()
if (const auto TypeInfo = abi::__cxa_current_exception_type(); TypeInfo)
return os::debug::demangle(TypeInfo->name());
#endif

return {};
}

static string_view exception_name(NTSTATUS const Code)
Expand Down
26 changes: 2 additions & 24 deletions far/map_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "global.hpp"

// Platform:
#include "platform.debug.hpp"
#include "platform.fs.hpp"

// Common:
Expand Down Expand Up @@ -97,29 +98,6 @@ map_file::map_file(string_view const ModuleName)

map_file::~map_file() = default;

static void undecorate(string& Symbol)
{
const auto UndecorateFlags =
UNDNAME_NO_FUNCTION_RETURNS |
UNDNAME_NO_ALLOCATION_MODEL |
UNDNAME_NO_ALLOCATION_LANGUAGE |
UNDNAME_NO_ACCESS_SPECIFIERS |
UNDNAME_NO_MEMBER_TYPE |
UNDNAME_32_BIT_DECODE;

if (wchar_t Buffer[MAX_SYM_NAME]; imports.UnDecorateSymbolNameW && imports.UnDecorateSymbolNameW(Symbol.c_str(), Buffer, static_cast<DWORD>(std::size(Buffer)), UndecorateFlags))
{
Symbol = Buffer;
return;
}

if (char Buffer[MAX_SYM_NAME]; imports.UnDecorateSymbolName && imports.UnDecorateSymbolName(encoding::ansi::get_bytes(Symbol).c_str(), Buffer, static_cast<DWORD>(std::size(Buffer)), UndecorateFlags))
{
encoding::ansi::get_chars(Buffer, Symbol);
return;
}
}

static map_file::info get_impl(uintptr_t const Address, std::map<uintptr_t, map_file::line>& Symbols)
{
auto [Begin, End] = Symbols.equal_range(Address);
Expand All @@ -132,7 +110,7 @@ static map_file::info get_impl(uintptr_t const Address, std::map<uintptr_t, map_
--Begin;
}

undecorate(Begin->second.Name);
os::debug::demangle(Begin->second.Name);

return
{
Expand Down
91 changes: 91 additions & 0 deletions far/platform.debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include <crtdbg.h>

#if !IS_MICROSOFT_SDK()
#include <cxxabi.h>
#endif


//----------------------------------------------------------------------------

namespace os::debug
Expand Down Expand Up @@ -316,6 +321,92 @@ namespace os::debug
return (frameContext.FrameType & STACK_FRAME_TYPE_INLINE) != 0;
}

#if !IS_MICROSOFT_SDK()
static bool demangle_abi(const char* const SymbolName, string& Dest)
{
auto Status = -1;

struct free_deleter
{
void operator()(void* Ptr) const
{
free(Ptr);
}
};

std::unique_ptr<char, free_deleter> const DemangledName(abi::__cxa_demangle(SymbolName, {}, {}, &Status));

if (Status || !DemangledName.get())
return false;

Dest = encoding::utf8::get_chars(DemangledName.get());
return true;
}
#endif

constexpr auto UndecorateFlags =
UNDNAME_NO_FUNCTION_RETURNS |
UNDNAME_NO_ALLOCATION_MODEL |
UNDNAME_NO_ALLOCATION_LANGUAGE |
UNDNAME_NO_ACCESS_SPECIFIERS |
UNDNAME_NO_MEMBER_TYPE |
UNDNAME_32_BIT_DECODE;

static bool demangle(const char* const SymbolName, string& Dest)
{
if (char Buffer[MAX_SYM_NAME]; imports.UnDecorateSymbolName && imports.UnDecorateSymbolName(SymbolName, Buffer, static_cast<DWORD>(std::size(Buffer)), UndecorateFlags))
{
// Sanity check + the symobol name can be demangled already and thus longer than MAX_SYM_NAME, we don't want to truncate it.
if (*Buffer && !std::string_view(SymbolName).starts_with(Buffer))
{
Dest = encoding::ansi::get_chars(Buffer);
return true;
}
}

// Empty or the same or failed to demangle
// For non-MSVC builds try to demangle it using ABI
#if !IS_MICROSOFT_SDK()
return debug::demangle_abi(SymbolName, Dest);
#endif
return false;
}

string demangle(const char* const SymbolName)
{
string Result;

if (!demangle(SymbolName, Result))
Result = encoding::ansi::get_chars(SymbolName);

return Result;
}

void demangle(string& SymbolName)
{
if (!imports.UnDecorateSymbolNameW)
{
demangle(encoding::ansi::get_bytes(SymbolName).c_str(), SymbolName);
return;
}

if (wchar_t Buffer[MAX_SYM_NAME]; imports.UnDecorateSymbolNameW(SymbolName.c_str(), Buffer, static_cast<DWORD>(std::size(Buffer)), UndecorateFlags))
{
// Sanity check + the symobol name can be demangled already and thus longer than MAX_SYM_NAME, we don't want to truncate it.
if (*Buffer && !SymbolName.starts_with(Buffer))
{
SymbolName = Buffer;
return;
}
}

// Empty or the same or failed to demangle
// For non-MSVC builds try to demangle it using ABI
#if !IS_MICROSOFT_SDK()
debug::demangle_abi(encoding::ansi::get_bytes(SymbolName).c_str(), SymbolName);
#endif
}

void crt_report_to_ui()
{
#ifdef _DEBUG
Expand Down
3 changes: 3 additions & 0 deletions far/platform.debug.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ namespace os::debug
);
}

string demangle(const char* SymbolName);
void demangle(string& SymbolName);

void crt_report_to_ui();
void crt_report_to_stderr();
}
Expand Down

0 comments on commit 02fc7de

Please sign in to comment.