[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:27 PST 2026


================
@@ -0,0 +1,115 @@
+// -*- 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>
+
+#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::__max_file_len]{0};
+  bool is_main_prog_{};
+
+  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;
+  }
+  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_) */
+  _Image& operator[](size_t __index) {
+    _LIBCPP_ASSERT(__index < count_, "index out of range");
+    return images_.at(__index);
----------------
frederick-vs-ja wrote:

Is it really intended that the index is always checked, and an exception is thrown when libc++'s assertion is disabled?

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


More information about the libcxx-commits mailing list