DwarfDebug.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. //===- llvm/CodeGen/DwarfDebug.h - Dwarf Debug Framework --------*- 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. // This file contains support for writing dwarf debug info into asm files.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFDEBUG_H
  13. #define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFDEBUG_H
  14. #include "AddressPool.h"
  15. #include "DebugLocStream.h"
  16. #include "DebugLocEntry.h"
  17. #include "DwarfFile.h"
  18. #include "llvm/ADT/ArrayRef.h"
  19. #include "llvm/ADT/DenseMap.h"
  20. #include "llvm/ADT/DenseSet.h"
  21. #include "llvm/ADT/MapVector.h"
  22. #include "llvm/ADT/STLExtras.h"
  23. #include "llvm/ADT/SetVector.h"
  24. #include "llvm/ADT/SmallPtrSet.h"
  25. #include "llvm/ADT/SmallVector.h"
  26. #include "llvm/ADT/StringMap.h"
  27. #include "llvm/ADT/StringRef.h"
  28. #include "llvm/BinaryFormat/Dwarf.h"
  29. #include "llvm/CodeGen/AccelTable.h"
  30. #include "llvm/CodeGen/DbgEntityHistoryCalculator.h"
  31. #include "llvm/CodeGen/DebugHandlerBase.h"
  32. #include "llvm/CodeGen/MachineInstr.h"
  33. #include "llvm/IR/DebugInfoMetadata.h"
  34. #include "llvm/IR/DebugLoc.h"
  35. #include "llvm/IR/Metadata.h"
  36. #include "llvm/MC/MCDwarf.h"
  37. #include "llvm/Support/Allocator.h"
  38. #include "llvm/Target/TargetOptions.h"
  39. #include <cassert>
  40. #include <cstdint>
  41. #include <limits>
  42. #include <memory>
  43. #include <utility>
  44. #include <vector>
  45. namespace llvm {
  46. class AsmPrinter;
  47. class ByteStreamer;
  48. class DebugLocEntry;
  49. class DIE;
  50. class DwarfCompileUnit;
  51. class DwarfExpression;
  52. class DwarfTypeUnit;
  53. class DwarfUnit;
  54. class LexicalScope;
  55. class MachineFunction;
  56. class MCSection;
  57. class MCSymbol;
  58. class MDNode;
  59. class Module;
  60. //===----------------------------------------------------------------------===//
  61. /// This class is defined as the common parent of DbgVariable and DbgLabel
  62. /// such that it could levarage polymorphism to extract common code for
  63. /// DbgVariable and DbgLabel.
  64. class DbgEntity {
  65. const DINode *Entity;
  66. const DILocation *InlinedAt;
  67. DIE *TheDIE = nullptr;
  68. unsigned SubclassID;
  69. public:
  70. enum DbgEntityKind {
  71. DbgVariableKind,
  72. DbgLabelKind
  73. };
  74. DbgEntity(const DINode *N, const DILocation *IA, unsigned ID)
  75. : Entity(N), InlinedAt(IA), SubclassID(ID) {}
  76. virtual ~DbgEntity() {}
  77. /// Accessors.
  78. /// @{
  79. const DINode *getEntity() const { return Entity; }
  80. const DILocation *getInlinedAt() const { return InlinedAt; }
  81. DIE *getDIE() const { return TheDIE; }
  82. unsigned getDbgEntityID() const { return SubclassID; }
  83. /// @}
  84. void setDIE(DIE &D) { TheDIE = &D; }
  85. static bool classof(const DbgEntity *N) {
  86. switch (N->getDbgEntityID()) {
  87. default:
  88. return false;
  89. case DbgVariableKind:
  90. case DbgLabelKind:
  91. return true;
  92. }
  93. }
  94. };
  95. //===----------------------------------------------------------------------===//
  96. /// This class is used to track local variable information.
  97. ///
  98. /// Variables can be created from allocas, in which case they're generated from
  99. /// the MMI table. Such variables can have multiple expressions and frame
  100. /// indices.
  101. ///
  102. /// Variables can be created from \c DBG_VALUE instructions. Those whose
  103. /// location changes over time use \a DebugLocListIndex, while those with a
  104. /// single location use \a ValueLoc and (optionally) a single entry of \a Expr.
  105. ///
  106. /// Variables that have been optimized out use none of these fields.
  107. class DbgVariable : public DbgEntity {
  108. /// Offset in DebugLocs.
  109. unsigned DebugLocListIndex = ~0u;
  110. /// Single value location description.
  111. std::unique_ptr<DbgValueLoc> ValueLoc = nullptr;
  112. struct FrameIndexExpr {
  113. int FI;
  114. const DIExpression *Expr;
  115. };
  116. mutable SmallVector<FrameIndexExpr, 1>
  117. FrameIndexExprs; /// Frame index + expression.
  118. public:
  119. /// Construct a DbgVariable.
  120. ///
  121. /// Creates a variable without any DW_AT_location. Call \a initializeMMI()
  122. /// for MMI entries, or \a initializeDbgValue() for DBG_VALUE instructions.
  123. DbgVariable(const DILocalVariable *V, const DILocation *IA)
  124. : DbgEntity(V, IA, DbgVariableKind) {}
  125. /// Initialize from the MMI table.
  126. void initializeMMI(const DIExpression *E, int FI) {
  127. assert(FrameIndexExprs.empty() && "Already initialized?");
  128. assert(!ValueLoc.get() && "Already initialized?");
  129. assert((!E || E->isValid()) && "Expected valid expression");
  130. assert(FI != std::numeric_limits<int>::max() && "Expected valid index");
  131. FrameIndexExprs.push_back({FI, E});
  132. }
  133. // Initialize variable's location.
  134. void initializeDbgValue(DbgValueLoc Value) {
  135. assert(FrameIndexExprs.empty() && "Already initialized?");
  136. assert(!ValueLoc && "Already initialized?");
  137. assert(!Value.getExpression()->isFragment() && "Fragments not supported.");
  138. ValueLoc = std::make_unique<DbgValueLoc>(Value);
  139. if (auto *E = ValueLoc->getExpression())
  140. if (E->getNumElements())
  141. FrameIndexExprs.push_back({0, E});
  142. }
  143. /// Initialize from a DBG_VALUE instruction.
  144. void initializeDbgValue(const MachineInstr *DbgValue);
  145. // Accessors.
  146. const DILocalVariable *getVariable() const {
  147. return cast<DILocalVariable>(getEntity());
  148. }
  149. const DIExpression *getSingleExpression() const {
  150. assert(ValueLoc.get() && FrameIndexExprs.size() <= 1);
  151. return FrameIndexExprs.size() ? FrameIndexExprs[0].Expr : nullptr;
  152. }
  153. void setDebugLocListIndex(unsigned O) { DebugLocListIndex = O; }
  154. unsigned getDebugLocListIndex() const { return DebugLocListIndex; }
  155. StringRef getName() const { return getVariable()->getName(); }
  156. const DbgValueLoc *getValueLoc() const { return ValueLoc.get(); }
  157. /// Get the FI entries, sorted by fragment offset.
  158. ArrayRef<FrameIndexExpr> getFrameIndexExprs() const;
  159. bool hasFrameIndexExprs() const { return !FrameIndexExprs.empty(); }
  160. void addMMIEntry(const DbgVariable &V);
  161. // Translate tag to proper Dwarf tag.
  162. dwarf::Tag getTag() const {
  163. // FIXME: Why don't we just infer this tag and store it all along?
  164. if (getVariable()->isParameter())
  165. return dwarf::DW_TAG_formal_parameter;
  166. return dwarf::DW_TAG_variable;
  167. }
  168. /// Return true if DbgVariable is artificial.
  169. bool isArtificial() const {
  170. if (getVariable()->isArtificial())
  171. return true;
  172. if (getType()->isArtificial())
  173. return true;
  174. return false;
  175. }
  176. bool isObjectPointer() const {
  177. if (getVariable()->isObjectPointer())
  178. return true;
  179. if (getType()->isObjectPointer())
  180. return true;
  181. return false;
  182. }
  183. bool hasComplexAddress() const {
  184. assert(ValueLoc.get() && "Expected DBG_VALUE, not MMI variable");
  185. assert((FrameIndexExprs.empty() ||
  186. (FrameIndexExprs.size() == 1 &&
  187. FrameIndexExprs[0].Expr->getNumElements())) &&
  188. "Invalid Expr for DBG_VALUE");
  189. return !FrameIndexExprs.empty();
  190. }
  191. const DIType *getType() const;
  192. static bool classof(const DbgEntity *N) {
  193. return N->getDbgEntityID() == DbgVariableKind;
  194. }
  195. };
  196. //===----------------------------------------------------------------------===//
  197. /// This class is used to track label information.
  198. ///
  199. /// Labels are collected from \c DBG_LABEL instructions.
  200. class DbgLabel : public DbgEntity {
  201. const MCSymbol *Sym; /// Symbol before DBG_LABEL instruction.
  202. public:
  203. /// We need MCSymbol information to generate DW_AT_low_pc.
  204. DbgLabel(const DILabel *L, const DILocation *IA, const MCSymbol *Sym = nullptr)
  205. : DbgEntity(L, IA, DbgLabelKind), Sym(Sym) {}
  206. /// Accessors.
  207. /// @{
  208. const DILabel *getLabel() const { return cast<DILabel>(getEntity()); }
  209. const MCSymbol *getSymbol() const { return Sym; }
  210. StringRef getName() const { return getLabel()->getName(); }
  211. /// @}
  212. /// Translate tag to proper Dwarf tag.
  213. dwarf::Tag getTag() const {
  214. return dwarf::DW_TAG_label;
  215. }
  216. static bool classof(const DbgEntity *N) {
  217. return N->getDbgEntityID() == DbgLabelKind;
  218. }
  219. };
  220. /// Used for tracking debug info about call site parameters.
  221. class DbgCallSiteParam {
  222. private:
  223. unsigned Register; ///< Parameter register at the callee entry point.
  224. DbgValueLoc Value; ///< Corresponding location for the parameter value at
  225. ///< the call site.
  226. public:
  227. DbgCallSiteParam(unsigned Reg, DbgValueLoc Val)
  228. : Register(Reg), Value(Val) {
  229. assert(Reg && "Parameter register cannot be undef");
  230. }
  231. unsigned getRegister() const { return Register; }
  232. DbgValueLoc getValue() const { return Value; }
  233. };
  234. /// Collection used for storing debug call site parameters.
  235. using ParamSet = SmallVector<DbgCallSiteParam, 4>;
  236. /// Helper used to pair up a symbol and its DWARF compile unit.
  237. struct SymbolCU {
  238. SymbolCU(DwarfCompileUnit *CU, const MCSymbol *Sym) : Sym(Sym), CU(CU) {}
  239. const MCSymbol *Sym;
  240. DwarfCompileUnit *CU;
  241. };
  242. /// The kind of accelerator tables we should emit.
  243. enum class AccelTableKind {
  244. Default, ///< Platform default.
  245. None, ///< None.
  246. Apple, ///< .apple_names, .apple_namespaces, .apple_types, .apple_objc.
  247. Dwarf, ///< DWARF v5 .debug_names.
  248. };
  249. /// Collects and handles dwarf debug information.
  250. class DwarfDebug : public DebugHandlerBase {
  251. /// All DIEValues are allocated through this allocator.
  252. BumpPtrAllocator DIEValueAllocator;
  253. /// Maps MDNode with its corresponding DwarfCompileUnit.
  254. MapVector<const MDNode *, DwarfCompileUnit *> CUMap;
  255. /// Maps a CU DIE with its corresponding DwarfCompileUnit.
  256. DenseMap<const DIE *, DwarfCompileUnit *> CUDieMap;
  257. /// List of all labels used in aranges generation.
  258. std::vector<SymbolCU> ArangeLabels;
  259. /// Size of each symbol emitted (for those symbols that have a specific size).
  260. DenseMap<const MCSymbol *, uint64_t> SymSize;
  261. /// Collection of abstract variables/labels.
  262. SmallVector<std::unique_ptr<DbgEntity>, 64> ConcreteEntities;
  263. /// Collection of DebugLocEntry. Stored in a linked list so that DIELocLists
  264. /// can refer to them in spite of insertions into this list.
  265. DebugLocStream DebugLocs;
  266. /// This is a collection of subprogram MDNodes that are processed to
  267. /// create DIEs.
  268. SetVector<const DISubprogram *, SmallVector<const DISubprogram *, 16>,
  269. SmallPtrSet<const DISubprogram *, 16>>
  270. ProcessedSPNodes;
  271. /// If nonnull, stores the current machine function we're processing.
  272. const MachineFunction *CurFn = nullptr;
  273. /// If nonnull, stores the CU in which the previous subprogram was contained.
  274. const DwarfCompileUnit *PrevCU;
  275. /// As an optimization, there is no need to emit an entry in the directory
  276. /// table for the same directory as DW_AT_comp_dir.
  277. StringRef CompilationDir;
  278. /// Holder for the file specific debug information.
  279. DwarfFile InfoHolder;
  280. /// Holders for the various debug information flags that we might need to
  281. /// have exposed. See accessor functions below for description.
  282. /// Map from MDNodes for user-defined types to their type signatures. Also
  283. /// used to keep track of which types we have emitted type units for.
  284. DenseMap<const MDNode *, uint64_t> TypeSignatures;
  285. DenseMap<const MCSection *, const MCSymbol *> SectionLabels;
  286. SmallVector<
  287. std::pair<std::unique_ptr<DwarfTypeUnit>, const DICompositeType *>, 1>
  288. TypeUnitsUnderConstruction;
  289. /// Whether to use the GNU TLS opcode (instead of the standard opcode).
  290. bool UseGNUTLSOpcode;
  291. /// Whether to use DWARF 2 bitfields (instead of the DWARF 4 format).
  292. bool UseDWARF2Bitfields;
  293. /// Whether to emit all linkage names, or just abstract subprograms.
  294. bool UseAllLinkageNames;
  295. /// Use inlined strings.
  296. bool UseInlineStrings = false;
  297. /// Allow emission of .debug_ranges section.
  298. bool UseRangesSection = true;
  299. /// True if the sections itself must be used as references and don't create
  300. /// temp symbols inside DWARF sections.
  301. bool UseSectionsAsReferences = false;
  302. ///Allow emission of the .debug_loc section.
  303. bool UseLocSection = true;
  304. /// Generate DWARF v4 type units.
  305. bool GenerateTypeUnits;
  306. /// DWARF5 Experimental Options
  307. /// @{
  308. AccelTableKind TheAccelTableKind;
  309. bool HasAppleExtensionAttributes;
  310. bool HasSplitDwarf;
  311. /// Whether to generate the DWARF v5 string offsets table.
  312. /// It consists of a series of contributions, each preceded by a header.
  313. /// The pre-DWARF v5 string offsets table for split dwarf is, in contrast,
  314. /// a monolithic sequence of string offsets.
  315. bool UseSegmentedStringOffsetsTable;
  316. /// Separated Dwarf Variables
  317. /// In general these will all be for bits that are left in the
  318. /// original object file, rather than things that are meant
  319. /// to be in the .dwo sections.
  320. /// Holder for the skeleton information.
  321. DwarfFile SkeletonHolder;
  322. /// Store file names for type units under fission in a line table
  323. /// header that will be emitted into debug_line.dwo.
  324. // FIXME: replace this with a map from comp_dir to table so that we
  325. // can emit multiple tables during LTO each of which uses directory
  326. // 0, referencing the comp_dir of all the type units that use it.
  327. MCDwarfDwoLineTable SplitTypeUnitFileTable;
  328. /// @}
  329. /// True iff there are multiple CUs in this module.
  330. bool SingleCU;
  331. bool IsDarwin;
  332. AddressPool AddrPool;
  333. /// Accelerator tables.
  334. AccelTable<DWARF5AccelTableData> AccelDebugNames;
  335. AccelTable<AppleAccelTableOffsetData> AccelNames;
  336. AccelTable<AppleAccelTableOffsetData> AccelObjC;
  337. AccelTable<AppleAccelTableOffsetData> AccelNamespace;
  338. AccelTable<AppleAccelTableTypeData> AccelTypes;
  339. // Identify a debugger for "tuning" the debug info.
  340. DebuggerKind DebuggerTuning = DebuggerKind::Default;
  341. MCDwarfDwoLineTable *getDwoLineTable(const DwarfCompileUnit &);
  342. const SmallVectorImpl<std::unique_ptr<DwarfCompileUnit>> &getUnits() {
  343. return InfoHolder.getUnits();
  344. }
  345. using InlinedEntity = DbgValueHistoryMap::InlinedEntity;
  346. void ensureAbstractEntityIsCreated(DwarfCompileUnit &CU,
  347. const DINode *Node,
  348. const MDNode *Scope);
  349. void ensureAbstractEntityIsCreatedIfScoped(DwarfCompileUnit &CU,
  350. const DINode *Node,
  351. const MDNode *Scope);
  352. DbgEntity *createConcreteEntity(DwarfCompileUnit &TheCU,
  353. LexicalScope &Scope,
  354. const DINode *Node,
  355. const DILocation *Location,
  356. const MCSymbol *Sym = nullptr);
  357. /// Construct a DIE for this abstract scope.
  358. void constructAbstractSubprogramScopeDIE(DwarfCompileUnit &SrcCU, LexicalScope *Scope);
  359. /// Construct DIEs for call site entries describing the calls in \p MF.
  360. void constructCallSiteEntryDIEs(const DISubprogram &SP, DwarfCompileUnit &CU,
  361. DIE &ScopeDIE, const MachineFunction &MF);
  362. template <typename DataT>
  363. void addAccelNameImpl(const DICompileUnit &CU, AccelTable<DataT> &AppleAccel,
  364. StringRef Name, const DIE &Die);
  365. void finishEntityDefinitions();
  366. void finishSubprogramDefinitions();
  367. /// Finish off debug information after all functions have been
  368. /// processed.
  369. void finalizeModuleInfo();
  370. /// Emit the debug info section.
  371. void emitDebugInfo();
  372. /// Emit the abbreviation section.
  373. void emitAbbreviations();
  374. /// Emit the string offsets table header.
  375. void emitStringOffsetsTableHeader();
  376. /// Emit a specified accelerator table.
  377. template <typename AccelTableT>
  378. void emitAccel(AccelTableT &Accel, MCSection *Section, StringRef TableName);
  379. /// Emit DWARF v5 accelerator table.
  380. void emitAccelDebugNames();
  381. /// Emit visible names into a hashed accelerator table section.
  382. void emitAccelNames();
  383. /// Emit objective C classes and categories into a hashed
  384. /// accelerator table section.
  385. void emitAccelObjC();
  386. /// Emit namespace dies into a hashed accelerator table.
  387. void emitAccelNamespaces();
  388. /// Emit type dies into a hashed accelerator table.
  389. void emitAccelTypes();
  390. /// Emit visible names and types into debug pubnames and pubtypes sections.
  391. void emitDebugPubSections();
  392. void emitDebugPubSection(bool GnuStyle, StringRef Name,
  393. DwarfCompileUnit *TheU,
  394. const StringMap<const DIE *> &Globals);
  395. /// Emit null-terminated strings into a debug str section.
  396. void emitDebugStr();
  397. /// Emit variable locations into a debug loc section.
  398. void emitDebugLoc();
  399. /// Emit variable locations into a debug loc dwo section.
  400. void emitDebugLocDWO();
  401. /// Emit address ranges into a debug aranges section.
  402. void emitDebugARanges();
  403. /// Emit address ranges into a debug ranges section.
  404. void emitDebugRanges();
  405. void emitDebugRangesDWO();
  406. /// Emit macros into a debug macinfo section.
  407. void emitDebugMacinfo();
  408. void emitMacro(DIMacro &M);
  409. void emitMacroFile(DIMacroFile &F, DwarfCompileUnit &U);
  410. void handleMacroNodes(DIMacroNodeArray Nodes, DwarfCompileUnit &U);
  411. /// DWARF 5 Experimental Split Dwarf Emitters
  412. /// Initialize common features of skeleton units.
  413. void initSkeletonUnit(const DwarfUnit &U, DIE &Die,
  414. std::unique_ptr<DwarfCompileUnit> NewU);
  415. /// Construct the split debug info compile unit for the debug info section.
  416. /// In DWARF v5, the skeleton unit DIE may have the following attributes:
  417. /// DW_AT_addr_base, DW_AT_comp_dir, DW_AT_dwo_name, DW_AT_high_pc,
  418. /// DW_AT_low_pc, DW_AT_ranges, DW_AT_stmt_list, and DW_AT_str_offsets_base.
  419. /// Prior to DWARF v5 it may also have DW_AT_GNU_dwo_id. DW_AT_GNU_dwo_name
  420. /// is used instead of DW_AT_dwo_name, Dw_AT_GNU_addr_base instead of
  421. /// DW_AT_addr_base, and DW_AT_GNU_ranges_base instead of DW_AT_rnglists_base.
  422. DwarfCompileUnit &constructSkeletonCU(const DwarfCompileUnit &CU);
  423. /// Emit the debug info dwo section.
  424. void emitDebugInfoDWO();
  425. /// Emit the debug abbrev dwo section.
  426. void emitDebugAbbrevDWO();
  427. /// Emit the debug line dwo section.
  428. void emitDebugLineDWO();
  429. /// Emit the dwo stringoffsets table header.
  430. void emitStringOffsetsTableHeaderDWO();
  431. /// Emit the debug str dwo section.
  432. void emitDebugStrDWO();
  433. /// Emit DWO addresses.
  434. void emitDebugAddr();
  435. /// Flags to let the linker know we have emitted new style pubnames. Only
  436. /// emit it here if we don't have a skeleton CU for split dwarf.
  437. void addGnuPubAttributes(DwarfCompileUnit &U, DIE &D) const;
  438. /// Create new DwarfCompileUnit for the given metadata node with tag
  439. /// DW_TAG_compile_unit.
  440. DwarfCompileUnit &getOrCreateDwarfCompileUnit(const DICompileUnit *DIUnit);
  441. void finishUnitAttributes(const DICompileUnit *DIUnit,
  442. DwarfCompileUnit &NewCU);
  443. /// Construct imported_module or imported_declaration DIE.
  444. void constructAndAddImportedEntityDIE(DwarfCompileUnit &TheCU,
  445. const DIImportedEntity *N);
  446. /// Register a source line with debug info. Returns the unique
  447. /// label that was emitted and which provides correspondence to the
  448. /// source line list.
  449. void recordSourceLine(unsigned Line, unsigned Col, const MDNode *Scope,
  450. unsigned Flags);
  451. /// Populate LexicalScope entries with variables' info.
  452. void collectEntityInfo(DwarfCompileUnit &TheCU, const DISubprogram *SP,
  453. DenseSet<InlinedEntity> &ProcessedVars);
  454. /// Build the location list for all DBG_VALUEs in the
  455. /// function that describe the same variable. If the resulting
  456. /// list has only one entry that is valid for entire variable's
  457. /// scope return true.
  458. bool buildLocationList(SmallVectorImpl<DebugLocEntry> &DebugLoc,
  459. const DbgValueHistoryMap::Entries &Entries);
  460. /// Collect variable information from the side table maintained by MF.
  461. void collectVariableInfoFromMFTable(DwarfCompileUnit &TheCU,
  462. DenseSet<InlinedEntity> &P);
  463. /// Emit the reference to the section.
  464. void emitSectionReference(const DwarfCompileUnit &CU);
  465. protected:
  466. /// Gather pre-function debug information.
  467. void beginFunctionImpl(const MachineFunction *MF) override;
  468. /// Gather and emit post-function debug information.
  469. void endFunctionImpl(const MachineFunction *MF) override;
  470. void skippedNonDebugFunction() override;
  471. public:
  472. //===--------------------------------------------------------------------===//
  473. // Main entry points.
  474. //
  475. DwarfDebug(AsmPrinter *A, Module *M);
  476. ~DwarfDebug() override;
  477. /// Emit all Dwarf sections that should come prior to the
  478. /// content.
  479. void beginModule();
  480. /// Emit all Dwarf sections that should come after the content.
  481. void endModule() override;
  482. /// Emits inital debug location directive.
  483. DebugLoc emitInitialLocDirective(const MachineFunction &MF, unsigned CUID);
  484. /// Process beginning of an instruction.
  485. void beginInstruction(const MachineInstr *MI) override;
  486. /// Perform an MD5 checksum of \p Identifier and return the lower 64 bits.
  487. static uint64_t makeTypeSignature(StringRef Identifier);
  488. /// Add a DIE to the set of types that we're going to pull into
  489. /// type units.
  490. void addDwarfTypeUnitType(DwarfCompileUnit &CU, StringRef Identifier,
  491. DIE &Die, const DICompositeType *CTy);
  492. friend class NonTypeUnitContext;
  493. class NonTypeUnitContext {
  494. DwarfDebug *DD;
  495. decltype(DwarfDebug::TypeUnitsUnderConstruction) TypeUnitsUnderConstruction;
  496. friend class DwarfDebug;
  497. NonTypeUnitContext(DwarfDebug *DD);
  498. public:
  499. NonTypeUnitContext(NonTypeUnitContext&&) = default;
  500. ~NonTypeUnitContext();
  501. };
  502. NonTypeUnitContext enterNonTypeUnitContext();
  503. /// Add a label so that arange data can be generated for it.
  504. void addArangeLabel(SymbolCU SCU) { ArangeLabels.push_back(SCU); }
  505. /// For symbols that have a size designated (e.g. common symbols),
  506. /// this tracks that size.
  507. void setSymbolSize(const MCSymbol *Sym, uint64_t Size) override {
  508. SymSize[Sym] = Size;
  509. }
  510. /// Returns whether we should emit all DW_AT_[MIPS_]linkage_name.
  511. /// If not, we still might emit certain cases.
  512. bool useAllLinkageNames() const { return UseAllLinkageNames; }
  513. /// Returns whether to use DW_OP_GNU_push_tls_address, instead of the
  514. /// standard DW_OP_form_tls_address opcode
  515. bool useGNUTLSOpcode() const { return UseGNUTLSOpcode; }
  516. /// Returns whether to use the DWARF2 format for bitfields instyead of the
  517. /// DWARF4 format.
  518. bool useDWARF2Bitfields() const { return UseDWARF2Bitfields; }
  519. /// Returns whether to use inline strings.
  520. bool useInlineStrings() const { return UseInlineStrings; }
  521. /// Returns whether ranges section should be emitted.
  522. bool useRangesSection() const { return UseRangesSection; }
  523. /// Returns whether to use sections as labels rather than temp symbols.
  524. bool useSectionsAsReferences() const {
  525. return UseSectionsAsReferences;
  526. }
  527. /// Returns whether .debug_loc section should be emitted.
  528. bool useLocSection() const { return UseLocSection; }
  529. /// Returns whether to generate DWARF v4 type units.
  530. bool generateTypeUnits() const { return GenerateTypeUnits; }
  531. // Experimental DWARF5 features.
  532. /// Returns what kind (if any) of accelerator tables to emit.
  533. AccelTableKind getAccelTableKind() const { return TheAccelTableKind; }
  534. bool useAppleExtensionAttributes() const {
  535. return HasAppleExtensionAttributes;
  536. }
  537. /// Returns whether or not to change the current debug info for the
  538. /// split dwarf proposal support.
  539. bool useSplitDwarf() const { return HasSplitDwarf; }
  540. /// Returns whether to generate a string offsets table with (possibly shared)
  541. /// contributions from each CU and type unit. This implies the use of
  542. /// DW_FORM_strx* indirect references with DWARF v5 and beyond. Note that
  543. /// DW_FORM_GNU_str_index is also an indirect reference, but it is used with
  544. /// a pre-DWARF v5 implementation of split DWARF sections, which uses a
  545. /// monolithic string offsets table.
  546. bool useSegmentedStringOffsetsTable() const {
  547. return UseSegmentedStringOffsetsTable;
  548. }
  549. bool shareAcrossDWOCUs() const;
  550. /// Returns the Dwarf Version.
  551. uint16_t getDwarfVersion() const;
  552. /// Returns the previous CU that was being updated
  553. const DwarfCompileUnit *getPrevCU() const { return PrevCU; }
  554. void setPrevCU(const DwarfCompileUnit *PrevCU) { this->PrevCU = PrevCU; }
  555. /// Returns the entries for the .debug_loc section.
  556. const DebugLocStream &getDebugLocs() const { return DebugLocs; }
  557. /// Emit an entry for the debug loc section. This can be used to
  558. /// handle an entry that's going to be emitted into the debug loc section.
  559. void emitDebugLocEntry(ByteStreamer &Streamer,
  560. const DebugLocStream::Entry &Entry,
  561. const DwarfCompileUnit *CU);
  562. /// Emit the location for a debug loc entry, including the size header.
  563. void emitDebugLocEntryLocation(const DebugLocStream::Entry &Entry,
  564. const DwarfCompileUnit *CU);
  565. void addSubprogramNames(const DICompileUnit &CU, const DISubprogram *SP,
  566. DIE &Die);
  567. AddressPool &getAddressPool() { return AddrPool; }
  568. void addAccelName(const DICompileUnit &CU, StringRef Name, const DIE &Die);
  569. void addAccelObjC(const DICompileUnit &CU, StringRef Name, const DIE &Die);
  570. void addAccelNamespace(const DICompileUnit &CU, StringRef Name,
  571. const DIE &Die);
  572. void addAccelType(const DICompileUnit &CU, StringRef Name, const DIE &Die,
  573. char Flags);
  574. const MachineFunction *getCurrentFunction() const { return CurFn; }
  575. /// A helper function to check whether the DIE for a given Scope is
  576. /// going to be null.
  577. bool isLexicalScopeDIENull(LexicalScope *Scope);
  578. /// Find the matching DwarfCompileUnit for the given CU DIE.
  579. DwarfCompileUnit *lookupCU(const DIE *Die) { return CUDieMap.lookup(Die); }
  580. const DwarfCompileUnit *lookupCU(const DIE *Die) const {
  581. return CUDieMap.lookup(Die);
  582. }
  583. /// \defgroup DebuggerTuning Predicates to tune DWARF for a given debugger.
  584. ///
  585. /// Returns whether we are "tuning" for a given debugger.
  586. /// @{
  587. bool tuneForGDB() const { return DebuggerTuning == DebuggerKind::GDB; }
  588. bool tuneForLLDB() const { return DebuggerTuning == DebuggerKind::LLDB; }
  589. bool tuneForSCE() const { return DebuggerTuning == DebuggerKind::SCE; }
  590. /// @}
  591. void addSectionLabel(const MCSymbol *Sym);
  592. const MCSymbol *getSectionLabel(const MCSection *S);
  593. static void emitDebugLocValue(const AsmPrinter &AP, const DIBasicType *BT,
  594. const DbgValueLoc &Value,
  595. DwarfExpression &DwarfExpr);
  596. };
  597. } // end namespace llvm
  598. #endif // LLVM_LIB_CODEGEN_ASMPRINTER_DWARFDEBUG_H