LLVM  10.0.0svn
BTFDebug.h
Go to the documentation of this file.
1 //===- BTFDebug.h -----------------------------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file contains support for writing BTF debug info.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_LIB_TARGET_BPF_BTFDEBUG_H
15 #define LLVM_LIB_TARGET_BPF_BTFDEBUG_H
16 
17 #include "llvm/ADT/StringMap.h"
19 #include <unordered_map>
20 #include "BTF.h"
21 
22 namespace llvm {
23 
24 class AsmPrinter;
25 class BTFDebug;
26 class DIType;
27 class MCStreamer;
28 class MCSymbol;
29 class MachineFunction;
30 
31 /// The base class for BTF type generation.
32 class BTFTypeBase {
33 protected:
34  uint8_t Kind;
38 
39 public:
40  BTFTypeBase() : IsCompleted(false) {}
41  virtual ~BTFTypeBase() = default;
42  void setId(uint32_t Id) { this->Id = Id; }
43  uint32_t getId() { return Id; }
44  uint32_t roundupToBytes(uint32_t NumBits) { return (NumBits + 7) >> 3; }
45  /// Get the size of this BTF type entry.
46  virtual uint32_t getSize() { return BTF::CommonTypeSize; }
47  /// Complete BTF type generation after all related DebugInfo types
48  /// have been visited so their BTF type id's are available
49  /// for cross referece.
50  virtual void completeType(BTFDebug &BDebug) {}
51  /// Emit types for this BTF type entry.
52  virtual void emitType(MCStreamer &OS);
53 };
54 
55 /// Handle several derived types include pointer, const,
56 /// volatile, typedef and restrict.
57 class BTFTypeDerived : public BTFTypeBase {
58  const DIDerivedType *DTy;
59  bool NeedsFixup;
60 
61 public:
62  BTFTypeDerived(const DIDerivedType *Ty, unsigned Tag, bool NeedsFixup);
63  void completeType(BTFDebug &BDebug);
64  void emitType(MCStreamer &OS);
65  void setPointeeType(uint32_t PointeeType);
66 };
67 
68 /// Handle struct or union forward declaration.
69 class BTFTypeFwd : public BTFTypeBase {
71 
72 public:
73  BTFTypeFwd(StringRef Name, bool IsUnion);
74  void completeType(BTFDebug &BDebug);
75  void emitType(MCStreamer &OS);
76 };
77 
78 /// Handle int type.
79 class BTFTypeInt : public BTFTypeBase {
81  uint32_t IntVal; ///< Encoding, offset, bits
82 
83 public:
84  BTFTypeInt(uint32_t Encoding, uint32_t SizeInBits, uint32_t OffsetInBits,
86  uint32_t getSize() { return BTFTypeBase::getSize() + sizeof(uint32_t); }
87  void completeType(BTFDebug &BDebug);
88  void emitType(MCStreamer &OS);
89 };
90 
91 /// Handle enumerate type.
92 class BTFTypeEnum : public BTFTypeBase {
93  const DICompositeType *ETy;
94  std::vector<struct BTF::BTFEnum> EnumValues;
95 
96 public:
97  BTFTypeEnum(const DICompositeType *ETy, uint32_t NumValues);
99  return BTFTypeBase::getSize() + EnumValues.size() * BTF::BTFEnumSize;
100  }
101  void completeType(BTFDebug &BDebug);
102  void emitType(MCStreamer &OS);
103 };
104 
105 /// Handle array type.
106 class BTFTypeArray : public BTFTypeBase {
107  struct BTF::BTFArray ArrayInfo;
108 
109 public:
110  BTFTypeArray(uint32_t ElemTypeId, uint32_t NumElems);
112  void completeType(BTFDebug &BDebug);
113  void emitType(MCStreamer &OS);
114 };
115 
116 /// Handle struct/union type.
117 class BTFTypeStruct : public BTFTypeBase {
118  const DICompositeType *STy;
119  bool HasBitField;
120  std::vector<struct BTF::BTFMember> Members;
121 
122 public:
123  BTFTypeStruct(const DICompositeType *STy, bool IsStruct, bool HasBitField,
124  uint32_t NumMembers);
126  return BTFTypeBase::getSize() + Members.size() * BTF::BTFMemberSize;
127  }
128  void completeType(BTFDebug &BDebug);
129  void emitType(MCStreamer &OS);
130  std::string getName();
131 };
132 
133 /// Handle function pointer.
135  const DISubroutineType *STy;
136  std::unordered_map<uint32_t, StringRef> FuncArgNames;
137  std::vector<struct BTF::BTFParam> Parameters;
138 
139 public:
140  BTFTypeFuncProto(const DISubroutineType *STy, uint32_t NumParams,
141  const std::unordered_map<uint32_t, StringRef> &FuncArgNames);
143  return BTFTypeBase::getSize() + Parameters.size() * BTF::BTFParamSize;
144  }
145  void completeType(BTFDebug &BDebug);
146  void emitType(MCStreamer &OS);
147 };
148 
149 /// Handle subprogram
150 class BTFTypeFunc : public BTFTypeBase {
151  StringRef Name;
152 
153 public:
154  BTFTypeFunc(StringRef FuncName, uint32_t ProtoTypeId);
156  void completeType(BTFDebug &BDebug);
157  void emitType(MCStreamer &OS);
158 };
159 
160 /// Handle variable instances
161 class BTFKindVar : public BTFTypeBase {
162  StringRef Name;
163  uint32_t Info;
164 
165 public:
166  BTFKindVar(StringRef VarName, uint32_t TypeId, uint32_t VarInfo);
168  void completeType(BTFDebug &BDebug);
169  void emitType(MCStreamer &OS);
170 };
171 
172 /// Handle data sections
173 class BTFKindDataSec : public BTFTypeBase {
174  AsmPrinter *Asm;
175  std::string Name;
176  std::vector<std::tuple<uint32_t, const MCSymbol *, uint32_t>> Vars;
177 
178 public:
179  BTFKindDataSec(AsmPrinter *AsmPrt, std::string SecName);
181  return BTFTypeBase::getSize() + BTF::BTFDataSecVarSize * Vars.size();
182  }
183  void addVar(uint32_t Id, const MCSymbol *Sym, uint32_t Size) {
184  Vars.push_back(std::make_tuple(Id, Sym, Size));
185  }
186  std::string getName() { return Name; }
187  void completeType(BTFDebug &BDebug);
188  void emitType(MCStreamer &OS);
189 };
190 
191 /// String table.
193  /// String table size in bytes.
194  uint32_t Size;
195  /// A mapping from string table offset to the index
196  /// of the Table. It is used to avoid putting
197  /// duplicated strings in the table.
198  std::unordered_map<uint32_t, uint32_t> OffsetToIdMap;
199  /// A vector of strings to represent the string table.
200  std::vector<std::string> Table;
201 
202 public:
203  BTFStringTable() : Size(0) {}
204  uint32_t getSize() { return Size; }
205  std::vector<std::string> &getTable() { return Table; }
206  /// Add a string to the string table and returns its offset
207  /// in the table.
208  uint32_t addString(StringRef S);
209 };
210 
211 /// Represent one func and its type id.
212 struct BTFFuncInfo {
213  const MCSymbol *Label; ///< Func MCSymbol
214  uint32_t TypeId; ///< Type id referring to .BTF type section
215 };
216 
217 /// Represent one line info.
218 struct BTFLineInfo {
219  MCSymbol *Label; ///< MCSymbol identifying insn for the lineinfo
220  uint32_t FileNameOff; ///< file name offset in the .BTF string table
221  uint32_t LineOff; ///< line offset in the .BTF string table
222  uint32_t LineNum; ///< the line number
223  uint32_t ColumnNum; ///< the column number
224 };
225 
226 /// Represent one offset relocation.
228  const MCSymbol *Label; ///< MCSymbol identifying insn for the reloc
229  uint32_t TypeID; ///< Type ID
230  uint32_t OffsetNameOff; ///< The string to traverse types
231 };
232 
233 /// Represent one extern relocation.
235  const MCSymbol *Label; ///< MCSymbol identifying insn for the reloc
236  uint32_t ExternNameOff; ///< The extern variable name
237 };
238 
239 /// Collect and emit BTF information.
240 class BTFDebug : public DebugHandlerBase {
241  MCStreamer &OS;
242  bool SkipInstruction;
243  bool LineInfoGenerated;
244  uint32_t SecNameOff;
245  uint32_t ArrayIndexTypeId;
246  bool MapDefNotCollected;
247  BTFStringTable StringTable;
248  std::vector<std::unique_ptr<BTFTypeBase>> TypeEntries;
249  std::unordered_map<const DIType *, uint32_t> DIToIdMap;
250  std::map<uint32_t, std::vector<BTFFuncInfo>> FuncInfoTable;
251  std::map<uint32_t, std::vector<BTFLineInfo>> LineInfoTable;
252  std::map<uint32_t, std::vector<BTFOffsetReloc>> OffsetRelocTable;
253  std::map<uint32_t, std::vector<BTFExternReloc>> ExternRelocTable;
255  std::map<std::string, std::unique_ptr<BTFKindDataSec>> DataSecEntries;
256  std::vector<BTFTypeStruct *> StructTypes;
257  std::map<std::string, int64_t> AccessOffsets;
258  std::map<StringRef, std::pair<bool, std::vector<BTFTypeDerived *>>>
259  FixupDerivedTypes;
260 
261  /// Add types to TypeEntries.
262  /// @{
263  /// Add types to TypeEntries and DIToIdMap.
264  uint32_t addType(std::unique_ptr<BTFTypeBase> TypeEntry, const DIType *Ty);
265  /// Add types to TypeEntries only and return type id.
266  uint32_t addType(std::unique_ptr<BTFTypeBase> TypeEntry);
267  /// @}
268 
269  /// IR type visiting functions.
270  /// @{
271  void visitTypeEntry(const DIType *Ty);
272  void visitTypeEntry(const DIType *Ty, uint32_t &TypeId, bool CheckPointer,
273  bool SeenPointer);
274  void visitBasicType(const DIBasicType *BTy, uint32_t &TypeId);
275  void visitSubroutineType(
276  const DISubroutineType *STy, bool ForSubprog,
277  const std::unordered_map<uint32_t, StringRef> &FuncArgNames,
278  uint32_t &TypeId);
279  void visitFwdDeclType(const DICompositeType *CTy, bool IsUnion,
280  uint32_t &TypeId);
281  void visitCompositeType(const DICompositeType *CTy, uint32_t &TypeId);
282  void visitStructType(const DICompositeType *STy, bool IsStruct,
283  uint32_t &TypeId);
284  void visitArrayType(const DICompositeType *ATy, uint32_t &TypeId);
285  void visitEnumType(const DICompositeType *ETy, uint32_t &TypeId);
286  void visitDerivedType(const DIDerivedType *DTy, uint32_t &TypeId,
287  bool CheckPointer, bool SeenPointer);
288  void visitMapDefType(const DIType *Ty, uint32_t &TypeId);
289  /// @}
290 
291  /// Get the file content for the subprogram. Certain lines of the file
292  /// later may be put into string table and referenced by line info.
293  std::string populateFileContent(const DISubprogram *SP);
294 
295  /// Construct a line info.
296  void constructLineInfo(const DISubprogram *SP, MCSymbol *Label, uint32_t Line,
297  uint32_t Column);
298 
299  /// Generate types and variables for globals.
300  void processGlobals(bool ProcessingMapDef);
301 
302  /// Generate one offset relocation record.
303  void generateOffsetReloc(const MachineInstr *MI, const MCSymbol *ORSym,
304  DIType *RootTy, StringRef AccessPattern);
305 
306  /// Populating unprocessed struct type.
307  unsigned populateStructType(const DIType *Ty);
308 
309  /// Process LD_imm64 instructions.
310  void processLDimm64(const MachineInstr *MI);
311 
312  /// Emit common header of .BTF and .BTF.ext sections.
313  void emitCommonHeader();
314 
315  /// Emit the .BTF section.
316  void emitBTFSection();
317 
318  /// Emit the .BTF.ext section.
319  void emitBTFExtSection();
320 
321 protected:
322  /// Gather pre-function debug information.
323  void beginFunctionImpl(const MachineFunction *MF) override;
324 
325  /// Post process after all instructions in this function are processed.
326  void endFunctionImpl(const MachineFunction *MF) override;
327 
328 public:
329  BTFDebug(AsmPrinter *AP);
330 
331  ///
332  bool InstLower(const MachineInstr *MI, MCInst &OutMI);
333 
334  /// Get the special array index type id.
336  assert(ArrayIndexTypeId);
337  return ArrayIndexTypeId;
338  }
339 
340  /// Add string to the string table.
341  size_t addString(StringRef S) { return StringTable.addString(S); }
342 
343  /// Get the type id for a particular DIType.
344  uint32_t getTypeId(const DIType *Ty) {
345  assert(Ty && "Invalid null Type");
346  assert(DIToIdMap.find(Ty) != DIToIdMap.end() &&
347  "DIType not added in the BDIToIdMap");
348  return DIToIdMap[Ty];
349  }
350 
351  void setSymbolSize(const MCSymbol *Symbol, uint64_t Size) override {}
352 
353  /// Process beginning of an instruction.
354  void beginInstruction(const MachineInstr *MI) override;
355 
356  /// Complete all the types and emit the BTF sections.
357  void endModule() override;
358 };
359 
360 } // end namespace llvm
361 
362 #endif
Handle struct/union type.
Definition: BTFDebug.h:117
Handle array type.
Definition: BTFDebug.h:106
Represent one func and its type id.
Definition: BTFDebug.h:212
virtual ~BTFTypeBase()=default
uint32_t OffsetNameOff
The string to traverse types.
Definition: BTFDebug.h:230
uint32_t ColumnNum
the column number
Definition: BTFDebug.h:223
This class represents lattice values for constants.
Definition: AllocatorList.h:23
uint32_t getSize()
Get the size of this BTF type entry.
Definition: BTFDebug.h:167
uint32_t getSize()
Get the size of this BTF type entry.
Definition: BTFDebug.h:142
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
uint32_t getArrayIndexTypeId()
Get the special array index type id.
Definition: BTFDebug.h:335
Handle data sections.
Definition: BTFDebug.h:173
uint32_t getSize()
Definition: BTFDebug.h:204
uint32_t getSize()
Get the size of this BTF type entry.
Definition: BTFDebug.h:98
uint32_t getSize()
Get the size of this BTF type entry.
Definition: BTFDebug.h:155
uint32_t getSize()
Get the size of this BTF type entry.
Definition: BTFDebug.h:180
The BTF common type definition.
Definition: BTF.h:108
Handle variable instances.
Definition: BTFDebug.h:161
virtual void emitType(MCStreamer &OS)
Emit types for this BTF type entry.
Definition: BTFDebug.cpp:34
static StringRef getName(Value *V)
uint32_t roundupToBytes(uint32_t NumBits)
Definition: BTFDebug.h:44
Handle subprogram.
Definition: BTFDebug.h:150
Subprogram description.
void setSymbolSize(const MCSymbol *Symbol, uint64_t Size) override
For symbols that have a size designated (e.g.
Definition: BTFDebug.h:351
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:158
Analysis containing CSE Info
Definition: CSEInfo.cpp:20
Represent one extern relocation.
Definition: BTFDebug.h:234
uint8_t Kind
Definition: BTFDebug.h:34
Streaming machine code generation interface.
Definition: MCStreamer.h:189
struct BTF::CommonType BTFType
Definition: BTFDebug.h:37
const MCSymbol * Label
MCSymbol identifying insn for the reloc.
Definition: BTFDebug.h:235
uint32_t ExternNameOff
The extern variable name.
Definition: BTFDebug.h:236
Collect and emit BTF information.
Definition: BTFDebug.h:240
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:78
void setId(uint32_t Id)
Definition: BTFDebug.h:42
Handle int type.
Definition: BTFDebug.h:79
constexpr char TypeName[]
Key for Kernel::Arg::Metadata::mTypeName.
Represent one line info.
Definition: BTFDebug.h:218
Represent one offset relocation.
Definition: BTFDebug.h:227
std::vector< std::string > & getTable()
Definition: BTFDebug.h:205
uint32_t getSize()
Get the size of this BTF type entry.
Definition: BTFDebug.h:111
Base class for types.
uint32_t LineNum
the line number
Definition: BTFDebug.h:222
virtual void completeType(BTFDebug &BDebug)
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition: BTFDebug.h:50
uint32_t Id
Definition: BTFDebug.h:36
virtual uint32_t getSize()
Get the size of this BTF type entry.
Definition: BTFDebug.h:46
This file contains the layout of .BTF and .BTF.ext ELF sections.
MCSymbol * Label
MCSymbol identifying insn for the lineinfo.
Definition: BTFDebug.h:219
uint32_t getTypeId(const DIType *Ty)
Get the type id for a particular DIType.
Definition: BTFDebug.h:344
The base class for BTF type generation.
Definition: BTFDebug.h:32
uint32_t getId()
Definition: BTFDebug.h:43
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:219
BTF_KIND_ARRAY is followed by one "struct BTFArray".
Definition: BTF.h:158
Type array for a subprogram.
Representation of each machine instruction.
Definition: MachineInstr.h:64
uint32_t addString(StringRef S)
Add a string to the string table and returns its offset in the table.
Definition: BTFDebug.cpp:367
uint32_t getSize()
Get the size of this BTF type entry.
Definition: BTFDebug.h:86
Handle function pointer.
Definition: BTFDebug.h:134
Base class for debug information backends.
uint32_t Size
Definition: Profile.cpp:46
String table.
Definition: BTFDebug.h:192
const MCSymbol * Label
Func MCSymbol.
Definition: BTFDebug.h:213
uint32_t FileNameOff
file name offset in the .BTF string table
Definition: BTFDebug.h:220
size_t addString(StringRef S)
Add string to the string table.
Definition: BTFDebug.h:341
Handle several derived types include pointer, const, volatile, typedef and restrict.
Definition: BTFDebug.h:57
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Handle enumerate type.
Definition: BTFDebug.h:92
Handle struct or union forward declaration.
Definition: BTFDebug.h:69
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
void addVar(uint32_t Id, const MCSymbol *Sym, uint32_t Size)
Definition: BTFDebug.h:183
const MCSymbol * Label
MCSymbol identifying insn for the reloc.
Definition: BTFDebug.h:228
std::string getName()
Definition: BTFDebug.h:186
uint32_t TypeId
Type id referring to .BTF type section.
Definition: BTFDebug.h:214
uint32_t LineOff
line offset in the .BTF string table
Definition: BTFDebug.h:221
uint32_t getSize()
Get the size of this BTF type entry.
Definition: BTFDebug.h:125
uint32_t TypeID
Type ID.
Definition: BTFDebug.h:229
Basic type, like &#39;int&#39; or &#39;float&#39;.