[Lldb-commits] [lldb] 710276a - [LLDB] Add a setting for printing ValueObject hex values without leading zeroes (#66548)

via lldb-commits lldb-commits at lists.llvm.org
Mon Sep 18 09:48:20 PDT 2023


Author: Walter Erquinigo
Date: 2023-09-18T12:48:16-04:00
New Revision: 710276a2505514634a7cc805461b1219dcef9337

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

LOG: [LLDB] Add a setting for printing ValueObject hex values without leading zeroes (#66548)

As suggested by Greg in https://github.com/llvm/llvm-project/pull/66534,
I'm adding a setting at the Target level that controls whether to show
leading zeroes in hex ValueObject values.

This has the benefit of reducing the amount of characters displayed in
certain interfaces, like VSCode.

Added: 
    

Modified: 
    lldb/include/lldb/Target/Target.h
    lldb/source/Core/DumpDataExtractor.cpp
    lldb/source/Target/Target.cpp
    lldb/source/Target/TargetProperties.td
    lldb/test/API/python_api/value/TestValueAPI.py
    lldb/test/API/python_api/value/main.c

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Target/Target.h b/lldb/include/lldb/Target/Target.h
index ed0ecbbddbf8149..e9e531d0e12640a 100644
--- a/lldb/include/lldb/Target/Target.h
+++ b/lldb/include/lldb/Target/Target.h
@@ -167,6 +167,8 @@ class TargetProperties : public Properties {
 
   bool GetEnableSyntheticValue() const;
 
+  bool ShowHexVariableValuesWithLeadingZeroes() const;
+
   uint32_t GetMaxZeroPaddingInFloatFormat() const;
 
   uint32_t GetMaximumNumberOfChildrenToDisplay() const;

diff  --git a/lldb/source/Core/DumpDataExtractor.cpp b/lldb/source/Core/DumpDataExtractor.cpp
index cb76b118325b7ef..986c9a181919ee0 100644
--- a/lldb/source/Core/DumpDataExtractor.cpp
+++ b/lldb/source/Core/DumpDataExtractor.cpp
@@ -620,10 +620,17 @@ lldb::offset_t lldb_private::DumpDataExtractor(
       case 2:
       case 4:
       case 8:
-        s->Printf(wantsuppercase ? "0x%*.*" PRIX64 : "0x%*.*" PRIx64,
-                  (int)(2 * item_byte_size), (int)(2 * item_byte_size),
-                  DE.GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size,
-                                       item_bit_offset));
+        if (Target::GetGlobalProperties()
+                .ShowHexVariableValuesWithLeadingZeroes()) {
+          s->Printf(wantsuppercase ? "0x%*.*" PRIX64 : "0x%*.*" PRIx64,
+                    (int)(2 * item_byte_size), (int)(2 * item_byte_size),
+                    DE.GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size,
+                                         item_bit_offset));
+        } else {
+          s->Printf(wantsuppercase ? "0x%" PRIX64 : "0x%" PRIx64,
+                    DE.GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size,
+                                         item_bit_offset));
+        }
         break;
       default: {
         assert(item_bit_size == 0 && item_bit_offset == 0);

diff  --git a/lldb/source/Target/Target.cpp b/lldb/source/Target/Target.cpp
index 35f5ef324fde667..8de4fd033ec8785 100644
--- a/lldb/source/Target/Target.cpp
+++ b/lldb/source/Target/Target.cpp
@@ -4236,8 +4236,8 @@ bool TargetProperties::SetPreferDynamicValue(lldb::DynamicValueType d) {
 }
 
 bool TargetProperties::GetPreloadSymbols() const {
-  if (INTERRUPT_REQUESTED(m_target->GetDebugger(), 
-      "Interrupted checking preload symbols")) {
+  if (INTERRUPT_REQUESTED(m_target->GetDebugger(),
+                          "Interrupted checking preload symbols")) {
     return false;
   }
   const uint32_t idx = ePropertyPreloadSymbols;
@@ -4539,6 +4539,12 @@ bool TargetProperties::GetEnableSyntheticValue() const {
       idx, g_target_properties[idx].default_uint_value != 0);
 }
 
+bool TargetProperties::ShowHexVariableValuesWithLeadingZeroes() const {
+  const uint32_t idx = ePropertyShowHexVariableValuesWithLeadingZeroes;
+  return GetPropertyAtIndexAs<bool>(
+      idx, g_target_properties[idx].default_uint_value != 0);
+}
+
 uint32_t TargetProperties::GetMaxZeroPaddingInFloatFormat() const {
   const uint32_t idx = ePropertyMaxZeroPaddingInFloatFormat;
   return GetPropertyAtIndexAs<uint64_t>(

diff  --git a/lldb/source/Target/TargetProperties.td b/lldb/source/Target/TargetProperties.td
index 3bfdfa8cfaa957f..154a6e5919ab0cd 100644
--- a/lldb/source/Target/TargetProperties.td
+++ b/lldb/source/Target/TargetProperties.td
@@ -82,6 +82,10 @@ let Definition = "target" in {
   def SaveObjectsDir: Property<"save-jit-objects-dir", "FileSpec">,
     DefaultStringValue<"">,
     Desc<"If specified, the directory to save intermediate object files generated by the LLVM JIT">;
+  def ShowHexVariableValuesWithLeadingZeroes: Property<"show-hex-variable-values-with-leading-zeroes", "Boolean">,
+    Global,
+    DefaultTrue,
+    Desc<"Whether to display leading zeroes when printing variable values in hex format.">;
   def MaxZeroPaddingInFloatFormat: Property<"max-zero-padding-in-float-format", "UInt64">,
     DefaultUnsignedValue<6>,
     Desc<"The maximum number of zeroes to insert when displaying a very small float before falling back to scientific notation.">;

diff  --git a/lldb/test/API/python_api/value/TestValueAPI.py b/lldb/test/API/python_api/value/TestValueAPI.py
index eb709ca4728b089..05670b8db9bb77a 100644
--- a/lldb/test/API/python_api/value/TestValueAPI.py
+++ b/lldb/test/API/python_api/value/TestValueAPI.py
@@ -3,9 +3,9 @@
 """
 
 import lldb
+from lldbsuite.test import lldbutil
 from lldbsuite.test.decorators import *
 from lldbsuite.test.lldbtest import *
-from lldbsuite.test import lldbutil
 
 
 class ValueAPITestCase(TestBase):
@@ -206,3 +206,18 @@ def test(self):
             -526164208,
             "signed sinthex == -526164208",
         )
+
+        # Check that hex value printing works as expected.
+        self.assertEqual(
+            frame0.FindVariable("fixed_int_ptr").GetValue(),
+            "0x00000000000000aa",
+        )
+        self.runCmd("settings set target.show-hex-values-with-leading-zeroes false")
+        self.assertEqual(
+            frame0.FindVariable("another_fixed_int_ptr").GetValue(),
+            "0xaa",
+        )
+        self.assertEqual(
+            frame0.FindVariable("a_null_int_ptr").GetValue(),
+            "0x0",
+        )

diff  --git a/lldb/test/API/python_api/value/main.c b/lldb/test/API/python_api/value/main.c
index bf00aba07661863..672b0df376dc5a2 100644
--- a/lldb/test/API/python_api/value/main.c
+++ b/lldb/test/API/python_api/value/main.c
@@ -44,13 +44,16 @@ int main (int argc, char const *argv[])
     int i;
     MyInt a = 12345;
     struct MyStruct s = { 11, 22 };
-    struct MyBiggerStruct f = { 33, 44, 55 }; 
+    struct MyBiggerStruct f = { 33, 44, 55 };
     int *my_int_ptr = &g_my_int;
     printf("my_int_ptr points to location %p\n", my_int_ptr);
+    int *fixed_int_ptr = (int*)(void*)0xAA;
+    int *another_fixed_int_ptr = (int*)(void*)0xAA;
+    int *a_null_int_ptr = NULL;
     const char **str_ptr = days_of_week;
     for (i = 0; i < 7; ++i)
         printf("%s\n", str_ptr[i]); // Break at this line
                                     // and do str_ptr_val.GetChildAtIndex(5, lldb.eNoDynamicValues, True).
-    
+
     return 0;
 }


        


More information about the lldb-commits mailing list