Skip to content

Commit

Permalink
refactor: refactoring stacktrace
Browse files Browse the repository at this point in the history
  • Loading branch information
OEOTYAN committed Aug 19, 2024
1 parent 7b4d9fb commit 4b98a38
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 27 deletions.
6 changes: 3 additions & 3 deletions src/ll/api/Expected.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace ll {
#if defined(LL_DEBUG)
struct ErrorInfoBase::Impl {
boost::stacktrace::stacktrace stacktrace = boost::stacktrace::stacktrace(1, 15);
stacktrace stacktrace{};
};
ErrorInfoBase::ErrorInfoBase() noexcept : impl(std::make_unique<Impl>()) {}
std::string Error::message() const noexcept {
Expand All @@ -28,8 +28,8 @@ std::string Error::message() const noexcept {
}

struct ExceptionError : ErrorInfoBase {
std::exception_ptr exc;
boost::stacktrace::stacktrace stacktrace;
std::exception_ptr exc;
stacktrace stacktrace;
ExceptionError(std::exception_ptr const& exc) noexcept
: exc(exc),
stacktrace(error_utils::stacktraceFromCurrentException()) {}
Expand Down
8 changes: 3 additions & 5 deletions src/ll/api/utils/ErrorUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
#include <system_error>
#include <typeinfo>

#include "boost/stacktrace.hpp"

#include "ll/api/utils/StacktraceUtils.h"
#include "ll/api/base/CompilerPredefine.h"
#include "ll/api/base/Macro.h"
#include "ll/api/base/StdInt.h"

#include "mc/common/wrapper/optional_ref.h"

#include "ll/api/base/CompilerPredefine.h"

namespace ll {
class Logger;
class OutputStream;
Expand Down Expand Up @@ -46,7 +44,7 @@ LLNDAPI std::system_error getLastSystemError() noexcept;

LLNDAPI std::exception_ptr createExceptionPtr(void*) noexcept;

LLNDAPI boost::stacktrace::stacktrace stacktraceFromCurrentException(size_t skip = 0, size_t maxDepth = ~0ull);
LLNDAPI stacktrace stacktraceFromCurrentException(size_t skip = 0, size_t maxDepth = ~0ull);

LLNDAPI std::string makeExceptionString(std::exception_ptr ePtr) noexcept;

Expand Down
15 changes: 6 additions & 9 deletions src/ll/api/utils/ErrorUtils_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,7 @@ std::exception_ptr createExceptionPtr(void* rec) noexcept {
__ExceptionPtrAssign(&res, &realType);
return res;
}
boost::stacktrace::stacktrace
stacktraceFromContext(optional_ref<_CONTEXT const> context, size_t skip, size_t maxDepth) {
stacktrace stacktraceFromContext(optional_ref<_CONTEXT const> context, size_t skip, size_t maxDepth) {
if (!context) {
return {};
}
Expand All @@ -191,11 +190,10 @@ stacktraceFromContext(optional_ref<_CONTEXT const> context, size_t skip, size_t

struct RealStacktrace {
std::vector<decltype(sf.AddrPC.Offset)> addresses;
ulong hash{};
} realStacktrace;

static_assert(sizeof(RealStacktrace) == sizeof(boost::stacktrace::stacktrace));
static_assert(sizeof(boost::stacktrace::frame) == sizeof(sf.AddrPC.Offset));
static_assert(sizeof(RealStacktrace) == sizeof(stacktrace));
static_assert(sizeof(stacktrace_entry) == sizeof(sf.AddrPC.Offset));

constexpr auto machine = IMAGE_FILE_MACHINE_AMD64;

Expand All @@ -218,12 +216,11 @@ stacktraceFromContext(optional_ref<_CONTEXT const> context, size_t skip, size_t
if (i < skip) {
continue;
}
realStacktrace.hash += (ulong)sf.AddrPC.Offset;
realStacktrace.addresses.push_back(sf.AddrPC.Offset);
}
return *reinterpret_cast<boost::stacktrace::stacktrace*>(&realStacktrace);
return *reinterpret_cast<stacktrace*>(&realStacktrace);
}
boost::stacktrace::stacktrace stacktraceFromCurrentException(size_t skip, size_t maxDepth) {
stacktrace stacktraceFromCurrentException(size_t skip, size_t maxDepth) {
return stacktraceFromContext(current_exception_context(), skip, maxDepth);
}

Expand Down Expand Up @@ -338,7 +335,7 @@ void printCurrentException(ll::OutputStream& stream, std::exception_ptr const& e
std::string res;
auto stacktrace = stacktraceFromCurrentException();
if (stacktrace.empty()) {
stacktrace = boost::stacktrace::stacktrace(2, 15);
stacktrace = ::ll::stacktrace{};
if (!stacktrace.empty()) {
res = "\ncurrent stacktrace:\n" + stacktrace_utils::toString(stacktrace);
}
Expand Down
11 changes: 8 additions & 3 deletions src/ll/api/utils/StacktraceUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@

#include "boost/stacktrace.hpp"

namespace ll {
using stacktrace = ::boost::stacktrace::stacktrace;
using stacktrace_entry = ::boost::stacktrace::frame;
} // namespace ll

namespace ll::inline utils::stacktrace_utils {
class SymbolLoader {
void* handle;
Expand All @@ -31,7 +36,7 @@ struct StackTraceEntryInfo {
};

LLNDAPI uintptr_t tryGetSymbolAddress(std::string_view);
LLNDAPI StackTraceEntryInfo getInfo(boost::stacktrace::frame const&);
LLNDAPI std::string toString(boost::stacktrace::frame const&);
LLNDAPI std::string toString(boost::stacktrace::stacktrace const&);
LLNDAPI StackTraceEntryInfo getInfo(stacktrace_entry const&);
LLNDAPI std::string toString(stacktrace_entry const&);
LLNDAPI std::string toString(stacktrace const&);
} // namespace ll::inline utils::stacktrace_utils
8 changes: 4 additions & 4 deletions src/ll/api/utils/StacktraceUtils_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,15 @@ uintptr_t tryGetSymbolAddress(std::string_view symbol) {
return data.getSymbol(symbol);
}

StackTraceEntryInfo getInfo(boost::stacktrace::frame const& entry) {
StackTraceEntryInfo getInfo(stacktrace_entry const& entry) {
DbgEngData data;

static auto processRange = sys_utils::getImageRange();

if (&*processRange.begin() <= entry.address() && entry.address() < &*processRange.end()) {
size_t length{};
uint disp{};
auto str = pl::symbol_provider::pl_lookup_symbol_disp(entry.address(), &length, &disp);
auto str = pl::symbol_provider::pl_lookup_symbol_disp((void*)entry.address(), &length, &disp);
if (length) {
static auto processName = sys_utils::getModuleFileName(nullptr);
StackTraceEntryInfo res{disp, processName + '!' + str[0]};
Expand All @@ -226,7 +226,7 @@ StackTraceEntryInfo getInfo(boost::stacktrace::frame const& entry) {
return data.getInfo(entry.address());
}

std::string toString(boost::stacktrace::frame const& entry) {
std::string toString(stacktrace_entry const& entry) {
std::string res = fmt::format("at: 0x{:0>12X}", (uint64)entry.address());
auto [displacement, name, line, file] = getInfo(entry);
std::string module;
Expand All @@ -251,7 +251,7 @@ std::string toString(boost::stacktrace::frame const& entry) {
return res;
}

std::string toString(boost::stacktrace::stacktrace const& t) {
std::string toString(stacktrace const& t) {
std::string res;
auto maxsize = std::to_string(t.size() - 1).size();
for (size_t i = 0; i < t.size(); i++) {
Expand Down
5 changes: 2 additions & 3 deletions src/ll/core/CrashLogger_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
#include "DbgHelp.h"

namespace ll::inline utils::error_utils {
boost::stacktrace::stacktrace
stacktraceFromContext(optional_ref<_CONTEXT const> context, size_t skip = 0, size_t maxDepth = ~0ull);
stacktrace stacktraceFromContext(optional_ref<_CONTEXT const> context, size_t skip = 0, size_t maxDepth = ~0ull);
}

namespace ll {
Expand Down Expand Up @@ -335,7 +334,7 @@ static LONG unhandledExceptionFilter(_In_ struct _EXCEPTION_POINTERS* e) {
crashInfo.logger.error("!!! Error in CrashLogger !!!");
ll::error_utils::printCurrentException(crashInfo.logger);
crashInfo.logger.error("");
crashInfo.logger.error("\n{}", ll::stacktrace_utils::toString(boost::stacktrace::stacktrace()));
crashInfo.logger.error("\n{}", ll::stacktrace_utils::toString(stacktrace()));
}
std::exit((int)e->ExceptionRecord->ExceptionCode);
}
Expand Down

0 comments on commit 4b98a38

Please sign in to comment.