Skip to content

Commit 12636b4

Browse files
simplify layout of stacktrace files: condense, collapse
1 parent 8ca8c15 commit 12636b4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1307
-1885
lines changed

libcxx/include/CMakeLists.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -728,10 +728,7 @@ set(files
728728
__ranges/zip_view.h
729729
__split_buffer
730730
__stacktrace/basic_stacktrace.h
731-
__stacktrace/alloc.h
732-
__stacktrace/context.h
733-
__stacktrace/entry.h
734-
__stacktrace/to_string.h
731+
__stacktrace/impl.h
735732
__stacktrace/stacktrace_entry.h
736733
__std_mbstate_t.h
737734
__stop_token/atomic_unique_lock.h

libcxx/include/__stacktrace/alloc.h

Lines changed: 0 additions & 97 deletions
This file was deleted.

libcxx/include/__stacktrace/basic_stacktrace.h

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#ifndef _LIBCPP_BASIC_STACKTRACE
1111
#define _LIBCPP_BASIC_STACKTRACE
1212

13-
#include <__stacktrace/entry.h>
13+
#include <__stacktrace/impl.h>
1414
#include <__stacktrace/stacktrace_entry.h>
1515

1616
#include <__config>
@@ -33,13 +33,7 @@
3333
#include <__vector/swap.h>
3434
#include <__vector/vector.h>
3535
#include <cstddef>
36-
#include <list>
3736
#include <memory>
38-
#include <string>
39-
40-
#include <__stacktrace/alloc.h>
41-
#include <__stacktrace/context.h>
42-
#include <__stacktrace/to_string.h>
4337

4438
_LIBCPP_BEGIN_NAMESPACE_STD
4539

@@ -67,13 +61,7 @@ class _LIBCPP_EXPORTED_FROM_ABI basic_stacktrace {
6761
[[no_unique_address]] _Allocator __alloc_;
6862
__entry_vec __entries_;
6963

70-
_LIBCPP_HIDE_FROM_ABI basic_stacktrace(const _Allocator& __alloc, std::pmr::list<__stacktrace::entry>&& __vec)
71-
: __alloc_(__alloc), __entries_(__alloc) {
72-
__entries_.reserve(__vec.size());
73-
for (auto& __entry : __vec) {
74-
__entries_.emplace_back(std::move(__entry));
75-
}
76-
}
64+
// _LIBCPP_HIDE_FROM_ABI basic_stacktrace(const _Allocator& __alloc, std::pmr::list<stacktrace_entry>&& __vec);
7765

7866
public:
7967
// (19.6.4.1)
@@ -96,28 +84,26 @@ class _LIBCPP_EXPORTED_FROM_ABI basic_stacktrace {
9684

9785
_LIBCPP_NO_TAIL_CALLS _LIBCPP_NOINLINE _LIBCPP_EXPORTED_FROM_ABI static basic_stacktrace
9886
current(const allocator_type& __caller_alloc = allocator_type()) noexcept(__kNoThrowAlloc) {
99-
__stacktrace::alloc __alloc(__caller_alloc);
100-
__stacktrace::context __tr{__alloc};
101-
__tr.do_stacktrace(1, /* infinite max_depth */ ~0);
102-
return {__caller_alloc, std::move(__tr.__entries_)};
87+
return current(1, /* no __max_depth */ ~0, __caller_alloc);
10388
}
10489

10590
_LIBCPP_NO_TAIL_CALLS _LIBCPP_NOINLINE _LIBCPP_EXPORTED_FROM_ABI static basic_stacktrace
10691
current(size_type __skip, const allocator_type& __caller_alloc = allocator_type()) noexcept(__kNoThrowAlloc) {
107-
__stacktrace::alloc __alloc(__caller_alloc);
108-
__stacktrace::context __tr{__alloc};
109-
__tr.do_stacktrace(__skip + 1, /* infinite max_depth */ ~0);
110-
return {__caller_alloc, std::move(__tr.__entries_)};
92+
return current(__skip + 1, /* no __max_depth */ ~0, __caller_alloc);
11193
}
11294

11395
_LIBCPP_NO_TAIL_CALLS _LIBCPP_NOINLINE _LIBCPP_EXPORTED_FROM_ABI static basic_stacktrace
11496
current(size_type __skip,
11597
size_type __max_depth,
11698
const allocator_type& __caller_alloc = allocator_type()) noexcept(__kNoThrowAlloc) {
99+
basic_stacktrace<_Allocator> __ret{__caller_alloc};
117100
__stacktrace::alloc __alloc(__caller_alloc);
118-
__stacktrace::context __tr{__alloc};
119-
__tr.do_stacktrace(__skip + 1, __max_depth);
120-
return {__caller_alloc, std::move(__tr.__entries_)};
101+
auto __resize = [&__ret](size_t __sz) { __ret.__entries_.resize(__sz); };
102+
auto __assign = [&__ret](size_t __index, stacktrace_entry&& __entry) {
103+
__ret.__entries_.at(__index) = std::move(__entry);
104+
};
105+
__stacktrace::__impl(__skip + 1, __max_depth, __alloc, __resize, __assign);
106+
return __ret;
121107
}
122108

123109
_LIBCPP_EXPORTED_FROM_ABI constexpr ~basic_stacktrace() = default;

libcxx/include/__stacktrace/entry.h

Lines changed: 0 additions & 44 deletions
This file was deleted.

libcxx/include/__stacktrace/impl.h

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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

Comments
 (0)