1 //===- BTFParser.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 // BTFParser reads .BTF and .BTF.ext ELF sections generated by LLVM 10 // BPF backend and provides introspection for the stored information. 11 // Currently the following information is accessible: 12 // - string table; 13 // - instruction offset to line information mapping. 14 // 15 // See llvm/DebugInfo/BTF/BTF.h for some details about binary format 16 // and links to Linux Kernel documentation. 17 // 18 //===----------------------------------------------------------------------===// 19 20 #ifndef LLVM_DEBUGINFO_BTF_BTFPARSER_H 21 #define LLVM_DEBUGINFO_BTF_BTFPARSER_H 22 23 #include "llvm/ADT/DenseMap.h" 24 #include "llvm/DebugInfo/BTF/BTF.h" 25 #include "llvm/Object/ObjectFile.h" 26 #include "llvm/Support/DataExtractor.h" 27 28 namespace llvm { 29 using object::ObjectFile; 30 using object::SectionedAddress; 31 using object::SectionRef; 32 33 class BTFParser { 34 using BTFLinesVector = SmallVector<BTF::BPFLineInfo, 0>; 35 36 // In BTF strings are stored as a continuous memory region with 37 // individual strings separated by 0 bytes. Strings are identified 38 // by an offset in such region. 39 // The `StringsTable` points to this region in the parsed ObjectFile. 40 StringRef StringsTable; 41 42 // Maps ELF section number to instruction line number information. 43 // Each BTFLinesVector is sorted by `InsnOffset` to allow fast lookups. 44 DenseMap<uint64_t, BTFLinesVector> SectionLines; 45 46 struct ParseContext; 47 Error parseBTF(ParseContext &Ctx, SectionRef BTF); 48 Error parseBTFExt(ParseContext &Ctx, SectionRef BTFExt); 49 Error parseLineInfo(ParseContext &Ctx, DataExtractor &Extractor, 50 uint64_t LineInfoStart, uint64_t LineInfoEnd); 51 52 public: 53 // Looks-up a string in the .BTF section's string table. 54 // Offset is relative to string table start. 55 StringRef findString(uint32_t Offset) const; 56 57 // Search for line information for a specific address, 58 // address match is exact (contrary to DWARFContext). 59 // Return nullptr if no information found. 60 // If information is present, return a pointer to object 61 // owned by this class. 62 const BTF::BPFLineInfo *findLineInfo(SectionedAddress Address) const; 63 64 // Fills instance of BTFParser with information stored in .BTF and 65 // .BTF.ext sections of the `Obj`. If this instance was already 66 // filled, old data is discarded. 67 // 68 // If information cannot be parsed: 69 // - return an error describing the failure; 70 // - state of the BTFParser might be incomplete but is not invalid, 71 // queries might be run against it, but some (or all) information 72 // might be unavailable; 73 Error parse(const ObjectFile &Obj); 74 75 // Return true if `Obj` has .BTF and .BTF.ext sections. 76 static bool hasBTFSections(const ObjectFile &Obj); 77 }; 78 79 } // namespace llvm 80 81 #endif // LLVM_DEBUGINFO_BTF_BTFPARSER_H 82