[libcxx-commits] [libcxx] [libc++] Add std::stacktrace (P0881R7) (PR #136528)

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Sun Jan 11 17:29:29 PST 2026


================
@@ -0,0 +1,360 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___BASIC_STACKTRACE_H
+#define _LIBCPP___BASIC_STACKTRACE_H
+
+#include <__assert>
+#include <__config>
+#include <__cstddef/size_t.h>
+#include <__functional/function.h>
+#include <__functional/hash.h>
+#include <__fwd/format.h>
+#include <__iterator/iterator.h>
+#include <__iterator/reverse_iterator.h>
+#include <__memory/allocator_traits.h>
+#include <__memory_resource/polymorphic_allocator.h>
+#include <__new/allocate.h>
+#include <__stacktrace/stacktrace_entry.h>
+#include <__type_traits/is_nothrow_constructible.h>
+#include <__vector/vector.h>
+#include <cstddef>
+#include <iostream>
+#include <string>
+#include <utility>
+#if _LIBCPP_HAS_LOCALIZATION
+#  include <__fwd/ostream.h>
+#endif // _LIBCPP_HAS_LOCALIZATION
+#if !defined(_WIN32)
+#  include <unwind.h>
+#endif
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
+#if _LIBCPP_STD_VER >= 23 && _LIBCPP_AVAILABILITY_HAS_STACKTRACE
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+namespace __stacktrace {
+
+template <typename _Tp, typename _Bp = _Tp>
+struct _Iters {
+  _Tp* __data_{};
+  size_t __size_{};
+
+  _Bp* data() { return (_Bp*)__data_; }
+  size_t size() const { return __size_; }
+  _Bp* begin() { return data(); }
+  _Bp* end() { return data() + size(); }
+};
+
+struct _Trace {
+  constexpr static size_t __default_max_depth  = 64;
+  constexpr static size_t __absolute_max_depth = 256;
+
+  using _EntryIters _LIBCPP_NODEBUG = _Iters<stacktrace_entry, _Entry>;
+  function<_EntryIters()> __entry_iters_;
+  function<_Entry&()> __entry_append_;
+
+  _LIBCPP_HIDE_FROM_ABI _Trace(function<_EntryIters()> __entry_iters, function<_Entry&()> __entry_append)
+      : __entry_iters_(__entry_iters), __entry_append_(__entry_append) {}
+
+  _LIBCPP_EXPORTED_FROM_ABI ostream& write_to(ostream& __os) const;
+  _LIBCPP_EXPORTED_FROM_ABI string to_string() const;
+
+  _LIBCPP_EXPORTED_FROM_ABI size_t hash() const;
+  _LIBCPP_HIDE_FROM_ABI static _Trace& base(auto& __trace);
+  _LIBCPP_HIDE_FROM_ABI static _Trace const& base(auto const& __trace);
+
+#  ifdef _WIN32
+  // Windows impl uses dbghelp and psapi DLLs to do the full stacktrace operation.
+  _LIBCPP_EXPORTED_FROM_ABI void windows_impl(size_t skip, size_t max_depth);
+#  else
+  // Non-windows: impl separated out into several smaller platform-dependent parts.
+  _LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE void populate_addrs(size_t __skip, size_t __depth);
+  _LIBCPP_EXPORTED_FROM_ABI void populate_images();
+#  endif
----------------
frederick-vs-ja wrote:

`write_to`, `windows_impl`, `populate_addrs`, and `populate_images` are pretty names, I think we need to __uglify them.

Also, it's questionable to me whether `hash` and `base` are good names. Maybe `hash_code`, `hash_value` or uglified versions would be better. (It's OK to use `hash`, `hash_code`, `hash_value`, `base`, and `to_string` as they're already reserved by other standard library components.)

https://github.com/llvm/llvm-project/pull/136528


More information about the libcxx-commits mailing list