Added result type and stacktrace capabilities.
This commit is contained in:
88
source/mijin/debug/stacktrace.cpp
Normal file
88
source/mijin/debug/stacktrace.cpp
Normal file
@@ -0,0 +1,88 @@
|
||||
|
||||
#include "./stacktrace.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <backtrace.h>
|
||||
|
||||
namespace mijin
|
||||
{
|
||||
namespace
|
||||
{
|
||||
//
|
||||
// internal defines
|
||||
//
|
||||
|
||||
//
|
||||
// internal constants
|
||||
//
|
||||
|
||||
//
|
||||
// internal types
|
||||
//
|
||||
|
||||
struct BacktraceData
|
||||
{
|
||||
std::optional<std::string> error;
|
||||
std::vector<Stackframe> stackframes;
|
||||
};
|
||||
|
||||
//
|
||||
// internal variables
|
||||
//
|
||||
|
||||
//
|
||||
// internal functions
|
||||
//
|
||||
|
||||
int backtraceFullCallback(void* data, std::uintptr_t programCounter, const char* filename, int lineno, const char* function)
|
||||
{
|
||||
BacktraceData& btData = *static_cast<BacktraceData*>(data);
|
||||
btData.stackframes.push_back({
|
||||
.address = reinterpret_cast<void*>(programCounter),
|
||||
.filename = filename ? filename : "<unknown>",
|
||||
.function = function ? function : "<unknown>",
|
||||
.lineNumber = lineno
|
||||
});
|
||||
return 0;
|
||||
}
|
||||
|
||||
void backtraceErrorCallback(void* data, const char* msg, int /* errnum */)
|
||||
{
|
||||
BacktraceData& btData = *static_cast<BacktraceData*>(data);
|
||||
btData.error = msg;
|
||||
}
|
||||
|
||||
backtrace_state* gBacktraceState = nullptr;
|
||||
} // namespace
|
||||
|
||||
//
|
||||
// public functions
|
||||
//
|
||||
|
||||
Result<Stacktrace> captureStacktrace(unsigned skipFrames) noexcept
|
||||
{
|
||||
BacktraceData btData;
|
||||
if (gBacktraceState == nullptr)
|
||||
{
|
||||
gBacktraceState = backtrace_create_state(/*filename = */ nullptr, /* threaded = */ false, &backtraceErrorCallback, &btData);
|
||||
}
|
||||
if (btData.error.has_value())
|
||||
{
|
||||
return ResultError(std::move(*btData.error));
|
||||
}
|
||||
if (gBacktraceState == nullptr)
|
||||
{
|
||||
return ResultError("Error initializing libbacktrace.");
|
||||
}
|
||||
backtrace_full(gBacktraceState, static_cast<int>(skipFrames) + 1, &backtraceFullCallback, &backtraceErrorCallback, &btData);
|
||||
if (btData.error.has_value())
|
||||
{
|
||||
return ResultError(std::move(*btData.error));
|
||||
}
|
||||
|
||||
return Stacktrace(std::move(btData.stackframes));
|
||||
}
|
||||
|
||||
} // namespace mijin
|
||||
85
source/mijin/debug/stacktrace.hpp
Normal file
85
source/mijin/debug/stacktrace.hpp
Normal file
@@ -0,0 +1,85 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#if !defined(MIJIN_DEBUG_STACKTRACE_HPP_INCLUDED)
|
||||
#define MIJIN_DEBUG_STACKTRACE_HPP_INCLUDED 1
|
||||
|
||||
#include <cmath>
|
||||
#include <iomanip>
|
||||
#include <vector>
|
||||
#include "./symbol_info.hpp"
|
||||
#include "../types/result.hpp"
|
||||
#include "../util/iterators.hpp"
|
||||
|
||||
namespace mijin
|
||||
{
|
||||
|
||||
//
|
||||
// public defines
|
||||
//
|
||||
|
||||
//
|
||||
// public constants
|
||||
//
|
||||
|
||||
//
|
||||
// public types
|
||||
//
|
||||
|
||||
struct Stackframe
|
||||
{
|
||||
void* address;
|
||||
std::string filename;
|
||||
std::string function;
|
||||
int lineNumber;
|
||||
};
|
||||
|
||||
class Stacktrace
|
||||
{
|
||||
private:
|
||||
std::vector<Stackframe> frames_;
|
||||
public:
|
||||
Stacktrace() = default;
|
||||
Stacktrace(const Stacktrace&) = default;
|
||||
Stacktrace(Stacktrace&&) = default;
|
||||
explicit Stacktrace(std::vector<Stackframe> frames) noexcept : frames_(std::move(frames)) {}
|
||||
|
||||
Stacktrace& operator=(const Stacktrace&) = default;
|
||||
Stacktrace& operator=(Stacktrace&&) = default;
|
||||
|
||||
[[nodiscard]] const std::vector<Stackframe>& getFrames() const noexcept { return frames_; }
|
||||
};
|
||||
|
||||
//
|
||||
// public functions
|
||||
//
|
||||
|
||||
[[nodiscard]] Result<Stacktrace> captureStacktrace(unsigned skipFrames = 0) noexcept;
|
||||
|
||||
template<typename TStream>
|
||||
TStream& operator<<(TStream& stream, const Stackframe& stackframe)
|
||||
{
|
||||
stream << "[" << stackframe.address << "] " << stackframe.filename << ":" << stackframe.lineNumber << " in " << demangleCPPIdentifier(stackframe.function.c_str());
|
||||
|
||||
return stream;
|
||||
}
|
||||
|
||||
template<typename TStream>
|
||||
TStream& operator<<(TStream& stream, const Stacktrace& stacktrace)
|
||||
{
|
||||
const int oldWidth = stream.width();
|
||||
const std::ios::fmtflags oldFlags = stream.flags();
|
||||
const int numDigits = static_cast<int>(std::ceil(std::log10(stacktrace.getFrames().size())));
|
||||
stream << std::left;
|
||||
for (const auto& [idx, frame] : mijin::enumerate(stacktrace.getFrames()))
|
||||
{
|
||||
stream << " #" << std::setw(numDigits) << idx << std::setw(0) << " at " << frame << "\n";
|
||||
}
|
||||
stream << std::setw(oldWidth);
|
||||
stream.flags(oldFlags);
|
||||
return stream;
|
||||
}
|
||||
|
||||
} // namespace mijin
|
||||
|
||||
#endif // !defined(MIJIN_DEBUG_STACKTRACE_HPP_INCLUDED)
|
||||
@@ -75,12 +75,12 @@ std::string demangleCPPIdentifier(const char* identifier)
|
||||
std::free(demangled); // NOLINT(cppcoreguidelines-owning-memory,cppcoreguidelines-no-malloc)
|
||||
return name;
|
||||
}
|
||||
return "";
|
||||
return identifier;
|
||||
}
|
||||
#else
|
||||
std::string demangleCPPIdentifier(const char* /* identifier */)
|
||||
std::string demangleCPPIdentifier(const char* identifier)
|
||||
{
|
||||
return "";
|
||||
return identifier;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
Reference in New Issue
Block a user