Author: David Spickett
Date: 2026-07-01T13:54:00+01:00
New Revision: b5f36b15d16e97dc4cdd04ab60a37ef4a51d1a76

URL: 
https://github.com/llvm/llvm-project/commit/b5f36b15d16e97dc4cdd04ab60a37ef4a51d1a76
DIFF: 
https://github.com/llvm/llvm-project/commit/b5f36b15d16e97dc4cdd04ab60a37ef4a51d1a76.diff

LOG: [lldb] Print register enum types only once (#204818)

Even if multiple fields reference them. I found this debugging AArch64
POE which has a register that contains 16 permission fields. All of
which use the same enum type.

Before these changes:
```
(lldb) register info por
<...>
Perm15: 0 = No Access, 1 = Read, 2 = Execute, 3 = Read, Execute, 4 = Write, 5 = 
Write, Read, 6 = Write, Execute, 7 = Read, Write, Execute <... 14 copies of the 
enum one per field...>
Perm0: 0 = No Access, 1 = Read, 2 = Execute, 3 = Read, Execute, 4 = Write, 5 = 
Write, Read, 6 = Write, Execute, 7 = Read, Write, Execute
```
After:
```
(lldb) register info por_el0
<...>
Perm15, Perm14, Perm13, Perm12, Perm11, Perm10, Perm9, Perm8, Perm7, Perm6, 
Perm5, Perm4, Perm3, Perm2, Perm1, Perm0: 0 = No Access, 1 = Read, 2 = Execute,
                                                                                
                                      3 = Read, Execute, 4 = Write,
                                                                                
                                      5 = Write, Read, 6 = Write, Execute,
                                                                                
                                      7 = Read, Write, Execute
```
Which looks weird because we indent the enum according to the list of
fields still, but I may change that later. Anyway, this won't be a
problem for most cases where 2 or 3 fields share a type. This register
is an extreme example.

A unit test has been added and a test for POE using the existing core
file tests. They both test the same thing but the POE test is there to
show a motivating example.

Assisted by: Codex (gpt 5.5)

Added: 
    

Modified: 
    lldb/source/Target/RegisterFlags.cpp
    lldb/test/API/functionalities/gdb_remote_client/TestXMLRegisterFlags.py
    lldb/test/API/linux/aarch64/permission_overlay/TestAArch64LinuxPOE.py
    lldb/unittests/Target/RegisterFlagsTest.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/source/Target/RegisterFlags.cpp 
b/lldb/source/Target/RegisterFlags.cpp
index 976e03870ad9e..ee1bdd3426255 100644
--- a/lldb/source/Target/RegisterFlags.cpp
+++ b/lldb/source/Target/RegisterFlags.cpp
@@ -10,8 +10,10 @@
 #include "lldb/Utility/Log.h"
 #include "lldb/Utility/StreamString.h"
 
+#include "llvm/ADT/MapVector.h"
 #include "llvm/ADT/StringExtras.h"
 
+#include <algorithm>
 #include <limits>
 #include <numeric>
 #include <optional>
@@ -286,31 +288,31 @@ static void DumpEnumerators(StreamString &strm, size_t 
indent,
 }
 
 std::string RegisterFlags::DumpEnums(uint32_t max_width) const {
-  StreamString strm;
-  bool printed_enumerators_once = false;
-
-  for (const auto &field : m_fields) {
-    const FieldEnum *enum_type = field.GetEnum();
-    if (!enum_type)
-      continue;
+  // Accumulate all fields that use the same enum, so that each enum is only
+  // printed once.
+  llvm::MapVector<const FieldEnum *, std::vector<std::string>> enum_uses;
+  for (const auto &field : m_fields)
+    if (const FieldEnum *enum_type = field.GetEnum())
+      enum_uses[enum_type].push_back(field.GetName());
 
-    const FieldEnum::Enumerators &enumerators = enum_type->GetEnumerators();
-    if (enumerators.empty())
-      continue;
+  StreamString strm;
+  bool printed_one_enumerator = false;
 
-    // Break between enumerators of 
diff erent fields.
-    if (printed_enumerators_once)
+  for (const auto &[enum_type, field_names] : enum_uses) {
+    // Break between unique enumerator types.
+    if (printed_one_enumerator)
       strm << "\n\n";
-    else
-      printed_enumerators_once = true;
 
-    std::string name_string = field.GetName() + ": ";
+    printed_one_enumerator = true;
+
+    std::string name_string = llvm::join(field_names, ", ") + ": ";
     size_t indent = name_string.size();
     size_t current_width = indent;
 
     strm << name_string;
 
-    DumpEnumerators(strm, indent, current_width, max_width, enumerators);
+    DumpEnumerators(strm, indent, current_width, max_width,
+                    enum_type->GetEnumerators());
   }
 
   return strm.GetString().str();

diff  --git 
a/lldb/test/API/functionalities/gdb_remote_client/TestXMLRegisterFlags.py 
b/lldb/test/API/functionalities/gdb_remote_client/TestXMLRegisterFlags.py
index 1d0fd00ede3f0..a2662f7b343fe 100644
--- a/lldb/test/API/functionalities/gdb_remote_client/TestXMLRegisterFlags.py
+++ b/lldb/test/API/functionalities/gdb_remote_client/TestXMLRegisterFlags.py
@@ -1005,9 +1005,7 @@ def test_many_fields_same_enum(self):
         expected_info = [
             dedent(
                 """\
-             f2: 1 = valid
-
-             f1: 1 = valid$"""
+             f2, f1: 1 = valid$"""
             )
         ]
         self.expect("register info x0", patterns=expected_info)

diff  --git 
a/lldb/test/API/linux/aarch64/permission_overlay/TestAArch64LinuxPOE.py 
b/lldb/test/API/linux/aarch64/permission_overlay/TestAArch64LinuxPOE.py
index 124ec08cb9972..34d2e10d3bb48 100644
--- a/lldb/test/API/linux/aarch64/permission_overlay/TestAArch64LinuxPOE.py
+++ b/lldb/test/API/linux/aarch64/permission_overlay/TestAArch64LinuxPOE.py
@@ -151,6 +151,17 @@ def test_poe_core(self):
                 substrs=[f" {self.EXPECTED_POR_EL0}\n" + 
self.EXPECTED_POR_EL0_FIELDS],
             )
 
+            # por_el0 is an unusal case where every field uses the same enum.
+            # We should print all the field names in a list, then the enum only
+            # once. Rather than printing the enum 15 times, once for each 
field.
+            self.expect(
+                "register info por_el0",
+                substrs=[
+                    ", ".join([f"Perm{n}" for n in range(15, -1, -1)])
+                    + ": 0 = No Access"
+                ],
+            )
+
         # Protection keys are listed in /proc/<pid>/smaps, which is not 
included
         # in core files.
         self.expect("memory region --all", substrs=["protection key:"], 
matching=False)

diff  --git a/lldb/unittests/Target/RegisterFlagsTest.cpp 
b/lldb/unittests/Target/RegisterFlagsTest.cpp
index ecffdd0fe44e6..4765791ae59f2 100644
--- a/lldb/unittests/Target/RegisterFlagsTest.cpp
+++ b/lldb/unittests/Target/RegisterFlagsTest.cpp
@@ -336,9 +336,22 @@ TEST(RegisterFlagsTest, DumpEnums) {
                               RegisterFlags::Field{"E", 0, 0},
                           })
                 .DumpEnums(80),
-            "B: 0 = an_enumerator, 1 = another_enumerator\n"
+            "B, D: 0 = an_enumerator, 1 = another_enumerator");
+
+  // Fields using the same enum should be grouped together.
+  FieldEnum repeated_enum("repeated_enum",
+                          {{0, "zero"}, {1, "one"}, {2, "two"}});
+  ASSERT_EQ(RegisterFlags("", 8,
+                          {
+                              RegisterFlags::Field{"A", 6, 7, &repeated_enum},
+                              RegisterFlags::Field{"B", 4, 5, &repeated_enum},
+                              RegisterFlags::Field{"C", 2, 3, &enum_2},
+                              RegisterFlags::Field{"D", 0, 1, &repeated_enum},
+                          })
+                .DumpEnums(80),
+            "A, B, D: 0 = zero, 1 = one, 2 = two\n"
             "\n"
-            "D: 0 = an_enumerator, 1 = another_enumerator");
+            "C: 0 = Cdef_enumerator_1, 1 = Cdef_enumerator_2");
 }
 
 TEST(RegisterFieldsTest, FlagsToXML) {


        
_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to