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;
46 //===----------------------------------------------------------------------===//
47 /// \brief This class is used to record source line correspondence.
49 unsigned Line; // Source line number.
50 unsigned Column; // Source column.
51 unsigned SourceID; // Source ID number.
52 MCSymbol *Label; // Label in code ID number.
54 SrcLineInfo(unsigned L, unsigned C, unsigned S, MCSymbol *label)
55 : Line(L), Column(C), SourceID(S), Label(label) {}
58 unsigned getLine() const { return Line; }
59 unsigned getColumn() const { return Column; }
60 unsigned getSourceID() const { return SourceID; }
61 MCSymbol *getLabel() const { return Label; }
64 /// \brief This struct describes location entries emitted in the .debug_loc
66 typedef struct DotDebugLocEntry {
67 const MCSymbol *Begin;
70 const MDNode *Variable;
79 enum EntryType EntryKind;
83 const ConstantFP *CFP;
84 const ConstantInt *CIP;
87 : Begin(0), End(0), Variable(0), Merged(false),
88 Constant(false) { Constants.Int = 0;}
89 DotDebugLocEntry(const MCSymbol *B, const MCSymbol *E, MachineLocation &L,
91 : Begin(B), End(E), Loc(L), Variable(V), Merged(false),
92 Constant(false) { Constants.Int = 0; EntryKind = E_Location; }
93 DotDebugLocEntry(const MCSymbol *B, const MCSymbol *E, int64_t i)
94 : Begin(B), End(E), Variable(0), Merged(false),
95 Constant(true) { Constants.Int = i; EntryKind = E_Integer; }
96 DotDebugLocEntry(const MCSymbol *B, const MCSymbol *E, const ConstantFP *FPtr)
97 : Begin(B), End(E), Variable(0), Merged(false),
98 Constant(true) { Constants.CFP = FPtr; EntryKind = E_ConstantFP; }
99 DotDebugLocEntry(const MCSymbol *B, const MCSymbol *E,
100 const ConstantInt *IPtr)
101 : Begin(B), End(E), Variable(0), Merged(false),
102 Constant(true) { Constants.CIP = IPtr; EntryKind = E_ConstantInt; }
104 /// \brief Empty entries are also used as a trigger to emit temp label. Such
105 /// labels are referenced is used to find debug_loc offset for a given DIE.
106 bool isEmpty() { return Begin == 0 && End == 0; }
107 bool isMerged() { return Merged; }
108 void Merge(DotDebugLocEntry *Next) {
109 if (!(Begin && Loc == Next->Loc && End == Next->Begin))
114 bool isLocation() const { return EntryKind == E_Location; }
115 bool isInt() const { return EntryKind == E_Integer; }
116 bool isConstantFP() const { return EntryKind == E_ConstantFP; }
117 bool isConstantInt() const { return EntryKind == E_ConstantInt; }
118 int64_t getInt() { return Constants.Int; }
119 const ConstantFP *getConstantFP() { return Constants.CFP; }
120 const ConstantInt *getConstantInt() { return Constants.CIP; }
123 //===----------------------------------------------------------------------===//
124 /// \brief This class is used to track local variable information.
126 DIVariable Var; // Variable Descriptor.
127 DIE *TheDIE; // Variable DIE.
128 unsigned DotDebugLocOffset; // Offset in DotDebugLocEntries.
129 DbgVariable *AbsVar; // Corresponding Abstract variable, if any.
130 const MachineInstr *MInsn; // DBG_VALUE instruction of the variable.
133 // AbsVar may be NULL.
134 DbgVariable(DIVariable V, DbgVariable *AV)
135 : Var(V), TheDIE(0), DotDebugLocOffset(~0U), AbsVar(AV), MInsn(0),
139 DIVariable getVariable() const { return Var; }
140 void setDIE(DIE *D) { TheDIE = D; }
141 DIE *getDIE() const { return TheDIE; }
142 void setDotDebugLocOffset(unsigned O) { DotDebugLocOffset = O; }
143 unsigned getDotDebugLocOffset() const { return DotDebugLocOffset; }
144 StringRef getName() const { return Var.getName(); }
145 DbgVariable *getAbstractVariable() const { return AbsVar; }
146 const MachineInstr *getMInsn() const { return MInsn; }
147 void setMInsn(const MachineInstr *M) { MInsn = M; }
148 int getFrameIndex() const { return FrameIndex; }
149 void setFrameIndex(int FI) { FrameIndex = FI; }
150 // Translate tag to proper Dwarf tag.
151 unsigned getTag() const {
152 if (Var.getTag() == dwarf::DW_TAG_arg_variable)
153 return dwarf::DW_TAG_formal_parameter;
155 return dwarf::DW_TAG_variable;
157 /// \brief Return true if DbgVariable is artificial.
158 bool isArtificial() const {
159 if (Var.isArtificial())
161 if (getType().isArtificial())
166 bool isObjectPointer() const {
167 if (Var.isObjectPointer())
169 if (getType().isObjectPointer())
174 bool variableHasComplexAddress() const {
175 assert(Var.Verify() && "Invalid complex DbgVariable!");
176 return Var.hasComplexAddress();
178 bool isBlockByrefVariable() const {
179 assert(Var.Verify() && "Invalid complex DbgVariable!");
180 return Var.isBlockByrefVariable();
182 unsigned getNumAddrElements() const {
183 assert(Var.Verify() && "Invalid complex DbgVariable!");
184 return Var.getNumAddrElements();
186 uint64_t getAddrElement(unsigned i) const {
187 return Var.getAddrElement(i);
189 DIType getType() const;
193 // A String->Symbol mapping of strings used by indirect
195 typedef StringMap<std::pair<MCSymbol*, unsigned>,
196 BumpPtrAllocator&> StrPool;
198 // A Symbol->pair<Symbol, unsigned> mapping of addresses used by indirect
200 typedef DenseMap<MCSymbol *, std::pair<MCSymbol *, unsigned> > AddrPool;
202 /// \brief Collects and handles information specific to a particular
203 /// collection of units.
205 // Target of Dwarf emission, used for sizing of abbreviations.
208 // Used to uniquely define abbreviations.
209 FoldingSet<DIEAbbrev> *AbbreviationsSet;
211 // A list of all the unique abbreviations in use.
212 std::vector<DIEAbbrev *> *Abbreviations;
214 // A pointer to all units in the section.
215 SmallVector<CompileUnit *, 1> CUs;
217 // Collection of strings for this unit and assorted symbols.
219 unsigned NextStringPoolNumber;
220 std::string StringPref;
222 // Collection of addresses for this unit and assorted labels.
223 AddrPool AddressPool;
224 unsigned NextAddrPoolNumber;
227 DwarfUnits(AsmPrinter *AP, FoldingSet<DIEAbbrev> *AS,
228 std::vector<DIEAbbrev *> *A, const char *Pref,
229 BumpPtrAllocator &DA) :
230 Asm(AP), AbbreviationsSet(AS), Abbreviations(A),
231 StringPool(DA), NextStringPoolNumber(0), StringPref(Pref),
232 AddressPool(), NextAddrPoolNumber(0) {}
234 /// \brief Compute the size and offset of a DIE given an incoming Offset.
235 unsigned computeSizeAndOffset(DIE *Die, unsigned Offset);
237 /// \brief Compute the size and offset of all the DIEs.
238 void computeSizeAndOffsets();
240 /// \brief Define a unique number for the abbreviation.
241 void assignAbbrevNumber(DIEAbbrev &Abbrev);
243 /// \brief Add a unit to the list of CUs.
244 void addUnit(CompileUnit *CU) { CUs.push_back(CU); }
246 /// \brief Emit all of the units to the section listed with the given
247 /// abbreviation section.
248 void emitUnits(DwarfDebug *, const MCSection *, const MCSection *,
251 /// \brief Emit all of the strings to the section given.
252 void emitStrings(const MCSection *, const MCSection *, const MCSymbol *);
254 /// \brief Emit all of the addresses to the section given.
255 void emitAddresses(const MCSection *);
257 /// \brief Returns the entry into the start of the pool.
258 MCSymbol *getStringPoolSym();
260 /// \brief Returns an entry into the string pool with the given
262 MCSymbol *getStringPoolEntry(StringRef Str);
264 /// \brief Returns the index into the string pool with the given
266 unsigned getStringPoolIndex(StringRef Str);
268 /// \brief Returns the string pool.
269 StrPool *getStringPool() { return &StringPool; }
271 /// \brief Returns the index into the address pool with the given
273 unsigned getAddrPoolIndex(MCSymbol *);
275 /// \brief Returns the address pool.
276 AddrPool *getAddrPool() { return &AddressPool; }
279 /// \brief Collects and handles dwarf debug information.
281 // Target of Dwarf emission.
284 // Collected machine module information.
285 MachineModuleInfo *MMI;
287 // All DIEValues are allocated through this allocator.
288 BumpPtrAllocator DIEValueAllocator;
290 //===--------------------------------------------------------------------===//
291 // Attribute used to construct specific Dwarf sections.
294 CompileUnit *FirstCU;
296 // Maps MDNode with its corresponding CompileUnit.
297 DenseMap <const MDNode *, CompileUnit *> CUMap;
299 // Maps subprogram MDNode with its corresponding CompileUnit.
300 DenseMap <const MDNode *, CompileUnit *> SPMap;
302 // Used to uniquely define abbreviations.
303 FoldingSet<DIEAbbrev> AbbreviationsSet;
305 // A list of all the unique abbreviations in use.
306 std::vector<DIEAbbrev *> Abbreviations;
308 // Source id map, i.e. pair of source filename and directory,
309 // separated by a zero byte, mapped to a unique id.
310 StringMap<unsigned, BumpPtrAllocator&> SourceIdMap;
312 // Provides a unique id per text section.
313 SetVector<const MCSection*> SectionMap;
315 // List of Arguments (DbgValues) for current function.
316 SmallVector<DbgVariable *, 8> CurrentFnArguments;
318 LexicalScopes LScopes;
320 // Collection of abstract subprogram DIEs.
321 DenseMap<const MDNode *, DIE *> AbstractSPDies;
323 // Collection of dbg variables of a scope.
324 DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> > ScopeVariables;
326 // Collection of abstract variables.
327 DenseMap<const MDNode *, DbgVariable *> AbstractVariables;
329 // Collection of DotDebugLocEntry.
330 SmallVector<DotDebugLocEntry, 4> DotDebugLocEntries;
332 // Collection of subprogram DIEs that are marked (at the end of the module)
334 SmallPtrSet<DIE *, 4> InlinedSubprogramDIEs;
336 // Keep track of inlined functions and their location. This
337 // information is used to populate the debug_inlined section.
338 typedef std::pair<const MCSymbol *, DIE *> InlineInfoLabels;
339 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> > InlineInfo;
340 SmallVector<const MDNode *, 4> InlinedSPNodes;
342 // This is a collection of subprogram MDNodes that are processed to
344 SmallPtrSet<const MDNode *, 16> ProcessedSPNodes;
346 // Maps instruction with label emitted before instruction.
347 DenseMap<const MachineInstr *, MCSymbol *> LabelsBeforeInsn;
349 // Maps instruction with label emitted after instruction.
350 DenseMap<const MachineInstr *, MCSymbol *> LabelsAfterInsn;
352 // Every user variable mentioned by a DBG_VALUE instruction in order of
354 SmallVector<const MDNode*, 8> UserVariables;
356 // For each user variable, keep a list of DBG_VALUE instructions in order.
357 // The list can also contain normal instructions that clobber the previous
359 typedef DenseMap<const MDNode*, SmallVector<const MachineInstr*, 4> >
361 DbgValueHistoryMap DbgValues;
363 SmallVector<const MCSymbol *, 8> DebugRangeSymbols;
365 // Previous instruction's location information. This is used to determine
366 // label location to indicate scope boundries in dwarf debug info.
367 DebugLoc PrevInstLoc;
370 // This location indicates end of function prologue and beginning of function
372 DebugLoc PrologEndLoc;
374 struct FunctionDebugFrameInfo {
376 std::vector<MachineMove> Moves;
378 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M)
379 : Number(Num), Moves(M) {}
382 std::vector<FunctionDebugFrameInfo> DebugFrames;
384 // Section Symbols: these are assembler temporary labels that are emitted at
385 // the beginning of each supported dwarf section. These are used to form
386 // section offsets and are created by EmitSectionLabels.
387 MCSymbol *DwarfInfoSectionSym, *DwarfAbbrevSectionSym;
388 MCSymbol *DwarfStrSectionSym, *TextSectionSym, *DwarfDebugRangeSectionSym;
389 MCSymbol *DwarfDebugLocSectionSym;
390 MCSymbol *FunctionBeginSym, *FunctionEndSym;
391 MCSymbol *DwarfAbbrevDWOSectionSym, *DwarfStrDWOSectionSym;
393 // As an optimization, there is no need to emit an entry in the directory
394 // table for the same directory as DW_at_comp_dir.
395 StringRef CompilationDir;
397 // Counter for assigning globally unique IDs for CUs.
398 unsigned GlobalCUIndexCount;
400 // Holder for the file specific debug information.
401 DwarfUnits InfoHolder;
403 // Holders for the various debug information flags that we might need to
404 // have exposed. See accessor functions below for description.
406 // Whether or not we're emitting info for older versions of gdb on darwin.
407 bool IsDarwinGDBCompat;
409 // DWARF5 Experimental Options
410 bool HasDwarfAccelTables;
413 // Separated Dwarf Variables
414 // In general these will all be for bits that are left in the
415 // original object file, rather than things that are meant
416 // to be in the .dwo sections.
418 // The CU left in the original object file for separated debug info.
419 CompileUnit *SkeletonCU;
421 // Used to uniquely define abbreviations for the skeleton emission.
422 FoldingSet<DIEAbbrev> SkeletonAbbrevSet;
424 // A list of all the unique abbreviations in use.
425 std::vector<DIEAbbrev *> SkeletonAbbrevs;
427 // Holder for the skeleton information.
428 DwarfUnits SkeletonHolder;
432 void addScopeVariable(LexicalScope *LS, DbgVariable *Var);
434 /// \brief Find abstract variable associated with Var.
435 DbgVariable *findAbstractVariable(DIVariable &Var, DebugLoc Loc);
437 /// \brief Find DIE for the given subprogram and attach appropriate
438 /// DW_AT_low_pc and DW_AT_high_pc attributes. If there are global
439 /// variables in this scope then create and insert DIEs for these
441 DIE *updateSubprogramScopeDIE(CompileUnit *SPCU, const MDNode *SPNode);
443 /// \brief Construct new DW_TAG_lexical_block for this scope and
444 /// attach DW_AT_low_pc/DW_AT_high_pc labels.
445 DIE *constructLexicalScopeDIE(CompileUnit *TheCU, LexicalScope *Scope);
447 /// \brief This scope represents inlined body of a function. Construct
448 /// DIE to represent this concrete inlined copy of the function.
449 DIE *constructInlinedScopeDIE(CompileUnit *TheCU, LexicalScope *Scope);
451 /// \brief Construct a DIE for this scope.
452 DIE *constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope);
454 /// \brief Emit initial Dwarf sections with a label at the start of each one.
455 void emitSectionLabels();
457 /// \brief Compute the size and offset of a DIE given an incoming Offset.
458 unsigned computeSizeAndOffset(DIE *Die, unsigned Offset);
460 /// \brief Compute the size and offset of all the DIEs.
461 void computeSizeAndOffsets();
463 /// \brief Attach DW_AT_inline attribute with inlined subprogram DIEs.
464 void computeInlinedDIEs();
466 /// \brief Collect info for variables that were optimized out.
467 void collectDeadVariables();
469 /// \brief Finish off debug information after all functions have been
471 void finalizeModuleInfo();
473 /// \brief Emit labels to close any remaining sections that have been left
477 /// \brief Emit a set of abbreviations to the specific section.
478 void emitAbbrevs(const MCSection *, std::vector<DIEAbbrev*> *);
480 /// \brief Emit the debug info section.
481 void emitDebugInfo();
483 /// \brief Emit the abbreviation section.
484 void emitAbbreviations();
486 /// \brief Emit the last address of the section and the end of
488 void emitEndOfLineMatrix(unsigned SectionEnd);
490 /// \brief Emit visible names into a hashed accelerator table section.
491 void emitAccelNames();
493 /// \brief Emit objective C classes and categories into a hashed
494 /// accelerator table section.
495 void emitAccelObjC();
497 /// \brief Emit namespace dies into a hashed accelerator table.
498 void emitAccelNamespaces();
500 /// \brief Emit type dies into a hashed accelerator table.
501 void emitAccelTypes();
503 /// \brief Emit visible types into a debug pubtypes section.
504 void emitDebugPubTypes();
506 /// \brief Emit visible names into a debug str section.
509 /// \brief Emit visible names into a debug loc section.
512 /// \brief Emit visible names into a debug aranges section.
513 void emitDebugARanges();
515 /// \brief Emit visible names into a debug ranges section.
516 void emitDebugRanges();
518 /// \brief Emit visible names into a debug macinfo section.
519 void emitDebugMacInfo();
521 /// \brief Emit inline info using custom format.
522 void emitDebugInlineInfo();
524 /// DWARF 5 Experimental Split Dwarf Emitters
526 /// \brief Construct the split debug info compile unit for the debug info
528 CompileUnit *constructSkeletonCU(const MDNode *);
530 /// \brief Emit the local split debug info section.
531 void emitSkeletonCU(const MCSection *);
533 /// \brief Emit the local split abbreviations.
534 void emitSkeletonAbbrevs(const MCSection *);
536 /// \brief Emit the debug info dwo section.
537 void emitDebugInfoDWO();
539 /// \brief Emit the debug abbrev dwo section.
540 void emitDebugAbbrevDWO();
542 /// \brief Emit the debug str dwo section.
543 void emitDebugStrDWO();
545 /// \brief Create new CompileUnit for the given metadata node with tag
546 /// DW_TAG_compile_unit.
547 CompileUnit *constructCompileUnit(const MDNode *N);
549 /// \brief Construct subprogram DIE.
550 void constructSubprogramDIE(CompileUnit *TheCU, const MDNode *N);
552 /// \brief Register a source line with debug info. Returns the unique
553 /// label that was emitted and which provides correspondence to the
554 /// source line list.
555 void recordSourceLine(unsigned Line, unsigned Col, const MDNode *Scope,
558 /// \brief Indentify instructions that are marking the beginning of or
559 /// ending of a scope.
560 void identifyScopeMarkers();
562 /// \brief If Var is an current function argument that add it in
563 /// CurrentFnArguments list.
564 bool addCurrentFnArgument(const MachineFunction *MF,
565 DbgVariable *Var, LexicalScope *Scope);
567 /// \brief Populate LexicalScope entries with variables' info.
568 void collectVariableInfo(const MachineFunction *,
569 SmallPtrSet<const MDNode *, 16> &ProcessedVars);
571 /// \brief Collect variable information from the side table maintained
573 void collectVariableInfoFromMMITable(const MachineFunction * MF,
574 SmallPtrSet<const MDNode *, 16> &P);
576 /// \brief Ensure that a label will be emitted before MI.
577 void requestLabelBeforeInsn(const MachineInstr *MI) {
578 LabelsBeforeInsn.insert(std::make_pair(MI, (MCSymbol*)0));
581 /// \brief Return Label preceding the instruction.
582 MCSymbol *getLabelBeforeInsn(const MachineInstr *MI);
584 /// \brief Ensure that a label will be emitted after MI.
585 void requestLabelAfterInsn(const MachineInstr *MI) {
586 LabelsAfterInsn.insert(std::make_pair(MI, (MCSymbol*)0));
589 /// \brief Return Label immediately following the instruction.
590 MCSymbol *getLabelAfterInsn(const MachineInstr *MI);
593 //===--------------------------------------------------------------------===//
594 // Main entry points.
596 DwarfDebug(AsmPrinter *A, Module *M);
599 /// \brief Collect debug info from named mdnodes such as llvm.dbg.enum
601 void collectInfoFromNamedMDNodes(const Module *M);
603 /// \brief Collect debug info using DebugInfoFinder.
604 /// FIXME - Remove this when DragonEgg switches to DIBuilder.
605 bool collectLegacyDebugInfo(const Module *M);
607 /// \brief Emit all Dwarf sections that should come prior to the
611 /// \brief Emit all Dwarf sections that should come after the content.
614 /// \brief Gather pre-function debug information.
615 void beginFunction(const MachineFunction *MF);
617 /// \brief Gather and emit post-function debug information.
618 void endFunction(const MachineFunction *MF);
620 /// \brief Process beginning of an instruction.
621 void beginInstruction(const MachineInstr *MI);
623 /// \brief Process end of an instruction.
624 void endInstruction(const MachineInstr *MI);
626 /// \brief Look up the source id with the given directory and source file
627 /// names. If none currently exists, create a new id and insert it in the
629 unsigned getOrCreateSourceID(StringRef DirName, StringRef FullName);
631 /// \brief Recursively Emits a debug information entry.
632 void emitDIE(DIE *Die, std::vector<DIEAbbrev *> *Abbrevs);
634 /// \brief Returns whether or not to limit some of our debug
635 /// output to the limitations of darwin gdb.
636 bool useDarwinGDBCompat() { return IsDarwinGDBCompat; }
638 // Experimental DWARF5 features.
640 /// \brief Returns whether or not to emit tables that dwarf consumers can
641 /// use to accelerate lookup.
642 bool useDwarfAccelTables() { return HasDwarfAccelTables; }
644 /// \brief Returns whether or not to change the current debug info for the
645 /// split dwarf proposal support.
646 bool useSplitDwarf() { return HasSplitDwarf; }
648 } // End of namespace llvm