215 lines
6.5 KiB
C++
215 lines
6.5 KiB
C++
#pragma once
|
|
|
|
#include <Void/Functional/Error.hpp>
|
|
#include <Void/Functional/Result.hpp>
|
|
#include <Void/Time/Clock.hpp>
|
|
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <filesystem>
|
|
#include <memory>
|
|
#include <optional>
|
|
#include <ostream>
|
|
#include <stop_token>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
namespace Void::Stacktrace
|
|
{
|
|
|
|
inline constexpr std::size_t kMaxCaptureFrames = 256;
|
|
inline constexpr std::size_t kDefaultMaxSnippetBytes = 4096;
|
|
inline constexpr std::size_t kDefaultMaxRenderedBytes = 65536;
|
|
inline constexpr std::size_t kDefaultMaxSourceScanBytes = 1048576;
|
|
inline constexpr std::size_t kDefaultMaxLineLength = 4096;
|
|
inline constexpr std::size_t kDefaultResolverCacheEntries = 1024;
|
|
inline constexpr std::size_t kDefaultResolverCacheBytes = 4ULL * 1024ULL * 1024ULL;
|
|
|
|
enum class StacktraceErrorCode : std::int32_t {
|
|
invalid_argument = 1,
|
|
unsafe_path = 2,
|
|
source_unreadable = 3,
|
|
scan_limit_exceeded = 4,
|
|
cancelled = 5,
|
|
timeout = 6,
|
|
backend_failed = 7
|
|
};
|
|
|
|
[[nodiscard]] inline Void::Result::Error
|
|
make_stacktrace_error(StacktraceErrorCode code, std::string message = {})
|
|
{
|
|
using Void::Result::Error;
|
|
using Void::Result::ErrorCode;
|
|
using Void::Result::ErrorKind;
|
|
|
|
ErrorKind kind = ErrorKind::internal;
|
|
switch (code) {
|
|
case StacktraceErrorCode::invalid_argument:
|
|
case StacktraceErrorCode::unsafe_path:
|
|
kind = ErrorKind::invalid_argument;
|
|
break;
|
|
case StacktraceErrorCode::source_unreadable:
|
|
kind = ErrorKind::not_found;
|
|
break;
|
|
case StacktraceErrorCode::scan_limit_exceeded:
|
|
kind = ErrorKind::exhausted;
|
|
break;
|
|
case StacktraceErrorCode::cancelled:
|
|
kind = ErrorKind::cancelled;
|
|
break;
|
|
case StacktraceErrorCode::timeout:
|
|
kind = ErrorKind::timeout;
|
|
break;
|
|
case StacktraceErrorCode::backend_failed:
|
|
kind = ErrorKind::internal;
|
|
break;
|
|
}
|
|
|
|
return Error{
|
|
kind,
|
|
std::move(message),
|
|
ErrorCode{Void::Result::kStacktraceErrorDomain,
|
|
static_cast<std::int32_t>(code)}};
|
|
}
|
|
|
|
struct Capabilities {
|
|
bool raw_capture{true};
|
|
bool symbols{true};
|
|
bool line_info{true};
|
|
bool async_signal_safe{false};
|
|
std::string_view backend_name{"boost.stacktrace"};
|
|
};
|
|
|
|
[[nodiscard]] Capabilities capabilities() noexcept;
|
|
|
|
struct CaptureOptions {
|
|
unsigned skip{0};
|
|
unsigned max_frames{32};
|
|
};
|
|
|
|
struct RawFrame {
|
|
std::uintptr_t address{0};
|
|
std::uintptr_t module_offset{0};
|
|
std::string module_path{};
|
|
std::string module_id{};
|
|
};
|
|
|
|
struct RawTrace {
|
|
std::vector<RawFrame> frames{};
|
|
bool truncated{false};
|
|
};
|
|
|
|
/// Capture raw instruction addresses and owned module identity metadata without
|
|
/// reading source files or emitting Void::Log events.
|
|
[[nodiscard]] Void::Result::Result<RawTrace>
|
|
capture(CaptureOptions options = {});
|
|
|
|
enum class FrameResolveStatus : std::uint8_t {
|
|
resolved = 0,
|
|
symbols_only,
|
|
unresolved,
|
|
unsupported,
|
|
cancelled,
|
|
timed_out
|
|
};
|
|
|
|
struct ResolveOptions {
|
|
bool resolve_symbols{true};
|
|
bool source_snippets{false};
|
|
std::vector<std::filesystem::path> allowed_source_roots{};
|
|
std::vector<std::string> redact_prefixes{};
|
|
int context_lines{3};
|
|
std::size_t max_snippet_bytes{kDefaultMaxSnippetBytes};
|
|
std::size_t max_bytes{kDefaultMaxRenderedBytes};
|
|
std::size_t max_source_scan_bytes{kDefaultMaxSourceScanBytes};
|
|
std::size_t max_line_bytes{kDefaultMaxLineLength};
|
|
std::stop_token stop{};
|
|
std::optional<Void::Time::MonotonicClock::time_point> deadline{};
|
|
const Void::Time::MonotonicClock* clock{
|
|
&Void::Time::SystemMonotonicClock::instance()};
|
|
};
|
|
|
|
struct ResolvedFrame {
|
|
RawFrame raw{};
|
|
std::string function_name{};
|
|
std::string source_file{};
|
|
int source_line{0};
|
|
std::string source_snippet{};
|
|
FrameResolveStatus status{FrameResolveStatus::unresolved};
|
|
std::optional<Void::Result::Error> diagnostic{};
|
|
};
|
|
|
|
struct ResolvedTrace {
|
|
std::vector<ResolvedFrame> frames{};
|
|
bool truncated{false};
|
|
bool incomplete{false};
|
|
std::size_t rendered_bytes{0};
|
|
};
|
|
|
|
/// Stateful bounded LRU resolver cache keyed by owned (module_id, module_offset).
|
|
class Resolver
|
|
{
|
|
public:
|
|
explicit Resolver(std::size_t max_entries = kDefaultResolverCacheEntries,
|
|
std::size_t max_bytes = kDefaultResolverCacheBytes);
|
|
~Resolver();
|
|
|
|
Resolver(const Resolver&) = delete;
|
|
Resolver& operator=(const Resolver&) = delete;
|
|
Resolver(Resolver&&) noexcept = delete;
|
|
Resolver& operator=(Resolver&&) noexcept = delete;
|
|
|
|
[[nodiscard]] Void::Result::Result<ResolvedTrace>
|
|
resolve(const RawTrace& raw, const ResolveOptions& options = {});
|
|
|
|
std::size_t invalidate_module(std::string_view module_id);
|
|
void clear_cache();
|
|
|
|
[[nodiscard]] std::size_t cache_entries() const;
|
|
[[nodiscard]] std::size_t cache_bytes() const;
|
|
|
|
private:
|
|
struct Impl;
|
|
std::unique_ptr<Impl> impl_;
|
|
};
|
|
|
|
[[nodiscard]] Void::Result::Result<ResolvedTrace>
|
|
resolve(const RawTrace& raw, const ResolveOptions& options = {});
|
|
|
|
[[nodiscard]] Void::Result::Result<std::string>
|
|
read_source_snippet_checked(const std::filesystem::path& file,
|
|
int line,
|
|
const ResolveOptions& options);
|
|
|
|
[[nodiscard]] std::string format_resolved_trace(
|
|
const ResolvedTrace& trace,
|
|
std::size_t max_bytes = kDefaultMaxRenderedBytes);
|
|
|
|
/// Async-signal-safe fixed-buffer fatal diagnostic marker writer using only
|
|
/// stack storage and raw descriptor write().
|
|
bool write_fatal_signal_marker(int fd, int sig) noexcept;
|
|
|
|
// ── Legacy compatibility API ─────────────────────────────────────────────────
|
|
|
|
struct StackFrame {
|
|
std::string function_name;
|
|
std::string source_file;
|
|
int source_line{0};
|
|
std::string source_snippet;
|
|
};
|
|
|
|
std::vector<StackFrame> capture_stacktrace(unsigned skip_frames = 0,
|
|
unsigned max_frames = 32);
|
|
|
|
Void::Result::Result<std::string, Void::Result::Error>
|
|
read_source_snippet(const std::string& file,
|
|
int line,
|
|
int context_lines = 3);
|
|
|
|
std::string format_stacktrace(const std::vector<StackFrame>& frames);
|
|
void print_stacktrace(std::ostream& os,
|
|
const std::vector<StackFrame>& frames);
|
|
|
|
} // namespace Void::Stacktrace
|