Skip to content

Commit 9a32997

Browse files
StringWrapper, Windows
1 parent 6f1adb9 commit 9a32997

File tree

5 files changed

+57
-24
lines changed

5 files changed

+57
-24
lines changed

libcxx/include/__stacktrace/basic_stacktrace.h

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ _LIBCPP_PUSH_MACROS
3232
#include <__type_traits/is_nothrow_constructible.h>
3333
#include <__vector/vector.h>
3434
#include <cstddef>
35+
#include <iostream>
3536
#include <string>
3637
#include <utility>
3738

@@ -102,7 +103,12 @@ class basic_stacktrace : private __stacktrace::_Trace {
102103
friend struct __stacktrace::_Trace;
103104

104105
vector<stacktrace_entry, _Allocator> __entries_;
105-
_LIBCPP_HIDE_FROM_ABI _EntryIters entry_iters() { return {__entries_.data(), __entries_.size()}; }
106+
_LIBCPP_HIDE_FROM_ABI _EntryIters entry_iters() {
107+
auto* __data = __entries_.data();
108+
auto __size = __entries_.size();
109+
std::cerr << "@@@ data:" << __data << " size:" << __size << '\n';
110+
return {__data, __size};
111+
}
106112
_LIBCPP_HIDE_FROM_ABI __stacktrace::_Entry& entry_append() {
107113
return (__stacktrace::_Entry&)__entries_.emplace_back();
108114
}
@@ -133,14 +139,12 @@ class basic_stacktrace : private __stacktrace::_Trace {
133139
// Creation and assignment [stacktrace.basic.cons]
134140

135141
_LIBCPP_ALWAYS_INLINE // Omit this function from the trace
136-
static basic_stacktrace
137-
current(const allocator_type& __alloc = allocator_type()) noexcept {
142+
static basic_stacktrace current(const allocator_type& __alloc = allocator_type()) noexcept {
138143
return current(0, __default_max_depth, __alloc);
139144
}
140145

141146
_LIBCPP_ALWAYS_INLINE // Omit this function from the trace
142-
static basic_stacktrace
143-
current(size_type __skip, const allocator_type& __alloc = allocator_type()) noexcept {
147+
static basic_stacktrace current(size_type __skip, const allocator_type& __alloc = allocator_type()) noexcept {
144148
return current(__skip, __default_max_depth, __alloc);
145149
}
146150

@@ -169,19 +173,32 @@ class basic_stacktrace : private __stacktrace::_Trace {
169173
_LIBCPP_HIDE_FROM_ABI explicit basic_stacktrace(const allocator_type& __alloc)
170174
: _Trace(entry_iters_fn(), entry_append_fn()), __entries_(__alloc) {}
171175

172-
_LIBCPP_HIDE_FROM_ABI basic_stacktrace(const basic_stacktrace& __other) noexcept = default;
173-
_LIBCPP_HIDE_FROM_ABI basic_stacktrace(basic_stacktrace&& __other) noexcept = default;
176+
_LIBCPP_HIDE_FROM_ABI basic_stacktrace(const basic_stacktrace& __other) noexcept
177+
: _Trace(entry_iters_fn(), entry_append_fn()) {
178+
__entries_ = __other.__entries_;
179+
}
180+
181+
_LIBCPP_HIDE_FROM_ABI basic_stacktrace(basic_stacktrace&& __other) noexcept
182+
: _Trace(entry_iters_fn(), entry_append_fn()) {
183+
__entries_ = std::move(__other.__entries_);
184+
}
174185

175186
_LIBCPP_HIDE_FROM_ABI basic_stacktrace(const basic_stacktrace& __other, const allocator_type& __alloc)
176187
: _Trace(entry_iters_fn(), entry_append_fn()), __entries_(__other.__entries_, __alloc) {}
177188

178189
_LIBCPP_HIDE_FROM_ABI basic_stacktrace(basic_stacktrace&& __other, const allocator_type& __alloc)
179190
: _Trace(entry_iters_fn(), entry_append_fn()), __entries_(std::move(__other.__entries_), __alloc) {}
180191

181-
_LIBCPP_HIDE_FROM_ABI basic_stacktrace& operator=(const basic_stacktrace& __other) = default;
192+
_LIBCPP_HIDE_FROM_ABI basic_stacktrace& operator=(const basic_stacktrace& __other) {
193+
__entries_ = __other.__entries_;
194+
return *this;
195+
}
182196
_LIBCPP_HIDE_FROM_ABI basic_stacktrace& operator=(basic_stacktrace&& __other) noexcept(
183197
allocator_traits<_Allocator>::propagate_on_container_move_assignment::value ||
184-
allocator_traits<_Allocator>::is_always_equal::value) = default;
198+
allocator_traits<_Allocator>::is_always_equal::value) {
199+
__entries_ = std::move(__other.__entries_);
200+
return *this;
201+
}
185202

186203
_LIBCPP_HIDE_FROM_ABI ~basic_stacktrace() = default;
187204

@@ -336,8 +353,8 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE inline void _Trace::populate_addrs(s
336353

337354
# endif // _WIN32
338355

339-
_Trace& _Trace::base(auto& __trace) { return *static_cast<_Trace*>(&__trace); }
340-
_Trace const& _Trace::base(auto const& __trace) { return *static_cast<_Trace const*>(&__trace); }
356+
_Trace& _Trace::base(auto& __trace) { return *static_cast<_Trace*>(std::addressof(__trace)); }
357+
_Trace const& _Trace::base(auto const& __trace) { return *static_cast<_Trace const*>(std::addressof(__trace)); }
341358

342359
} // namespace __stacktrace
343360
_LIBCPP_END_NAMESPACE_STD

libcxx/include/__stacktrace/stacktrace_entry.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ struct _StringWrapper {
4646
// XXX FIXME TODO:
4747
// Figure out a solution for creating strings while respecting
4848
// the caller's allocator they provided:
49-
// 1. properly type-erase basic_strings' allocator types
49+
// 1. properly typeerase basic_strings' allocator types
5050
// 2. move all code into headers (seems like a bad idea)
5151
// 3. leave these as oversized char arrays, seems suboptimal
5252
// 4. just use std::string, which is just plain wrong
5353
// 5. ...?
5454

55-
char __chars_[1024];
55+
char __chars_[1024]{0};
5656

5757
_LIBCPP_HIDE_FROM_ABI std::string_view view() const { return __chars_; }
5858

@@ -62,10 +62,16 @@ struct _StringWrapper {
6262
__chars_[__size] = 0;
6363
return *this;
6464
}
65+
66+
_LIBCPP_HIDE_FROM_ABI ~_StringWrapper() = default;
67+
_LIBCPP_HIDE_FROM_ABI _StringWrapper() = default;
68+
_LIBCPP_HIDE_FROM_ABI _StringWrapper(_StringWrapper const&) = default;
69+
_LIBCPP_HIDE_FROM_ABI _StringWrapper(_StringWrapper&&) = default;
70+
_LIBCPP_HIDE_FROM_ABI _StringWrapper& operator=(_StringWrapper const&) = default;
71+
_LIBCPP_HIDE_FROM_ABI _StringWrapper& operator=(_StringWrapper&&) = default;
6572
};
6673

6774
struct _Entry {
68-
constexpr static size_t __max_sym_len = 512;
6975
# if defined(PATH_MAX)
7076
constexpr static size_t __max_file_len = PATH_MAX;
7177
# elif defined(MAX_PATH)

libcxx/lib/abi/arm64-apple-darwin.libcxxabi.v1.stable.exceptions.nonew.abilist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,10 @@
413413
{'is_defined': True, 'name': '__ZNKSt3__110moneypunctIwLb1EE16do_negative_signEv', 'type': 'FUNC'}
414414
{'is_defined': True, 'name': '__ZNKSt3__110moneypunctIwLb1EE16do_positive_signEv', 'type': 'FUNC'}
415415
{'is_defined': True, 'name': '__ZNKSt3__110moneypunctIwLb1EE16do_thousands_sepEv', 'type': 'FUNC'}
416+
{'is_defined': True, 'name': '__ZNKSt3__112__stacktrace6_Entry4hashEv', 'type': 'FUNC'}
416417
{'is_defined': True, 'name': '__ZNKSt3__112__stacktrace6_Entry8write_toERNS_13basic_ostreamIcNS_11char_traitsIcEEEE', 'type': 'FUNC'}
417418
{'is_defined': True, 'name': '__ZNKSt3__112__stacktrace6_Entry9to_stringEv', 'type': 'FUNC'}
419+
{'is_defined': True, 'name': '__ZNKSt3__112__stacktrace6_Trace4hashEv', 'type': 'FUNC'}
418420
{'is_defined': True, 'name': '__ZNKSt3__112__stacktrace6_Trace8write_toERNS_13basic_ostreamIcNS_11char_traitsIcEEEE', 'type': 'FUNC'}
419421
{'is_defined': True, 'name': '__ZNKSt3__112__stacktrace6_Trace9to_stringEv', 'type': 'FUNC'}
420422
{'is_defined': True, 'name': '__ZNKSt3__112bad_weak_ptr4whatEv', 'type': 'FUNC'}

libcxx/src/stacktrace/windows_impl.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010

1111
# define WIN32_LEAN_AND_MEAN
1212
# include <windows.h>
13-
14-
# include <cstring>
13+
//
1514
# include <dbghelp.h>
16-
# include <mutex>
1715
# include <psapi.h>
18-
# include <stacktrace>
16+
//
17+
# include <__stacktrace/basic_stacktrace.h>
18+
# include <__stacktrace/stacktrace_entry.h>
19+
# include <cstring>
20+
# include <mutex>
1921

2022
# if defined(_MSC_VER)
2123
# pragma comment(lib, "dbghelp")
@@ -50,7 +52,7 @@ _LIBCPP_EXPORTED_FROM_ABI void _Trace::windows_impl(size_t skip, size_t max_dept
5052
std::lock_guard<std::mutex> api_guard(api_mutex);
5153

5254
HANDLE proc = GetCurrentProcess();
53-
HMODULE exe = GetModuleHandle(nullptr);
55+
HMODULE exe = GetModuleHandleA(nullptr);
5456
if (!exe) {
5557
return;
5658
}
@@ -160,12 +162,19 @@ _LIBCPP_EXPORTED_FROM_ABI void _Trace::windows_impl(size_t skip, size_t max_dept
160162
return;
161163
}
162164

165+
// https://learn.microsoft.com/en-us/cpp/build/reference/h-restrict-length-of-external-names
166+
constexpr static size_t __max_sym_len = 2047;
167+
163168
for (_Entry& entry : __entry_iters_()) {
164-
char space[sizeof(IMAGEHLP_SYMBOL) + _Entry::__max_sym_len + 1];
169+
char space[sizeof(IMAGEHLP_SYMBOL) + __max_sym_len + 1];
165170
IMAGEHLP_SYMBOL* sym = reinterpret_cast<IMAGEHLP_SYMBOL*>(space);
166171
sym->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL);
167-
sym->MaxNameLength = _Entry::__max_sym_len;
168-
DWORD symdisp{0};
172+
sym->MaxNameLength = __max_sym_len;
173+
# if defined(_WIN64)
174+
DWORD64 symdisp;
175+
# else
176+
DWORD32 symdisp;
177+
# endif
169178
DWORD linedisp{0};
170179
IMAGEHLP_LINE line;
171180
if (SymGetSymFromAddr(proc, entry.__addr_, &symdisp, sym)) {

libcxx/test/std/diagnostics/stacktrace/basic.hash.pass.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ int main(int, char**) {
3131
assert(hash_val_empty != hash_val_nonempty);
3232

3333
std::stacktrace_entry empty_entry;
34-
assert(std::hash<std::stacktrace_entry>()(empty_entry) == 0);
3534
auto nonempty_entry = trace[0];
36-
assert(std::hash<std::stacktrace_entry>()(nonempty_entry) != 0);
35+
assert(std::hash<std::stacktrace_entry>()(nonempty_entry) != std::hash<std::stacktrace_entry>()(empty_entry));
3736

3837
return 0;
3938
}

0 commit comments

Comments
 (0)