Skip to content

Commit 14ea2bc

Browse files
walter-erquinigozahiraam
authored andcommitted
[LLDB] Add a setting for printing ValueObject hex values without leading 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.
1 parent c2dd9c9 commit 14ea2bc

File tree

6 files changed

+46
-9
lines changed

6 files changed

+46
-9
lines changed

lldb/include/lldb/Target/Target.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ class TargetProperties : public Properties {
167167

168168
bool GetEnableSyntheticValue() const;
169169

170+
bool ShowHexVariableValuesWithLeadingZeroes() const;
171+
170172
uint32_t GetMaxZeroPaddingInFloatFormat() const;
171173

172174
uint32_t GetMaximumNumberOfChildrenToDisplay() const;

lldb/source/Core/DumpDataExtractor.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -620,10 +620,17 @@ lldb::offset_t lldb_private::DumpDataExtractor(
620620
case 2:
621621
case 4:
622622
case 8:
623-
s->Printf(wantsuppercase ? "0x%*.*" PRIX64 : "0x%*.*" PRIx64,
624-
(int)(2 * item_byte_size), (int)(2 * item_byte_size),
625-
DE.GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size,
626-
item_bit_offset));
623+
if (Target::GetGlobalProperties()
624+
.ShowHexVariableValuesWithLeadingZeroes()) {
625+
s->Printf(wantsuppercase ? "0x%*.*" PRIX64 : "0x%*.*" PRIx64,
626+
(int)(2 * item_byte_size), (int)(2 * item_byte_size),
627+
DE.GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size,
628+
item_bit_offset));
629+
} else {
630+
s->Printf(wantsuppercase ? "0x%" PRIX64 : "0x%" PRIx64,
631+
DE.GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size,
632+
item_bit_offset));
633+
}
627634
break;
628635
default: {
629636
assert(item_bit_size == 0 && item_bit_offset == 0);

lldb/source/Target/Target.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4236,8 +4236,8 @@ bool TargetProperties::SetPreferDynamicValue(lldb::DynamicValueType d) {
42364236
}
42374237

42384238
bool TargetProperties::GetPreloadSymbols() const {
4239-
if (INTERRUPT_REQUESTED(m_target->GetDebugger(),
4240-
"Interrupted checking preload symbols")) {
4239+
if (INTERRUPT_REQUESTED(m_target->GetDebugger(),
4240+
"Interrupted checking preload symbols")) {
42414241
return false;
42424242
}
42434243
const uint32_t idx = ePropertyPreloadSymbols;
@@ -4539,6 +4539,12 @@ bool TargetProperties::GetEnableSyntheticValue() const {
45394539
idx, g_target_properties[idx].default_uint_value != 0);
45404540
}
45414541

4542+
bool TargetProperties::ShowHexVariableValuesWithLeadingZeroes() const {
4543+
const uint32_t idx = ePropertyShowHexVariableValuesWithLeadingZeroes;
4544+
return GetPropertyAtIndexAs<bool>(
4545+
idx, g_target_properties[idx].default_uint_value != 0);
4546+
}
4547+
45424548
uint32_t TargetProperties::GetMaxZeroPaddingInFloatFormat() const {
45434549
const uint32_t idx = ePropertyMaxZeroPaddingInFloatFormat;
45444550
return GetPropertyAtIndexAs<uint64_t>(

lldb/source/Target/TargetProperties.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ let Definition = "target" in {
8282
def SaveObjectsDir: Property<"save-jit-objects-dir", "FileSpec">,
8383
DefaultStringValue<"">,
8484
Desc<"If specified, the directory to save intermediate object files generated by the LLVM JIT">;
85+
def ShowHexVariableValuesWithLeadingZeroes: Property<"show-hex-variable-values-with-leading-zeroes", "Boolean">,
86+
Global,
87+
DefaultTrue,
88+
Desc<"Whether to display leading zeroes when printing variable values in hex format.">;
8589
def MaxZeroPaddingInFloatFormat: Property<"max-zero-padding-in-float-format", "UInt64">,
8690
DefaultUnsignedValue<6>,
8791
Desc<"The maximum number of zeroes to insert when displaying a very small float before falling back to scientific notation.">;

lldb/test/API/python_api/value/TestValueAPI.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
"""
44

55
import lldb
6+
from lldbsuite.test import lldbutil
67
from lldbsuite.test.decorators import *
78
from lldbsuite.test.lldbtest import *
8-
from lldbsuite.test import lldbutil
99

1010

1111
class ValueAPITestCase(TestBase):
@@ -206,3 +206,18 @@ def test(self):
206206
-526164208,
207207
"signed sinthex == -526164208",
208208
)
209+
210+
# Check that hex value printing works as expected.
211+
self.assertEqual(
212+
frame0.FindVariable("fixed_int_ptr").GetValue(),
213+
"0x00000000000000aa",
214+
)
215+
self.runCmd("settings set target.show-hex-values-with-leading-zeroes false")
216+
self.assertEqual(
217+
frame0.FindVariable("another_fixed_int_ptr").GetValue(),
218+
"0xaa",
219+
)
220+
self.assertEqual(
221+
frame0.FindVariable("a_null_int_ptr").GetValue(),
222+
"0x0",
223+
)

lldb/test/API/python_api/value/main.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,16 @@ int main (int argc, char const *argv[])
4444
int i;
4545
MyInt a = 12345;
4646
struct MyStruct s = { 11, 22 };
47-
struct MyBiggerStruct f = { 33, 44, 55 };
47+
struct MyBiggerStruct f = { 33, 44, 55 };
4848
int *my_int_ptr = &g_my_int;
4949
printf("my_int_ptr points to location %p\n", my_int_ptr);
50+
int *fixed_int_ptr = (int*)(void*)0xAA;
51+
int *another_fixed_int_ptr = (int*)(void*)0xAA;
52+
int *a_null_int_ptr = NULL;
5053
const char **str_ptr = days_of_week;
5154
for (i = 0; i < 7; ++i)
5255
printf("%s\n", str_ptr[i]); // Break at this line
5356
// and do str_ptr_val.GetChildAtIndex(5, lldb.eNoDynamicValues, True).
54-
57+
5558
return 0;
5659
}

0 commit comments

Comments
 (0)