STX  1.0.0
backtrace.h
Go to the documentation of this file.
1 
30 #pragma once
31 
32 #include <cstdint>
33 
35 #include "stx/report.h"
36 
44 
46 
47 namespace backtrace {
48 
49 enum class SignalError {
51  Unknown,
53  SigErr
54 };
55 
56 inline SpanReport operator>>(ReportQuery, SignalError const &err) noexcept {
57  switch (err) {
58  case SignalError::Unknown:
59  return SpanReport(
60  "Uknown signal given, 'handle_signal' can only handle 'SIGSEGV', "
61  "'SIGILL' and 'SIGFPE'.");
62  case SignalError::SigErr:
63  return SpanReport("'std::signal' returned 'SIGERR'");
64  }
65 }
66 
68 struct CharSpan {
69  char *data;
70  size_t size;
71  CharSpan(char *data_, size_t size_) noexcept : data{data_}, size{size_} {}
72  CharSpan(CharSpan const &) noexcept = default;
73  CharSpan(CharSpan &&) noexcept = default;
74  CharSpan &operator=(CharSpan const &) noexcept = default;
75  CharSpan &operator=(CharSpan &&) noexcept = default;
76  ~CharSpan() noexcept = default;
77 };
78 
82 struct Symbol {
84  auto raw() const noexcept -> std::string_view;
85 
91  explicit Symbol(CharSpan sym) noexcept : symbol_{sym} {};
92 
93  private:
94  CharSpan symbol_;
95 };
96 
98 struct Frame {
107 
108  constexpr explicit Frame() = default;
109  Frame(Frame &&) = default;
110  Frame &operator=(Frame &&) = default;
111  Frame(Frame const &) = default;
112  Frame &operator=(Frame const &) = default;
113  ~Frame() = default;
114 };
115 
116 using Callback = bool (*)(Frame, int);
117 
157 // all memory passed to the callback is cleared after each call. Hence we only
158 // use one stack memory for the callback feed-loop.
159 int trace(Callback callback, int skip_count = 0);
160 
176 auto handle_signal(int signal) noexcept -> Result<void (*)(int), SignalError>;
177 
178 } // namespace backtrace
179 
size_t size
Definition: backtrace.h:70
std::signal returned SIG_ERR
Option< uintptr_t > sp
address on the call stack
Definition: backtrace.h:102
int trace(Callback callback, int skip_count=0)
Mutable type-erased view over a contiguous character container.
Definition: backtrace.h:68
SpanReport operator>>(ReportQuery, SignalError const &err) noexcept
Definition: backtrace.h:56
Option< Symbol > symbol
function&#39;s symbol name. possibly demangled.
Definition: backtrace.h:106
An Unknown error occurred.
reperesents an active stack frame.
Definition: backtrace.h:98
Option< uintptr_t > offset
offset of the function&#39;s call-site to the callee on the instruction block.
Definition: backtrace.h:104
Definition: option_result.h:90
Option< uintptr_t > ip
instruction pointer
Definition: backtrace.h:100
Definition: report.h:265
#define STX_END_NAMESPACE
Definition: config.h:329
CharSpan(char *data_, size_t size_) noexcept
Definition: backtrace.h:71
bool(*)(Frame, int) Callback
Definition: backtrace.h:116
SignalError
Definition: backtrace.h:49
auto handle_signal(int signal) noexcept -> Result< void(*)(int), SignalError >
Definition: backtrace.h:82
#define STX_BEGIN_NAMESPACE
Definition: config.h:325
Tag type for dispatching reports.
Definition: report.h:55
Symbol(CharSpan sym) noexcept
Definition: backtrace.h:91
char * data
Definition: backtrace.h:69