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;
78 enum EntryType EntryKind;
82 const ConstantFP *CFP;
83 const ConstantInt *CIP;
86 : Begin(0), End(0), Variable(0), Merged(false),
87 Constant(false) { Constants.Int = 0;}
88 DotDebugLocEntry(const MCSymbol *B, const MCSymbol *E, MachineLocation &L,
90 : Begin(B), End(E), Loc(L), Variable(V), Merged(false),
91 Constant(false) { Constants.Int = 0; EntryKind = E_Location; }
92 DotDebugLocEntry(const MCSymbol *B, const MCSymbol *E, int64_t i)
93 : Begin(B), End(E), Variable(0), Merged(false),
94 Constant(true) { Constants.Int = i; EntryKind = E_Integer; }
95 DotDebugLocEntry(const MCSymbol *B, const MCSymbol *E, const ConstantFP *FPtr)
96 : Begin(B), End(E), Variable(0), Merged(false),
97 Constant(true) { Constants.CFP = FPtr; EntryKind = E_ConstantFP; }
98 DotDebugLocEntry(const MCSymbol *B, const MCSymbol *E,
99 const ConstantInt *IPtr)
100 : Begin(B), End(E), Variable(0), Merged(false),
101 Constant(true) { Constants.CIP = IPtr; EntryKind = E_ConstantInt; }
103 /// \brief Empty entries are also used as a trigger to emit temp label. Such
104 /// labels are referenced is used to find debug_loc offset for a given DIE.
105 bool isEmpty() { return Begin == 0 && End == 0; }
106 bool isMerged() { return Merged; }
107 void Merge(DotDebugLocEntry *Next) {
108 if (!(Begin && Loc == Next->Loc && End == Next->Begin))
113 bool isLocation() const { return EntryKind == E_Location; }
114 bool isInt() const { return EntryKind == E_Integer; }
115 bool isConstantFP() const { return EntryKind == E_ConstantFP; }
116 bool isConstantInt() const { return EntryKind == E_ConstantInt; }
117 int64_t getInt() { return Constants.Int; }
118 const ConstantFP *getConstantFP() { return Constants.CFP; }
119 const ConstantInt *getConstantInt() { return Constants.CIP; }
122 //===----------------------------------------------------------------------===//
123 /// \brief This class is used to track local variable information.
125 DIVariable Var; // Variable Descriptor.
126 DIE *TheDIE; // Variable DIE.
127 unsigned DotDebugLocOffset; // Offset in DotDebugLocEntries.
128 DbgVariable *AbsVar; // Corresponding Abstract variable, if any.
129 const MachineInstr *MInsn; // DBG_VALUE instruction of the variable.
132 // AbsVar may be NULL.
133 DbgVariable(DIVariable V, DbgVariable *AV)
134 : Var(V), TheDIE(0), DotDebugLocOffset(~0U), AbsVar(AV), MInsn(0),
138 DIVariable getVariable() const { return Var; }
139 void setDIE(DIE *D) { TheDIE = D; }
140 DIE *getDIE() const { return TheDIE; }
141 void setDotDebugLocOffset(unsigned O) { DotDebugLocOffset = O; }
142 unsigned getDotDebugLocOffset() const { return DotDebugLocOffset; }
143 StringRef getName() const { return Var.getName(); }
144 DbgVariable *getAbstractVariable() const { return AbsVar; }
145 const MachineInstr *getMInsn() const { return MInsn; }
146 void setMInsn(const MachineInstr *M) { MInsn = M; }
147 int getFrameIndex() const { return FrameIndex; }
148 void setFrameIndex(int FI) { FrameIndex = FI; }
149 // Translate tag to proper Dwarf tag.
150 unsigned getTag() const {
151 if (Var.getTag() == dwarf::DW_TAG_arg_variable)
152 return dwarf::DW_TAG_formal_parameter;
154 return dwarf::DW_TAG_variable;
156 /// \brief Return true if DbgVariable is artificial.
157 bool isArtificial() const {
158 if (Var.isArtificial())
160 if (getType().isArtificial())
165 bool isObjectPointer() const {
166 if (Var.isObjectPointer())
168 if (getType().isObjectPointer())
173 bool variableHasComplexAddress() const {
174 assert(Var.Verify() && "Invalid complex DbgVariable!");
175 return Var.hasComplexAddress();
177 bool isBlockByrefVariable() const {
178 assert(Var.Verify() && "Invalid complex DbgVariable!");
179 return Var.isBlockByrefVariable();
181 unsigned getNumAddrElements() const {
182 assert(Var.Verify() && "Invalid complex DbgVariable!");
183 return Var.getNumAddrElements();
185 uint64_t getAddrElement(unsigned i) const {
186 return Var.getAddrElement(i);
188 DIType getType() const;
192 // A String->Symbol mapping of strings used by indirect
194 typedef StringMap<std::pair<MCSymbol*, unsigned>,
195 BumpPtrAllocator&> StrPool;
197 // A Symbol->unsigned mapping of addresses used by indirect
199 typedef DenseMap<const MCExpr *, unsigned> AddrPool;
201 /// \brief Collects and handles information specific to a particular
202 /// collection of units.
204 // Target of Dwarf emission, used for sizing of abbreviations.
207 // Used to uniquely define abbreviations.
208 FoldingSet<DIEAbbrev> *AbbreviationsSet;
210 // A list of all the unique abbreviations in use.
211 std::vector<DIEAbbrev *> *Abbreviations;
213 // A pointer to all units in the section.
214 SmallVector<CompileUnit *, 1> CUs;
216 // Collection of strings for this unit and assorted symbols.
218 unsigned NextStringPoolNumber;
219 std::string StringPref;
221 // Collection of addresses for this unit and assorted labels.
222 AddrPool AddressPool;
223 unsigned NextAddrPoolNumber;
226 DwarfUnits(AsmPrinter *AP, FoldingSet<DIEAbbrev> *AS,
227 std::vector<DIEAbbrev *> *A, const char *Pref,
228 BumpPtrAllocator &DA) :
229 Asm(AP), AbbreviationsSet(AS), Abbreviations(A),
230 StringPool(DA), NextStringPoolNumber(0), StringPref(Pref),
231 AddressPool(), NextAddrPoolNumber(0) {}
233 /// \brief Compute the size and offset of a DIE given an incoming Offset.
234 unsigned computeSizeAndOffset(DIE *Die, unsigned Offset);
236 /// \brief Compute the size and offset of all the DIEs.
237 void computeSizeAndOffsets();
239 /// \brief Define a unique number for the abbreviation.
240 void assignAbbrevNumber(DIEAbbrev &Abbrev);
242 /// \brief Add a unit to the list of CUs.
243 void addUnit(CompileUnit *CU) { CUs.push_back(CU); }
245 /// \brief Emit all of the units to the section listed with the given
246 /// abbreviation section.
247 void emitUnits(DwarfDebug *, const MCSection *, const MCSection *,
250 /// \brief Emit all of the strings to the section given.
251 void emitStrings(const MCSection *, const MCSection *, const MCSymbol *);
253 /// \brief Emit all of the addresses to the section given.
254 void emitAddresses(const MCSection *);
256 /// \brief Returns the entry into the start of the pool.
257 MCSymbol *getStringPoolSym();
259 /// \brief Returns an entry into the string pool with the given
261 MCSymbol *getStringPoolEntry(StringRef Str);
263 /// \brief Returns the index into the string pool with the given
265 unsigned getStringPoolIndex(StringRef Str);
267 /// \brief Returns the string pool.
268 StrPool *getStringPool() { return &StringPool; }
270 /// \brief Returns the index into the address pool with the given
272 unsigned getAddrPoolIndex(const MCExpr *);
273 unsigned getAddrPoolIndex(const MCSymbol *);
275 /// \brief Returns the address pool.
276 AddrPool *getAddrPool() { return &AddressPool; }
278 /// \brief for a given compile unit DIE, returns offset from beginning of
280 unsigned getCUOffset(DIE *Die);
283 /// \brief Collects and handles dwarf debug information.
285 // Target of Dwarf emission.
288 // Collected machine module information.
289 MachineModuleInfo *MMI;
291 // All DIEValues are allocated through this allocator.
292 BumpPtrAllocator DIEValueAllocator;
294 //===--------------------------------------------------------------------===//
295 // Attribute used to construct specific Dwarf sections.
298 CompileUnit *FirstCU;
300 // Maps MDNode with its corresponding CompileUnit.
301 DenseMap <const MDNode *, CompileUnit *> CUMap;
303 // Maps subprogram MDNode with its corresponding CompileUnit.
304 DenseMap <const MDNode *, CompileUnit *> SPMap;
306 // Used to uniquely define abbreviations.
307 FoldingSet<DIEAbbrev> AbbreviationsSet;
309 // A list of all the unique abbreviations in use.
310 std::vector<DIEAbbrev *> Abbreviations;
312 // Stores the current file ID for a given compile unit.
313 DenseMap <unsigned, unsigned> FileIDCUMap;
314 // Source id map, i.e. CUID, source filename and directory,
315 // separated by a zero byte, mapped to a unique id.
316 StringMap<unsigned, BumpPtrAllocator&> SourceIdMap;
318 // Provides a unique id per text section.
319 SetVector<const MCSection*> SectionMap;
321 // List of Arguments (DbgValues) for current function.
322 SmallVector<DbgVariable *, 8> CurrentFnArguments;
324 LexicalScopes LScopes;
326 // Collection of abstract subprogram DIEs.
327 DenseMap<const MDNode *, DIE *> AbstractSPDies;
329 // Collection of dbg variables of a scope.
330 DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> > ScopeVariables;
332 // Collection of abstract variables.
333 DenseMap<const MDNode *, DbgVariable *> AbstractVariables;
335 // Collection of DotDebugLocEntry.
336 SmallVector<DotDebugLocEntry, 4> DotDebugLocEntries;
338 // Collection of subprogram DIEs that are marked (at the end of the module)
340 SmallPtrSet<DIE *, 4> InlinedSubprogramDIEs;
342 // Keep track of inlined functions and their location. This
343 // information is used to populate the debug_inlined section.
344 typedef std::pair<const MCSymbol *, DIE *> InlineInfoLabels;
345 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> > InlineInfo;
346 SmallVector<const MDNode *, 4> InlinedSPNodes;
348 // This is a collection of subprogram MDNodes that are processed to
350 SmallPtrSet<const MDNode *, 16> ProcessedSPNodes;
352 // Maps instruction with label emitted before instruction.
353 DenseMap<const MachineInstr *, MCSymbol *> LabelsBeforeInsn;
355 // Maps instruction with label emitted after instruction.
356 DenseMap<const MachineInstr *, MCSymbol *> LabelsAfterInsn;
358 // Every user variable mentioned by a DBG_VALUE instruction in order of
360 SmallVector<const MDNode*, 8> UserVariables;
362 // For each user variable, keep a list of DBG_VALUE instructions in order.
363 // The list can also contain normal instructions that clobber the previous
365 typedef DenseMap<const MDNode*, SmallVector<const MachineInstr*, 4> >
367 DbgValueHistoryMap DbgValues;
369 SmallVector<const MCSymbol *, 8> DebugRangeSymbols;
371 // Previous instruction's location information. This is used to determine
372 // label location to indicate scope boundries in dwarf debug info.
373 DebugLoc PrevInstLoc;
376 // This location indicates end of function prologue and beginning of function
378 DebugLoc PrologEndLoc;
380 // Section Symbols: these are assembler temporary labels that are emitted at
381 // the beginning of each supported dwarf section. These are used to form
382 // section offsets and are created by EmitSectionLabels.
383 MCSymbol *DwarfInfoSectionSym, *DwarfAbbrevSectionSym;
384 MCSymbol *DwarfStrSectionSym, *TextSectionSym, *DwarfDebugRangeSectionSym;
385 MCSymbol *DwarfDebugLocSectionSym, *DwarfLineSectionSym, *DwarfAddrSectionSym;
386 MCSymbol *FunctionBeginSym, *FunctionEndSym;
387 MCSymbol *DwarfAbbrevDWOSectionSym, *DwarfStrDWOSectionSym;
389 // As an optimization, there is no need to emit an entry in the directory
390 // table for the same directory as DW_at_comp_dir.
391 StringRef CompilationDir;
393 // Counter for assigning globally unique IDs for CUs.
394 unsigned GlobalCUIndexCount;
396 // Holder for the file specific debug information.
397 DwarfUnits InfoHolder;
399 // Holders for the various debug information flags that we might need to
400 // have exposed. See accessor functions below for description.
402 // Whether or not we're emitting info for older versions of gdb on darwin.
403 bool IsDarwinGDBCompat;
405 // DWARF5 Experimental Options
406 bool HasDwarfAccelTables;
409 unsigned DwarfVersion;
411 // Separated Dwarf Variables
412 // In general these will all be for bits that are left in the
413 // original object file, rather than things that are meant
414 // to be in the .dwo sections.
416 // The CUs left in the original object file for separated debug info.
417 SmallVector<CompileUnit *, 1> SkeletonCUs;
419 // Used to uniquely define abbreviations for the skeleton emission.
420 FoldingSet<DIEAbbrev> SkeletonAbbrevSet;
422 // A list of all the unique abbreviations in use.
423 std::vector<DIEAbbrev *> SkeletonAbbrevs;
425 // Holder for the skeleton information.
426 DwarfUnits SkeletonHolder;
428 typedef SmallVector<std::pair<const MDNode *, const MDNode *>, 32>
430 ImportedEntityMap ScopesWithImportedEntities;
434 void addScopeVariable(LexicalScope *LS, DbgVariable *Var);
436 /// \brief Find abstract variable associated with Var.
437 DbgVariable *findAbstractVariable(DIVariable &Var, DebugLoc Loc);
439 /// \brief Find DIE for the given subprogram and attach appropriate
440 /// DW_AT_low_pc and DW_AT_high_pc attributes. If there are global
441 /// variables in this scope then create and insert DIEs for these
443 DIE *updateSubprogramScopeDIE(CompileUnit *SPCU, const MDNode *SPNode);
445 /// \brief Construct new DW_TAG_lexical_block for this scope and
446 /// attach DW_AT_low_pc/DW_AT_high_pc labels.
447 DIE *constructLexicalScopeDIE(CompileUnit *TheCU, LexicalScope *Scope);
449 /// \brief This scope represents inlined body of a function. Construct
450 /// DIE to represent this concrete inlined copy of the function.
451 DIE *constructInlinedScopeDIE(CompileUnit *TheCU, LexicalScope *Scope);
453 /// \brief Construct a DIE for this scope.
454 DIE *constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope);
456 /// \brief Emit initial Dwarf sections with a label at the start of each one.
457 void emitSectionLabels();
459 /// \brief Compute the size and offset of a DIE given an incoming Offset.
460 unsigned computeSizeAndOffset(DIE *Die, unsigned Offset);
462 /// \brief Compute the size and offset of all the DIEs.
463 void computeSizeAndOffsets();
465 /// \brief Attach DW_AT_inline attribute with inlined subprogram DIEs.
466 void computeInlinedDIEs();
468 /// \brief Collect info for variables that were optimized out.
469 void collectDeadVariables();
471 /// \brief Finish off debug information after all functions have been
473 void finalizeModuleInfo();
475 /// \brief Emit labels to close any remaining sections that have been left
479 /// \brief Emit a set of abbreviations to the specific section.
480 void emitAbbrevs(const MCSection *, std::vector<DIEAbbrev*> *);
482 /// \brief Emit the debug info section.
483 void emitDebugInfo();
485 /// \brief Emit the abbreviation section.
486 void emitAbbreviations();
488 /// \brief Emit the last address of the section and the end of
490 void emitEndOfLineMatrix(unsigned SectionEnd);
492 /// \brief Emit visible names into a hashed accelerator table section.
493 void emitAccelNames();
495 /// \brief Emit objective C classes and categories into a hashed
496 /// accelerator table section.
497 void emitAccelObjC();
499 /// \brief Emit namespace dies into a hashed accelerator table.
500 void emitAccelNamespaces();
502 /// \brief Emit type dies into a hashed accelerator table.
503 void emitAccelTypes();
505 /// \brief Emit visible names into a debug pubnames section.
506 void emitDebugPubnames();
508 /// \brief Emit visible types into a debug pubtypes section.
509 void emitDebugPubTypes();
511 /// \brief Emit visible names into a debug str section.
514 /// \brief Emit visible names into a debug loc section.
517 /// \brief Emit visible names into a debug aranges section.
518 void emitDebugARanges();
520 /// \brief Emit visible names into a debug ranges section.
521 void emitDebugRanges();
523 /// \brief Emit visible names into a debug macinfo section.
524 void emitDebugMacInfo();
526 /// \brief Emit inline info using custom format.
527 void emitDebugInlineInfo();
529 /// DWARF 5 Experimental Split Dwarf Emitters
531 /// \brief Construct the split debug info compile unit for the debug info
533 CompileUnit *constructSkeletonCU(const MDNode *);
535 /// \brief Emit the local split abbreviations.
536 void emitSkeletonAbbrevs(const MCSection *);
538 /// \brief Emit the debug info dwo section.
539 void emitDebugInfoDWO();
541 /// \brief Emit the debug abbrev dwo section.
542 void emitDebugAbbrevDWO();
544 /// \brief Emit the debug str dwo section.
545 void emitDebugStrDWO();
547 /// \brief Create new CompileUnit for the given metadata node with tag
548 /// DW_TAG_compile_unit.
549 CompileUnit *constructCompileUnit(const MDNode *N);
551 /// \brief Construct subprogram DIE.
552 void constructSubprogramDIE(CompileUnit *TheCU, const MDNode *N);
554 /// \brief Construct imported_module or imported_declaration DIE.
555 void constructImportedEntityDIE(CompileUnit *TheCU, const MDNode *N);
557 /// \brief Construct import_module DIE.
558 void constructImportedEntityDIE(CompileUnit *TheCU, const MDNode *N,
561 /// \brief Construct import_module DIE.
562 void constructImportedEntityDIE(CompileUnit *TheCU,
563 const DIImportedEntity &Module,
566 /// \brief Register a source line with debug info. Returns the unique
567 /// label that was emitted and which provides correspondence to the
568 /// source line list.
569 void recordSourceLine(unsigned Line, unsigned Col, const MDNode *Scope,
572 /// \brief Indentify instructions that are marking the beginning of or
573 /// ending of a scope.
574 void identifyScopeMarkers();
576 /// \brief If Var is an current function argument that add it in
577 /// CurrentFnArguments list.
578 bool addCurrentFnArgument(const MachineFunction *MF,
579 DbgVariable *Var, LexicalScope *Scope);
581 /// \brief Populate LexicalScope entries with variables' info.
582 void collectVariableInfo(const MachineFunction *,
583 SmallPtrSet<const MDNode *, 16> &ProcessedVars);
585 /// \brief Collect variable information from the side table maintained
587 void collectVariableInfoFromMMITable(const MachineFunction * MF,
588 SmallPtrSet<const MDNode *, 16> &P);
590 /// \brief Ensure that a label will be emitted before MI.
591 void requestLabelBeforeInsn(const MachineInstr *MI) {
592 LabelsBeforeInsn.insert(std::make_pair(MI, (MCSymbol*)0));
595 /// \brief Return Label preceding the instruction.
596 MCSymbol *getLabelBeforeInsn(const MachineInstr *MI);
598 /// \brief Ensure that a label will be emitted after MI.
599 void requestLabelAfterInsn(const MachineInstr *MI) {
600 LabelsAfterInsn.insert(std::make_pair(MI, (MCSymbol*)0));
603 /// \brief Return Label immediately following the instruction.
604 MCSymbol *getLabelAfterInsn(const MachineInstr *MI);
607 //===--------------------------------------------------------------------===//
608 // Main entry points.
610 DwarfDebug(AsmPrinter *A, Module *M);
613 /// \brief Emit all Dwarf sections that should come prior to the
617 /// \brief Emit all Dwarf sections that should come after the content.
620 /// \brief Gather pre-function debug information.
621 void beginFunction(const MachineFunction *MF);
623 /// \brief Gather and emit post-function debug information.
624 void endFunction(const MachineFunction *MF);
626 /// \brief Process beginning of an instruction.
627 void beginInstruction(const MachineInstr *MI);
629 /// \brief Process end of an instruction.
630 void endInstruction(const MachineInstr *MI);
632 /// \brief Look up the source id with the given directory and source file
633 /// names. If none currently exists, create a new id and insert it in the
635 unsigned getOrCreateSourceID(StringRef DirName, StringRef FullName,
638 /// \brief Recursively Emits a debug information entry.
639 void emitDIE(DIE *Die, std::vector<DIEAbbrev *> *Abbrevs);
641 /// \brief Returns whether or not to limit some of our debug
642 /// output to the limitations of darwin gdb.
643 bool useDarwinGDBCompat() { return IsDarwinGDBCompat; }
645 // Experimental DWARF5 features.
647 /// \brief Returns whether or not to emit tables that dwarf consumers can
648 /// use to accelerate lookup.
649 bool useDwarfAccelTables() { return HasDwarfAccelTables; }
651 /// \brief Returns whether or not to change the current debug info for the
652 /// split dwarf proposal support.
653 bool useSplitDwarf() { return HasSplitDwarf; }
655 /// Returns the Dwarf Version.
656 unsigned getDwarfVersion() const { return DwarfVersion; }
658 } // End of namespace llvm