CodeViewDebug.h 12 KB

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