|
| 1 | +// -*- C++ -*- |
| 2 | +//===----------------------------------------------------------------------===// |
| 3 | +// |
| 4 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | +// See https://llvm.org/LICENSE.txt for license information. |
| 6 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | + |
| 10 | +#ifndef _LIBCPP_STACKTRACE_IMPL |
| 11 | +#define _LIBCPP_STACKTRACE_IMPL |
| 12 | + |
| 13 | +#include <__cstddef/byte.h> |
| 14 | +#include <__cstddef/ptrdiff_t.h> |
| 15 | +#include <__cstddef/size_t.h> |
| 16 | +#include <__format/formatter.h> |
| 17 | +#include <__functional/function.h> |
| 18 | +#include <__functional/hash.h> |
| 19 | +#include <__fwd/format.h> |
| 20 | +#include <__fwd/ostream.h> |
| 21 | +#include <__fwd/sstream.h> |
| 22 | +#include <__fwd/vector.h> |
| 23 | +#include <__iterator/iterator.h> |
| 24 | +#include <__iterator/iterator_traits.h> |
| 25 | +#include <__iterator/reverse_access.h> |
| 26 | +#include <__iterator/reverse_iterator.h> |
| 27 | +#include <__memory/allocator.h> |
| 28 | +#include <__memory/allocator_traits.h> |
| 29 | +#include <__memory_resource/memory_resource.h> |
| 30 | +#include <__memory_resource/polymorphic_allocator.h> |
| 31 | +#include <__utility/move.h> |
| 32 | +#include <__vector/pmr.h> |
| 33 | +#include <__vector/swap.h> |
| 34 | +#include <__vector/vector.h> |
| 35 | +#include <list> |
| 36 | +#include <string> |
| 37 | + |
| 38 | +_LIBCPP_BEGIN_NAMESPACE_STD |
| 39 | + |
| 40 | +template <class _Allocator> |
| 41 | +class basic_stacktrace; |
| 42 | + |
| 43 | +class stacktrace_entry; |
| 44 | + |
| 45 | +namespace __stacktrace { |
| 46 | + |
| 47 | +/** Per-stacktrace-invocation allocator which wraps a caller-provided allocator of any type. |
| 48 | +This is intended to be used with `std::pmr::` containers and strings throughout the stacktrace |
| 49 | +creation process. */ |
| 50 | +struct _LIBCPP_HIDE_FROM_ABI alloc final : std::pmr::memory_resource { |
| 51 | + template <class _Allocator> |
| 52 | + explicit alloc(_Allocator const& __a) { |
| 53 | + // Take the given allocator type, and rebind with a new type having <byte> as the template arg |
| 54 | + using _AT = std::allocator_traits<_Allocator>; |
| 55 | + using _BA = typename _AT::template rebind_alloc<std::byte>; |
| 56 | + auto __ba = _BA(__a); |
| 57 | + __alloc_func_ = [__ba](size_t __sz) mutable { return __ba.allocate(__sz); }; |
| 58 | + __dealloc_func_ = [__ba](void* __ptr, size_t __sz) mutable { return __ba.deallocate((std::byte*)__ptr, __sz); }; |
| 59 | + __alloc_ptr_ = std::addressof(__a); |
| 60 | + } |
| 61 | + |
| 62 | + _LIBCPP_HIDE_FROM_ABI ~alloc() override = default; |
| 63 | + |
| 64 | + _LIBCPP_HIDE_FROM_ABI void* do_allocate(size_t __size, size_t /*__align*/) override { return __alloc_func_(__size); } |
| 65 | + |
| 66 | + _LIBCPP_HIDE_FROM_ABI void do_deallocate(void* __ptr, size_t __size, size_t /*__align*/) override { |
| 67 | + __dealloc_func_((std::byte*)__ptr, __size); |
| 68 | + } |
| 69 | + |
| 70 | + _LIBCPP_HIDE_FROM_ABI bool do_is_equal(std::pmr::memory_resource const& __rhs) const noexcept override { |
| 71 | + auto* __rhs_ba = dynamic_cast<alloc const*>(&__rhs); |
| 72 | + return __rhs_ba && (__rhs_ba->__alloc_ptr_ == __alloc_ptr_); |
| 73 | + } |
| 74 | + |
| 75 | + _LIBCPP_HIDE_FROM_ABI std::pmr::string new_string(size_t __size = 0) { |
| 76 | + std::pmr::string __ret{this}; |
| 77 | + if (__size) { |
| 78 | + __ret.reserve(__size); |
| 79 | + __ret[0] = 0; |
| 80 | + } |
| 81 | + return __ret; |
| 82 | + } |
| 83 | + |
| 84 | + _LIBCPP_HIDE_FROM_ABI std::pmr::string hex_string(uintptr_t __addr) { |
| 85 | + char __ret[19]; // "0x" + 16 digits + NUL |
| 86 | + auto __size = snprintf(__ret, sizeof(__ret), "0x%016llx", (unsigned long long)__addr); |
| 87 | + return {__ret, size_t(__size), this}; |
| 88 | + } |
| 89 | + |
| 90 | + _LIBCPP_HIDE_FROM_ABI std::pmr::string u64_string(uintptr_t __val) { |
| 91 | + char __ret[21]; // 20 digits max + NUL |
| 92 | + auto __size = snprintf(__ret, sizeof(__ret), "%zu", __val); |
| 93 | + return {__ret, size_t(__size), this}; |
| 94 | + } |
| 95 | + |
| 96 | + template <typename _Tp> |
| 97 | + _LIBCPP_HIDE_FROM_ABI std::pmr::list<_Tp> new_list() { |
| 98 | + return std::pmr::list<_Tp>{this}; |
| 99 | + } |
| 100 | + |
| 101 | + std::pmr::list<std::pmr::string> new_string_list() { return new_list<std::pmr::string>(); } |
| 102 | + |
| 103 | +private: |
| 104 | + std::function<std::byte*(size_t)> __alloc_func_; |
| 105 | + std::function<void(std::byte*, size_t)> __dealloc_func_; |
| 106 | + /** Only used for checking equality */ |
| 107 | + void const* __alloc_ptr_; |
| 108 | +}; |
| 109 | + |
| 110 | +/** Contains fields which will be used to generate the final `std::stacktrace_entry`. |
| 111 | +This is an intermediate object which owns strings allocated via the caller-provided allocator, |
| 112 | +which are later freed back to that allocator and converted to plain `std::string`s. */ |
| 113 | +struct entry { |
| 114 | + /** Caller's / faulting insn's address, including ASLR/slide */ |
| 115 | + uintptr_t __addr_{}; |
| 116 | + |
| 117 | + /** the address minus its image's slide offset */ |
| 118 | + uintptr_t __addr_unslid_{}; |
| 119 | + |
| 120 | + /** entry's description (symbol name) */ |
| 121 | + std::pmr::string __desc_{}; |
| 122 | + |
| 123 | + /** source file name */ |
| 124 | + std::pmr::string __file_{}; |
| 125 | + |
| 126 | + /** line number in source file */ |
| 127 | + uint32_t __line_{}; |
| 128 | + |
| 129 | + /* implicit */ operator std::stacktrace_entry(); |
| 130 | +}; |
| 131 | + |
| 132 | +struct __to_string { |
| 133 | + _LIBCPP_EXPORTED_FROM_ABI string operator()(stacktrace_entry const& __entry); |
| 134 | + |
| 135 | + _LIBCPP_EXPORTED_FROM_ABI void operator()(ostream& __os, stacktrace_entry const& __entry); |
| 136 | + |
| 137 | + _LIBCPP_EXPORTED_FROM_ABI void operator()(ostream& __os, std::stacktrace_entry const* __entries, size_t __count); |
| 138 | + |
| 139 | + _LIBCPP_EXPORTED_FROM_ABI string operator()(std::stacktrace_entry const* __entries, size_t __count); |
| 140 | + |
| 141 | + template <class _Allocator> |
| 142 | + _LIBCPP_EXPORTED_FROM_ABI string operator()(basic_stacktrace<_Allocator> const& __st) { |
| 143 | + return (*this)(__st.__entries_.data(), __st.__entries_.size()); |
| 144 | + } |
| 145 | +}; |
| 146 | + |
| 147 | +_LIBCPP_NO_TAIL_CALLS _LIBCPP_NOINLINE _LIBCPP_EXPORTED_FROM_ABI void |
| 148 | +__impl(size_t __skip, |
| 149 | + size_t __max_depth, |
| 150 | + alloc& __alloc, |
| 151 | + std::function<void(size_t)> __resize_func, |
| 152 | + std::function<void(size_t, std::stacktrace_entry&&)> __assign_func); |
| 153 | + |
| 154 | +} // namespace __stacktrace |
| 155 | +_LIBCPP_END_NAMESPACE_STD |
| 156 | + |
| 157 | +#endif // _LIBCPP_STACKTRACE_IMPL |
0 commit comments