Skip to content
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

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

Merged
merged 1 commit into from
Sep 18, 2023

Conversation

walter-erquinigo
Copy link
Member

As suggested by Greg in #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.

@llvmbot llvmbot added the lldb label Sep 15, 2023
@walter-erquinigo walter-erquinigo marked this pull request as ready for review September 15, 2023 20:44
@llvmbot
Copy link
Member

llvmbot commented Sep 15, 2023

@llvm/pr-subscribers-lldb

Changes

As suggested by Greg in #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.


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

6 Files Affected:

  • (modified) lldb/include/lldb/Target/Target.h (+2)
  • (modified) lldb/source/Core/DumpDataExtractor.cpp (+11-4)
  • (modified) lldb/source/Target/Target.cpp (+8-2)
  • (modified) lldb/source/Target/TargetProperties.td (+3)
  • (modified) lldb/test/API/python_api/value/TestValueAPI.py (+16-1)
  • (modified) lldb/test/API/python_api/value/main.c (+5-2)
diff --git a/lldb/include/lldb/Target/Target.h b/lldb/include/lldb/Target/Target.h
index ed0ecbbddbf8149..b2dcd3b0b3d30a0 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 ShouldShowHexValuesWithLeadingZeroes() 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..fa9c5209ed490ff 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 (exe_scope->CalculateTarget()
+                ->ShouldShowHexValuesWithLeadingZeroes()) {
+          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..87746d7aa3c32aa 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::ShouldShowHexValuesWithLeadingZeroes() const {
+  const uint32_t idx = ePropertyShowHexValuesWithLeadingZeroes;
+  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..9f639842a80841a 100644
--- a/lldb/source/Target/TargetProperties.td
+++ b/lldb/source/Target/TargetProperties.td
@@ -82,6 +82,9 @@ 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 ShowHexValuesWithLeadingZeroes: Property<"show-hex-values-with-leading-zeroes", "Boolean">,
+    DefaultTrue,
+    Desc<"Whether to display leading zeroes in hex ValueObject values.">;
   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;
 }

Copy link
Collaborator

@clayborg clayborg left a comment

Choose a reason for hiding this comment

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

Nice! LGTM

(int)(2 * item_byte_size), (int)(2 * item_byte_size),
DE.GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size,
item_bit_offset));
if (exe_scope->CalculateTarget()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually do you need to test if "exe_scope" is not NULL here?

@jimingham
Copy link
Collaborator

There are other places we print hex numbers that don't go through the DataExtractor. For instance, the "location" for a ValueObject is printed directly, so this change wouldn't catch it. It's a little off-putting if you set a global setting to "not print leading zeros" and then sometimes we still print leading zeros.

This is not an objection to the contents of this patch, but I think to be complete we're going to end up finding more places where we should be checking this policy setting.

@walter-erquinigo walter-erquinigo changed the title [LLDB] Add a setting for print hex values without leading zeroes [LLDB] Add a setting for printing ValueObject hex values without leading zeroes Sep 15, 2023
@walter-erquinigo
Copy link
Member Author

@jimingham , that's a good catch. Probably I should rename this because I want this setting to affect only SBValueObject::GetValue(), which is want ends up being sent to IDEs as a common interface. Trying to control all places that print as hex value is a bit too much and it'll be hard to enforce in the long term. Any particular suggestion? Because I can't really come up with a good name better than show-leading-zeroes-in-valueobject-values.

@jimingham
Copy link
Collaborator

That really should be show-leading-zeros-in-valueobject-variables-using-hex-format, shouldn't it? I think you can actually just document what it actually does in the help string and then you shouldn't have to make the name overly pedantic. The one you have already is probably enough.

@clayborg
Copy link
Collaborator

@jimingham , that's a good catch. Probably I should rename this because I want this setting to affect only SBValueObject::GetValue(), which is want ends up being sent to IDEs as a common interface. Trying to control all places that print as hex value is a bit too much and it'll be hard to enforce in the long term. Any particular suggestion? Because I can't really come up with a good name better than show-leading-zeroes-in-valueobject-values.

I think the setting name is ok as is. No one outside of LLDB developers have any idea what "valueobject" means. Also this setting is "show-hex-values-with-leading-zeroes" which does mention "values" so it can be ok for not all hex values emitted that are not values to have leading zeroes. The location is not a "value" technically and we might want some values coming out with leading zeroes like any command that dumps out table style output like "image dymp symtab" and other areas. Maybe we can name this setting:

show-hex-variable-values-with-leading-zeroes

The confirm this is for variable values only and not a system wide thing?

If we switch the setting to be global in the target settings we can get the global target settings without needing to check the exe_scope. Thoughts?

@walter-erquinigo
Copy link
Member Author

I like Greg's proposal of naming it show-hex-variable-values-with-leading-zeroes. Seems very clear. I also like the idea of making it a global target config, which would simplify the code.

…roes

As suggested by Greg in llvm#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.
@walter-erquinigo
Copy link
Member Author

I've updated the PR following Greg's suggestions.

Copy link
Collaborator

@clayborg clayborg left a comment

Choose a reason for hiding this comment

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

LGTM. Jim? You ok with this?

@jimingham
Copy link
Collaborator

Sure.

@walter-erquinigo
Copy link
Member Author

Thanks Jim and Greg!

@walter-erquinigo walter-erquinigo merged commit 710276a into llvm:main Sep 18, 2023
@walter-erquinigo walter-erquinigo deleted the walter/hex branch September 18, 2023 16:48
ZijunZhaoCCK pushed a commit to ZijunZhaoCCK/llvm-project that referenced this pull request Sep 19, 2023
…ing zeroes (llvm#66548)

As suggested by Greg in llvm#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.
@DavidSpickett
Copy link
Collaborator

This is failing on Arm 32 bit (https://lab.llvm.org/buildbot/#/builders/17/builds/43300), I'll reproduce and fix it. I expect it just needs to expect 8 hex characters instead of 16.

@DavidSpickett
Copy link
Collaborator

9568601

@walter-erquinigo
Copy link
Member Author

walter-erquinigo commented Sep 19, 2023 via email

zahiraam pushed a commit to tahonermann/llvm-project that referenced this pull request Oct 24, 2023
…ing zeroes (llvm#66548)

As suggested by Greg in llvm#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.
zahiraam pushed a commit to tahonermann/llvm-project that referenced this pull request Oct 24, 2023
…ing zeroes (llvm#66548)

As suggested by Greg in llvm#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.
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.

5 participants