BTFDebug.h 13 KB

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