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