[Lldb-commits] [lldb] [lldb] Extend FindTypes to optionally search by mangled type name (PR #113007)

Augusto Noronha via lldb-commits lldb-commits at lists.llvm.org
Tue Oct 29 15:42:55 PDT 2024


https://github.com/augusto2112 updated https://github.com/llvm/llvm-project/pull/113007

>From eab35bd50d89a16494d8f08421a12aef77356c43 Mon Sep 17 00:00:00 2001
From: Augusto Noronha <anoronha at apple.com>
Date: Tue, 22 Oct 2024 14:34:44 -0700
Subject: [PATCH 1/2] [lldb] Extend FindTypes to optionally search by mangled
 type name

Swift types have mangled type names. This adds functionality to look
up those types through the FindTypes API by searching for the mangled
type name instead of the regular name.
---
 lldb/include/lldb/Symbol/Type.h                  |  9 +++++++++
 .../Plugins/SymbolFile/CTF/SymbolFileCTF.cpp     | 16 +++++++++++-----
 .../Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp |  9 +++++++++
 .../SymbolFile/NativePDB/SymbolFileNativePDB.cpp |  5 ++++-
 .../Plugins/SymbolFile/PDB/SymbolFilePDB.cpp     |  5 ++++-
 5 files changed, 37 insertions(+), 7 deletions(-)

diff --git a/lldb/include/lldb/Symbol/Type.h b/lldb/include/lldb/Symbol/Type.h
index 03d9f927997476..a8e5b81585e169 100644
--- a/lldb/include/lldb/Symbol/Type.h
+++ b/lldb/include/lldb/Symbol/Type.h
@@ -84,6 +84,9 @@ FLAGS_ENUM(TypeQueryOptions){
     /// matching type is found. When false, the type query should find all
     /// matching types.
     e_find_one = (1u << 4),
+    // If set, treat TypeQuery::m_name as a mangled name that should be
+    // searched.
+    e_search_by_mangled_name = (1u << 5),
 };
 LLDB_MARK_AS_BITMASK_ENUM(TypeQueryOptions)
 
@@ -300,6 +303,12 @@ class TypeQuery {
       m_options &= ~e_find_one;
   }
 
+  /// Returns true if the type query is supposed to treat the name to be
+  /// searched as a mangled name.
+  bool GetSearchByMangledName() const {
+    return (m_options & e_search_by_mangled_name) != 0;
+  }
+
   /// Access the internal compiler context array.
   ///
   /// Clients can use this to populate the context manually.
diff --git a/lldb/source/Plugins/SymbolFile/CTF/SymbolFileCTF.cpp b/lldb/source/Plugins/SymbolFile/CTF/SymbolFileCTF.cpp
index bb738c3dcc54a0..4d08c53f752098 100644
--- a/lldb/source/Plugins/SymbolFile/CTF/SymbolFileCTF.cpp
+++ b/lldb/source/Plugins/SymbolFile/CTF/SymbolFileCTF.cpp
@@ -1032,11 +1032,17 @@ void SymbolFileCTF::FindTypes(const lldb_private::TypeQuery &match,
 
   ConstString name = match.GetTypeBasename();
   for (TypeSP type_sp : GetTypeList().Types()) {
-    if (type_sp && type_sp->GetName() == name) {
-      results.InsertUnique(type_sp);
-      if (results.Done(match))
-        return;
-    }
+    if (!type_sp)
+      continue;
+    auto type_name =
+        match.GetSearchByMangledName()
+            ? type_sp->GetForwardCompilerType().GetMangledTypeName()
+            : type_sp->GetName();
+    if (type_name != name)
+      continue;
+    results.InsertUnique(type_sp);
+    if (results.Done(match))
+      return;
   }
 }
 
diff --git a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
index e5b8eee8d08c24..704c8d64dbfb2f 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
+++ b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
@@ -2758,6 +2758,15 @@ void SymbolFileDWARF::FindTypes(const TypeQuery &query, TypeResults &results) {
         return true; // Keep iterating over index types, language mismatch.
     }
 
+    // Since mangled names are unique, we only need to check if the names are
+    // the same.
+    if (query.GetSearchByMangledName()) {
+      if (die.GetMangledName() == query.GetTypeBasename().GetStringRef())
+        if (Type *matching_type = ResolveType(die, true, true))
+          results.InsertUnique(matching_type->shared_from_this());
+      return !results.Done(query); // Keep iterating if we aren't done.
+    }
+
     // Check the context matches
     std::vector<lldb_private::CompilerContext> die_context;
     if (query.GetModuleSearch())
diff --git a/lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp b/lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp
index 7fded6a31a3af5..b55518a136c997 100644
--- a/lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp
+++ b/lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp
@@ -1735,7 +1735,10 @@ void SymbolFileNativePDB::FindTypes(const lldb_private::TypeQuery &query,
       continue;
 
     // We resolved a type. Get the fully qualified name to ensure it matches.
-    ConstString name = type_sp->GetQualifiedName();
+    ConstString name =
+        query.GetSearchByMangledName()
+            ? type_sp->GetForwardCompilerType().GetMangledTypeName()
+            : type_sp->GetQualifiedName();
     TypeQuery type_match(name.GetStringRef(), TypeQueryOptions::e_exact_match);
     if (query.ContextMatches(type_match.GetContextRef())) {
       results.InsertUnique(type_sp);
diff --git a/lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp b/lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp
index 4fc48b4d133382..0ea18d8f74baa3 100644
--- a/lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp
+++ b/lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp
@@ -1561,7 +1561,10 @@ void SymbolFilePDB::FindTypes(const lldb_private::TypeQuery &query,
     if (iter == m_types.end())
       continue;
     // We resolved a type. Get the fully qualified name to ensure it matches.
-    ConstString name = iter->second->GetQualifiedName();
+    ConstString name =
+        query.GetSearchByMangledName()
+            ? iter->second->GetForwardCompilerType().GetMangledTypeName()
+            : iter->second->GetQualifiedName();
     TypeQuery type_match(name.GetStringRef(), TypeQueryOptions::e_exact_match);
     if (query.ContextMatches(type_match.GetContextRef())) {
       type_results.InsertUnique(iter->second);

>From 465a1fe31d3b378d2f0e7af48715cc1500c54f05 Mon Sep 17 00:00:00 2001
From: Augusto Noronha <anoronha at apple.com>
Date: Tue, 29 Oct 2024 15:42:44 -0700
Subject: [PATCH 2/2] address comments

---
 lldb/include/lldb/Symbol/Type.h                     |  7 +++++++
 lldb/source/Plugins/SymbolFile/DWARF/DWARFDIE.cpp   |  4 ++--
 lldb/source/Plugins/SymbolFile/DWARF/DWARFDIE.h     |  2 +-
 .../Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp    | 13 +++++++++----
 4 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/lldb/include/lldb/Symbol/Type.h b/lldb/include/lldb/Symbol/Type.h
index a8e5b81585e169..91188fe6ea4830 100644
--- a/lldb/include/lldb/Symbol/Type.h
+++ b/lldb/include/lldb/Symbol/Type.h
@@ -309,6 +309,13 @@ class TypeQuery {
     return (m_options & e_search_by_mangled_name) != 0;
   }
 
+  void SetSearchByMangledName(bool b) {
+    if (b)
+      m_options |= e_search_by_mangled_name;
+    else
+      m_options &= ~e_search_by_mangled_name;
+  }
+
   /// Access the internal compiler context array.
   ///
   /// Clients can use this to populate the context manually.
diff --git a/lldb/source/Plugins/SymbolFile/DWARF/DWARFDIE.cpp b/lldb/source/Plugins/SymbolFile/DWARF/DWARFDIE.cpp
index d83740f8e2113b..4c9f1d8505f6e6 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/DWARFDIE.cpp
+++ b/lldb/source/Plugins/SymbolFile/DWARF/DWARFDIE.cpp
@@ -199,9 +199,9 @@ DWARFDIE::LookupDeepestBlock(lldb::addr_t address) const {
   return result;
 }
 
-const char *DWARFDIE::GetMangledName() const {
+const char *DWARFDIE::GetMangledName(bool substitute_name_allowed) const {
   if (IsValid())
-    return m_die->GetMangledName(m_cu);
+    return m_die->GetMangledName(m_cu, substitute_name_allowed);
   else
     return nullptr;
 }
diff --git a/lldb/source/Plugins/SymbolFile/DWARF/DWARFDIE.h b/lldb/source/Plugins/SymbolFile/DWARF/DWARFDIE.h
index e1318953a384cd..077b78eb26d0c3 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/DWARFDIE.h
+++ b/lldb/source/Plugins/SymbolFile/DWARF/DWARFDIE.h
@@ -28,7 +28,7 @@ class DWARFDIE : public DWARFBaseDIE {
   // Accessors
 
   // Accessing information about a DIE
-  const char *GetMangledName() const;
+  const char *GetMangledName(bool substitute_name_allowed = true) const;
 
   const char *GetPubname() const;
 
diff --git a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
index 704c8d64dbfb2f..30df4302a02492 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
+++ b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
@@ -2761,10 +2761,15 @@ void SymbolFileDWARF::FindTypes(const TypeQuery &query, TypeResults &results) {
     // Since mangled names are unique, we only need to check if the names are
     // the same.
     if (query.GetSearchByMangledName()) {
-      if (die.GetMangledName() == query.GetTypeBasename().GetStringRef())
-        if (Type *matching_type = ResolveType(die, true, true))
-          results.InsertUnique(matching_type->shared_from_this());
-      return !results.Done(query); // Keep iterating if we aren't done.
+      if (die.GetMangledName(/*substitute_name_allowed=*/false) !=
+          query.GetTypeBasename().GetStringRef())
+        return true; // Keep iterating over index types, mangled name mismatch.
+      if (Type *matching_type = ResolveType(die, true, true)) {
+        results.InsertUnique(matching_type->shared_from_this());
+        return !results.Done(query); // Keep iterating if we aren't done.
+      }
+      return true; // Keep iterating over index types, weren't able to resolve
+                   // this type
     }
 
     // Check the context matches



More information about the lldb-commits mailing list