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