BTFDebug.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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, bool IsSigned);
  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. /// Handle 64-bit enumerate type.
  191. class BTFTypeEnum64 : public BTFTypeBase {
  192. const DICompositeType *ETy;
  193. std::vector<struct BTF::BTFEnum64> EnumValues;
  194. public:
  195. BTFTypeEnum64(const DICompositeType *ETy, uint32_t NumValues, bool IsSigned);
  196. uint32_t getSize() override {
  197. return BTFTypeBase::getSize() + EnumValues.size() * BTF::BTFEnum64Size;
  198. }
  199. void completeType(BTFDebug &BDebug) override;
  200. void emitType(MCStreamer &OS) override;
  201. };
  202. class BTFTypeTypeTag : public BTFTypeBase {
  203. const DIDerivedType *DTy;
  204. StringRef Tag;
  205. public:
  206. BTFTypeTypeTag(uint32_t NextTypeId, StringRef Tag);
  207. BTFTypeTypeTag(const DIDerivedType *DTy, StringRef Tag);
  208. void completeType(BTFDebug &BDebug) override;
  209. };
  210. /// String table.
  211. class BTFStringTable {
  212. /// String table size in bytes.
  213. uint32_t Size;
  214. /// A mapping from string table offset to the index
  215. /// of the Table. It is used to avoid putting
  216. /// duplicated strings in the table.
  217. std::map<uint32_t, uint32_t> OffsetToIdMap;
  218. /// A vector of strings to represent the string table.
  219. std::vector<std::string> Table;
  220. public:
  221. BTFStringTable() : Size(0) {}
  222. uint32_t getSize() { return Size; }
  223. std::vector<std::string> &getTable() { return Table; }
  224. /// Add a string to the string table and returns its offset
  225. /// in the table.
  226. uint32_t addString(StringRef S);
  227. };
  228. /// Represent one func and its type id.
  229. struct BTFFuncInfo {
  230. const MCSymbol *Label; ///< Func MCSymbol
  231. uint32_t TypeId; ///< Type id referring to .BTF type section
  232. };
  233. /// Represent one line info.
  234. struct BTFLineInfo {
  235. MCSymbol *Label; ///< MCSymbol identifying insn for the lineinfo
  236. uint32_t FileNameOff; ///< file name offset in the .BTF string table
  237. uint32_t LineOff; ///< line offset in the .BTF string table
  238. uint32_t LineNum; ///< the line number
  239. uint32_t ColumnNum; ///< the column number
  240. };
  241. /// Represent one field relocation.
  242. struct BTFFieldReloc {
  243. const MCSymbol *Label; ///< MCSymbol identifying insn for the reloc
  244. uint32_t TypeID; ///< Type ID
  245. uint32_t OffsetNameOff; ///< The string to traverse types
  246. uint32_t RelocKind; ///< What to patch the instruction
  247. };
  248. /// Collect and emit BTF information.
  249. class BTFDebug : public DebugHandlerBase {
  250. MCStreamer &OS;
  251. bool SkipInstruction;
  252. bool LineInfoGenerated;
  253. uint32_t SecNameOff;
  254. uint32_t ArrayIndexTypeId;
  255. bool MapDefNotCollected;
  256. BTFStringTable StringTable;
  257. std::vector<std::unique_ptr<BTFTypeBase>> TypeEntries;
  258. std::unordered_map<const DIType *, uint32_t> DIToIdMap;
  259. std::map<uint32_t, std::vector<BTFFuncInfo>> FuncInfoTable;
  260. std::map<uint32_t, std::vector<BTFLineInfo>> LineInfoTable;
  261. std::map<uint32_t, std::vector<BTFFieldReloc>> FieldRelocTable;
  262. StringMap<std::vector<std::string>> FileContent;
  263. std::map<std::string, std::unique_ptr<BTFKindDataSec>> DataSecEntries;
  264. std::vector<BTFTypeStruct *> StructTypes;
  265. std::map<const GlobalVariable *, std::pair<int64_t, uint32_t>> PatchImms;
  266. std::map<const DICompositeType *,
  267. std::vector<std::pair<const DIDerivedType *, BTFTypeDerived *>>>
  268. FixupDerivedTypes;
  269. std::set<const Function *>ProtoFunctions;
  270. /// Add types to TypeEntries.
  271. /// @{
  272. /// Add types to TypeEntries and DIToIdMap.
  273. uint32_t addType(std::unique_ptr<BTFTypeBase> TypeEntry, const DIType *Ty);
  274. /// Add types to TypeEntries only and return type id.
  275. uint32_t addType(std::unique_ptr<BTFTypeBase> TypeEntry);
  276. /// @}
  277. /// IR type visiting functions.
  278. /// @{
  279. void visitTypeEntry(const DIType *Ty);
  280. void visitTypeEntry(const DIType *Ty, uint32_t &TypeId, bool CheckPointer,
  281. bool SeenPointer);
  282. void visitBasicType(const DIBasicType *BTy, uint32_t &TypeId);
  283. void visitSubroutineType(
  284. const DISubroutineType *STy, bool ForSubprog,
  285. const std::unordered_map<uint32_t, StringRef> &FuncArgNames,
  286. uint32_t &TypeId);
  287. void visitFwdDeclType(const DICompositeType *CTy, bool IsUnion,
  288. uint32_t &TypeId);
  289. void visitCompositeType(const DICompositeType *CTy, uint32_t &TypeId);
  290. void visitStructType(const DICompositeType *STy, bool IsStruct,
  291. uint32_t &TypeId);
  292. void visitArrayType(const DICompositeType *ATy, uint32_t &TypeId);
  293. void visitEnumType(const DICompositeType *ETy, uint32_t &TypeId);
  294. void visitDerivedType(const DIDerivedType *DTy, uint32_t &TypeId,
  295. bool CheckPointer, bool SeenPointer);
  296. void visitMapDefType(const DIType *Ty, uint32_t &TypeId);
  297. /// @}
  298. /// Get the file content for the subprogram. Certain lines of the file
  299. /// later may be put into string table and referenced by line info.
  300. std::string populateFileContent(const DISubprogram *SP);
  301. /// Construct a line info.
  302. void constructLineInfo(const DISubprogram *SP, MCSymbol *Label, uint32_t Line,
  303. uint32_t Column);
  304. /// Generate types and variables for globals.
  305. void processGlobals(bool ProcessingMapDef);
  306. /// Generate types for function prototypes.
  307. void processFuncPrototypes(const Function *);
  308. /// Generate types for decl annotations.
  309. void processDeclAnnotations(DINodeArray Annotations, uint32_t BaseTypeId,
  310. int ComponentId);
  311. /// Generate types for DISubprogram and it's arguments.
  312. uint32_t processDISubprogram(const DISubprogram *SP, uint32_t ProtoTypeId,
  313. uint8_t Scope);
  314. /// Generate BTF type_tag's. If BaseTypeId is nonnegative, the last
  315. /// BTF type_tag in the chain points to BaseTypeId. Otherwise, it points to
  316. /// the base type of DTy. Return the type id of the first BTF type_tag
  317. /// in the chain. If no type_tag's are generated, a negative value
  318. /// is returned.
  319. int genBTFTypeTags(const DIDerivedType *DTy, int BaseTypeId);
  320. /// Generate one field relocation record.
  321. void generatePatchImmReloc(const MCSymbol *ORSym, uint32_t RootId,
  322. const GlobalVariable *, bool IsAma);
  323. /// Populating unprocessed type on demand.
  324. unsigned populateType(const DIType *Ty);
  325. /// Process global variables referenced by relocation instructions
  326. /// and extern function references.
  327. void processGlobalValue(const MachineOperand &MO);
  328. /// Emit common header of .BTF and .BTF.ext sections.
  329. void emitCommonHeader();
  330. /// Emit the .BTF section.
  331. void emitBTFSection();
  332. /// Emit the .BTF.ext section.
  333. void emitBTFExtSection();
  334. protected:
  335. /// Gather pre-function debug information.
  336. void beginFunctionImpl(const MachineFunction *MF) override;
  337. /// Post process after all instructions in this function are processed.
  338. void endFunctionImpl(const MachineFunction *MF) override;
  339. public:
  340. BTFDebug(AsmPrinter *AP);
  341. ///
  342. bool InstLower(const MachineInstr *MI, MCInst &OutMI);
  343. /// Get the special array index type id.
  344. uint32_t getArrayIndexTypeId() {
  345. assert(ArrayIndexTypeId);
  346. return ArrayIndexTypeId;
  347. }
  348. /// Add string to the string table.
  349. size_t addString(StringRef S) { return StringTable.addString(S); }
  350. /// Get the type id for a particular DIType.
  351. uint32_t getTypeId(const DIType *Ty) {
  352. assert(Ty && "Invalid null Type");
  353. assert(DIToIdMap.find(Ty) != DIToIdMap.end() &&
  354. "DIType not added in the BDIToIdMap");
  355. return DIToIdMap[Ty];
  356. }
  357. void setSymbolSize(const MCSymbol *Symbol, uint64_t Size) override {}
  358. /// Process beginning of an instruction.
  359. void beginInstruction(const MachineInstr *MI) override;
  360. /// Complete all the types and emit the BTF sections.
  361. void endModule() override;
  362. };
  363. } // end namespace llvm
  364. #endif