Skip to content

Commit 544e95c

Browse files
committed
[llvm-debuginfo-analyzer] Add support for DWARF DW_AT_byte_size
1 parent d597452 commit 544e95c

File tree

14 files changed

+101
-22
lines changed

14 files changed

+101
-22
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ using LVElementKindSet = std::set<LVElementKind>;
6464
using LVElementDispatch = std::map<LVElementKind, LVElementGetFunction>;
6565
using LVElementRequest = std::vector<LVElementGetFunction>;
6666

67+
// Assume 8-bit bytes; this is consistent, e.g. with
68+
// lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp.
69+
constexpr unsigned int DWARF_CHAR_BIT = 8u;
70+
6771
class LVElement : public LVObject {
6872
enum class Property {
6973
IsLine, // A logical line.
@@ -239,6 +243,9 @@ class LVElement : public LVObject {
239243
virtual bool isBase() const { return false; }
240244
virtual bool isTemplateParam() const { return false; }
241245

246+
uint32_t getStorageSizeInBytes() const {
247+
return (getBitSize() + (DWARF_CHAR_BIT - 1)) / DWARF_CHAR_BIT;
248+
}
242249
virtual uint32_t getBitSize() const { return 0; }
243250
virtual void setBitSize(uint32_t Size) {}
244251

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

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

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

@@ -269,6 +272,9 @@ class LVScope : public LVElement {
269272
bool removeElement(LVElement *Element) override;
270273
void updateLevel(LVScope *Parent, bool Moved) override;
271274

275+
uint32_t getBitSize() const override { return BitSize; }
276+
void setBitSize(uint32_t Size) override { BitSize = Size; }
277+
272278
void resolve() override;
273279
void resolveName() override;
274280
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/LVScope.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1011,9 +1011,12 @@ void LVScope::printExtra(raw_ostream &OS, bool Full) const {
10111011
// Do not print any type or name for a lexical block.
10121012
if (!getIsBlock()) {
10131013
OS << " " << formattedName(getName());
1014-
if (!getIsAggregate())
1014+
if (!getIsAggregate()) {
10151015
OS << " -> " << typeOffsetAsString()
10161016
<< formattedNames(getTypeQualifiedName(), typeAsString());
1017+
}
1018+
if (uint32_t Size = getStorageSizeInBytes())
1019+
OS << " [Size = " << Size << "]";
10171020
}
10181021
OS << "\n";
10191022

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,10 @@ 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 (uint32_t Size = getStorageSizeInBytes())
297+
OS << " [Size = " << Size << "]";
298+
OS << "\n";
296299
}
297300

298301
//===----------------------------------------------------------------------===//

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
@@ -307,6 +307,9 @@ void LVDWARFReader::processOneAttribute(const DWARFDie &Die,
307307
case dwarf::DW_AT_bit_size:
308308
CurrentElement->setBitSize(GetAsUnsignedConstant());
309309
break;
310+
case dwarf::DW_AT_byte_size:
311+
CurrentElement->setBitSize(GetAsUnsignedConstant() * DWARF_CHAR_BIT);
312+
break;
310313
case dwarf::DW_AT_call_file:
311314
CurrentElement->setCallFilenameIndex(IncrementFileIndex
312315
? GetAsUnsignedConstant() + 1

llvm/test/tools/llvm-debuginfo-analyzer/COFF/04-coff-missing-nested-enumerators.test

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@
3838
; ONE-NEXT: [001] {CompileUnit} 'pr-46466.cpp'
3939
; ONE-NEXT: [002] {Producer} 'clang version 15.0.0 {{.*}}'
4040
; ONE-NEXT: [002] {Variable} extern 'S' -> 'Struct'
41-
; ONE-NEXT: [002] 1 {Struct} 'Struct'
41+
; ONE-NEXT: [002] 1 {Struct} 'Struct' [Size = 1]
4242
; ONE-NEXT: [003] {Member} public 'U' -> 'Union'
43-
; ONE-NEXT: [003] 2 {Union} 'Union'
43+
; ONE-NEXT: [003] 2 {Union} 'Union' [Size = 1]
4444
; ONE-NEXT: [004] 3 {Enumeration} 'NestedEnum' -> 'int'
4545
; ONE-NEXT: [005] {Enumerator} 'BLUE' = '0x1'
4646
; ONE-NEXT: [005] {Enumerator} 'RED' = '0x0'
@@ -51,9 +51,9 @@
5151
; ONE-NEXT: [001] {CompileUnit} 'pr-46466.cpp'
5252
; ONE-NEXT: [002] {Producer} 'Microsoft (R) Optimizing Compiler'
5353
; ONE-NEXT: [002] {Variable} extern 'S' -> 'Struct'
54-
; ONE-NEXT: [002] 1 {Struct} 'Struct'
54+
; ONE-NEXT: [002] 1 {Struct} 'Struct' [Size = 1]
5555
; ONE-NEXT: [003] {Member} public 'U' -> 'Union'
56-
; ONE-NEXT: [003] 2 {Union} 'Union'
56+
; ONE-NEXT: [003] 2 {Union} 'Union' [Size = 1]
5757
; ONE-NEXT: [004] 3 {Enumeration} 'NestedEnum' -> 'int'
5858
; ONE-NEXT: [005] {Enumerator} 'BLUE' = '0x1'
5959
; ONE-NEXT: [005] {Enumerator} 'RED' = '0x0'
@@ -74,8 +74,8 @@
7474
; TWO-NEXT: [000] {File} 'pr-46466-codeview-clang.o' -> COFF-x86-64
7575
; TWO-EMPTY:
7676
; TWO-NEXT: [001] {CompileUnit} 'pr-46466.cpp'
77-
; TWO-NEXT: [002] 1 {Struct} 'Struct'
78-
; TWO-NEXT: [003] 2 {Union} 'Union'
77+
; TWO-NEXT: [002] 1 {Struct} 'Struct' [Size = 1]
78+
; TWO-NEXT: [003] 2 {Union} 'Union' [Size = 1]
7979
; TWO-NEXT: [004] 3 {Enumeration} 'NestedEnum' -> 'int'
8080
; TWO-NEXT: [005] {Enumerator} 'BLUE' = '0x1'
8181
; TWO-NEXT: [005] {Enumerator} 'RED' = '0x0'
@@ -84,8 +84,8 @@
8484
; TWO-NEXT: [000] {File} 'pr-46466-codeview-msvc.o' -> COFF-x86-64
8585
; TWO-EMPTY:
8686
; TWO-NEXT: [001] {CompileUnit} 'pr-46466.cpp'
87-
; TWO-NEXT: [002] 1 {Struct} 'Struct'
88-
; TWO-NEXT: [003] 2 {Union} 'Union'
87+
; TWO-NEXT: [002] 1 {Struct} 'Struct' [Size = 1]
88+
; TWO-NEXT: [003] 2 {Union} 'Union' [Size = 1]
8989
; TWO-NEXT: [004] 3 {Enumeration} 'NestedEnum' -> 'int'
9090
; TWO-NEXT: [005] {Enumerator} 'BLUE' = '0x1'
9191
; TWO-NEXT: [005] {Enumerator} 'RED' = '0x0'

llvm/test/tools/llvm-debuginfo-analyzer/COFF/06-coff-full-logical-view.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
; ONE-NEXT: {File} 'test.cpp'
3535
; ONE-NEXT: {Public} 'foo' [0x0000000000:0x0000000046]
3636
; ONE-NEXT: [0x0000000000][002] {TypeAlias} 'INTPTR' -> [0x0000001001]'* const int'
37-
; ONE-NEXT: [0x0000000030][002] {BaseType} 'bool'
37+
; ONE-NEXT: [0x0000000030][002] {BaseType} 'bool' [Size = 1]
3838
; ONE-NEXT: [0x0000000000][002] {Function} extern not_inlined 'foo' -> [0x0000000074]'int'
3939
; ONE-NEXT: [0x0000000000][003] {Range} Lines 2:9 [0x0000000000:0x0000000046]
4040
; ONE-NEXT: [0x0000000000][003] {Linkage} 0x1 '?foo@@YAHPEBHI_N@Z'
@@ -80,8 +80,8 @@
8080
; ONE-NEXT: [0x000000003e][003] {Code} 'movl 0x1c(%rsp), %eax'
8181
; ONE-NEXT: [0x0000000042][003] {Code} 'addq $0x20, %rsp'
8282
; ONE-NEXT: [0x0000000046][003] {Code} 'retq'
83-
; ONE-NEXT: [0x0000000074][002] {BaseType} 'int'
84-
; ONE-NEXT: [0x0000000075][002] {BaseType} 'unsigned'
83+
; ONE-NEXT: [0x0000000074][002] {BaseType} 'int' [Size = 4]
84+
; ONE-NEXT: [0x0000000075][002] {BaseType} 'unsigned' [Size = 4]
8585
; ONE-EMPTY:
8686
; ONE-NEXT: -----------------------------
8787
; ONE-NEXT: Element Total Printed

llvm/test/tools/llvm-debuginfo-analyzer/DWARF/04-dwarf-missing-nested-enumerators.test

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
; ONE-NEXT: [001] {CompileUnit} 'pr-46466.cpp'
3939
; ONE-NEXT: [002] {Producer} 'clang version 15.0.0 {{.*}}'
4040
; ONE-NEXT: [002] 8 {Variable} extern 'S' -> 'Struct'
41-
; ONE-NEXT: [002] 1 {Struct} 'Struct'
41+
; ONE-NEXT: [002] 1 {Struct} 'Struct' [Size = 1]
4242
; ONE-NEXT: [003] 5 {Member} public 'U' -> 'Union'
4343
; ONE-EMPTY:
4444
; ONE-NEXT: Logical View:
@@ -47,9 +47,9 @@
4747
; ONE-NEXT: [001] {CompileUnit} 'pr-46466.cpp'
4848
; ONE-NEXT: [002] {Producer} 'GNU C++14 10.3.0 {{.*}}'
4949
; ONE-NEXT: [002] 8 {Variable} extern 'S' -> 'Struct'
50-
; ONE-NEXT: [002] 1 {Struct} 'Struct'
50+
; ONE-NEXT: [002] 1 {Struct} 'Struct' [Size = 1]
5151
; ONE-NEXT: [003] 5 {Member} public 'U' -> 'Union'
52-
; ONE-NEXT: [003] 2 {Union} 'Union'
52+
; ONE-NEXT: [003] 2 {Union} 'Union' [Size = 1]
5353
; ONE-NEXT: [004] 3 {Enumeration} 'NestedEnum' -> 'unsigned int'
5454
; ONE-NEXT: [005] {Enumerator} 'BLUE' = '0x1'
5555
; ONE-NEXT: [005] {Enumerator} 'RED' = '0x0'
@@ -75,8 +75,8 @@
7575
; TWO-NEXT: [000] {File} 'pr-46466-dwarf-gcc.o' -> elf64-x86-64
7676
; TWO-EMPTY:
7777
; TWO-NEXT: [001] {CompileUnit} 'pr-46466.cpp'
78-
; TWO-NEXT: [002] 1 {Struct} 'Struct'
79-
; TWO-NEXT: [003] 2 {Union} 'Union'
78+
; TWO-NEXT: [002] 1 {Struct} 'Struct' [Size = 1]
79+
; TWO-NEXT: [003] 2 {Union} 'Union' [Size = 1]
8080
; TWO-NEXT: [004] 3 {Enumeration} 'NestedEnum' -> 'unsigned int'
8181
; TWO-NEXT: [005] {Enumerator} 'BLUE' = '0x1'
8282
; TWO-NEXT: [005] {Enumerator} 'RED' = '0x0'

llvm/test/tools/llvm-debuginfo-analyzer/DWARF/06-dwarf-full-logical-view.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
; ONE-NEXT: {File} 'test.cpp'
3333
; ONE-NEXT: {Public} 'foo' [0x0000000000:0x000000003a]
3434
; ONE-NEXT: [0x000000000b][002] {Range} Lines 2:9 [0x0000000000:0x000000003a]
35-
; ONE-NEXT: [0x00000000bc][002] {BaseType} 'bool'
36-
; ONE-NEXT: [0x0000000099][002] {BaseType} 'int'
37-
; ONE-NEXT: [0x00000000b5][002] {BaseType} 'unsigned int'
35+
; ONE-NEXT: [0x00000000bc][002] {BaseType} 'bool' [Size = 1]
36+
; ONE-NEXT: [0x0000000099][002] {BaseType} 'int' [Size = 4]
37+
; ONE-NEXT: [0x00000000b5][002] {BaseType} 'unsigned int' [Size = 4]
3838
; ONE-EMPTY:
3939
; ONE-NEXT: [0x00000000a0][002] {Source} '/data/projects/tests/input/general/test.cpp'
4040
; ONE-NEXT: [0x00000000a0][002] 1 {TypeAlias} 'INTPTR' -> [0x00000000ab]'* const int'

llvm/test/tools/llvm-debuginfo-analyzer/DWARF/dw-at-specification.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@
2525
; CHECK: [001] {CompileUnit} 'a.cpp'
2626
; CHECK: [002] {Producer} 'clang version 11.0.0 ({{.*}})'
2727
; CHECK: [002] {Variable} 'Arr' -> 'const int [3]'
28-
; CHECK: [002] 1 {Struct} 'S'
28+
; CHECK: [002] 1 {Struct} 'S' [Size = 1]
2929
; CHECK: [003] 2 {Member} extern public 'Arr' -> 'const int [1]'

llvm/unittests/DebugInfo/LogicalView/CodeViewReaderTest.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "llvm/Testing/Support/Error.h"
2222

2323
#include "gtest/gtest.h"
24+
#include <algorithm>
2425

2526
using namespace llvm;
2627
using namespace llvm::logicalview;
@@ -128,6 +129,26 @@ void checkElementPropertiesClangCodeview(LVReader *Reader) {
128129
const LVLines *Lines = Foo->getLines();
129130
ASSERT_NE(Lines, nullptr);
130131
EXPECT_EQ(Lines->size(), 0x10u);
132+
133+
// Check size of types in CompileUnit.
134+
const LVTypes *Types = CompileUnit->getTypes();
135+
ASSERT_NE(Types, nullptr);
136+
EXPECT_EQ(Types->size(), 6u);
137+
138+
const auto BoolType =
139+
std::find_if(Types->begin(), Types->end(), [](const LVElement *elt) {
140+
return elt->getName() == "bool";
141+
});
142+
ASSERT_NE(BoolType, Types->end());
143+
const auto IntType =
144+
std::find_if(Types->begin(), Types->end(), [](const LVElement *elt) {
145+
return elt->getName() == "int";
146+
});
147+
ASSERT_NE(IntType, Types->end());
148+
EXPECT_EQ(static_cast<LVType *>(*BoolType)->getBitSize(), 8u);
149+
EXPECT_EQ(static_cast<LVType *>(*BoolType)->getStorageSizeInBytes(), 1u);
150+
EXPECT_EQ(static_cast<LVType *>(*IntType)->getBitSize(), 32u);
151+
EXPECT_EQ(static_cast<LVType *>(*IntType)->getStorageSizeInBytes(), 4u);
131152
}
132153

133154
// Check the logical elements basic properties (MSVC - Codeview).
@@ -194,6 +215,26 @@ void checkElementPropertiesMsvcCodeview(LVReader *Reader) {
194215
const LVLines *Lines = Foo->getLines();
195216
ASSERT_NE(Lines, nullptr);
196217
EXPECT_EQ(Lines->size(), 0x0eu);
218+
219+
// Check size of types in CompileUnit.
220+
const LVTypes *Types = CompileUnit->getTypes();
221+
ASSERT_NE(Types, nullptr);
222+
EXPECT_EQ(Types->size(), 8u);
223+
224+
const auto BoolType =
225+
std::find_if(Types->begin(), Types->end(), [](const LVElement *elt) {
226+
return elt->getName() == "bool";
227+
});
228+
ASSERT_NE(BoolType, Types->end());
229+
const auto IntType =
230+
std::find_if(Types->begin(), Types->end(), [](const LVElement *elt) {
231+
return elt->getName() == "int";
232+
});
233+
ASSERT_NE(IntType, Types->end());
234+
EXPECT_EQ(static_cast<LVType *>(*BoolType)->getBitSize(), 8u);
235+
EXPECT_EQ(static_cast<LVType *>(*BoolType)->getStorageSizeInBytes(), 1u);
236+
EXPECT_EQ(static_cast<LVType *>(*IntType)->getBitSize(), 32u);
237+
EXPECT_EQ(static_cast<LVType *>(*IntType)->getStorageSizeInBytes(), 4u);
197238
}
198239

199240
// Check the logical elements basic properties (MSVC library - Codeview).

llvm/unittests/DebugInfo/LogicalView/DWARFReaderTest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ void checkElementSelection(LVReader *Reader) {
155155
ASSERT_NE(Element, nullptr);
156156
EXPECT_NE(Element->getName().find("INTEGER"), StringRef::npos);
157157
EXPECT_EQ(Element->getIsType(), 1);
158+
// Underlying type is `int`
159+
const LVElement *UnderlyingType =
160+
static_cast<LVType *>(Element)->getUnderlyingType();
161+
EXPECT_EQ(UnderlyingType->getIsType(), 1);
162+
EXPECT_EQ(UnderlyingType->getBitSize(), 32u);
163+
EXPECT_EQ(UnderlyingType->getStorageSizeInBytes(), 4u);
158164

159165
Element = MapElements[0x000000000f]; // 'movl %edx, %eax'
160166
ASSERT_NE(Element, nullptr);

0 commit comments

Comments
 (0)