CodeViewDebug.h 12 KB

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