2019-07-01 22:11:11 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/types.h>
|
2020-05-27 19:11:04 +00:00
|
|
|
|
2019-07-01 22:11:11 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <array>
|
|
|
|
#include <optional>
|
2019-08-18 00:03:19 +00:00
|
|
|
#include <functional>
|
2022-06-27 21:00:37 +00:00
|
|
|
#include <csignal>
|
2019-07-01 22:11:11 +00:00
|
|
|
|
2022-06-10 08:22:31 +00:00
|
|
|
#ifdef OS_DARWIN
|
2019-07-01 22:11:11 +00:00
|
|
|
// ucontext is not available without _XOPEN_SOURCE
|
2021-04-09 00:28:24 +00:00
|
|
|
# ifdef __clang__
|
|
|
|
# pragma clang diagnostic ignored "-Wreserved-id-macro"
|
|
|
|
# endif
|
2019-09-04 15:17:18 +00:00
|
|
|
# define _XOPEN_SOURCE 700
|
2019-07-01 22:11:11 +00:00
|
|
|
#endif
|
|
|
|
#include <ucontext.h>
|
|
|
|
|
|
|
|
struct NoCapture
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Tries to capture current stack trace using libunwind or signal context
|
2019-07-25 19:54:43 +00:00
|
|
|
/// NOTE: StackTrace calculation is signal safe only if updatePHDRCache() was called beforehand.
|
2019-07-01 22:11:11 +00:00
|
|
|
class StackTrace
|
|
|
|
{
|
|
|
|
public:
|
2020-05-27 19:11:04 +00:00
|
|
|
struct Frame
|
|
|
|
{
|
|
|
|
const void * virtual_addr = nullptr;
|
|
|
|
void * physical_addr = nullptr;
|
|
|
|
std::optional<std::string> symbol;
|
|
|
|
std::optional<std::string> object;
|
|
|
|
std::optional<std::string> file;
|
|
|
|
std::optional<UInt64> line;
|
|
|
|
};
|
2021-02-03 05:53:21 +00:00
|
|
|
|
2021-06-01 08:53:17 +00:00
|
|
|
/* NOTE: It cannot be larger right now, since otherwise it
|
|
|
|
* will not fit into minimal PIPE_BUF (512) in TraceCollector.
|
|
|
|
*/
|
|
|
|
static constexpr size_t capacity = 45;
|
|
|
|
|
2020-05-27 19:11:04 +00:00
|
|
|
using FramePointers = std::array<void *, capacity>;
|
2020-06-10 13:30:12 +00:00
|
|
|
using Frames = std::array<Frame, capacity>;
|
2019-07-01 22:11:11 +00:00
|
|
|
|
|
|
|
/// Tries to capture stack trace
|
|
|
|
StackTrace();
|
|
|
|
|
|
|
|
/// Tries to capture stack trace. Fallbacks on parsing caller address from
|
|
|
|
/// signal context if no stack trace could be captured
|
2021-01-19 12:34:27 +00:00
|
|
|
explicit StackTrace(const ucontext_t & signal_context);
|
2019-07-01 22:11:11 +00:00
|
|
|
|
|
|
|
/// Creates empty object for deferred initialization
|
2021-01-19 12:34:27 +00:00
|
|
|
explicit StackTrace(NoCapture);
|
2019-07-01 22:11:11 +00:00
|
|
|
|
|
|
|
size_t getSize() const;
|
2019-07-31 21:40:29 +00:00
|
|
|
size_t getOffset() const;
|
2020-05-27 19:11:04 +00:00
|
|
|
const FramePointers & getFramePointers() const;
|
2019-07-01 22:11:11 +00:00
|
|
|
std::string toString() const;
|
|
|
|
|
2020-05-27 19:11:04 +00:00
|
|
|
static std::string toString(void ** frame_pointers, size_t offset, size_t size);
|
2021-08-28 03:43:43 +00:00
|
|
|
static std::string toStringStatic(const FramePointers & frame_pointers, size_t offset, size_t size);
|
|
|
|
static void dropCache();
|
2020-06-23 21:27:04 +00:00
|
|
|
static void symbolize(const FramePointers & frame_pointers, size_t offset, size_t size, StackTrace::Frames & frames);
|
2020-01-02 06:56:53 +00:00
|
|
|
|
2019-08-17 22:39:26 +00:00
|
|
|
void toStringEveryLine(std::function<void(const std::string &)> callback) const;
|
2021-01-19 12:34:27 +00:00
|
|
|
|
2019-07-01 22:11:11 +00:00
|
|
|
protected:
|
|
|
|
void tryCapture();
|
|
|
|
|
2019-07-03 15:23:46 +00:00
|
|
|
size_t size = 0;
|
2019-07-31 21:40:29 +00:00
|
|
|
size_t offset = 0; /// How many frames to skip while displaying.
|
2020-05-27 19:11:04 +00:00
|
|
|
FramePointers frame_pointers{};
|
2019-07-01 22:11:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
std::string signalToErrorMessage(int sig, const siginfo_t & info, const ucontext_t & context);
|