2023-03-02 22:27:54 +00:00
|
|
|
#include "StackTrace.h"
|
|
|
|
|
|
|
|
#include <base/FnTraits.h>
|
|
|
|
#include <base/constexpr_helpers.h>
|
|
|
|
#include <base/demangle.h>
|
2019-08-28 20:49:37 +00:00
|
|
|
|
2019-07-29 22:26:44 +00:00
|
|
|
#include <Common/Dwarf.h>
|
|
|
|
#include <Common/Elf.h>
|
2020-06-01 18:10:19 +00:00
|
|
|
#include <Common/MemorySanitizer.h>
|
2023-03-02 22:27:54 +00:00
|
|
|
#include <Common/SymbolIndex.h>
|
2019-08-28 20:49:37 +00:00
|
|
|
|
2023-05-06 16:14:01 +00:00
|
|
|
#include <IO/WriteBufferFromString.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include <IO/Operators.h>
|
|
|
|
|
2022-07-28 22:13:02 +00:00
|
|
|
#include <atomic>
|
2019-07-29 22:26:44 +00:00
|
|
|
#include <filesystem>
|
2023-03-02 22:27:54 +00:00
|
|
|
#include <map>
|
2022-09-25 12:06:13 +00:00
|
|
|
#include <mutex>
|
2019-08-28 20:49:37 +00:00
|
|
|
#include <sstream>
|
2019-07-29 22:26:44 +00:00
|
|
|
#include <unordered_map>
|
2023-03-02 22:27:54 +00:00
|
|
|
#include <fmt/format.h>
|
2019-06-28 18:06:38 +00:00
|
|
|
|
2022-09-28 08:45:15 +00:00
|
|
|
#include "config.h"
|
2020-04-16 12:31:57 +00:00
|
|
|
|
2019-08-28 20:49:37 +00:00
|
|
|
#if USE_UNWIND
|
2020-04-16 12:31:57 +00:00
|
|
|
# include <libunwind.h>
|
2019-08-28 20:49:37 +00:00
|
|
|
#endif
|
2019-06-28 18:06:38 +00:00
|
|
|
|
2022-07-28 22:13:02 +00:00
|
|
|
namespace
|
|
|
|
{
|
2023-03-02 22:27:54 +00:00
|
|
|
/// Currently this variable is set up once on server startup.
|
|
|
|
/// But we use atomic just in case, so it is possible to be modified at runtime.
|
|
|
|
std::atomic<bool> show_addresses = true;
|
2022-07-29 23:45:06 +00:00
|
|
|
|
2023-03-02 22:27:54 +00:00
|
|
|
bool shouldShowAddress(const void * addr)
|
|
|
|
{
|
|
|
|
/// If the address is less than 4096, most likely it is a nullptr dereference with offset,
|
|
|
|
/// and showing this offset is secure nevertheless.
|
|
|
|
/// NOTE: 4096 is the page size on x86 and it can be different on other systems,
|
|
|
|
/// but for the purpose of this branch, it does not matter.
|
|
|
|
if (reinterpret_cast<uintptr_t>(addr) < 4096)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return show_addresses.load(std::memory_order_relaxed);
|
|
|
|
}
|
2022-07-28 22:13:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StackTrace::setShowAddresses(bool show)
|
|
|
|
{
|
|
|
|
show_addresses.store(show, std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
2023-03-02 22:27:54 +00:00
|
|
|
std::string SigsegvErrorString(const siginfo_t & info, [[maybe_unused]] const ucontext_t & context)
|
2019-06-28 18:06:38 +00:00
|
|
|
{
|
2023-03-02 22:27:54 +00:00
|
|
|
using namespace std::string_literals;
|
|
|
|
std::string address
|
|
|
|
= info.si_addr == nullptr ? "NULL pointer"s : (shouldShowAddress(info.si_addr) ? fmt::format("{}", info.si_addr) : ""s);
|
2019-06-28 18:06:38 +00:00
|
|
|
|
2023-03-02 22:27:54 +00:00
|
|
|
const std::string_view access =
|
2022-06-10 08:22:31 +00:00
|
|
|
#if defined(__x86_64__) && !defined(OS_FREEBSD) && !defined(OS_DARWIN) && !defined(__arm__) && !defined(__powerpc__)
|
2023-03-02 22:27:54 +00:00
|
|
|
(context.uc_mcontext.gregs[REG_ERR] & 0x02) ? "write" : "read";
|
|
|
|
#else
|
|
|
|
"";
|
2019-06-28 18:06:38 +00:00
|
|
|
#endif
|
|
|
|
|
2023-03-02 22:27:54 +00:00
|
|
|
std::string_view message;
|
|
|
|
|
|
|
|
switch (info.si_code)
|
|
|
|
{
|
|
|
|
case SEGV_ACCERR:
|
|
|
|
message = "Attempted access has violated the permissions assigned to the memory area";
|
2019-06-28 18:06:38 +00:00
|
|
|
break;
|
2023-03-02 22:27:54 +00:00
|
|
|
case SEGV_MAPERR:
|
|
|
|
message = "Address not mapped to object";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
message = "Unknown si_code";
|
|
|
|
break;
|
|
|
|
}
|
2019-06-28 18:06:38 +00:00
|
|
|
|
2023-03-02 22:27:54 +00:00
|
|
|
return fmt::format("Address: {}. Access: {}. {}.", std::move(address), access, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr std::string_view SigbusErrorString(int si_code)
|
|
|
|
{
|
|
|
|
switch (si_code)
|
|
|
|
{
|
|
|
|
case BUS_ADRALN:
|
|
|
|
return "Invalid address alignment.";
|
|
|
|
case BUS_ADRERR:
|
|
|
|
return "Non-existent physical address.";
|
|
|
|
case BUS_OBJERR:
|
|
|
|
return "Object specific hardware error.";
|
|
|
|
|
|
|
|
// Linux specific
|
2019-06-28 18:06:38 +00:00
|
|
|
#if defined(BUS_MCEERR_AR)
|
2023-03-02 22:27:54 +00:00
|
|
|
case BUS_MCEERR_AR:
|
|
|
|
return "Hardware memory error: action required.";
|
2019-06-28 18:06:38 +00:00
|
|
|
#endif
|
|
|
|
#if defined(BUS_MCEERR_AO)
|
2023-03-02 22:27:54 +00:00
|
|
|
case BUS_MCEERR_AO:
|
|
|
|
return "Hardware memory error: action optional.";
|
2019-06-28 18:06:38 +00:00
|
|
|
#endif
|
2023-03-02 22:27:54 +00:00
|
|
|
default:
|
|
|
|
return "Unknown si_code.";
|
|
|
|
}
|
|
|
|
}
|
2019-06-28 18:06:38 +00:00
|
|
|
|
2023-03-02 22:27:54 +00:00
|
|
|
constexpr std::string_view SigfpeErrorString(int si_code)
|
|
|
|
{
|
|
|
|
switch (si_code)
|
|
|
|
{
|
|
|
|
case FPE_INTDIV:
|
|
|
|
return "Integer divide by zero.";
|
|
|
|
case FPE_INTOVF:
|
|
|
|
return "Integer overflow.";
|
|
|
|
case FPE_FLTDIV:
|
|
|
|
return "Floating point divide by zero.";
|
|
|
|
case FPE_FLTOVF:
|
|
|
|
return "Floating point overflow.";
|
|
|
|
case FPE_FLTUND:
|
|
|
|
return "Floating point underflow.";
|
|
|
|
case FPE_FLTRES:
|
|
|
|
return "Floating point inexact result.";
|
|
|
|
case FPE_FLTINV:
|
|
|
|
return "Floating point invalid operation.";
|
|
|
|
case FPE_FLTSUB:
|
|
|
|
return "Subscript out of range.";
|
|
|
|
default:
|
|
|
|
return "Unknown si_code.";
|
|
|
|
}
|
|
|
|
}
|
2019-06-28 18:06:38 +00:00
|
|
|
|
2023-03-02 22:27:54 +00:00
|
|
|
constexpr std::string_view SigillErrorString(int si_code)
|
|
|
|
{
|
|
|
|
switch (si_code)
|
|
|
|
{
|
|
|
|
case ILL_ILLOPC:
|
|
|
|
return "Illegal opcode.";
|
|
|
|
case ILL_ILLOPN:
|
|
|
|
return "Illegal operand.";
|
|
|
|
case ILL_ILLADR:
|
|
|
|
return "Illegal addressing mode.";
|
|
|
|
case ILL_ILLTRP:
|
|
|
|
return "Illegal trap.";
|
|
|
|
case ILL_PRVOPC:
|
|
|
|
return "Privileged opcode.";
|
|
|
|
case ILL_PRVREG:
|
|
|
|
return "Privileged register.";
|
|
|
|
case ILL_COPROC:
|
|
|
|
return "Coprocessor error.";
|
|
|
|
case ILL_BADSTK:
|
|
|
|
return "Internal stack error.";
|
|
|
|
default:
|
|
|
|
return "Unknown si_code.";
|
|
|
|
}
|
|
|
|
}
|
2019-06-28 18:06:38 +00:00
|
|
|
|
2023-03-02 22:27:54 +00:00
|
|
|
std::string signalToErrorMessage(int sig, const siginfo_t & info, [[maybe_unused]] const ucontext_t & context)
|
|
|
|
{
|
|
|
|
switch (sig)
|
|
|
|
{
|
|
|
|
case SIGSEGV:
|
|
|
|
return SigsegvErrorString(info, context);
|
|
|
|
case SIGBUS:
|
|
|
|
return std::string{SigbusErrorString(info.si_code)};
|
|
|
|
case SIGILL:
|
|
|
|
return std::string{SigillErrorString(info.si_code)};
|
2020-05-30 08:01:15 +00:00
|
|
|
case SIGFPE:
|
2023-03-02 22:27:54 +00:00
|
|
|
return std::string{SigfpeErrorString(info.si_code)};
|
2020-05-30 08:01:15 +00:00
|
|
|
case SIGTSTP:
|
2023-03-02 22:27:54 +00:00
|
|
|
return "This is a signal used for debugging purposes by the user.";
|
|
|
|
default:
|
|
|
|
return "";
|
2019-12-02 17:29:19 +00:00
|
|
|
}
|
2019-06-28 18:06:38 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 21:40:29 +00:00
|
|
|
static void * getCallerAddress(const ucontext_t & context)
|
2019-06-28 18:06:38 +00:00
|
|
|
{
|
|
|
|
#if defined(__x86_64__)
|
|
|
|
/// Get the address at the time the signal was raised from the RIP (x86-64)
|
2022-06-10 08:22:31 +00:00
|
|
|
# if defined(OS_FREEBSD)
|
2019-06-28 18:06:38 +00:00
|
|
|
return reinterpret_cast<void *>(context.uc_mcontext.mc_rip);
|
2022-06-10 08:22:31 +00:00
|
|
|
# elif defined(OS_DARWIN)
|
2019-06-28 18:06:38 +00:00
|
|
|
return reinterpret_cast<void *>(context.uc_mcontext->__ss.__rip);
|
2020-05-29 21:36:47 +00:00
|
|
|
# else
|
2019-06-28 18:06:38 +00:00
|
|
|
return reinterpret_cast<void *>(context.uc_mcontext.gregs[REG_RIP]);
|
2020-05-29 21:36:47 +00:00
|
|
|
# endif
|
2022-06-10 08:22:31 +00:00
|
|
|
#elif defined(OS_DARWIN) && defined(__aarch64__)
|
2021-03-11 17:12:52 +00:00
|
|
|
return reinterpret_cast<void *>(context.uc_mcontext->__ss.__pc);
|
2022-06-10 08:22:31 +00:00
|
|
|
#elif defined(OS_FREEBSD) && defined(__aarch64__)
|
2021-07-22 12:06:45 +00:00
|
|
|
return reinterpret_cast<void *>(context.uc_mcontext.mc_gpregs.gp_elr);
|
2019-06-28 18:06:38 +00:00
|
|
|
#elif defined(__aarch64__)
|
|
|
|
return reinterpret_cast<void *>(context.uc_mcontext.pc);
|
2022-08-19 18:45:51 +00:00
|
|
|
#elif defined(__powerpc64__) && defined(__linux__)
|
2021-04-01 07:44:26 +00:00
|
|
|
return reinterpret_cast<void *>(context.uc_mcontext.gp_regs[PT_NIP]);
|
2022-08-19 18:45:51 +00:00
|
|
|
#elif defined(__powerpc64__) && defined(__FreeBSD__)
|
|
|
|
return reinterpret_cast<void *>(context.uc_mcontext.mc_srr0);
|
2021-11-11 18:18:41 +00:00
|
|
|
#elif defined(__riscv)
|
|
|
|
return reinterpret_cast<void *>(context.uc_mcontext.__gregs[REG_PC]);
|
2023-01-20 21:16:55 +00:00
|
|
|
#elif defined(__s390x__)
|
|
|
|
return reinterpret_cast<void *>(context.uc_mcontext.psw.addr);
|
2019-07-29 22:26:44 +00:00
|
|
|
#else
|
2019-06-28 18:06:38 +00:00
|
|
|
return nullptr;
|
2019-07-29 22:26:44 +00:00
|
|
|
#endif
|
2019-06-28 18:06:38 +00:00
|
|
|
}
|
|
|
|
|
2023-03-02 22:27:54 +00:00
|
|
|
// FIXME: looks like this is used only for Sentry but duplicates the whole algo, maybe replace?
|
2021-09-28 21:01:16 +00:00
|
|
|
void StackTrace::symbolize(
|
2023-03-02 22:27:54 +00:00
|
|
|
const StackTrace::FramePointers & frame_pointers, [[maybe_unused]] size_t offset, size_t size, StackTrace::Frames & frames)
|
2020-05-27 19:11:04 +00:00
|
|
|
{
|
2022-06-10 08:22:31 +00:00
|
|
|
#if defined(__ELF__) && !defined(OS_FREEBSD)
|
2020-11-30 14:30:55 +00:00
|
|
|
auto symbol_index_ptr = DB::SymbolIndex::instance();
|
|
|
|
const DB::SymbolIndex & symbol_index = *symbol_index_ptr;
|
2020-05-27 19:11:04 +00:00
|
|
|
std::unordered_map<std::string, DB::Dwarf> dwarfs;
|
|
|
|
|
2020-05-29 21:36:47 +00:00
|
|
|
for (size_t i = 0; i < offset; ++i)
|
2020-06-10 13:30:12 +00:00
|
|
|
frames[i].virtual_addr = frame_pointers[i];
|
2020-05-27 19:11:04 +00:00
|
|
|
|
|
|
|
for (size_t i = offset; i < size; ++i)
|
|
|
|
{
|
2020-06-10 13:30:12 +00:00
|
|
|
StackTrace::Frame & current_frame = frames[i];
|
2020-05-27 19:11:04 +00:00
|
|
|
current_frame.virtual_addr = frame_pointers[i];
|
|
|
|
const auto * object = symbol_index.findObject(current_frame.virtual_addr);
|
|
|
|
uintptr_t virtual_offset = object ? uintptr_t(object->address_begin) : 0;
|
|
|
|
current_frame.physical_addr = reinterpret_cast<void *>(uintptr_t(current_frame.virtual_addr) - virtual_offset);
|
|
|
|
|
|
|
|
if (object)
|
|
|
|
{
|
|
|
|
current_frame.object = object->name;
|
2023-03-02 22:27:54 +00:00
|
|
|
if (std::error_code ec; std::filesystem::exists(current_frame.object.value(), ec) && !ec)
|
2020-05-27 19:11:04 +00:00
|
|
|
{
|
2021-01-20 13:03:25 +00:00
|
|
|
auto dwarf_it = dwarfs.try_emplace(object->name, object->elf).first;
|
2020-05-27 19:11:04 +00:00
|
|
|
|
|
|
|
DB::Dwarf::LocationInfo location;
|
2021-01-19 12:34:27 +00:00
|
|
|
std::vector<DB::Dwarf::SymbolizedFrame> inline_frames;
|
|
|
|
if (dwarf_it->second.findAddress(
|
|
|
|
uintptr_t(current_frame.physical_addr), location, DB::Dwarf::LocationInfoMode::FAST, inline_frames))
|
2020-05-29 21:36:47 +00:00
|
|
|
{
|
2020-05-27 19:11:04 +00:00
|
|
|
current_frame.file = location.file.toString();
|
|
|
|
current_frame.line = location.line;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
current_frame.object = "?";
|
|
|
|
|
2023-03-02 22:27:54 +00:00
|
|
|
if (const auto * symbol = symbol_index.findSymbol(current_frame.virtual_addr))
|
|
|
|
current_frame.symbol = demangle(symbol->name);
|
2020-05-27 19:11:04 +00:00
|
|
|
else
|
|
|
|
current_frame.symbol = "?";
|
|
|
|
}
|
2020-05-29 21:36:47 +00:00
|
|
|
#else
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
2020-06-10 13:30:12 +00:00
|
|
|
frames[i].virtual_addr = frame_pointers[i];
|
2020-05-27 19:11:04 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-07-01 22:11:11 +00:00
|
|
|
StackTrace::StackTrace(const ucontext_t & signal_context)
|
2019-06-28 18:06:38 +00:00
|
|
|
{
|
2019-07-01 22:11:11 +00:00
|
|
|
tryCapture();
|
2019-06-28 18:06:38 +00:00
|
|
|
|
2021-02-02 03:46:54 +00:00
|
|
|
/// This variable from signal handler is not instrumented by Memory Sanitizer.
|
|
|
|
__msan_unpoison(&signal_context, sizeof(signal_context));
|
|
|
|
|
2019-07-31 21:40:29 +00:00
|
|
|
void * caller_address = getCallerAddress(signal_context);
|
|
|
|
|
|
|
|
if (size == 0 && caller_address)
|
2019-07-01 22:11:11 +00:00
|
|
|
{
|
2020-05-27 19:11:04 +00:00
|
|
|
frame_pointers[0] = caller_address;
|
2019-07-31 21:40:29 +00:00
|
|
|
size = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/// Skip excessive stack frames that we have created while finding stack trace.
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
2020-05-27 19:11:04 +00:00
|
|
|
if (frame_pointers[i] == caller_address)
|
2019-07-31 21:40:29 +00:00
|
|
|
{
|
|
|
|
offset = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-06-28 18:06:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-01 22:11:11 +00:00
|
|
|
void StackTrace::tryCapture()
|
|
|
|
{
|
|
|
|
#if USE_UNWIND
|
2020-05-27 19:11:04 +00:00
|
|
|
size = unw_backtrace(frame_pointers.data(), capacity);
|
|
|
|
__msan_unpoison(frame_pointers.data(), size * sizeof(frame_pointers[0]));
|
2023-03-02 22:27:54 +00:00
|
|
|
#else
|
|
|
|
size = 0;
|
2019-07-01 22:11:11 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-03-06 22:30:44 +00:00
|
|
|
/// ClickHouse uses bundled libc++ so type names will be the same on every system thus it's safe to hardcode them
|
2023-03-02 22:27:54 +00:00
|
|
|
constexpr std::pair<std::string_view, std::string_view> replacements[]
|
|
|
|
= {{"::__1", ""}, {"std::basic_string<char, std::char_traits<char>, std::allocator<char>>", "String"}};
|
|
|
|
|
|
|
|
String collapseNames(String && haystack)
|
2019-07-01 22:11:11 +00:00
|
|
|
{
|
2023-03-02 22:27:54 +00:00
|
|
|
// TODO: surely there is a written version already for better in place search&replace
|
|
|
|
for (auto [needle, to] : replacements)
|
|
|
|
{
|
|
|
|
size_t pos = 0;
|
|
|
|
while ((pos = haystack.find(needle, pos)) != std::string::npos)
|
|
|
|
{
|
|
|
|
haystack.replace(pos, needle.length(), to);
|
|
|
|
pos += to.length();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return haystack;
|
2019-06-28 18:06:38 +00:00
|
|
|
}
|
|
|
|
|
2023-03-02 22:27:54 +00:00
|
|
|
struct StackTraceRefTriple
|
2019-07-01 22:11:11 +00:00
|
|
|
{
|
2023-03-02 22:27:54 +00:00
|
|
|
const StackTrace::FramePointers & pointers;
|
|
|
|
size_t offset;
|
|
|
|
size_t size;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct StackTraceTriple
|
|
|
|
{
|
|
|
|
StackTrace::FramePointers pointers;
|
|
|
|
size_t offset;
|
|
|
|
size_t size;
|
|
|
|
};
|
2019-06-28 18:06:38 +00:00
|
|
|
|
2023-03-02 22:27:54 +00:00
|
|
|
template <class T>
|
|
|
|
concept MaybeRef = std::is_same_v<T, StackTraceTriple> || std::is_same_v<T, StackTraceRefTriple>;
|
|
|
|
|
|
|
|
constexpr bool operator<(const MaybeRef auto & left, const MaybeRef auto & right)
|
2020-05-27 19:11:04 +00:00
|
|
|
{
|
2023-03-02 22:27:54 +00:00
|
|
|
return std::tuple{left.pointers, left.size, left.offset} < std::tuple{right.pointers, right.size, right.offset};
|
2020-05-27 19:11:04 +00:00
|
|
|
}
|
|
|
|
|
2023-03-02 22:27:54 +00:00
|
|
|
static void
|
|
|
|
toStringEveryLineImpl([[maybe_unused]] bool fatal, const StackTraceRefTriple & stack_trace, Fn<void(std::string_view)> auto && callback)
|
2019-06-28 18:06:38 +00:00
|
|
|
{
|
2023-03-02 22:27:54 +00:00
|
|
|
if (stack_trace.size == 0)
|
2019-08-17 22:39:26 +00:00
|
|
|
return callback("<Empty trace>");
|
2019-06-28 18:06:38 +00:00
|
|
|
|
2022-06-10 08:22:31 +00:00
|
|
|
#if defined(__ELF__) && !defined(OS_FREEBSD)
|
2023-03-02 22:27:54 +00:00
|
|
|
|
|
|
|
using enum DB::Dwarf::LocationInfoMode;
|
|
|
|
const auto mode = fatal ? FULL_WITH_INLINE : FAST;
|
|
|
|
|
2020-11-30 14:30:55 +00:00
|
|
|
auto symbol_index_ptr = DB::SymbolIndex::instance();
|
|
|
|
const DB::SymbolIndex & symbol_index = *symbol_index_ptr;
|
2023-03-02 22:27:54 +00:00
|
|
|
std::unordered_map<String, DB::Dwarf> dwarfs;
|
2020-06-22 07:22:13 +00:00
|
|
|
|
2023-03-02 22:27:54 +00:00
|
|
|
for (size_t i = stack_trace.offset; i < stack_trace.size; ++i)
|
2019-06-28 18:06:38 +00:00
|
|
|
{
|
2021-01-20 13:03:25 +00:00
|
|
|
std::vector<DB::Dwarf::SymbolizedFrame> inline_frames;
|
2023-03-02 22:27:54 +00:00
|
|
|
const void * virtual_addr = stack_trace.pointers[i];
|
2020-06-22 07:22:13 +00:00
|
|
|
const auto * object = symbol_index.findObject(virtual_addr);
|
|
|
|
uintptr_t virtual_offset = object ? uintptr_t(object->address_begin) : 0;
|
|
|
|
const void * physical_addr = reinterpret_cast<const void *>(uintptr_t(virtual_addr) - virtual_offset);
|
|
|
|
|
2023-05-06 18:54:52 +00:00
|
|
|
DB::WriteBufferFromOwnString out;
|
2020-01-28 14:39:24 +00:00
|
|
|
out << i << ". ";
|
2019-06-28 18:06:38 +00:00
|
|
|
|
2023-03-02 22:27:54 +00:00
|
|
|
if (std::error_code ec; object && std::filesystem::exists(object->name, ec) && !ec)
|
2019-07-29 22:26:44 +00:00
|
|
|
{
|
2023-03-02 22:27:54 +00:00
|
|
|
auto dwarf_it = dwarfs.try_emplace(object->name, object->elf).first;
|
2020-01-28 14:39:24 +00:00
|
|
|
|
2023-03-02 22:27:54 +00:00
|
|
|
DB::Dwarf::LocationInfo location;
|
2019-07-29 22:26:44 +00:00
|
|
|
|
2023-03-02 22:27:54 +00:00
|
|
|
if (dwarf_it->second.findAddress(uintptr_t(physical_addr), location, mode, inline_frames))
|
|
|
|
out << location.file.toString() << ":" << location.line << ": ";
|
2020-05-27 19:11:04 +00:00
|
|
|
}
|
2023-03-02 22:27:54 +00:00
|
|
|
|
|
|
|
if (const auto * const symbol = symbol_index.findSymbol(virtual_addr))
|
|
|
|
out << collapseNames(demangle(symbol->name));
|
2020-06-22 07:22:13 +00:00
|
|
|
else
|
|
|
|
out << "?";
|
|
|
|
|
2022-07-29 23:45:06 +00:00
|
|
|
if (shouldShowAddress(physical_addr))
|
2023-05-06 16:14:01 +00:00
|
|
|
{
|
|
|
|
out << " @ ";
|
|
|
|
DB::writePointerHex(physical_addr, out);
|
|
|
|
}
|
2022-07-28 22:13:02 +00:00
|
|
|
|
2020-06-22 07:22:13 +00:00
|
|
|
out << " in " << (object ? object->name : "?");
|
2019-08-21 00:48:34 +00:00
|
|
|
|
2021-01-20 13:03:25 +00:00
|
|
|
for (size_t j = 0; j < inline_frames.size(); ++j)
|
|
|
|
{
|
|
|
|
const auto & frame = inline_frames[j];
|
2023-03-02 22:27:54 +00:00
|
|
|
callback(fmt::format(
|
|
|
|
"{}.{}. inlined from {}:{}: {}",
|
|
|
|
i,
|
|
|
|
j + 1,
|
|
|
|
frame.location.file.toString(),
|
|
|
|
frame.location.line,
|
|
|
|
collapseNames(demangle(frame.name))));
|
2021-01-20 13:03:25 +00:00
|
|
|
}
|
|
|
|
|
2019-08-21 00:48:34 +00:00
|
|
|
callback(out.str());
|
|
|
|
}
|
2020-06-22 07:22:13 +00:00
|
|
|
#else
|
2023-03-02 22:27:54 +00:00
|
|
|
for (size_t i = stack_trace.offset; i < stack_trace.size; ++i)
|
|
|
|
if (const void * const addr = stack_trace.pointers[i]; shouldShowAddress(addr))
|
|
|
|
callback(fmt::format("{}. {}", i, addr));
|
2020-06-22 07:22:13 +00:00
|
|
|
#endif
|
2019-08-17 22:39:26 +00:00
|
|
|
}
|
2019-06-28 18:06:38 +00:00
|
|
|
|
2023-03-02 22:27:54 +00:00
|
|
|
void StackTrace::toStringEveryLine(std::function<void(std::string_view)> callback) const
|
2020-01-02 06:56:53 +00:00
|
|
|
{
|
2023-03-02 22:27:54 +00:00
|
|
|
toStringEveryLineImpl(true, {frame_pointers, offset, size}, std::move(callback));
|
2021-08-25 13:05:48 +00:00
|
|
|
}
|
|
|
|
|
2023-03-02 22:27:54 +00:00
|
|
|
using StackTraceCache = std::map<StackTraceTriple, String, std::less<>>;
|
2022-09-25 12:06:13 +00:00
|
|
|
|
|
|
|
static StackTraceCache & cacheInstance()
|
2021-08-28 03:43:43 +00:00
|
|
|
{
|
2022-09-25 12:06:13 +00:00
|
|
|
static StackTraceCache cache;
|
2021-08-28 03:43:43 +00:00
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
2022-09-25 12:06:13 +00:00
|
|
|
static std::mutex stacktrace_cache_mutex;
|
|
|
|
|
2023-03-02 22:27:54 +00:00
|
|
|
String toStringCached(const StackTrace::FramePointers & pointers, size_t offset, size_t size)
|
2021-08-25 13:05:48 +00:00
|
|
|
{
|
|
|
|
/// Calculation of stack trace text is extremely slow.
|
|
|
|
/// We use simple cache because otherwise the server could be overloaded by trash queries.
|
2022-09-25 12:06:13 +00:00
|
|
|
/// Note that this cache can grow unconditionally, but practically it should be small.
|
|
|
|
std::lock_guard lock{stacktrace_cache_mutex};
|
|
|
|
|
2023-03-02 22:27:54 +00:00
|
|
|
StackTraceCache & cache = cacheInstance();
|
2023-03-04 18:37:28 +00:00
|
|
|
const StackTraceRefTriple key{pointers, offset, size};
|
2023-03-02 22:27:54 +00:00
|
|
|
|
2023-03-04 18:37:28 +00:00
|
|
|
if (auto it = cache.find(key); it != cache.end())
|
2023-03-02 22:27:54 +00:00
|
|
|
return it->second;
|
|
|
|
else
|
|
|
|
{
|
2023-05-06 16:14:01 +00:00
|
|
|
DB::WriteBufferFromOwnString out;
|
2023-03-04 18:37:28 +00:00
|
|
|
toStringEveryLineImpl(false, key, [&](std::string_view str) { out << str << '\n'; });
|
2023-03-02 22:27:54 +00:00
|
|
|
|
|
|
|
return cache.emplace(StackTraceTriple{pointers, offset, size}, out.str()).first->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string StackTrace::toString() const
|
|
|
|
{
|
|
|
|
return toStringCached(frame_pointers, offset, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string StackTrace::toString(void ** frame_pointers_raw, size_t offset, size_t size)
|
|
|
|
{
|
|
|
|
__msan_unpoison(frame_pointers_raw, size * sizeof(*frame_pointers_raw));
|
|
|
|
|
2023-03-04 18:37:28 +00:00
|
|
|
StackTrace::FramePointers frame_pointers{};
|
2023-03-02 22:27:54 +00:00
|
|
|
std::copy_n(frame_pointers_raw, size, frame_pointers.begin());
|
2022-09-25 12:06:13 +00:00
|
|
|
|
2023-03-02 22:27:54 +00:00
|
|
|
return toStringCached(frame_pointers, offset, size);
|
2021-08-28 03:43:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StackTrace::dropCache()
|
|
|
|
{
|
2022-09-25 12:06:13 +00:00
|
|
|
std::lock_guard lock{stacktrace_cache_mutex};
|
|
|
|
cacheInstance().clear();
|
2020-01-02 06:56:53 +00:00
|
|
|
}
|