[libcxx-commits] [libcxx] [libc++] Fix gdb pretty printer for strings (PR #176882)

Johan Bengtsson via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jan 20 02:21:35 PST 2026


https://github.com/JohanBengtssonIAR updated https://github.com/llvm/llvm-project/pull/176882

>From b590deba7b07afc027b764a2260c3ddd50ff800c Mon Sep 17 00:00:00 2001
From: Johan Bengtsson <johan.bengtsson at iar.com>
Date: Tue, 20 Jan 2026 10:40:40 +0100
Subject: [PATCH 1/2] [libc++] Fix gdb pretty printer for strings

The gdb pretty printer for strings reports an error when printing a string that
is small enough to fit inline in the string object. The problem is that the
lazy_string method can't be applied directly to an array value. The fix is to
cast the array to a pointer and apply lazy_string to that value.
---
 libcxx/utils/gdb/libcxx/printers.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libcxx/utils/gdb/libcxx/printers.py b/libcxx/utils/gdb/libcxx/printers.py
index 1c8ef6d7feb97..4c04894db7f5d 100644
--- a/libcxx/utils/gdb/libcxx/printers.py
+++ b/libcxx/utils/gdb/libcxx/printers.py
@@ -198,7 +198,8 @@ def to_string(self):
             data = long_field["__data_"]
             size = long_field["__size_"]
         else:
-            data = short_field["__data_"]
+            char_ptr = gdb.lookup_type("char").pointer()
+            data = short_field["__data_"].cast(char_ptr)
             size = short_field["__size_"]
         return data.lazy_string(length=size)
 

>From 230a69c7b08387b9008b7e91ef06d6de1702e135 Mon Sep 17 00:00:00 2001
From: Johan Bengtsson <johan.bengtsson at iar.com>
Date: Tue, 20 Jan 2026 11:07:06 +0100
Subject: [PATCH 2/2] Make string pointer type based on type of data field

Instead of hardcoding the type of the pointer to use for lazy string to char
the pointer type is derived from the type of the array elements. This fixes the
problem when the string has non-char content.
---
 libcxx/utils/gdb/libcxx/printers.py | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/libcxx/utils/gdb/libcxx/printers.py b/libcxx/utils/gdb/libcxx/printers.py
index 4c04894db7f5d..a144ea4ac7ad0 100644
--- a/libcxx/utils/gdb/libcxx/printers.py
+++ b/libcxx/utils/gdb/libcxx/printers.py
@@ -198,8 +198,9 @@ def to_string(self):
             data = long_field["__data_"]
             size = long_field["__size_"]
         else:
-            char_ptr = gdb.lookup_type("char").pointer()
-            data = short_field["__data_"].cast(char_ptr)
+            data = short_field["__data_"]
+            ptr_type = data.type().target().pointer()
+            data = data.cast(ptr_type)
             size = short_field["__size_"]
         return data.lazy_string(length=size)
 



More information about the libcxx-commits mailing list