Modules/Callstack/public/StackTrace.hpp
agent a4cd5661f8 Callstack: add stack capture and thread sampling
StackTrace captures the calling thread with _Unwind_Backtrace, symbolizes
addresses with dladdr and __cxa_demangle, and samples other threads through a
real-time signal. Each target writes into its own slot; the sampler also reads
/proc task name, scheduler state and wait channel, and keeps unanswered threads
in the result. Windows captures stacks and module offsets but reports that
remote sampling is unsupported. Debugger detection is exposed separately.
2026-10-27 23:14:24 +03:00

41 lines
979 B
C++

#pragma once
#include <cstddef>
#include <cstdint>
#include <string>
#include <vector>
namespace tp {
struct FrameSymbol {
std::string module;
uintptr_t moduleOffset = 0;
std::string function;
std::string file;
size_t line = 0;
};
struct ThreadSample {
int tid = 0;
std::string name;
std::string state;
std::string waitChannel;
bool answered = false;
std::vector<uintptr_t> frames;
};
class StackTrace {
public:
static constexpr size_t kMaxFrames = 64;
static size_t captureCallingThread(uintptr_t* frames, size_t capacity);
[[nodiscard]] static FrameSymbol symbolize(uintptr_t address);
[[nodiscard]] static std::string describe(uintptr_t address);
[[nodiscard]] static bool canSampleThreads();
[[nodiscard]] static bool isDebuggerAttached();
static void prepareThreadSampling();
[[nodiscard]] static std::vector<ThreadSample> sampleAllThreads(double answerTimeoutMs = 200);
[[nodiscard]] static int callingThreadId();
};
}