|
| 1 | +// Eggs.Stacktrace |
| 2 | +// |
| 3 | +// Copyright (c) 2026 Agustin Berge |
| 4 | +// |
| 5 | +// Distributed under the Boost Software License, Version 1.0. |
| 6 | +// See accompanying file LICENSE.txt or copy at |
| 7 | +// http://www.boost.org/LICENSE_1_0.txt |
| 8 | + |
| 9 | +// dladdr() is a GNU extension; expose it before any system headers. |
| 10 | +#ifndef _GNU_SOURCE |
| 11 | +# define _GNU_SOURCE |
| 12 | +#endif |
| 13 | + |
| 14 | +#include <eggs/stacktrace.hpp> |
| 15 | + |
| 16 | +#include <algorithm> |
| 17 | +#include <cassert> |
| 18 | +#include <cstddef> |
| 19 | +#include <cstdint> |
| 20 | +#include <cstdlib> |
| 21 | +#include <limits> |
| 22 | +#include <string> |
| 23 | +#include <vector> |
| 24 | + |
| 25 | +#include "detail/asan_backtrace.hpp" |
| 26 | +#include "detail/backend.hpp" |
| 27 | + |
| 28 | +#include <cxxabi.h> |
| 29 | +#include <dlfcn.h> |
| 30 | +#include <execinfo.h> |
| 31 | + |
| 32 | +namespace eggs { |
| 33 | +namespace detail { |
| 34 | + |
| 35 | +void capture( |
| 36 | + std::vector<stacktrace_entry>& frames, std::size_t skip, |
| 37 | + std::size_t max_depth |
| 38 | +) noexcept |
| 39 | +{ |
| 40 | + if (max_depth == 0) return; |
| 41 | + |
| 42 | + // +1 to skip this capture() frame itself in the backtrace. |
| 43 | + std::size_t const overhead = |
| 44 | + 1 + (asan_backtrace_adds_extra_frame() ? 1 : 0); |
| 45 | + if (skip > std::numeric_limits<std::size_t>::max() - overhead) return; |
| 46 | + std::size_t const first = skip + overhead; |
| 47 | + |
| 48 | + constexpr int kInitial = 128; |
| 49 | + constexpr int kHardLimit = 1 << 16; |
| 50 | + |
| 51 | + void* stack_buf[kInitial]; |
| 52 | + void** buf = stack_buf; |
| 53 | + int cap = kInitial; |
| 54 | + int got = ::backtrace(buf, cap); |
| 55 | + assert(got >= 0 && "backtrace() returned a negative count"); |
| 56 | + |
| 57 | + std::vector<void*> heap_buf; |
| 58 | + try { |
| 59 | + while (got == cap && cap < kHardLimit) { |
| 60 | + std::size_t const available = |
| 61 | + static_cast<std::size_t>(cap) > first |
| 62 | + ? static_cast<std::size_t>(cap) - first |
| 63 | + : 0; |
| 64 | + if (available >= max_depth) break; |
| 65 | + |
| 66 | + cap *= 2; |
| 67 | + heap_buf.resize(static_cast<std::size_t>(cap)); |
| 68 | + buf = heap_buf.data(); |
| 69 | + got = ::backtrace(buf, cap); |
| 70 | + assert(got >= 0 && "backtrace() returned a negative count"); |
| 71 | + } |
| 72 | + } catch (...) { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + std::size_t const got_sz = static_cast<std::size_t>(got); |
| 77 | + if (got_sz <= first) return; |
| 78 | + |
| 79 | + std::size_t const take = std::min(got_sz - first, max_depth); |
| 80 | + try { |
| 81 | + for (std::size_t i = first; i < first + take; ++i) |
| 82 | + frames.push_back(capture_helper::make(buf[i])); |
| 83 | + } catch (...) { |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +std::string symbolize_description(void* address) |
| 88 | +{ |
| 89 | + if (!address) return {}; |
| 90 | + |
| 91 | + ::Dl_info info{}; |
| 92 | + if (::dladdr(address, &info) == 0 || !info.dli_sname) return {}; |
| 93 | + |
| 94 | + int status = -1; |
| 95 | + char* demangled = |
| 96 | + abi::__cxa_demangle(info.dli_sname, nullptr, nullptr, &status); |
| 97 | + std::string result; |
| 98 | + try { |
| 99 | + result = (status == 0 && demangled) ? demangled : info.dli_sname; |
| 100 | + } catch (...) { |
| 101 | + std::free(demangled); |
| 102 | + throw; |
| 103 | + } |
| 104 | + std::free(demangled); |
| 105 | + return result; |
| 106 | +} |
| 107 | + |
| 108 | +std::string symbolize_source_file(void*) |
| 109 | +{ |
| 110 | + return {}; |
| 111 | +} |
| 112 | + |
| 113 | +std::uint_least32_t symbolize_source_line(void*) |
| 114 | +{ |
| 115 | + return 0; |
| 116 | +} |
| 117 | + |
| 118 | +} // namespace detail |
| 119 | +} // namespace eggs |
0 commit comments