Skip to content

Commit afd2920

Browse files
committed
[lldb] Add ability to show enum as name and value at the same time
When an enum is used to represent certain data it can be useful to know its name and the value of it. For instance, register fields are often set in source code as numbers, but in the debugger you'd like to see the meaning as well. (lldb) register read fpcr fpcr = 0x00000000 = (... RMode = RN (0), ...) Often you do just want the meaning but the value saves you having to manually decode it if you want to confirm what your source code has done, or try to replicate the current state in your source code. I have not added tests for this because right now the use case for this is registers and those will have their own test cases to cover this. If we decide to expose this to formatters then this will need more testing.
1 parent f0cbdd3 commit afd2920

File tree

7 files changed

+31
-13
lines changed

7 files changed

+31
-13
lines changed

lldb/include/lldb/Core/ValueObject.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,12 @@ class ValueObject {
757757

758758
AddressType GetAddressTypeOfChildren();
759759

760+
void SetEnumsAlwaysShowValue(bool always) {
761+
m_enums_always_show_value = always;
762+
}
763+
764+
bool GetEnumsAlwaysShowValue() { return m_enums_always_show_value; }
765+
760766
void SetHasCompleteType() {
761767
m_flags.m_did_calculate_complete_objc_class_type = true;
762768
}
@@ -889,6 +895,7 @@ class ValueObject {
889895
lldb::SyntheticChildrenSP m_synthetic_children_sp;
890896
ProcessModID m_user_id_of_forced_summary;
891897
AddressType m_address_type_of_ptr_or_ref_children = eAddressTypeInvalid;
898+
bool m_enums_always_show_value = false;
892899

893900
llvm::SmallVector<uint8_t, 16> m_value_checksum;
894901

lldb/include/lldb/Symbol/CompilerType.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,8 @@ class CompilerType {
490490
bool DumpTypeValue(Stream *s, lldb::Format format, const DataExtractor &data,
491491
lldb::offset_t data_offset, size_t data_byte_size,
492492
uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
493-
ExecutionContextScope *exe_scope);
493+
ExecutionContextScope *exe_scope,
494+
bool enums_always_show_value = false);
494495

495496
/// Dump to stdout.
496497
void DumpTypeDescription(lldb::DescriptionLevel level =

lldb/include/lldb/Symbol/TypeSystem.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,8 @@ class TypeSystem : public PluginInterface,
398398
lldb::offset_t data_offset, size_t data_byte_size,
399399
uint32_t bitfield_bit_size,
400400
uint32_t bitfield_bit_offset,
401-
ExecutionContextScope *exe_scope) = 0;
401+
ExecutionContextScope *exe_scope,
402+
bool enums_always_show_value = false) = 0;
402403

403404
/// Dump the type to stdout.
404405
virtual void DumpTypeDescription(

lldb/source/DataFormatters/TypeFormat.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ bool TypeFormatImpl_Format::FormatObject(ValueObject *valobj,
108108
*size, // Byte size of item in "m_data"
109109
valobj->GetBitfieldBitSize(), // Bitfield bit size
110110
valobj->GetBitfieldBitOffset(), // Bitfield bit offset
111-
exe_scope);
111+
exe_scope, valobj->GetEnumsAlwaysShowValue());
112112
// Given that we do not want to set the ValueObject's m_error for a
113113
// formatting error (or else we wouldn't be able to reformat until a
114114
// next update), an empty string is treated as a "false" return from

lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8474,7 +8474,7 @@ void TypeSystemClang::DumpFromSymbolFile(Stream &s,
84748474
static bool DumpEnumValue(const clang::QualType &qual_type, Stream &s,
84758475
const DataExtractor &data, lldb::offset_t byte_offset,
84768476
size_t byte_size, uint32_t bitfield_bit_offset,
8477-
uint32_t bitfield_bit_size) {
8477+
uint32_t bitfield_bit_size, bool always_show_value) {
84788478
const clang::EnumType *enutype =
84798479
llvm::cast<clang::EnumType>(qual_type.getTypePtr());
84808480
const clang::EnumDecl *enum_decl = enutype->getDecl();
@@ -8501,7 +8501,11 @@ static bool DumpEnumValue(const clang::QualType &qual_type, Stream &s,
85018501
++num_enumerators;
85028502
if (val == enum_svalue) {
85038503
// Found an exact match, that's all we need to do.
8504-
s.PutCString(enumerator->getNameAsString());
8504+
if (always_show_value)
8505+
s.Printf("%s (%" PRIi64 ")", enumerator->getNameAsString().c_str(),
8506+
enum_svalue);
8507+
else
8508+
s.PutCString(enumerator->getNameAsString());
85058509
return true;
85068510
}
85078511
}
@@ -8556,7 +8560,7 @@ bool TypeSystemClang::DumpTypeValue(
85568560
lldb::opaque_compiler_type_t type, Stream &s, lldb::Format format,
85578561
const lldb_private::DataExtractor &data, lldb::offset_t byte_offset,
85588562
size_t byte_size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
8559-
ExecutionContextScope *exe_scope) {
8563+
ExecutionContextScope *exe_scope, bool enums_always_show_value) {
85608564
if (!type)
85618565
return false;
85628566
if (IsAggregateType(type)) {
@@ -8568,8 +8572,10 @@ bool TypeSystemClang::DumpTypeValue(
85688572

85698573
if (type_class == clang::Type::Elaborated) {
85708574
qual_type = llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
8571-
return DumpTypeValue(qual_type.getAsOpaquePtr(), s, format, data, byte_offset, byte_size,
8572-
bitfield_bit_size, bitfield_bit_offset, exe_scope);
8575+
return DumpTypeValue(qual_type.getAsOpaquePtr(), s, format, data,
8576+
byte_offset, byte_size, bitfield_bit_size,
8577+
bitfield_bit_offset, exe_scope,
8578+
enums_always_show_value);
85738579
}
85748580

85758581
switch (type_class) {
@@ -8595,7 +8601,7 @@ bool TypeSystemClang::DumpTypeValue(
85958601
// treat as a bitfield
85968602
bitfield_bit_offset, // Offset in bits of a bitfield value if
85978603
// bitfield_bit_size != 0
8598-
exe_scope);
8604+
exe_scope, enums_always_show_value);
85998605
} break;
86008606

86018607
case clang::Type::Enum:
@@ -8604,7 +8610,8 @@ bool TypeSystemClang::DumpTypeValue(
86048610
if ((format == eFormatEnum || format == eFormatDefault) &&
86058611
GetCompleteType(type))
86068612
return DumpEnumValue(qual_type, s, data, byte_offset, byte_size,
8607-
bitfield_bit_offset, bitfield_bit_size);
8613+
bitfield_bit_offset, bitfield_bit_size,
8614+
enums_always_show_value);
86088615
// format was not enum, just fall through and dump the value as
86098616
// requested....
86108617
[[fallthrough]];

lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,8 @@ class TypeSystemClang : public TypeSystem {
10471047
lldb::Format format, const DataExtractor &data,
10481048
lldb::offset_t data_offset, size_t data_byte_size,
10491049
uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
1050-
ExecutionContextScope *exe_scope) override;
1050+
ExecutionContextScope *exe_scope,
1051+
bool enums_always_show_value = false) override;
10511052

10521053
void DumpTypeDescription(
10531054
lldb::opaque_compiler_type_t type,

lldb/source/Symbol/CompilerType.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,12 +1016,13 @@ bool CompilerType::DumpTypeValue(Stream *s, lldb::Format format,
10161016
lldb::offset_t byte_offset, size_t byte_size,
10171017
uint32_t bitfield_bit_size,
10181018
uint32_t bitfield_bit_offset,
1019-
ExecutionContextScope *exe_scope) {
1019+
ExecutionContextScope *exe_scope,
1020+
bool enums_always_show_value) {
10201021
if (IsValid())
10211022
if (auto type_system_sp = GetTypeSystem())
10221023
return type_system_sp->DumpTypeValue(
10231024
m_type, *s, format, data, byte_offset, byte_size, bitfield_bit_size,
1024-
bitfield_bit_offset, exe_scope);
1025+
bitfield_bit_offset, exe_scope, enums_always_show_value);
10251026
return false;
10261027
}
10271028

0 commit comments

Comments
 (0)