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" 18 #include "llvm/CodeGen/DebugHandlerBase.h" 19 #include "llvm/CodeGen/MachineInstr.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 MCStreamer; 30 class MCSymbol; 31 class MachineFunction; 32 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 41 public: 42 BTFTypeBase() : IsCompleted(false) {} 43 virtual ~BTFTypeBase() = default; 44 void setId(uint32_t Id) { this->Id = Id; } 45 uint32_t getId() { return Id; } 46 uint32_t roundupToBytes(uint32_t NumBits) { return (NumBits + 7) >> 3; } 47 /// Get the size of this BTF type entry. 48 virtual uint32_t getSize() { return BTF::CommonTypeSize; } 49 /// Complete BTF type generation after all related DebugInfo types 50 /// have been visited so their BTF type id's are available 51 /// for cross referece. 52 virtual void completeType(BTFDebug &BDebug) {} 53 /// Emit types for this BTF type entry. 54 virtual void emitType(MCStreamer &OS); 55 }; 56 57 /// Handle several derived types include pointer, const, 58 /// volatile, typedef and restrict. 59 class BTFTypeDerived : public BTFTypeBase { 60 const DIDerivedType *DTy; 61 bool NeedsFixup; 62 63 public: 64 BTFTypeDerived(const DIDerivedType *Ty, unsigned Tag, bool NeedsFixup); 65 void completeType(BTFDebug &BDebug); 66 void emitType(MCStreamer &OS); 67 void setPointeeType(uint32_t PointeeType); 68 }; 69 70 /// Handle struct or union forward declaration. 71 class BTFTypeFwd : public BTFTypeBase { 72 StringRef Name; 73 74 public: 75 BTFTypeFwd(StringRef Name, bool IsUnion); 76 void completeType(BTFDebug &BDebug); 77 void emitType(MCStreamer &OS); 78 }; 79 80 /// Handle int type. 81 class BTFTypeInt : public BTFTypeBase { 82 StringRef Name; 83 uint32_t IntVal; ///< Encoding, offset, bits 84 85 public: 86 BTFTypeInt(uint32_t Encoding, uint32_t SizeInBits, uint32_t OffsetInBits, 87 StringRef TypeName); 88 uint32_t getSize() { return BTFTypeBase::getSize() + sizeof(uint32_t); } 89 void completeType(BTFDebug &BDebug); 90 void emitType(MCStreamer &OS); 91 }; 92 93 /// Handle enumerate type. 94 class BTFTypeEnum : public BTFTypeBase { 95 const DICompositeType *ETy; 96 std::vector<struct BTF::BTFEnum> EnumValues; 97 98 public: 99 BTFTypeEnum(const DICompositeType *ETy, uint32_t NumValues); 100 uint32_t getSize() { 101 return BTFTypeBase::getSize() + EnumValues.size() * BTF::BTFEnumSize; 102 } 103 void completeType(BTFDebug &BDebug); 104 void emitType(MCStreamer &OS); 105 }; 106 107 /// Handle array type. 108 class BTFTypeArray : public BTFTypeBase { 109 struct BTF::BTFArray ArrayInfo; 110 111 public: 112 BTFTypeArray(uint32_t ElemTypeId, uint32_t NumElems); 113 uint32_t getSize() { return BTFTypeBase::getSize() + BTF::BTFArraySize; } 114 void completeType(BTFDebug &BDebug); 115 void emitType(MCStreamer &OS); 116 }; 117 118 /// Handle struct/union type. 119 class BTFTypeStruct : public BTFTypeBase { 120 const DICompositeType *STy; 121 bool HasBitField; 122 std::vector<struct BTF::BTFMember> Members; 123 124 public: 125 BTFTypeStruct(const DICompositeType *STy, bool IsStruct, bool HasBitField, 126 uint32_t NumMembers); 127 uint32_t getSize() { 128 return BTFTypeBase::getSize() + Members.size() * BTF::BTFMemberSize; 129 } 130 void completeType(BTFDebug &BDebug); 131 void emitType(MCStreamer &OS); 132 std::string getName(); 133 }; 134 135 /// Handle function pointer. 136 class BTFTypeFuncProto : public BTFTypeBase { 137 const DISubroutineType *STy; 138 std::unordered_map<uint32_t, StringRef> FuncArgNames; 139 std::vector<struct BTF::BTFParam> Parameters; 140 141 public: 142 BTFTypeFuncProto(const DISubroutineType *STy, uint32_t NumParams, 143 const std::unordered_map<uint32_t, StringRef> &FuncArgNames); 144 uint32_t getSize() { 145 return BTFTypeBase::getSize() + Parameters.size() * BTF::BTFParamSize; 146 } 147 void completeType(BTFDebug &BDebug); 148 void emitType(MCStreamer &OS); 149 }; 150 151 /// Handle subprogram 152 class BTFTypeFunc : public BTFTypeBase { 153 StringRef Name; 154 155 public: 156 BTFTypeFunc(StringRef FuncName, uint32_t ProtoTypeId, uint32_t Scope); 157 uint32_t getSize() { return BTFTypeBase::getSize(); } 158 void completeType(BTFDebug &BDebug); 159 void emitType(MCStreamer &OS); 160 }; 161 162 /// Handle variable instances 163 class BTFKindVar : public BTFTypeBase { 164 StringRef Name; 165 uint32_t Info; 166 167 public: 168 BTFKindVar(StringRef VarName, uint32_t TypeId, uint32_t VarInfo); 169 uint32_t getSize() { return BTFTypeBase::getSize() + 4; } 170 void completeType(BTFDebug &BDebug); 171 void emitType(MCStreamer &OS); 172 }; 173 174 /// Handle data sections 175 class BTFKindDataSec : public BTFTypeBase { 176 AsmPrinter *Asm; 177 std::string Name; 178 std::vector<std::tuple<uint32_t, const MCSymbol *, uint32_t>> Vars; 179 180 public: 181 BTFKindDataSec(AsmPrinter *AsmPrt, std::string SecName); 182 uint32_t getSize() { 183 return BTFTypeBase::getSize() + BTF::BTFDataSecVarSize * Vars.size(); 184 } 185 void addVar(uint32_t Id, const MCSymbol *Sym, uint32_t Size) { 186 Vars.push_back(std::make_tuple(Id, Sym, Size)); 187 } 188 std::string getName() { return Name; } 189 void completeType(BTFDebug &BDebug); 190 void emitType(MCStreamer &OS); 191 }; 192 193 /// String table. 194 class BTFStringTable { 195 /// String table size in bytes. 196 uint32_t Size; 197 /// A mapping from string table offset to the index 198 /// of the Table. It is used to avoid putting 199 /// duplicated strings in the table. 200 std::map<uint32_t, uint32_t> OffsetToIdMap; 201 /// A vector of strings to represent the string table. 202 std::vector<std::string> Table; 203 204 public: 205 BTFStringTable() : Size(0) {} 206 uint32_t getSize() { return Size; } 207 std::vector<std::string> &getTable() { return Table; } 208 /// Add a string to the string table and returns its offset 209 /// in the table. 210 uint32_t addString(StringRef S); 211 }; 212 213 /// Represent one func and its type id. 214 struct BTFFuncInfo { 215 const MCSymbol *Label; ///< Func MCSymbol 216 uint32_t TypeId; ///< Type id referring to .BTF type section 217 }; 218 219 /// Represent one line info. 220 struct BTFLineInfo { 221 MCSymbol *Label; ///< MCSymbol identifying insn for the lineinfo 222 uint32_t FileNameOff; ///< file name offset in the .BTF string table 223 uint32_t LineOff; ///< line offset in the .BTF string table 224 uint32_t LineNum; ///< the line number 225 uint32_t ColumnNum; ///< the column number 226 }; 227 228 /// Represent one field relocation. 229 struct BTFFieldReloc { 230 const MCSymbol *Label; ///< MCSymbol identifying insn for the reloc 231 uint32_t TypeID; ///< Type ID 232 uint32_t OffsetNameOff; ///< The string to traverse types 233 uint32_t RelocKind; ///< What to patch the instruction 234 }; 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<std::string, uint32_t> PatchImms; 254 std::map<StringRef, std::pair<bool, std::vector<BTFTypeDerived *>>> 255 FixupDerivedTypes; 256 std::set<const Function *>ProtoFunctions; 257 258 /// Add types to TypeEntries. 259 /// @{ 260 /// Add types to TypeEntries and DIToIdMap. 261 uint32_t addType(std::unique_ptr<BTFTypeBase> TypeEntry, const DIType *Ty); 262 /// Add types to TypeEntries only and return type id. 263 uint32_t addType(std::unique_ptr<BTFTypeBase> TypeEntry); 264 /// @} 265 266 /// IR type visiting functions. 267 /// @{ 268 void visitTypeEntry(const DIType *Ty); 269 void visitTypeEntry(const DIType *Ty, uint32_t &TypeId, bool CheckPointer, 270 bool SeenPointer); 271 void visitBasicType(const DIBasicType *BTy, uint32_t &TypeId); 272 void visitSubroutineType( 273 const DISubroutineType *STy, bool ForSubprog, 274 const std::unordered_map<uint32_t, StringRef> &FuncArgNames, 275 uint32_t &TypeId); 276 void visitFwdDeclType(const DICompositeType *CTy, bool IsUnion, 277 uint32_t &TypeId); 278 void visitCompositeType(const DICompositeType *CTy, uint32_t &TypeId); 279 void visitStructType(const DICompositeType *STy, bool IsStruct, 280 uint32_t &TypeId); 281 void visitArrayType(const DICompositeType *ATy, uint32_t &TypeId); 282 void visitEnumType(const DICompositeType *ETy, uint32_t &TypeId); 283 void visitDerivedType(const DIDerivedType *DTy, uint32_t &TypeId, 284 bool CheckPointer, bool SeenPointer); 285 void visitMapDefType(const DIType *Ty, uint32_t &TypeId); 286 /// @} 287 288 /// Get the file content for the subprogram. Certain lines of the file 289 /// later may be put into string table and referenced by line info. 290 std::string populateFileContent(const DISubprogram *SP); 291 292 /// Construct a line info. 293 void constructLineInfo(const DISubprogram *SP, MCSymbol *Label, uint32_t Line, 294 uint32_t Column); 295 296 /// Generate types and variables for globals. 297 void processGlobals(bool ProcessingMapDef); 298 299 /// Generate types for function prototypes. 300 void processFuncPrototypes(const Function *); 301 302 /// Generate one field relocation record. 303 void generateFieldReloc(const MCSymbol *ORSym, DIType *RootTy, 304 StringRef AccessPattern); 305 306 /// Populating unprocessed struct type. 307 unsigned populateStructType(const DIType *Ty); 308 309 /// Process relocation instructions. 310 void processReloc(const MachineOperand &MO); 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. 335 uint32_t getArrayIndexTypeId() { 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 363