Skip to content

[lldb] Add summary for NSIndirectTaggedPointerString #136025

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

kastiglione
Copy link
Contributor

@kastiglione kastiglione commented Apr 16, 2025

rdar://143164164

@llvmbot
Copy link
Member

llvmbot commented Apr 16, 2025

@llvm/pr-subscribers-lldb

Author: Dave Lee (kastiglione)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/136025.diff

3 Files Affected:

  • (modified) lldb/source/Plugins/Language/ObjC/NSString.cpp (+45-6)
  • (modified) lldb/source/Plugins/Language/ObjC/NSString.h (+4)
  • (modified) lldb/source/Plugins/Language/ObjC/ObjCLanguage.cpp (+4)
diff --git a/lldb/source/Plugins/Language/ObjC/NSString.cpp b/lldb/source/Plugins/Language/ObjC/NSString.cpp
index a99d042572bfe..11b4c6c5df8af 100644
--- a/lldb/source/Plugins/Language/ObjC/NSString.cpp
+++ b/lldb/source/Plugins/Language/ObjC/NSString.cpp
@@ -63,12 +63,17 @@ bool lldb_private::formatters::NSStringSummaryProvider(
   if (class_name.empty())
     return false;
 
-  bool is_tagged_ptr = class_name == "NSTaggedPointerString" &&
-                       descriptor->GetTaggedPointerInfo();
-  // for a tagged pointer, the descriptor has everything we need
-  if (is_tagged_ptr)
-    return NSTaggedString_SummaryProvider(valobj, descriptor, stream,
-                                          summary_options);
+  // for tagged pointers, the descriptor has everything needed.
+  bool is_tagged = descriptor->GetTaggedPointerInfo();
+  if (is_tagged) {
+    if (class_name == "NSTaggedPointerString")
+      return NSTaggedString_SummaryProvider(valobj, descriptor, stream,
+                                            summary_options);
+
+    if (class_name == "NSIndirectTaggedPointerString")
+      return NSIndirectTaggedString_SummaryProvider(valobj, descriptor, stream,
+                                                    summary_options);
+  }
 
   auto &additionals_map(NSString_Additionals::GetAdditionalSummaries());
   auto iter = additionals_map.find(class_name_cs), end = additionals_map.end();
@@ -368,3 +373,37 @@ bool lldb_private::formatters::NSTaggedString_SummaryProvider(
   stream << suffix;
   return true;
 }
+
+bool lldb_private::formatters::NSIndirectTaggedString_SummaryProvider(
+    ValueObject &valobj, ObjCLanguageRuntime::ClassDescriptorSP descriptor,
+    Stream &stream, const TypeSummaryOptions &summary_options) {
+  if (!descriptor)
+    return false;
+
+  uint64_t payload = 0;
+  if (!descriptor->GetTaggedPointerInfo(nullptr, nullptr, &payload))
+    return false;
+
+  // First 47 bits are the address of the contents.
+  addr_t ptr = payload & 0x7fffffffffffULL;
+  // Next 13 bits are the string's length.
+  size_t size = (payload >> 47) & 0x1fff;
+
+  Status status;
+  char buf[8192];
+  if (auto process_sp = valobj.GetProcessSP())
+    if (process_sp->ReadMemory(ptr, buf, size, status)) {
+      llvm::StringRef prefix, suffix;
+      if (auto *language = Language::FindPlugin(summary_options.GetLanguage()))
+        std::tie(prefix, suffix) =
+            language->GetFormatterPrefixSuffix("NSString");
+      stream << prefix << '"';
+      stream.PutCString({buf, size});
+      stream << '"' << suffix;
+      return true;
+    }
+
+  if (status.Fail())
+    stream.Format("<{0}>", status);
+  return false;
+}
diff --git a/lldb/source/Plugins/Language/ObjC/NSString.h b/lldb/source/Plugins/Language/ObjC/NSString.h
index 8c9fcf955f1f8..5d405b30b6817 100644
--- a/lldb/source/Plugins/Language/ObjC/NSString.h
+++ b/lldb/source/Plugins/Language/ObjC/NSString.h
@@ -25,6 +25,10 @@ bool NSTaggedString_SummaryProvider(
     ValueObject &valobj, ObjCLanguageRuntime::ClassDescriptorSP descriptor,
     Stream &stream, const TypeSummaryOptions &summary_options);
 
+bool NSIndirectTaggedString_SummaryProvider(
+    ValueObject &valobj, ObjCLanguageRuntime::ClassDescriptorSP descriptor,
+    Stream &stream, const TypeSummaryOptions &summary_options);
+
 bool NSAttributedStringSummaryProvider(ValueObject &valobj, Stream &stream,
                                        const TypeSummaryOptions &options);
 
diff --git a/lldb/source/Plugins/Language/ObjC/ObjCLanguage.cpp b/lldb/source/Plugins/Language/ObjC/ObjCLanguage.cpp
index c835b439a64dd..3b8e21cbb9269 100644
--- a/lldb/source/Plugins/Language/ObjC/ObjCLanguage.cpp
+++ b/lldb/source/Plugins/Language/ObjC/ObjCLanguage.cpp
@@ -691,6 +691,10 @@ static void LoadObjCFormatters(TypeCategoryImplSP objc_category_sp) {
   AddCXXSummary(
       objc_category_sp, lldb_private::formatters::NSStringSummaryProvider,
       "NSString summary provider", "NSTaggedPointerString", appkit_flags);
+  AddCXXSummary(objc_category_sp,
+                lldb_private::formatters::NSStringSummaryProvider,
+                "NSString summary provider", "NSIndirectTaggedPointerString",
+                appkit_flags);
 
   AddCXXSummary(objc_category_sp,
                 lldb_private::formatters::NSAttributedStringSummaryProvider,

@kastiglione kastiglione marked this pull request as draft April 16, 2025 20:36
@kastiglione kastiglione marked this pull request as ready for review April 16, 2025 22:24
size_t size = (payload >> 47) & 0x1fff;

Status status;
char buf[8192];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given how deeply nested lldb stacks often are, I would probably use a std::vector here and allocate this on the heap.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and then you can reserve() exactly size

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call, done

@adrian-prantl
Copy link
Collaborator

LGTM with some nits

std::tie(prefix, suffix) =
language->GetFormatterPrefixSuffix("NSString");
stream << prefix << '"';
stream.PutCString({buf.data(), size});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does PutCString scan through the buffer until it finds a NUL byte or does it copy size bytes (which would be faster).

I doubt this affects the performance measurably. Just being curious.

Copy link
Contributor Author

@kastiglione kastiglione Apr 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or does it copy size bytes (which would be faster).

It does this, the faster option.

@kastiglione kastiglione merged commit 18855ec into llvm:main Apr 17, 2025
10 checks passed
@kastiglione kastiglione deleted the lldb-Add-summary-for-NSIndirectTaggedPointerString branch April 17, 2025 00:31
kastiglione added a commit to swiftlang/llvm-project that referenced this pull request Apr 17, 2025
var-const pushed a commit to ldionne/llvm-project that referenced this pull request Apr 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants