1 //===-- llvm/CodeGen/DwarfDebug.h - Dwarf Debug Framework ------*- C++ -*--===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains support for writing dwarf debug info into asm files.
12 //===----------------------------------------------------------------------===//
14 #ifndef CODEGEN_ASMPRINTER_DWARFDEBUG_H__
15 #define CODEGEN_ASMPRINTER_DWARFDEBUG_H__
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/FoldingSet.h"
20 #include "llvm/ADT/SetVector.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/ADT/StringMap.h"
23 #include "llvm/CodeGen/AsmPrinter.h"
24 #include "llvm/CodeGen/LexicalScopes.h"
25 #include "llvm/DebugInfo.h"
26 #include "llvm/MC/MachineLocation.h"
27 #include "llvm/Support/Allocator.h"
28 #include "llvm/Support/DebugLoc.h"
36 class MachineFrameInfo;
37 class MachineModuleInfo;
45 //===----------------------------------------------------------------------===//
46 /// \brief This class is used to record source line correspondence.
48 unsigned Line; // Source line number.
49 unsigned Column; // Source column.
50 unsigned SourceID; // Source ID number.
51 MCSymbol *Label; // Label in code ID number.
53 SrcLineInfo(unsigned L, unsigned C, unsigned S, MCSymbol *label)
54 : Line(L), Column(C), SourceID(S), Label(label) {}
57 unsigned getLine() const { return Line; }
58 unsigned getColumn() const { return Column; }
59 unsigned getSourceID() const { return SourceID; }
60 MCSymbol *getLabel() const { return Label; }
63 /// \brief This struct describes location entries emitted in the .debug_loc
65 typedef struct DotDebugLocEntry {
66 const MCSymbol *Begin;
69 const MDNode *Variable;
77 enum EntryType EntryKind;
81 const ConstantFP *CFP;
82 const ConstantInt *CIP;
84 DotDebugLocEntry() : Begin(0), End(0), Variable(0), Merged(false) {
87 DotDebugLocEntry(const MCSymbol *B, const MCSymbol *E, MachineLocation &L,
89 : Begin(B), End(E), Loc(L), Variable(V), Merged(false) {
91 EntryKind = E_Location;
93 DotDebugLocEntry(const MCSymbol *B, const MCSymbol *E, int64_t i)
94 : Begin(B), End(E), Variable(0), Merged(false) {
96 EntryKind = E_Integer;
98 DotDebugLocEntry(const MCSymbol *B, const MCSymbol *E, const ConstantFP *FPtr)
99 : Begin(B), End(E), Variable(0), Merged(false) {
100 Constants.CFP = FPtr;
101 EntryKind = E_ConstantFP;
103 DotDebugLocEntry(const MCSymbol *B, const MCSymbol *E,
104 const ConstantInt *IPtr)
105 : Begin(B), End(E), Variable(0), Merged(false) {
106 Constants.CIP = IPtr;
107 EntryKind = E_ConstantInt;
110 /// \brief Empty entries are also used as a trigger to emit temp label. Such
111 /// labels are referenced is used to find debug_loc offset for a given DIE.
112 bool isEmpty() { return Begin == 0 && End == 0; }
113 bool isMerged() { return Merged; }
114 void Merge(DotDebugLocEntry *Next) {
115 if (!(Begin && Loc == Next->Loc && End == Next->Begin))
120 bool isLocation() const { return EntryKind == E_Location; }
121 bool isInt() const { return EntryKind == E_Integer; }
122 bool isConstantFP() const { return EntryKind == E_ConstantFP; }
123 bool isConstantInt() const { return EntryKind == E_ConstantInt; }
124 int64_t getInt() const { return Constants.Int; }
125 const ConstantFP *getConstantFP() const { return Constants.CFP; }
126 const ConstantInt *getConstantInt() const { return Constants.CIP; }
129 //===----------------------------------------------------------------------===//
130 /// \brief This class is used to track local variable information.
132 DIVariable Var; // Variable Descriptor.
133 DIE *TheDIE; // Variable DIE.
134 unsigned DotDebugLocOffset; // Offset in DotDebugLocEntries.
135 DbgVariable *AbsVar; // Corresponding Abstract variable, if any.
136 const MachineInstr *MInsn; // DBG_VALUE instruction of the variable.
139 // AbsVar may be NULL.
140 DbgVariable(DIVariable V, DbgVariable *AV)
141 : Var(V), TheDIE(0), DotDebugLocOffset(~0U), AbsVar(AV), MInsn(0),
145 DIVariable getVariable() const { return Var; }
146 void setDIE(DIE *D) { TheDIE = D; }
147 DIE *getDIE() const { return TheDIE; }
148 void setDotDebugLocOffset(unsigned O) { DotDebugLocOffset = O; }
149 unsigned getDotDebugLocOffset() const { return DotDebugLocOffset; }
150 StringRef getName() const { return Var.getName(); }
151 DbgVariable *getAbstractVariable() const { return AbsVar; }
152 const MachineInstr *getMInsn() const { return MInsn; }
153 void setMInsn(const MachineInstr *M) { MInsn = M; }
154 int getFrameIndex() const { return FrameIndex; }
155 void setFrameIndex(int FI) { FrameIndex = FI; }
156 // Translate tag to proper Dwarf tag.
157 unsigned getTag() const {
158 if (Var.getTag() == dwarf::DW_TAG_arg_variable)
159 return dwarf::DW_TAG_formal_parameter;
161 return dwarf::DW_TAG_variable;
163 /// \brief Return true if DbgVariable is artificial.
164 bool isArtificial() const {
165 if (Var.isArtificial())
167 if (getType().isArtificial())
172 bool isObjectPointer() const {
173 if (Var.isObjectPointer())
175 if (getType().isObjectPointer())
180 bool variableHasComplexAddress() const {
181 assert(Var.Verify() && "Invalid complex DbgVariable!");
182 return Var.hasComplexAddress();
184 bool isBlockByrefVariable() const {
185 assert(Var.Verify() && "Invalid complex DbgVariable!");
186 return Var.isBlockByrefVariable();
188 unsigned getNumAddrElements() const {
189 assert(Var.Verify() && "Invalid complex DbgVariable!");
190 return Var.getNumAddrElements();
192 uint64_t getAddrElement(unsigned i) const {
193 return Var.getAddrElement(i);
195 DIType getType() const;
198 /// \brief Collects and handles information specific to a particular
199 /// collection of units.
201 // Target of Dwarf emission, used for sizing of abbreviations.
204 // Used to uniquely define abbreviations.
205 FoldingSet<DIEAbbrev> *AbbreviationsSet;
207 // A list of all the unique abbreviations in use.
208 std::vector<DIEAbbrev *> *Abbreviations;
210 // A pointer to all units in the section.
211 SmallVector<CompileUnit *, 1> CUs;
213 // Collection of strings for this unit and assorted symbols.
214 // A String->Symbol mapping of strings used by indirect
216 typedef StringMap<std::pair<MCSymbol*, unsigned>,
217 BumpPtrAllocator&> StrPool;
219 unsigned NextStringPoolNumber;
220 std::string StringPref;
222 // Collection of addresses for this unit and assorted labels.
223 // A Symbol->unsigned mapping of addresses used by indirect
225 typedef DenseMap<const MCExpr *, unsigned> AddrPool;
226 AddrPool AddressPool;
227 unsigned NextAddrPoolNumber;
230 DwarfUnits(AsmPrinter *AP, FoldingSet<DIEAbbrev> *AS,
231 std::vector<DIEAbbrev *> *A, const char *Pref,
232 BumpPtrAllocator &DA) :
233 Asm(AP), AbbreviationsSet(AS), Abbreviations(A),
234 StringPool(DA), NextStringPoolNumber(0), StringPref(Pref),
235 AddressPool(), NextAddrPoolNumber(0) {}
237 /// \brief Compute the size and offset of a DIE given an incoming Offset.
238 unsigned computeSizeAndOffset(DIE *Die, unsigned Offset);
240 /// \brief Compute the size and offset of all the DIEs.
241 void computeSizeAndOffsets();
243 /// \brief Define a unique number for the abbreviation.
244 void assignAbbrevNumber(DIEAbbrev &Abbrev);
246 /// \brief Add a unit to the list of CUs.
247 void addUnit(CompileUnit *CU) { CUs.push_back(CU); }
249 /// \brief Emit all of the units to the section listed with the given
250 /// abbreviation section.
251 void emitUnits(DwarfDebug *, const MCSection *, const MCSection *,
254 /// \brief Emit all of the strings to the section given.
255 void emitStrings(const MCSection *, const MCSection *, const MCSymbol *);
257 /// \brief Emit all of the addresses to the section given.
258 void emitAddresses(const MCSection *);
260 /// \brief Returns the entry into the start of the pool.
261 MCSymbol *getStringPoolSym();
263 /// \brief Returns an entry into the string pool with the given
265 MCSymbol *getStringPoolEntry(StringRef Str);
267 /// \brief Returns the index into the string pool with the given
269 unsigned getStringPoolIndex(StringRef Str);
271 /// \brief Returns the string pool.
272 StrPool *getStringPool() { return &StringPool; }
274 /// \brief Returns the index into the address pool with the given
276 unsigned getAddrPoolIndex(const MCExpr *);
277 unsigned getAddrPoolIndex(const MCSymbol *);
279 /// \brief Returns the address pool.
280 AddrPool *getAddrPool() { return &AddressPool; }
282 /// \brief for a given compile unit DIE, returns offset from beginning of
284 unsigned getCUOffset(DIE *Die);
287 /// \brief Collects and handles dwarf debug information.
289 // Target of Dwarf emission.
292 // Collected machine module information.
293 MachineModuleInfo *MMI;
295 // All DIEValues are allocated through this allocator.
296 BumpPtrAllocator DIEValueAllocator;
298 //===--------------------------------------------------------------------===//
299 // Attribute used to construct specific Dwarf sections.
302 CompileUnit *FirstCU;
304 // Maps MDNode with its corresponding CompileUnit.
305 DenseMap <const MDNode *, CompileUnit *> CUMap;
307 // Maps subprogram MDNode with its corresponding CompileUnit.
308 DenseMap <const MDNode *, CompileUnit *> SPMap;
310 // Used to uniquely define abbreviations.
311 FoldingSet<DIEAbbrev> AbbreviationsSet;
313 // A list of all the unique abbreviations in use.
314 std::vector<DIEAbbrev *> Abbreviations;
316 // Stores the current file ID for a given compile unit.
317 DenseMap <unsigned, unsigned> FileIDCUMap;
318 // Source id map, i.e. CUID, source filename and directory,
319 // separated by a zero byte, mapped to a unique id.
320 StringMap<unsigned, BumpPtrAllocator&> SourceIdMap;
322 // Provides a unique id per text section.
323 SetVector<const MCSection*> SectionMap;
325 // List of Arguments (DbgValues) for current function.
326 SmallVector<DbgVariable *, 8> CurrentFnArguments;
328 LexicalScopes LScopes;
330 // Collection of abstract subprogram DIEs.
331 DenseMap<const MDNode *, DIE *> AbstractSPDies;
333 // Collection of dbg variables of a scope.
334 typedef DenseMap<LexicalScope *,
335 SmallVector<DbgVariable *, 8> > ScopeVariablesMap;
336 ScopeVariablesMap ScopeVariables;
338 // Collection of abstract variables.
339 DenseMap<const MDNode *, DbgVariable *> AbstractVariables;
341 // Collection of DotDebugLocEntry.
342 SmallVector<DotDebugLocEntry, 4> DotDebugLocEntries;
344 // Collection of subprogram DIEs that are marked (at the end of the module)
346 SmallPtrSet<DIE *, 4> InlinedSubprogramDIEs;
348 // Keep track of inlined functions and their location. This
349 // information is used to populate the debug_inlined section.
350 typedef std::pair<const MCSymbol *, DIE *> InlineInfoLabels;
351 typedef DenseMap<const MDNode *,
352 SmallVector<InlineInfoLabels, 4> > InlineInfoMap;
353 InlineInfoMap InlineInfo;
354 SmallVector<const MDNode *, 4> InlinedSPNodes;
356 // This is a collection of subprogram MDNodes that are processed to
358 SmallPtrSet<const MDNode *, 16> ProcessedSPNodes;
360 // Maps instruction with label emitted before instruction.
361 DenseMap<const MachineInstr *, MCSymbol *> LabelsBeforeInsn;
363 // Maps instruction with label emitted after instruction.
364 DenseMap<const MachineInstr *, MCSymbol *> LabelsAfterInsn;
366 // Every user variable mentioned by a DBG_VALUE instruction in order of
368 SmallVector<const MDNode*, 8> UserVariables;
370 // For each user variable, keep a list of DBG_VALUE instructions in order.
371 // The list can also contain normal instructions that clobber the previous
373 typedef DenseMap<const MDNode*, SmallVector<const MachineInstr*, 4> >
375 DbgValueHistoryMap DbgValues;
377 SmallVector<const MCSymbol *, 8> DebugRangeSymbols;
379 // Previous instruction's location information. This is used to determine
380 // label location to indicate scope boundries in dwarf debug info.
381 DebugLoc PrevInstLoc;
384 // This location indicates end of function prologue and beginning of function
386 DebugLoc PrologEndLoc;
388 // Section Symbols: these are assembler temporary labels that are emitted at
389 // the beginning of each supported dwarf section. These are used to form
390 // section offsets and are created by EmitSectionLabels.
391 MCSymbol *DwarfInfoSectionSym, *DwarfAbbrevSectionSym;
392 MCSymbol *DwarfStrSectionSym, *TextSectionSym, *DwarfDebugRangeSectionSym;
393 MCSymbol *DwarfDebugLocSectionSym, *DwarfLineSectionSym, *DwarfAddrSectionSym;
394 MCSymbol *FunctionBeginSym, *FunctionEndSym;
395 MCSymbol *DwarfAbbrevDWOSectionSym, *DwarfStrDWOSectionSym;
397 // As an optimization, there is no need to emit an entry in the directory
398 // table for the same directory as DW_AT_comp_dir.
399 StringRef CompilationDir;
401 // Counter for assigning globally unique IDs for CUs.
402 unsigned GlobalCUIndexCount;
404 // Holder for the file specific debug information.
405 DwarfUnits InfoHolder;
407 // Holders for the various debug information flags that we might need to
408 // have exposed. See accessor functions below for description.
410 // Whether or not we're emitting info for older versions of gdb on darwin.
411 bool IsDarwinGDBCompat;
413 // Holder for imported entities.
414 typedef SmallVector<std::pair<const MDNode *, const MDNode *>, 32>
416 ImportedEntityMap ScopesWithImportedEntities;
418 // DWARF5 Experimental Options
419 bool HasDwarfAccelTables;
422 unsigned DwarfVersion;
424 // Separated Dwarf Variables
425 // In general these will all be for bits that are left in the
426 // original object file, rather than things that are meant
427 // to be in the .dwo sections.
429 // The CUs left in the original object file for separated debug info.
430 SmallVector<CompileUnit *, 1> SkeletonCUs;
432 // Used to uniquely define abbreviations for the skeleton emission.
433 FoldingSet<DIEAbbrev> SkeletonAbbrevSet;
435 // A list of all the unique abbreviations in use.
436 std::vector<DIEAbbrev *> SkeletonAbbrevs;
438 // Holder for the skeleton information.
439 DwarfUnits SkeletonHolder;
443 void addScopeVariable(LexicalScope *LS, DbgVariable *Var);
445 /// \brief Find abstract variable associated with Var.
446 DbgVariable *findAbstractVariable(DIVariable &Var, DebugLoc Loc);
448 /// \brief Find DIE for the given subprogram and attach appropriate
449 /// DW_AT_low_pc and DW_AT_high_pc attributes. If there are global
450 /// variables in this scope then create and insert DIEs for these
452 DIE *updateSubprogramScopeDIE(CompileUnit *SPCU, const MDNode *SPNode);
454 /// \brief Construct new DW_TAG_lexical_block for this scope and
455 /// attach DW_AT_low_pc/DW_AT_high_pc labels.
456 DIE *constructLexicalScopeDIE(CompileUnit *TheCU, LexicalScope *Scope);
458 /// \brief This scope represents inlined body of a function. Construct
459 /// DIE to represent this concrete inlined copy of the function.
460 DIE *constructInlinedScopeDIE(CompileUnit *TheCU, LexicalScope *Scope);
462 /// \brief Construct a DIE for this scope.
463 DIE *constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope);
465 /// \brief Emit initial Dwarf sections with a label at the start of each one.
466 void emitSectionLabels();
468 /// \brief Compute the size and offset of a DIE given an incoming Offset.
469 unsigned computeSizeAndOffset(DIE *Die, unsigned Offset);
471 /// \brief Compute the size and offset of all the DIEs.
472 void computeSizeAndOffsets();
474 /// \brief Attach DW_AT_inline attribute with inlined subprogram DIEs.
475 void computeInlinedDIEs();
477 /// \brief Collect info for variables that were optimized out.
478 void collectDeadVariables();
480 /// \brief Finish off debug information after all functions have been
482 void finalizeModuleInfo();
484 /// \brief Emit labels to close any remaining sections that have been left
488 /// \brief Emit a set of abbreviations to the specific section.
489 void emitAbbrevs(const MCSection *, std::vector<DIEAbbrev*> *);
491 /// \brief Emit the debug info section.
492 void emitDebugInfo();
494 /// \brief Emit the abbreviation section.
495 void emitAbbreviations();
497 /// \brief Emit the last address of the section and the end of
499 void emitEndOfLineMatrix(unsigned SectionEnd);
501 /// \brief Emit visible names into a hashed accelerator table section.
502 void emitAccelNames();
504 /// \brief Emit objective C classes and categories into a hashed
505 /// accelerator table section.
506 void emitAccelObjC();
508 /// \brief Emit namespace dies into a hashed accelerator table.
509 void emitAccelNamespaces();
511 /// \brief Emit type dies into a hashed accelerator table.
512 void emitAccelTypes();
514 /// \brief Emit visible names into a debug pubnames section.
515 void emitDebugPubnames();
517 /// \brief Emit visible types into a debug pubtypes section.
518 void emitDebugPubTypes();
520 /// \brief Emit visible names into a debug str section.
523 /// \brief Emit visible names into a debug loc section.
526 /// \brief Emit visible names into a debug aranges section.
527 void emitDebugARanges();
529 /// \brief Emit visible names into a debug ranges section.
530 void emitDebugRanges();
532 /// \brief Emit visible names into a debug macinfo section.
533 void emitDebugMacInfo();
535 /// \brief Emit inline info using custom format.
536 void emitDebugInlineInfo();
538 /// DWARF 5 Experimental Split Dwarf Emitters
540 /// \brief Construct the split debug info compile unit for the debug info
542 CompileUnit *constructSkeletonCU(const MDNode *);
544 /// \brief Emit the local split abbreviations.
545 void emitSkeletonAbbrevs(const MCSection *);
547 /// \brief Emit the debug info dwo section.
548 void emitDebugInfoDWO();
550 /// \brief Emit the debug abbrev dwo section.
551 void emitDebugAbbrevDWO();
553 /// \brief Emit the debug str dwo section.
554 void emitDebugStrDWO();
556 /// \brief Create new CompileUnit for the given metadata node with tag
557 /// DW_TAG_compile_unit.
558 CompileUnit *constructCompileUnit(const MDNode *N);
560 /// \brief Construct subprogram DIE.
561 void constructSubprogramDIE(CompileUnit *TheCU, const MDNode *N);
563 /// \brief Construct imported_module or imported_declaration DIE.
564 void constructImportedEntityDIE(CompileUnit *TheCU, const MDNode *N);
566 /// \brief Construct import_module DIE.
567 void constructImportedEntityDIE(CompileUnit *TheCU, const MDNode *N,
570 /// \brief Construct import_module DIE.
571 void constructImportedEntityDIE(CompileUnit *TheCU,
572 const DIImportedEntity &Module,
575 /// \brief Register a source line with debug info. Returns the unique
576 /// label that was emitted and which provides correspondence to the
577 /// source line list.
578 void recordSourceLine(unsigned Line, unsigned Col, const MDNode *Scope,
581 /// \brief Indentify instructions that are marking the beginning of or
582 /// ending of a scope.
583 void identifyScopeMarkers();
585 /// \brief If Var is an current function argument that add it in
586 /// CurrentFnArguments list.
587 bool addCurrentFnArgument(const MachineFunction *MF,
588 DbgVariable *Var, LexicalScope *Scope);
590 /// \brief Populate LexicalScope entries with variables' info.
591 void collectVariableInfo(const MachineFunction *,
592 SmallPtrSet<const MDNode *, 16> &ProcessedVars);
594 /// \brief Collect variable information from the side table maintained
596 void collectVariableInfoFromMMITable(const MachineFunction * MF,
597 SmallPtrSet<const MDNode *, 16> &P);
599 /// \brief Ensure that a label will be emitted before MI.
600 void requestLabelBeforeInsn(const MachineInstr *MI) {
601 LabelsBeforeInsn.insert(std::make_pair(MI, (MCSymbol*)0));
604 /// \brief Return Label preceding the instruction.
605 MCSymbol *getLabelBeforeInsn(const MachineInstr *MI);
607 /// \brief Ensure that a label will be emitted after MI.
608 void requestLabelAfterInsn(const MachineInstr *MI) {
609 LabelsAfterInsn.insert(std::make_pair(MI, (MCSymbol*)0));
612 /// \brief Return Label immediately following the instruction.
613 MCSymbol *getLabelAfterInsn(const MachineInstr *MI);
616 //===--------------------------------------------------------------------===//
617 // Main entry points.
619 DwarfDebug(AsmPrinter *A, Module *M);
622 /// \brief Emit all Dwarf sections that should come prior to the
626 /// \brief Emit all Dwarf sections that should come after the content.
629 /// \brief Gather pre-function debug information.
630 void beginFunction(const MachineFunction *MF);
632 /// \brief Gather and emit post-function debug information.
633 void endFunction(const MachineFunction *MF);
635 /// \brief Process beginning of an instruction.
636 void beginInstruction(const MachineInstr *MI);
638 /// \brief Process end of an instruction.
639 void endInstruction(const MachineInstr *MI);
641 /// \brief Look up the source id with the given directory and source file
642 /// names. If none currently exists, create a new id and insert it in the
644 unsigned getOrCreateSourceID(StringRef DirName, StringRef FullName,
647 /// \brief Recursively Emits a debug information entry.
648 void emitDIE(DIE *Die, std::vector<DIEAbbrev *> *Abbrevs);
650 /// \brief Returns whether or not to limit some of our debug
651 /// output to the limitations of darwin gdb.
652 bool useDarwinGDBCompat() { return IsDarwinGDBCompat; }
654 // Experimental DWARF5 features.
656 /// \brief Returns whether or not to emit tables that dwarf consumers can
657 /// use to accelerate lookup.
658 bool useDwarfAccelTables() { return HasDwarfAccelTables; }
660 /// \brief Returns whether or not to change the current debug info for the
661 /// split dwarf proposal support.
662 bool useSplitDwarf() { return HasSplitDwarf; }
664 /// Returns the Dwarf Version.
665 unsigned getDwarfVersion() const { return DwarfVersion; }
667 } // End of namespace llvm