mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-12-16 19:32:07 +00:00
54 lines
1.3 KiB
C++
54 lines
1.3 KiB
C++
#pragma once
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
#include <array>
|
|
#include <optional>
|
|
#include <functional>
|
|
#include <signal.h>
|
|
|
|
#ifdef __APPLE__
|
|
// ucontext is not available without _XOPEN_SOURCE
|
|
#define _XOPEN_SOURCE 700
|
|
#endif
|
|
#include <ucontext.h>
|
|
|
|
struct NoCapture
|
|
{
|
|
};
|
|
|
|
/// Tries to capture current stack trace using libunwind or signal context
|
|
/// NOTE: StackTrace calculation is signal safe only if updatePHDRCache() was called beforehand.
|
|
class StackTrace
|
|
{
|
|
public:
|
|
static constexpr size_t capacity = 32;
|
|
using Frames = std::array<void *, capacity>;
|
|
|
|
/// 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
|
|
StackTrace(const ucontext_t & signal_context);
|
|
|
|
/// Creates empty object for deferred initialization
|
|
StackTrace(NoCapture);
|
|
|
|
size_t getSize() const;
|
|
size_t getOffset() const;
|
|
const Frames & getFrames() const;
|
|
std::string toString() const;
|
|
|
|
void toStringEveryLine(std::function<void(const std::string &)> callback) const;
|
|
|
|
protected:
|
|
void tryCapture();
|
|
|
|
size_t size = 0;
|
|
size_t offset = 0; /// How many frames to skip while displaying.
|
|
Frames frames{};
|
|
};
|
|
|
|
std::string signalToErrorMessage(int sig, const siginfo_t & info, const ucontext_t & context);
|