[libcxx-commits] [libcxx] Add C++23 stacktrace (P0881R7) (PR #136528)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Mon Sep 1 01:29:57 PDT 2025


================
@@ -0,0 +1,119 @@
+// -*- 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_STACKTRACE_IMAGES_H
+#define _LIBCPP_STACKTRACE_IMAGES_H
+
+#include <__config>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
+#if _LIBCPP_STD_VER >= 23
+
+#  include <__stacktrace/stacktrace_entry.h>
+#  include <array>
+#  include <cstdint>
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+namespace __stacktrace {
+
+struct image;
+struct images;
+
+struct image {
+  uintptr_t loaded_at_{};
+  uintptr_t slide_{};
+  char name_[__stacktrace::entry_base::__max_file_len]{0};
+  bool is_main_prog_{};
+
+  _LIBCPP_HIDE_FROM_ABI bool operator<(image const& __rhs) const {
+    if (loaded_at_ < __rhs.loaded_at_) {
+      return true;
+    }
+    if (loaded_at_ > __rhs.loaded_at_) {
+      return false;
+    }
+    return strcmp(name_, __rhs.name_) < 0;
+  }
+  _LIBCPP_HIDE_FROM_ABI operator bool() const { return name_[0]; }
+};
+
+/**
+ * Contains an array `images_`, which will include `prog_image` objects (see above)
+ * collected in an OS-dependent way.  After construction these images will be sorted
+ * according to their load address; there will also be two sentinels with dummy
+ * addresses (0x0000... and 0xFFFF...) to simplify search functions.
+ *
+ * After construction, images_ and count_ look like:
+ *  [0]             [1]             [2]             [3]       ...     [count_ - 1]
+ *  (sentinel)      foo.exe         libc++so.1      libc.so.6         (sentinel)
+ *  0x000000000000  0x000100000000  0x633b00000000  0x7c5500000000    0xffffffffffff
+ */
+struct images {
+  constexpr static size_t k_max_images = 256;
+  std::array<image, k_max_images + 2> images_{}; // space for the L/R sentinels
+  unsigned count_{0};                            // image count, including sentinels
+
+  /** An OS-specific constructor is defined. */
+  _LIBCPP_EXPORTED_FROM_ABI images();
+
+  /** Get prog_image by index (0 <= index < count_) */
+  _LIBCPP_HIDE_FROM_ABI image& operator[](size_t __index) {
+    _LIBCPP_ASSERT(__index < count_, "index out of range");
+    return images_.at(__index);
+  }
+
+  /** Image representing the main program, or nullptr if we couldn't find it */
+  _LIBCPP_HIDE_FROM_ABI image* main_prog_image() {
----------------
philnik777 wrote:

You don't need `_LIBCPP_HIDE_FROM_ABI` in the dylib.

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


More information about the libcxx-commits mailing list