1#pragma once
2
3#include <string>
4#include <vector>
5#include <array>
6#include <optional>
7#include <functional>
8#include <signal.h>
9
10#ifdef __APPLE__
11// ucontext is not available without _XOPEN_SOURCE
12# pragma clang diagnostic ignored "-Wreserved-id-macro"
13# define _XOPEN_SOURCE 700
14#endif
15#include <ucontext.h>
16
17struct NoCapture
18{
19};
20
21/// Tries to capture current stack trace using libunwind or signal context
22/// NOTE: StackTrace calculation is signal safe only if updatePHDRCache() was called beforehand.
23class StackTrace
24{
25public:
26 static constexpr size_t capacity = 32;
27 using Frames = std::array<void *, capacity>;
28
29 /// Tries to capture stack trace
30 StackTrace();
31
32 /// Tries to capture stack trace. Fallbacks on parsing caller address from
33 /// signal context if no stack trace could be captured
34 StackTrace(const ucontext_t & signal_context);
35
36 /// Creates empty object for deferred initialization
37 StackTrace(NoCapture);
38
39 size_t getSize() const;
40 size_t getOffset() const;
41 const Frames & getFrames() const;
42 std::string toString() const;
43
44 void toStringEveryLine(std::function<void(const std::string &)> callback) const;
45
46protected:
47 void tryCapture();
48
49 size_t size = 0;
50 size_t offset = 0; /// How many frames to skip while displaying.
51 Frames frames{};
52};
53
54std::string signalToErrorMessage(int sig, const siginfo_t & info, const ucontext_t & context);
55