1 //===- BTFDebug.h -----------------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 /// This file contains support for writing BTF debug info.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_LIB_TARGET_BPF_BTFDEBUG_H
15 #define LLVM_LIB_TARGET_BPF_BTFDEBUG_H
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/CodeGen/DebugHandlerBase.h"
19 #include <unordered_map>
29 class MachineFunction
;
31 /// The base class for BTF type generation.
36 struct BTF::CommonType BTFType
;
39 virtual ~BTFTypeBase() = default;
40 void setId(uint32_t Id
) { this->Id
= Id
; }
41 uint32_t getId() { return Id
; }
42 uint32_t roundupToBytes(uint32_t NumBits
) { return (NumBits
+ 7) >> 3; }
43 /// Get the size of this BTF type entry.
44 virtual uint32_t getSize() { return BTF::CommonTypeSize
; }
45 /// Complete BTF type generation after all related DebugInfo types
46 /// have been visited so their BTF type id's are available
47 /// for cross referece.
48 virtual void completeType(BTFDebug
&BDebug
) {}
49 /// Emit types for this BTF type entry.
50 virtual void emitType(MCStreamer
&OS
);
53 /// Handle several derived types include pointer, const,
54 /// volatile, typedef and restrict.
55 class BTFTypeDerived
: public BTFTypeBase
{
56 const DIDerivedType
*DTy
;
59 BTFTypeDerived(const DIDerivedType
*Ty
, unsigned Tag
);
60 void completeType(BTFDebug
&BDebug
);
61 void emitType(MCStreamer
&OS
);
64 /// Handle struct or union forward declaration.
65 class BTFTypeFwd
: public BTFTypeBase
{
69 BTFTypeFwd(StringRef Name
, bool IsUnion
);
70 void completeType(BTFDebug
&BDebug
);
71 void emitType(MCStreamer
&OS
);
75 class BTFTypeInt
: public BTFTypeBase
{
77 uint32_t IntVal
; ///< Encoding, offset, bits
80 BTFTypeInt(uint32_t Encoding
, uint32_t SizeInBits
, uint32_t OffsetInBits
,
82 uint32_t getSize() { return BTFTypeBase::getSize() + sizeof(uint32_t); }
83 void completeType(BTFDebug
&BDebug
);
84 void emitType(MCStreamer
&OS
);
87 /// Handle enumerate type.
88 class BTFTypeEnum
: public BTFTypeBase
{
89 const DICompositeType
*ETy
;
90 std::vector
<struct BTF::BTFEnum
> EnumValues
;
93 BTFTypeEnum(const DICompositeType
*ETy
, uint32_t NumValues
);
95 return BTFTypeBase::getSize() + EnumValues
.size() * BTF::BTFEnumSize
;
97 void completeType(BTFDebug
&BDebug
);
98 void emitType(MCStreamer
&OS
);
101 /// Handle array type.
102 class BTFTypeArray
: public BTFTypeBase
{
103 const DICompositeType
*ATy
;
104 struct BTF::BTFArray ArrayInfo
;
107 BTFTypeArray(const DICompositeType
*ATy
);
108 uint32_t getSize() { return BTFTypeBase::getSize() + BTF::BTFArraySize
; }
109 void completeType(BTFDebug
&BDebug
);
110 void emitType(MCStreamer
&OS
);
113 /// Handle struct/union type.
114 class BTFTypeStruct
: public BTFTypeBase
{
115 const DICompositeType
*STy
;
117 std::vector
<struct BTF::BTFMember
> Members
;
120 BTFTypeStruct(const DICompositeType
*STy
, bool IsStruct
, bool HasBitField
,
121 uint32_t NumMembers
);
123 return BTFTypeBase::getSize() + Members
.size() * BTF::BTFMemberSize
;
125 void completeType(BTFDebug
&BDebug
);
126 void emitType(MCStreamer
&OS
);
129 /// Handle function pointer.
130 class BTFTypeFuncProto
: public BTFTypeBase
{
131 const DISubroutineType
*STy
;
132 std::unordered_map
<uint32_t, StringRef
> FuncArgNames
;
133 std::vector
<struct BTF::BTFParam
> Parameters
;
136 BTFTypeFuncProto(const DISubroutineType
*STy
, uint32_t NumParams
,
137 const std::unordered_map
<uint32_t, StringRef
> &FuncArgNames
);
139 return BTFTypeBase::getSize() + Parameters
.size() * BTF::BTFParamSize
;
141 void completeType(BTFDebug
&BDebug
);
142 void emitType(MCStreamer
&OS
);
145 /// Handle subprogram
146 class BTFTypeFunc
: public BTFTypeBase
{
150 BTFTypeFunc(StringRef FuncName
, uint32_t ProtoTypeId
);
151 uint32_t getSize() { return BTFTypeBase::getSize(); }
152 void completeType(BTFDebug
&BDebug
);
153 void emitType(MCStreamer
&OS
);
157 class BTFStringTable
{
158 /// String table size in bytes.
160 /// A mapping from string table offset to the index
161 /// of the Table. It is used to avoid putting
162 /// duplicated strings in the table.
163 std::unordered_map
<uint32_t, uint32_t> OffsetToIdMap
;
164 /// A vector of strings to represent the string table.
165 std::vector
<std::string
> Table
;
168 BTFStringTable() : Size(0) {}
169 uint32_t getSize() { return Size
; }
170 std::vector
<std::string
> &getTable() { return Table
; }
171 /// Add a string to the string table and returns its offset
173 uint32_t addString(StringRef S
);
176 /// Represent one func and its type id.
178 const MCSymbol
*Label
; ///< Func MCSymbol
179 uint32_t TypeId
; ///< Type id referring to .BTF type section
182 /// Represent one line info.
184 MCSymbol
*Label
; ///< MCSymbol identifying insn for the lineinfo
185 uint32_t FileNameOff
; ///< file name offset in the .BTF string table
186 uint32_t LineOff
; ///< line offset in the .BTF string table
187 uint32_t LineNum
; ///< the line number
188 uint32_t ColumnNum
; ///< the column number
191 /// Collect and emit BTF information.
192 class BTFDebug
: public DebugHandlerBase
{
194 bool SkipInstruction
;
195 bool LineInfoGenerated
;
197 uint32_t ArrayIndexTypeId
;
198 BTFStringTable StringTable
;
199 std::vector
<std::unique_ptr
<BTFTypeBase
>> TypeEntries
;
200 std::unordered_map
<const DIType
*, uint32_t> DIToIdMap
;
201 std::unordered_map
<uint32_t, std::vector
<BTFFuncInfo
>> FuncInfoTable
;
202 std::unordered_map
<uint32_t, std::vector
<BTFLineInfo
>> LineInfoTable
;
203 StringMap
<std::vector
<std::string
>> FileContent
;
205 /// Add types to TypeEntries.
207 /// Add types to TypeEntries and DIToIdMap.
208 void addType(std::unique_ptr
<BTFTypeBase
> TypeEntry
, const DIType
*Ty
);
209 /// Add types to TypeEntries only and return type id.
210 uint32_t addType(std::unique_ptr
<BTFTypeBase
> TypeEntry
);
213 /// IR type visiting functions.
215 void visitTypeEntry(const DIType
*Ty
);
216 void visitBasicType(const DIBasicType
*BTy
);
217 void visitSubroutineType(
218 const DISubroutineType
*STy
, bool ForSubprog
,
219 const std::unordered_map
<uint32_t, StringRef
> &FuncArgNames
,
221 void visitFwdDeclType(const DICompositeType
*CTy
, bool IsUnion
);
222 void visitCompositeType(const DICompositeType
*CTy
);
223 void visitStructType(const DICompositeType
*STy
, bool IsStruct
);
224 void visitArrayType(const DICompositeType
*ATy
);
225 void visitEnumType(const DICompositeType
*ETy
);
226 void visitDerivedType(const DIDerivedType
*DTy
);
229 /// Get the file content for the subprogram. Certain lines of the file
230 /// later may be put into string table and referenced by line info.
231 std::string
populateFileContent(const DISubprogram
*SP
);
233 /// Construct a line info.
234 void constructLineInfo(const DISubprogram
*SP
, MCSymbol
*Label
, uint32_t Line
,
237 /// Emit common header of .BTF and .BTF.ext sections.
238 void emitCommonHeader();
240 /// Emit the .BTF section.
241 void emitBTFSection();
243 /// Emit the .BTF.ext section.
244 void emitBTFExtSection();
247 /// Gather pre-function debug information.
248 void beginFunctionImpl(const MachineFunction
*MF
) override
;
250 /// Post process after all instructions in this function are processed.
251 void endFunctionImpl(const MachineFunction
*MF
) override
;
254 BTFDebug(AsmPrinter
*AP
);
256 /// Get the special array index type id.
257 uint32_t getArrayIndexTypeId() {
258 assert(ArrayIndexTypeId
);
259 return ArrayIndexTypeId
;
262 /// Add string to the string table.
263 size_t addString(StringRef S
) { return StringTable
.addString(S
); }
265 /// Get the type id for a particular DIType.
266 uint32_t getTypeId(const DIType
*Ty
) {
267 assert(Ty
&& "Invalid null Type");
268 assert(DIToIdMap
.find(Ty
) != DIToIdMap
.end() &&
269 "DIType not added in the BDIToIdMap");
270 return DIToIdMap
[Ty
];
273 void setSymbolSize(const MCSymbol
*Symbol
, uint64_t Size
) override
{}
275 /// Process beginning of an instruction.
276 void beginInstruction(const MachineInstr
*MI
) override
;
278 /// Complete all the types and emit the BTF sections.
279 void endModule() override
;
282 } // end namespace llvm