Skip to content

Commit 9cac4bf

Browse files
authored
[llvm-debuginfo-analyzer] Add support for DWARF DW_AT_byte_size (#139110)
This PR was split from #137228 (which introduced support for `DW_TAG_module` and `DW_AT_byte_size`). This PR improves `LVDWARFReader` by introducing handling of `DW_AT_byte_size`. Most DWARF emitters include this attribute for types to specify the size of an entity of the given type.
1 parent 83de1ef commit 9cac4bf

File tree

20 files changed

+141
-31
lines changed

20 files changed

+141
-31
lines changed

llvm/docs/CommandGuide/llvm-debuginfo-analyzer.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ toolchain name, binary file format, etc.
161161
transformations, in order to display built-in types (int, bool, etc.);
162162
parameters and arguments used during template instantiation; parent
163163
name hierarchy; array dimensions information; compiler generated
164-
elements and the underlying types associated with the types aliases.
164+
elements; type sizes and the underlying types associated with the types
165+
aliases.
165166

166167
.. code-block:: text
167168
@@ -171,6 +172,7 @@ toolchain name, binary file format, etc.
171172
=encoded: Template arguments encoded in the template name.
172173
=qualified: The element type include parents in its name.
173174
=reference: Element declaration and definition references.
175+
=size: Sizes for compound and base types.
174176
=subrange: Subrange encoding information for arrays.
175177
=typename: Template parameters.
176178
=underlying: Underlying type for type definitions.
@@ -258,6 +260,7 @@ toolchain name, binary file format, etc.
258260
=qualified
259261
=qualifier
260262
=register
263+
=size
261264
=subrange
262265
=system
263266
=typename

llvm/include/llvm/DebugInfo/LogicalView/Core/LVElement.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "llvm/DebugInfo/LogicalView/Core/LVObject.h"
1818
#include "llvm/Support/Casting.h"
19+
#include "llvm/Support/MathExtras.h"
1920
#include <map>
2021
#include <set>
2122
#include <vector>
@@ -65,6 +66,10 @@ using LVElementKindSet = std::set<LVElementKind>;
6566
using LVElementDispatch = std::map<LVElementKind, LVElementGetFunction>;
6667
using LVElementRequest = std::vector<LVElementGetFunction>;
6768

69+
// Assume 8-bit bytes; this is consistent, e.g. with
70+
// lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp.
71+
constexpr unsigned int DWARF_CHAR_BIT = 8u;
72+
6873
class LVElement : public LVObject {
6974
enum class Property {
7075
IsLine, // A logical line.
@@ -240,6 +245,9 @@ class LVElement : public LVObject {
240245
virtual bool isBase() const { return false; }
241246
virtual bool isTemplateParam() const { return false; }
242247

248+
uint32_t getStorageSizeInBytes() const {
249+
return llvm::divideCeil(getBitSize(), DWARF_CHAR_BIT);
250+
}
243251
virtual uint32_t getBitSize() const { return 0; }
244252
virtual void setBitSize(uint32_t Size) {}
245253

llvm/include/llvm/DebugInfo/LogicalView/Core/LVOptions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ enum class LVAttributeKind {
119119
Range, // --attribute=range
120120
Reference, // --attribute=reference
121121
Register, // --attribute=register
122+
Size, // --attribute=size
122123
Standard, // --attribute=standard
123124
Subrange, // --attribute=subrange
124125
System, // --attribute=system
@@ -349,6 +350,7 @@ class LVOptions {
349350
ATTRIBUTE_OPTION(Range);
350351
ATTRIBUTE_OPTION(Reference);
351352
ATTRIBUTE_OPTION(Register);
353+
ATTRIBUTE_OPTION(Size);
352354
ATTRIBUTE_OPTION(Standard);
353355
ATTRIBUTE_OPTION(Subrange);
354356
ATTRIBUTE_OPTION(System);

llvm/include/llvm/DebugInfo/LogicalView/Core/LVScope.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ class LVScope : public LVElement {
9494
LVProperties<Property> Properties;
9595
static LVScopeDispatch Dispatch;
9696

97+
// Size in bits if this scope represents also a compound type.
98+
uint32_t BitSize = 0;
99+
97100
// Coverage factor in units (bytes).
98101
unsigned CoverageFactor = 0;
99102

@@ -271,6 +274,9 @@ class LVScope : public LVElement {
271274
bool removeElement(LVElement *Element) override;
272275
void updateLevel(LVScope *Parent, bool Moved) override;
273276

277+
uint32_t getBitSize() const override { return BitSize; }
278+
void setBitSize(uint32_t Size) override { BitSize = Size; }
279+
274280
void resolve() override;
275281
void resolveName() override;
276282
void resolveReferences() override;

llvm/include/llvm/DebugInfo/LogicalView/Core/LVType.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ class LVType : public LVElement {
5656
LVProperties<Property> Properties;
5757
static LVTypeDispatch Dispatch;
5858

59+
// Size in bits of a symbol of this type.
60+
uint32_t BitSize = 0;
61+
5962
// Find the current type in the given 'Targets'.
6063
LVType *findIn(const LVTypes *Targets) const;
6164

@@ -109,6 +112,10 @@ class LVType : public LVElement {
109112
virtual LVElement *getUnderlyingType() { return nullptr; }
110113
virtual void setUnderlyingType(LVElement *Element) {}
111114

115+
// Return the size in bits of an entity of this type.
116+
uint32_t getBitSize() const override { return BitSize; }
117+
void setBitSize(uint32_t Size) override { BitSize = Size; }
118+
112119
void resolveName() override;
113120
void resolveReferences() override;
114121

llvm/lib/DebugInfo/LogicalView/Core/LVOptions.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ void LVOptions::resolveDependencies() {
6767
setAttributeQualified();
6868
setAttributeQualifier();
6969
setAttributeRegister();
70+
setAttributeSize();
7071
setAttributeSubrange();
7172
setAttributeSystem();
7273
setAttributeTypename();
@@ -208,7 +209,7 @@ void LVOptions::resolveDependencies() {
208209
// 1) Sort the CUs, to get a fast compare.
209210
// 2) Encode template instantiations, so the names include template
210211
// parameter information.
211-
// 3) Include qualified types.
212+
// 3) Include qualified types and their sizes.
212213
// 4) Include any inserted abstract references.
213214
// 5) For added/missing elements add the '+' or '-' tags.
214215
if (getCompareExecute()) {
@@ -221,6 +222,7 @@ void LVOptions::resolveDependencies() {
221222
setAttributeInserted();
222223
setAttributeMissing();
223224
setAttributeQualified();
225+
setAttributeSize();
224226
}
225227

226228
// Enable formatting for printing (indentation, print children).
@@ -312,9 +314,10 @@ void LVOptions::print(raw_ostream &OS) const {
312314
<< "Range: " << getAttributeRange() << ", "
313315
<< "Reference: " << getAttributeReference() << "\n"
314316
<< "Register: " << getAttributeRegister() << ", "
317+
<< "Size: " << getAttributeSize() << ", "
315318
<< "Standard: " << getAttributeStandard() << ", "
316-
<< "Subrange: " << getAttributeSubrange() << ", "
317-
<< "System: " << getAttributeSystem() << "\n"
319+
<< "Subrange: " << getAttributeSubrange() << "\n"
320+
<< "System: " << getAttributeSystem() << ", "
318321
<< "Typename: " << getAttributeTypename() << ", "
319322
<< "Underlying: " << getAttributeUnderlying() << ", "
320323
<< "Zero: " << getAttributeZero() << "\n";

llvm/lib/DebugInfo/LogicalView/Core/LVScope.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1018,9 +1018,13 @@ void LVScope::printExtra(raw_ostream &OS, bool Full) const {
10181018
// Do not print any type or name for a lexical block.
10191019
if (!getIsBlock()) {
10201020
OS << " " << formattedName(getName());
1021-
if (!getIsAggregate())
1021+
if (!getIsAggregate()) {
10221022
OS << " -> " << typeOffsetAsString()
10231023
<< formattedNames(getTypeQualifiedName(), typeAsString());
1024+
}
1025+
if (options().getAttributeSize())
1026+
if (uint32_t Size = getStorageSizeInBytes())
1027+
OS << " [Size = " << Size << "]";
10241028
}
10251029
OS << "\n";
10261030

llvm/lib/DebugInfo/LogicalView/Core/LVType.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,11 @@ void LVType::print(raw_ostream &OS, bool Full) const {
292292
}
293293

294294
void LVType::printExtra(raw_ostream &OS, bool Full) const {
295-
OS << formattedKind(kind()) << " " << formattedName(getName()) << "\n";
295+
OS << formattedKind(kind()) << " " << formattedName(getName());
296+
if (options().getAttributeSize())
297+
if (uint32_t Size = getStorageSizeInBytes())
298+
OS << " [Size = " << Size << "]";
299+
OS << "\n";
296300
}
297301

298302
//===----------------------------------------------------------------------===//

llvm/lib/DebugInfo/LogicalView/Readers/LVCodeViewVisitor.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,6 +1987,7 @@ Error LVLogicalVisitor::visitKnownRecord(CVType &Record, ClassRecord &Class,
19871987
Scope->setName(Class.getName());
19881988
if (Class.hasUniqueName())
19891989
Scope->setLinkageName(Class.getUniqueName());
1990+
Scope->setBitSize(Class.getSize() * DWARF_CHAR_BIT);
19901991

19911992
if (Class.isNested()) {
19921993
Scope->setIsNested();
@@ -2455,6 +2456,7 @@ Error LVLogicalVisitor::visitKnownRecord(CVType &Record, UnionRecord &Union,
24552456
Scope->setName(Union.getName());
24562457
if (Union.hasUniqueName())
24572458
Scope->setLinkageName(Union.getUniqueName());
2459+
Scope->setBitSize(Union.getSize() * DWARF_CHAR_BIT);
24582460

24592461
if (Union.isNested()) {
24602462
Scope->setIsNested();
@@ -3208,6 +3210,7 @@ LVType *LVLogicalVisitor::createBaseType(TypeIndex TI, StringRef TypeName) {
32083210

32093211
if (createElement(TIR, SimpleKind)) {
32103212
CurrentType->setName(TypeName);
3213+
CurrentType->setBitSize(getSizeInBytesForTypeIndex(TIR) * DWARF_CHAR_BIT);
32113214
Reader->getCompileUnit()->addElement(CurrentType);
32123215
}
32133216
return CurrentType;

llvm/lib/DebugInfo/LogicalView/Readers/LVDWARFReader.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,9 @@ void LVDWARFReader::processOneAttribute(const DWARFDie &Die,
310310
case dwarf::DW_AT_bit_size:
311311
CurrentElement->setBitSize(GetAsUnsignedConstant());
312312
break;
313+
case dwarf::DW_AT_byte_size:
314+
CurrentElement->setBitSize(GetAsUnsignedConstant() * DWARF_CHAR_BIT);
315+
break;
313316
case dwarf::DW_AT_call_file:
314317
CurrentElement->setCallFilenameIndex(IncrementFileIndex
315318
? GetAsUnsignedConstant() + 1

0 commit comments

Comments
 (0)