CodeViewDebug.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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/GlobalTypeTableBuilder.h"
  25. #include "llvm/DebugInfo/CodeView/TypeIndex.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. /// 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::GlobalTypeTableBuilder 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. bool UseReferenceType = false;
  84. };
  85. struct InlineSite {
  86. SmallVector<LocalVariable, 1> InlinedLocals;
  87. SmallVector<const DILocation *, 1> ChildSites;
  88. const DISubprogram *Inlinee = nullptr;
  89. /// The ID of the inline site or function used with .cv_loc. Not a type
  90. /// index.
  91. unsigned SiteFuncId = 0;
  92. };
  93. // Combines information from DILexicalBlock and LexicalScope.
  94. struct LexicalBlock {
  95. SmallVector<LocalVariable, 1> Locals;
  96. SmallVector<LexicalBlock *, 1> Children;
  97. const MCSymbol *Begin;
  98. const MCSymbol *End;
  99. StringRef Name;
  100. };
  101. // For each function, store a vector of labels to its instructions, as well as
  102. // to the end of the function.
  103. struct FunctionInfo {
  104. FunctionInfo() = default;
  105. // Uncopyable.
  106. FunctionInfo(const FunctionInfo &FI) = delete;
  107. /// Map from inlined call site to inlined instructions and child inlined
  108. /// call sites. Listed in program order.
  109. std::unordered_map<const DILocation *, InlineSite> InlineSites;
  110. /// Ordered list of top-level inlined call sites.
  111. SmallVector<const DILocation *, 1> ChildSites;
  112. SmallVector<LocalVariable, 1> Locals;
  113. std::unordered_map<const DILexicalBlockBase*, LexicalBlock> LexicalBlocks;
  114. // Lexical blocks containing local variables.
  115. SmallVector<LexicalBlock *, 1> ChildBlocks;
  116. std::vector<std::pair<MCSymbol *, MDNode *>> Annotations;
  117. const MCSymbol *Begin = nullptr;
  118. const MCSymbol *End = nullptr;
  119. unsigned FuncId = 0;
  120. unsigned LastFileId = 0;
  121. bool HaveLineInfo = false;
  122. };
  123. FunctionInfo *CurFn = nullptr;
  124. // Map used to seperate variables according to the lexical scope they belong
  125. // in. This is populated by recordLocalVariable() before
  126. // collectLexicalBlocks() separates the variables between the FunctionInfo
  127. // and LexicalBlocks.
  128. DenseMap<const LexicalScope *, SmallVector<LocalVariable, 1>> ScopeVariables;
  129. /// The set of comdat .debug$S sections that we've seen so far. Each section
  130. /// must start with a magic version number that must only be emitted once.
  131. /// This set tracks which sections we've already opened.
  132. DenseSet<MCSectionCOFF *> ComdatDebugSections;
  133. /// Switch to the appropriate .debug$S section for GVSym. If GVSym, the symbol
  134. /// of an emitted global value, is in a comdat COFF section, this will switch
  135. /// to a new .debug$S section in that comdat. This method ensures that the
  136. /// section starts with the magic version number on first use. If GVSym is
  137. /// null, uses the main .debug$S section.
  138. void switchToDebugSectionForSymbol(const MCSymbol *GVSym);
  139. /// The next available function index for use with our .cv_* directives. Not
  140. /// to be confused with type indices for LF_FUNC_ID records.
  141. unsigned NextFuncId = 0;
  142. InlineSite &getInlineSite(const DILocation *InlinedAt,
  143. const DISubprogram *Inlinee);
  144. codeview::TypeIndex getFuncIdForSubprogram(const DISubprogram *SP);
  145. void calculateRanges(LocalVariable &Var,
  146. const DbgValueHistoryMap::InstrRanges &Ranges);
  147. static void collectInlineSiteChildren(SmallVectorImpl<unsigned> &Children,
  148. const FunctionInfo &FI,
  149. const InlineSite &Site);
  150. /// Remember some debug info about each function. Keep it in a stable order to
  151. /// emit at the end of the TU.
  152. MapVector<const Function *, std::unique_ptr<FunctionInfo>> FnDebugInfo;
  153. /// Map from full file path to .cv_file id. Full paths are built from DIFiles
  154. /// and are stored in FileToFilepathMap;
  155. DenseMap<StringRef, unsigned> FileIdMap;
  156. /// All inlined subprograms in the order they should be emitted.
  157. SmallSetVector<const DISubprogram *, 4> InlinedSubprograms;
  158. /// Map from a pair of DI metadata nodes and its DI type (or scope) that can
  159. /// be nullptr, to CodeView type indices. Primarily indexed by
  160. /// {DIType*, DIType*} and {DISubprogram*, DIType*}.
  161. ///
  162. /// The second entry in the key is needed for methods as DISubroutineType
  163. /// representing static method type are shared with non-method function type.
  164. DenseMap<std::pair<const DINode *, const DIType *>, codeview::TypeIndex>
  165. TypeIndices;
  166. /// Map from DICompositeType* to complete type index. Non-record types are
  167. /// always looked up in the normal TypeIndices map.
  168. DenseMap<const DICompositeType *, codeview::TypeIndex> CompleteTypeIndices;
  169. /// Complete record types to emit after all active type lowerings are
  170. /// finished.
  171. SmallVector<const DICompositeType *, 4> DeferredCompleteTypes;
  172. /// Number of type lowering frames active on the stack.
  173. unsigned TypeEmissionLevel = 0;
  174. codeview::TypeIndex VBPType;
  175. const DISubprogram *CurrentSubprogram = nullptr;
  176. // The UDTs we have seen while processing types; each entry is a pair of type
  177. // index and type name.
  178. std::vector<std::pair<std::string, const DIType *>> LocalUDTs;
  179. std::vector<std::pair<std::string, const DIType *>> GlobalUDTs;
  180. using FileToFilepathMapTy = std::map<const DIFile *, std::string>;
  181. FileToFilepathMapTy FileToFilepathMap;
  182. StringRef getFullFilepath(const DIFile *File);
  183. unsigned maybeRecordFile(const DIFile *F);
  184. void maybeRecordLocation(const DebugLoc &DL, const MachineFunction *MF);
  185. void clear();
  186. void setCurrentSubprogram(const DISubprogram *SP) {
  187. CurrentSubprogram = SP;
  188. LocalUDTs.clear();
  189. }
  190. /// Emit the magic version number at the start of a CodeView type or symbol
  191. /// section. Appears at the front of every .debug$S or .debug$T or .debug$P
  192. /// section.
  193. void emitCodeViewMagicVersion();
  194. void emitTypeInformation();
  195. void emitTypeGlobalHashes();
  196. void emitCompilerInformation();
  197. void emitInlineeLinesSubsection();
  198. void emitDebugInfoForThunk(const Function *GV,
  199. FunctionInfo &FI,
  200. const MCSymbol *Fn);
  201. void emitDebugInfoForFunction(const Function *GV, FunctionInfo &FI);
  202. void emitDebugInfoForGlobals();
  203. void emitDebugInfoForRetainedTypes();
  204. void
  205. emitDebugInfoForUDTs(ArrayRef<std::pair<std::string, const DIType *>> UDTs);
  206. void emitDebugInfoForGlobal(const DIGlobalVariable *DIGV,
  207. const GlobalVariable *GV, MCSymbol *GVSym);
  208. /// Opens a subsection of the given kind in a .debug$S codeview section.
  209. /// Returns an end label for use with endCVSubsection when the subsection is
  210. /// finished.
  211. MCSymbol *beginCVSubsection(codeview::DebugSubsectionKind Kind);
  212. void endCVSubsection(MCSymbol *EndLabel);
  213. void emitInlinedCallSite(const FunctionInfo &FI, const DILocation *InlinedAt,
  214. const InlineSite &Site);
  215. using InlinedVariable = DbgValueHistoryMap::InlinedVariable;
  216. void collectVariableInfo(const DISubprogram *SP);
  217. void collectVariableInfoFromMFTable(DenseSet<InlinedVariable> &Processed);
  218. // Construct the lexical block tree for a routine, pruning emptpy lexical
  219. // scopes, and populate it with local variables.
  220. void collectLexicalBlockInfo(SmallVectorImpl<LexicalScope *> &Scopes,
  221. SmallVectorImpl<LexicalBlock *> &Blocks,
  222. SmallVectorImpl<LocalVariable> &Locals);
  223. void collectLexicalBlockInfo(LexicalScope &Scope,
  224. SmallVectorImpl<LexicalBlock *> &ParentBlocks,
  225. SmallVectorImpl<LocalVariable> &ParentLocals);
  226. /// Records information about a local variable in the appropriate scope. In
  227. /// particular, locals from inlined code live inside the inlining site.
  228. void recordLocalVariable(LocalVariable &&Var, const LexicalScope *LS);
  229. /// Emits local variables in the appropriate order.
  230. void emitLocalVariableList(ArrayRef<LocalVariable> Locals);
  231. /// Emits an S_LOCAL record and its associated defined ranges.
  232. void emitLocalVariable(const LocalVariable &Var);
  233. /// Emits a sequence of lexical block scopes and their children.
  234. void emitLexicalBlockList(ArrayRef<LexicalBlock *> Blocks,
  235. const FunctionInfo& FI);
  236. /// Emit a lexical block scope and its children.
  237. void emitLexicalBlock(const LexicalBlock &Block, const FunctionInfo& FI);
  238. /// Translates the DIType to codeview if necessary and returns a type index
  239. /// for it.
  240. codeview::TypeIndex getTypeIndex(DITypeRef TypeRef,
  241. DITypeRef ClassTyRef = DITypeRef());
  242. codeview::TypeIndex getTypeIndexForReferenceTo(DITypeRef TypeRef);
  243. codeview::TypeIndex getMemberFunctionType(const DISubprogram *SP,
  244. const DICompositeType *Class);
  245. codeview::TypeIndex getScopeIndex(const DIScope *Scope);
  246. codeview::TypeIndex getVBPTypeIndex();
  247. void addToUDTs(const DIType *Ty);
  248. void addUDTSrcLine(const DIType *Ty, codeview::TypeIndex TI);
  249. codeview::TypeIndex lowerType(const DIType *Ty, const DIType *ClassTy);
  250. codeview::TypeIndex lowerTypeAlias(const DIDerivedType *Ty);
  251. codeview::TypeIndex lowerTypeArray(const DICompositeType *Ty);
  252. codeview::TypeIndex lowerTypeBasic(const DIBasicType *Ty);
  253. codeview::TypeIndex lowerTypePointer(
  254. const DIDerivedType *Ty,
  255. codeview::PointerOptions PO = codeview::PointerOptions::None);
  256. codeview::TypeIndex lowerTypeMemberPointer(
  257. const DIDerivedType *Ty,
  258. codeview::PointerOptions PO = codeview::PointerOptions::None);
  259. codeview::TypeIndex lowerTypeModifier(const DIDerivedType *Ty);
  260. codeview::TypeIndex lowerTypeFunction(const DISubroutineType *Ty);
  261. codeview::TypeIndex lowerTypeVFTableShape(const DIDerivedType *Ty);
  262. codeview::TypeIndex lowerTypeMemberFunction(const DISubroutineType *Ty,
  263. const DIType *ClassTy,
  264. int ThisAdjustment,
  265. bool IsStaticMethod);
  266. codeview::TypeIndex lowerTypeEnum(const DICompositeType *Ty);
  267. codeview::TypeIndex lowerTypeClass(const DICompositeType *Ty);
  268. codeview::TypeIndex lowerTypeUnion(const DICompositeType *Ty);
  269. /// Symbol records should point to complete types, but type records should
  270. /// always point to incomplete types to avoid cycles in the type graph. Only
  271. /// use this entry point when generating symbol records. The complete and
  272. /// incomplete type indices only differ for record types. All other types use
  273. /// the same index.
  274. codeview::TypeIndex getCompleteTypeIndex(DITypeRef TypeRef);
  275. codeview::TypeIndex lowerCompleteTypeClass(const DICompositeType *Ty);
  276. codeview::TypeIndex lowerCompleteTypeUnion(const DICompositeType *Ty);
  277. struct TypeLoweringScope;
  278. void emitDeferredCompleteTypes();
  279. void collectMemberInfo(ClassInfo &Info, const DIDerivedType *DDTy);
  280. ClassInfo collectClassInfo(const DICompositeType *Ty);
  281. /// Common record member lowering functionality for record types, which are
  282. /// structs, classes, and unions. Returns the field list index and the member
  283. /// count.
  284. std::tuple<codeview::TypeIndex, codeview::TypeIndex, unsigned, bool>
  285. lowerRecordFieldList(const DICompositeType *Ty);
  286. /// Inserts {{Node, ClassTy}, TI} into TypeIndices and checks for duplicates.
  287. codeview::TypeIndex recordTypeIndexForDINode(const DINode *Node,
  288. codeview::TypeIndex TI,
  289. const DIType *ClassTy = nullptr);
  290. unsigned getPointerSizeInBytes();
  291. protected:
  292. /// Gather pre-function debug information.
  293. void beginFunctionImpl(const MachineFunction *MF) override;
  294. /// Gather post-function debug information.
  295. void endFunctionImpl(const MachineFunction *) override;
  296. public:
  297. CodeViewDebug(AsmPrinter *AP);
  298. void setSymbolSize(const MCSymbol *, uint64_t) override {}
  299. /// Emit the COFF section that holds the line table information.
  300. void endModule() override;
  301. /// Process beginning of an instruction.
  302. void beginInstruction(const MachineInstr *MI) override;
  303. };
  304. } // end namespace llvm
  305. #endif // LLVM_LIB_CODEGEN_ASMPRINTER_CODEVIEWDEBUG_H