CodeViewDebug.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. //===- llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.h --------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file contains support for writing Microsoft CodeView debug info.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_CODEVIEWDEBUG_H
  14. #define LLVM_LIB_CODEGEN_ASMPRINTER_CODEVIEWDEBUG_H
  15. #include "DbgValueHistoryCalculator.h"
  16. #include "DebugHandlerBase.h"
  17. #include "llvm/ADT/ArrayRef.h"
  18. #include "llvm/ADT/DenseMap.h"
  19. #include "llvm/ADT/DenseSet.h"
  20. #include "llvm/ADT/MapVector.h"
  21. #include "llvm/ADT/SetVector.h"
  22. #include "llvm/ADT/SmallVector.h"
  23. #include "llvm/DebugInfo/CodeView/CodeView.h"
  24. #include "llvm/DebugInfo/CodeView/TypeIndex.h"
  25. #include "llvm/DebugInfo/CodeView/TypeTableBuilder.h"
  26. #include "llvm/IR/DebugLoc.h"
  27. #include "llvm/Support/Allocator.h"
  28. #include "llvm/Support/Compiler.h"
  29. #include <cstdint>
  30. #include <map>
  31. #include <string>
  32. #include <tuple>
  33. #include <unordered_map>
  34. #include <utility>
  35. #include <vector>
  36. namespace llvm {
  37. struct ClassInfo;
  38. class StringRef;
  39. class AsmPrinter;
  40. class Function;
  41. class GlobalVariable;
  42. class MCSectionCOFF;
  43. class MCStreamer;
  44. class MCSymbol;
  45. class MachineFunction;
  46. /// \brief Collects and handles line tables information in a CodeView format.
  47. class LLVM_LIBRARY_VISIBILITY CodeViewDebug : public DebugHandlerBase {
  48. MCStreamer &OS;
  49. BumpPtrAllocator Allocator;
  50. codeview::TypeTableBuilder TypeTable;
  51. /// Represents the most general definition range.
  52. struct LocalVarDefRange {
  53. /// Indicates that variable data is stored in memory relative to the
  54. /// specified register.
  55. int InMemory : 1;
  56. /// Offset of variable data in memory.
  57. int DataOffset : 31;
  58. /// Non-zero if this is a piece of an aggregate.
  59. uint16_t IsSubfield : 1;
  60. /// Offset into aggregate.
  61. uint16_t StructOffset : 15;
  62. /// Register containing the data or the register base of the memory
  63. /// location containing the data.
  64. uint16_t CVRegister;
  65. /// Compares all location fields. This includes all fields except the label
  66. /// ranges.
  67. bool isDifferentLocation(LocalVarDefRange &O) {
  68. return InMemory != O.InMemory || DataOffset != O.DataOffset ||
  69. IsSubfield != O.IsSubfield || StructOffset != O.StructOffset ||
  70. CVRegister != O.CVRegister;
  71. }
  72. SmallVector<std::pair<const MCSymbol *, const MCSymbol *>, 1> Ranges;
  73. };
  74. static LocalVarDefRange createDefRangeMem(uint16_t CVRegister, int Offset);
  75. static LocalVarDefRange createDefRangeGeneral(uint16_t CVRegister,
  76. bool InMemory, int Offset,
  77. bool IsSubfield,
  78. uint16_t StructOffset);
  79. /// Similar to DbgVariable in DwarfDebug, but not dwarf-specific.
  80. struct LocalVariable {
  81. const DILocalVariable *DIVar = nullptr;
  82. SmallVector<LocalVarDefRange, 1> DefRanges;
  83. };
  84. struct InlineSite {
  85. SmallVector<LocalVariable, 1> InlinedLocals;
  86. SmallVector<const DILocation *, 1> ChildSites;
  87. const DISubprogram *Inlinee = nullptr;
  88. /// The ID of the inline site or function used with .cv_loc. Not a type
  89. /// index.
  90. unsigned SiteFuncId = 0;
  91. };
  92. // For each function, store a vector of labels to its instructions, as well as
  93. // to the end of the function.
  94. struct FunctionInfo {
  95. /// Map from inlined call site to inlined instructions and child inlined
  96. /// call sites. Listed in program order.
  97. std::unordered_map<const DILocation *, InlineSite> InlineSites;
  98. /// Ordered list of top-level inlined call sites.
  99. SmallVector<const DILocation *, 1> ChildSites;
  100. SmallVector<LocalVariable, 1> Locals;
  101. DebugLoc LastLoc;
  102. const MCSymbol *Begin = nullptr;
  103. const MCSymbol *End = nullptr;
  104. unsigned FuncId = 0;
  105. unsigned LastFileId = 0;
  106. bool HaveLineInfo = false;
  107. };
  108. FunctionInfo *CurFn = nullptr;
  109. /// The set of comdat .debug$S sections that we've seen so far. Each section
  110. /// must start with a magic version number that must only be emitted once.
  111. /// This set tracks which sections we've already opened.
  112. DenseSet<MCSectionCOFF *> ComdatDebugSections;
  113. /// Switch to the appropriate .debug$S section for GVSym. If GVSym, the symbol
  114. /// of an emitted global value, is in a comdat COFF section, this will switch
  115. /// to a new .debug$S section in that comdat. This method ensures that the
  116. /// section starts with the magic version number on first use. If GVSym is
  117. /// null, uses the main .debug$S section.
  118. void switchToDebugSectionForSymbol(const MCSymbol *GVSym);
  119. /// The next available function index for use with our .cv_* directives. Not
  120. /// to be confused with type indices for LF_FUNC_ID records.
  121. unsigned NextFuncId = 0;
  122. InlineSite &getInlineSite(const DILocation *InlinedAt,
  123. const DISubprogram *Inlinee);
  124. codeview::TypeIndex getFuncIdForSubprogram(const DISubprogram *SP);
  125. static void collectInlineSiteChildren(SmallVectorImpl<unsigned> &Children,
  126. const FunctionInfo &FI,
  127. const InlineSite &Site);
  128. /// Remember some debug info about each function. Keep it in a stable order to
  129. /// emit at the end of the TU.
  130. MapVector<const Function *, FunctionInfo> FnDebugInfo;
  131. /// Map from DIFile to .cv_file id.
  132. DenseMap<const DIFile *, unsigned> FileIdMap;
  133. /// All inlined subprograms in the order they should be emitted.
  134. SmallSetVector<const DISubprogram *, 4> InlinedSubprograms;
  135. /// Map from a pair of DI metadata nodes and its DI type (or scope) that can
  136. /// be nullptr, to CodeView type indices. Primarily indexed by
  137. /// {DIType*, DIType*} and {DISubprogram*, DIType*}.
  138. ///
  139. /// The second entry in the key is needed for methods as DISubroutineType
  140. /// representing static method type are shared with non-method function type.
  141. DenseMap<std::pair<const DINode *, const DIType *>, codeview::TypeIndex>
  142. TypeIndices;
  143. /// Map from DICompositeType* to complete type index. Non-record types are
  144. /// always looked up in the normal TypeIndices map.
  145. DenseMap<const DICompositeType *, codeview::TypeIndex> CompleteTypeIndices;
  146. /// Complete record types to emit after all active type lowerings are
  147. /// finished.
  148. SmallVector<const DICompositeType *, 4> DeferredCompleteTypes;
  149. /// Number of type lowering frames active on the stack.
  150. unsigned TypeEmissionLevel = 0;
  151. codeview::TypeIndex VBPType;
  152. const DISubprogram *CurrentSubprogram = nullptr;
  153. // The UDTs we have seen while processing types; each entry is a pair of type
  154. // index and type name.
  155. std::vector<std::pair<std::string, codeview::TypeIndex>> LocalUDTs,
  156. GlobalUDTs;
  157. using FileToFilepathMapTy = std::map<const DIFile *, std::string>;
  158. FileToFilepathMapTy FileToFilepathMap;
  159. StringRef getFullFilepath(const DIFile *S);
  160. unsigned maybeRecordFile(const DIFile *F);
  161. void maybeRecordLocation(const DebugLoc &DL, const MachineFunction *MF);
  162. void clear();
  163. void setCurrentSubprogram(const DISubprogram *SP) {
  164. CurrentSubprogram = SP;
  165. LocalUDTs.clear();
  166. }
  167. /// Emit the magic version number at the start of a CodeView type or symbol
  168. /// section. Appears at the front of every .debug$S or .debug$T section.
  169. void emitCodeViewMagicVersion();
  170. void emitTypeInformation();
  171. void emitCompilerInformation();
  172. void emitInlineeLinesSubsection();
  173. void emitDebugInfoForFunction(const Function *GV, FunctionInfo &FI);
  174. void emitDebugInfoForGlobals();
  175. void emitDebugInfoForRetainedTypes();
  176. void emitDebugInfoForUDTs(
  177. ArrayRef<std::pair<std::string, codeview::TypeIndex>> UDTs);
  178. void emitDebugInfoForGlobal(const DIGlobalVariable *DIGV,
  179. const GlobalVariable *GV, MCSymbol *GVSym);
  180. /// Opens a subsection of the given kind in a .debug$S codeview section.
  181. /// Returns an end label for use with endCVSubsection when the subsection is
  182. /// finished.
  183. MCSymbol *beginCVSubsection(codeview::DebugSubsectionKind Kind);
  184. void endCVSubsection(MCSymbol *EndLabel);
  185. void emitInlinedCallSite(const FunctionInfo &FI, const DILocation *InlinedAt,
  186. const InlineSite &Site);
  187. using InlinedVariable = DbgValueHistoryMap::InlinedVariable;
  188. void collectVariableInfo(const DISubprogram *SP);
  189. void collectVariableInfoFromMFTable(DenseSet<InlinedVariable> &Processed);
  190. /// Records information about a local variable in the appropriate scope. In
  191. /// particular, locals from inlined code live inside the inlining site.
  192. void recordLocalVariable(LocalVariable &&Var, const DILocation *Loc);
  193. /// Emits local variables in the appropriate order.
  194. void emitLocalVariableList(ArrayRef<LocalVariable> Locals);
  195. /// Emits an S_LOCAL record and its associated defined ranges.
  196. void emitLocalVariable(const LocalVariable &Var);
  197. /// Translates the DIType to codeview if necessary and returns a type index
  198. /// for it.
  199. codeview::TypeIndex getTypeIndex(DITypeRef TypeRef,
  200. DITypeRef ClassTyRef = DITypeRef());
  201. codeview::TypeIndex getMemberFunctionType(const DISubprogram *SP,
  202. const DICompositeType *Class);
  203. codeview::TypeIndex getScopeIndex(const DIScope *Scope);
  204. codeview::TypeIndex getVBPTypeIndex();
  205. void addToUDTs(const DIType *Ty, codeview::TypeIndex TI);
  206. codeview::TypeIndex lowerType(const DIType *Ty, const DIType *ClassTy);
  207. codeview::TypeIndex lowerTypeAlias(const DIDerivedType *Ty);
  208. codeview::TypeIndex lowerTypeArray(const DICompositeType *Ty);
  209. codeview::TypeIndex lowerTypeBasic(const DIBasicType *Ty);
  210. codeview::TypeIndex lowerTypePointer(const DIDerivedType *Ty);
  211. codeview::TypeIndex lowerTypeMemberPointer(const DIDerivedType *Ty);
  212. codeview::TypeIndex lowerTypeModifier(const DIDerivedType *Ty);
  213. codeview::TypeIndex lowerTypeFunction(const DISubroutineType *Ty);
  214. codeview::TypeIndex lowerTypeVFTableShape(const DIDerivedType *Ty);
  215. codeview::TypeIndex lowerTypeMemberFunction(const DISubroutineType *Ty,
  216. const DIType *ClassTy,
  217. int ThisAdjustment);
  218. codeview::TypeIndex lowerTypeEnum(const DICompositeType *Ty);
  219. codeview::TypeIndex lowerTypeClass(const DICompositeType *Ty);
  220. codeview::TypeIndex lowerTypeUnion(const DICompositeType *Ty);
  221. /// Symbol records should point to complete types, but type records should
  222. /// always point to incomplete types to avoid cycles in the type graph. Only
  223. /// use this entry point when generating symbol records. The complete and
  224. /// incomplete type indices only differ for record types. All other types use
  225. /// the same index.
  226. codeview::TypeIndex getCompleteTypeIndex(DITypeRef TypeRef);
  227. codeview::TypeIndex lowerCompleteTypeClass(const DICompositeType *Ty);
  228. codeview::TypeIndex lowerCompleteTypeUnion(const DICompositeType *Ty);
  229. struct TypeLoweringScope;
  230. void emitDeferredCompleteTypes();
  231. void collectMemberInfo(ClassInfo &Info, const DIDerivedType *DDTy);
  232. ClassInfo collectClassInfo(const DICompositeType *Ty);
  233. /// Common record member lowering functionality for record types, which are
  234. /// structs, classes, and unions. Returns the field list index and the member
  235. /// count.
  236. std::tuple<codeview::TypeIndex, codeview::TypeIndex, unsigned, bool>
  237. lowerRecordFieldList(const DICompositeType *Ty);
  238. /// Inserts {{Node, ClassTy}, TI} into TypeIndices and checks for duplicates.
  239. codeview::TypeIndex recordTypeIndexForDINode(const DINode *Node,
  240. codeview::TypeIndex TI,
  241. const DIType *ClassTy = nullptr);
  242. unsigned getPointerSizeInBytes();
  243. protected:
  244. /// \brief Gather pre-function debug information.
  245. void beginFunctionImpl(const MachineFunction *MF) override;
  246. /// \brief Gather post-function debug information.
  247. void endFunctionImpl(const MachineFunction *) override;
  248. public:
  249. CodeViewDebug(AsmPrinter *Asm);
  250. void setSymbolSize(const MCSymbol *, uint64_t) override {}
  251. /// \brief Emit the COFF section that holds the line table information.
  252. void endModule() override;
  253. /// \brief Process beginning of an instruction.
  254. void beginInstruction(const MachineInstr *MI) override;
  255. };
  256. } // end namespace llvm
  257. #endif // LLVM_LIB_CODEGEN_ASMPRINTER_CODEVIEWDEBUG_H