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;
199 // A String->Symbol mapping of strings used by indirect
201 typedef StringMap<std::pair<MCSymbol*, unsigned>,
202 BumpPtrAllocator&> StrPool;
204 // A Symbol->unsigned mapping of addresses used by indirect
206 typedef DenseMap<const MCExpr *, unsigned> AddrPool;
208 /// \brief Collects and handles information specific to a particular
209 /// collection of units.
211 // Target of Dwarf emission, used for sizing of abbreviations.
214 // Used to uniquely define abbreviations.
215 FoldingSet<DIEAbbrev> *AbbreviationsSet;
217 // A list of all the unique abbreviations in use.
218 std::vector<DIEAbbrev *> *Abbreviations;
220 // A pointer to all units in the section.
221 SmallVector<CompileUnit *, 1> CUs;
223 // Collection of strings for this unit and assorted symbols.
225 unsigned NextStringPoolNumber;
226 std::string StringPref;
228 // Collection of addresses for this unit and assorted labels.
229 AddrPool AddressPool;
230 unsigned NextAddrPoolNumber;
233 DwarfUnits(AsmPrinter *AP, FoldingSet<DIEAbbrev> *AS,
234 std::vector<DIEAbbrev *> *A, const char *Pref,
235 BumpPtrAllocator &DA) :
236 Asm(AP), AbbreviationsSet(AS), Abbreviations(A),
237 StringPool(DA), NextStringPoolNumber(0), StringPref(Pref),
238 AddressPool(), NextAddrPoolNumber(0) {}
240 /// \brief Compute the size and offset of a DIE given an incoming Offset.
241 unsigned computeSizeAndOffset(DIE *Die, unsigned Offset);
243 /// \brief Compute the size and offset of all the DIEs.
244 void computeSizeAndOffsets();
246 /// \brief Define a unique number for the abbreviation.
247 void assignAbbrevNumber(DIEAbbrev &Abbrev);
249 /// \brief Add a unit to the list of CUs.
250 void addUnit(CompileUnit *CU) { CUs.push_back(CU); }
252 /// \brief Emit all of the units to the section listed with the given
253 /// abbreviation section.
254 void emitUnits(DwarfDebug *, const MCSection *, const MCSection *,
257 /// \brief Emit all of the strings to the section given.
258 void emitStrings(const MCSection *, const MCSection *, const MCSymbol *);
260 /// \brief Emit all of the addresses to the section given.
261 void emitAddresses(const MCSection *);
263 /// \brief Returns the entry into the start of the pool.
264 MCSymbol *getStringPoolSym();
266 /// \brief Returns an entry into the string pool with the given
268 MCSymbol *getStringPoolEntry(StringRef Str);
270 /// \brief Returns the index into the string pool with the given
272 unsigned getStringPoolIndex(StringRef Str);
274 /// \brief Returns the string pool.
275 StrPool *getStringPool() { return &StringPool; }
277 /// \brief Returns the index into the address pool with the given
279 unsigned getAddrPoolIndex(const MCExpr *);
280 unsigned getAddrPoolIndex(const MCSymbol *);
282 /// \brief Returns the address pool.
283 AddrPool *getAddrPool() { return &AddressPool; }
285 /// \brief for a given compile unit DIE, returns offset from beginning of
287 unsigned getCUOffset(DIE *Die);
290 /// \brief Collects and handles dwarf debug information.
292 // Target of Dwarf emission.
295 // Collected machine module information.
296 MachineModuleInfo *MMI;
298 // All DIEValues are allocated through this allocator.
299 BumpPtrAllocator DIEValueAllocator;
301 //===--------------------------------------------------------------------===//
302 // Attribute used to construct specific Dwarf sections.
305 CompileUnit *FirstCU;
307 // Maps MDNode with its corresponding CompileUnit.
308 DenseMap <const MDNode *, CompileUnit *> CUMap;
310 // Maps subprogram MDNode with its corresponding CompileUnit.
311 DenseMap <const MDNode *, CompileUnit *> SPMap;
313 // Used to uniquely define abbreviations.
314 FoldingSet<DIEAbbrev> AbbreviationsSet;
316 // A list of all the unique abbreviations in use.
317 std::vector<DIEAbbrev *> Abbreviations;
319 // Stores the current file ID for a given compile unit.
320 DenseMap <unsigned, unsigned> FileIDCUMap;
321 // Source id map, i.e. CUID, source filename and directory,
322 // separated by a zero byte, mapped to a unique id.
323 StringMap<unsigned, BumpPtrAllocator&> SourceIdMap;
325 // Provides a unique id per text section.
326 SetVector<const MCSection*> SectionMap;
328 // List of Arguments (DbgValues) for current function.
329 SmallVector<DbgVariable *, 8> CurrentFnArguments;
331 LexicalScopes LScopes;
333 // Collection of abstract subprogram DIEs.
334 DenseMap<const MDNode *, DIE *> AbstractSPDies;
336 // Collection of dbg variables of a scope.
337 typedef DenseMap<LexicalScope *,
338 SmallVector<DbgVariable *, 8> > ScopeVariablesMap;
339 ScopeVariablesMap ScopeVariables;
341 // Collection of abstract variables.
342 DenseMap<const MDNode *, DbgVariable *> AbstractVariables;
344 // Collection of DotDebugLocEntry.
345 SmallVector<DotDebugLocEntry, 4> DotDebugLocEntries;
347 // Collection of subprogram DIEs that are marked (at the end of the module)
349 SmallPtrSet<DIE *, 4> InlinedSubprogramDIEs;
351 // Keep track of inlined functions and their location. This
352 // information is used to populate the debug_inlined section.
353 typedef std::pair<const MCSymbol *, DIE *> InlineInfoLabels;
354 typedef DenseMap<const MDNode *,
355 SmallVector<InlineInfoLabels, 4> > InlineInfoMap;
356 InlineInfoMap InlineInfo;
357 SmallVector<const MDNode *, 4> InlinedSPNodes;
359 // This is a collection of subprogram MDNodes that are processed to
361 SmallPtrSet<const MDNode *, 16> ProcessedSPNodes;
363 // Maps instruction with label emitted before instruction.
364 DenseMap<const MachineInstr *, MCSymbol *> LabelsBeforeInsn;
366 // Maps instruction with label emitted after instruction.
367 DenseMap<const MachineInstr *, MCSymbol *> LabelsAfterInsn;
369 // Every user variable mentioned by a DBG_VALUE instruction in order of
371 SmallVector<const MDNode*, 8> UserVariables;
373 // For each user variable, keep a list of DBG_VALUE instructions in order.
374 // The list can also contain normal instructions that clobber the previous
376 typedef DenseMap<const MDNode*, SmallVector<const MachineInstr*, 4> >
378 DbgValueHistoryMap DbgValues;
380 SmallVector<const MCSymbol *, 8> DebugRangeSymbols;
382 // Previous instruction's location information. This is used to determine
383 // label location to indicate scope boundries in dwarf debug info.
384 DebugLoc PrevInstLoc;
387 // This location indicates end of function prologue and beginning of function
389 DebugLoc PrologEndLoc;
391 // Section Symbols: these are assembler temporary labels that are emitted at
392 // the beginning of each supported dwarf section. These are used to form
393 // section offsets and are created by EmitSectionLabels.
394 MCSymbol *DwarfInfoSectionSym, *DwarfAbbrevSectionSym;
395 MCSymbol *DwarfStrSectionSym, *TextSectionSym, *DwarfDebugRangeSectionSym;
396 MCSymbol *DwarfDebugLocSectionSym, *DwarfLineSectionSym, *DwarfAddrSectionSym;
397 MCSymbol *FunctionBeginSym, *FunctionEndSym;
398 MCSymbol *DwarfAbbrevDWOSectionSym, *DwarfStrDWOSectionSym;
400 // As an optimization, there is no need to emit an entry in the directory
401 // table for the same directory as DW_AT_comp_dir.
402 StringRef CompilationDir;
404 // Counter for assigning globally unique IDs for CUs.
405 unsigned GlobalCUIndexCount;
407 // Holder for the file specific debug information.
408 DwarfUnits InfoHolder;
410 // Holders for the various debug information flags that we might need to
411 // have exposed. See accessor functions below for description.
413 // Whether or not we're emitting info for older versions of gdb on darwin.
414 bool IsDarwinGDBCompat;
416 // Holder for imported entities.
417 typedef SmallVector<std::pair<const MDNode *, const MDNode *>, 32>
419 ImportedEntityMap ScopesWithImportedEntities;
421 // DWARF5 Experimental Options
422 bool HasDwarfAccelTables;
425 unsigned DwarfVersion;
427 // Separated Dwarf Variables
428 // In general these will all be for bits that are left in the
429 // original object file, rather than things that are meant
430 // to be in the .dwo sections.
432 // The CUs left in the original object file for separated debug info.
433 SmallVector<CompileUnit *, 1> SkeletonCUs;
435 // Used to uniquely define abbreviations for the skeleton emission.
436 FoldingSet<DIEAbbrev> SkeletonAbbrevSet;
438 // A list of all the unique abbreviations in use.
439 std::vector<DIEAbbrev *> SkeletonAbbrevs;
441 // Holder for the skeleton information.
442 DwarfUnits SkeletonHolder;
446 void addScopeVariable(LexicalScope *LS, DbgVariable *Var);
448 /// \brief Find abstract variable associated with Var.
449 DbgVariable *findAbstractVariable(DIVariable &Var, DebugLoc Loc);
451 /// \brief Find DIE for the given subprogram and attach appropriate
452 /// DW_AT_low_pc and DW_AT_high_pc attributes. If there are global
453 /// variables in this scope then create and insert DIEs for these
455 DIE *updateSubprogramScopeDIE(CompileUnit *SPCU, const MDNode *SPNode);
457 /// \brief Construct new DW_TAG_lexical_block for this scope and
458 /// attach DW_AT_low_pc/DW_AT_high_pc labels.
459 DIE *constructLexicalScopeDIE(CompileUnit *TheCU, LexicalScope *Scope);
461 /// \brief This scope represents inlined body of a function. Construct
462 /// DIE to represent this concrete inlined copy of the function.
463 DIE *constructInlinedScopeDIE(CompileUnit *TheCU, LexicalScope *Scope);
465 /// \brief Construct a DIE for this scope.
466 DIE *constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope);
468 /// \brief Emit initial Dwarf sections with a label at the start of each one.
469 void emitSectionLabels();
471 /// \brief Compute the size and offset of a DIE given an incoming Offset.
472 unsigned computeSizeAndOffset(DIE *Die, unsigned Offset);
474 /// \brief Compute the size and offset of all the DIEs.
475 void computeSizeAndOffsets();
477 /// \brief Attach DW_AT_inline attribute with inlined subprogram DIEs.
478 void computeInlinedDIEs();
480 /// \brief Collect info for variables that were optimized out.
481 void collectDeadVariables();
483 /// \brief Finish off debug information after all functions have been
485 void finalizeModuleInfo();
487 /// \brief Emit labels to close any remaining sections that have been left
491 /// \brief Emit a set of abbreviations to the specific section.
492 void emitAbbrevs(const MCSection *, std::vector<DIEAbbrev*> *);
494 /// \brief Emit the debug info section.
495 void emitDebugInfo();
497 /// \brief Emit the abbreviation section.
498 void emitAbbreviations();
500 /// \brief Emit the last address of the section and the end of
502 void emitEndOfLineMatrix(unsigned SectionEnd);
504 /// \brief Emit visible names into a hashed accelerator table section.
505 void emitAccelNames();
507 /// \brief Emit objective C classes and categories into a hashed
508 /// accelerator table section.
509 void emitAccelObjC();
511 /// \brief Emit namespace dies into a hashed accelerator table.
512 void emitAccelNamespaces();
514 /// \brief Emit type dies into a hashed accelerator table.
515 void emitAccelTypes();
517 /// \brief Emit visible names into a debug pubnames section.
518 void emitDebugPubnames();
520 /// \brief Emit visible types into a debug pubtypes section.
521 void emitDebugPubTypes();
523 /// \brief Emit visible names into a debug str section.
526 /// \brief Emit visible names into a debug loc section.
529 /// \brief Emit visible names into a debug aranges section.
530 void emitDebugARanges();
532 /// \brief Emit visible names into a debug ranges section.
533 void emitDebugRanges();
535 /// \brief Emit visible names into a debug macinfo section.
536 void emitDebugMacInfo();
538 /// \brief Emit inline info using custom format.
539 void emitDebugInlineInfo();
541 /// DWARF 5 Experimental Split Dwarf Emitters
543 /// \brief Construct the split debug info compile unit for the debug info
545 CompileUnit *constructSkeletonCU(const MDNode *);
547 /// \brief Emit the local split abbreviations.
548 void emitSkeletonAbbrevs(const MCSection *);
550 /// \brief Emit the debug info dwo section.
551 void emitDebugInfoDWO();
553 /// \brief Emit the debug abbrev dwo section.
554 void emitDebugAbbrevDWO();
556 /// \brief Emit the debug str dwo section.
557 void emitDebugStrDWO();
559 /// \brief Create new CompileUnit for the given metadata node with tag
560 /// DW_TAG_compile_unit.
561 CompileUnit *constructCompileUnit(const MDNode *N);
563 /// \brief Construct subprogram DIE.
564 void constructSubprogramDIE(CompileUnit *TheCU, const MDNode *N);
566 /// \brief Construct imported_module or imported_declaration DIE.
567 void constructImportedEntityDIE(CompileUnit *TheCU, const MDNode *N);
569 /// \brief Construct import_module DIE.
570 void constructImportedEntityDIE(CompileUnit *TheCU, const MDNode *N,
573 /// \brief Construct import_module DIE.
574 void constructImportedEntityDIE(CompileUnit *TheCU,
575 const DIImportedEntity &Module,
578 /// \brief Register a source line with debug info. Returns the unique
579 /// label that was emitted and which provides correspondence to the
580 /// source line list.
581 void recordSourceLine(unsigned Line, unsigned Col, const MDNode *Scope,
584 /// \brief Indentify instructions that are marking the beginning of or
585 /// ending of a scope.
586 void identifyScopeMarkers();
588 /// \brief If Var is an current function argument that add it in
589 /// CurrentFnArguments list.
590 bool addCurrentFnArgument(const MachineFunction *MF,
591 DbgVariable *Var, LexicalScope *Scope);
593 /// \brief Populate LexicalScope entries with variables' info.
594 void collectVariableInfo(const MachineFunction *,
595 SmallPtrSet<const MDNode *, 16> &ProcessedVars);
597 /// \brief Collect variable information from the side table maintained
599 void collectVariableInfoFromMMITable(const MachineFunction * MF,
600 SmallPtrSet<const MDNode *, 16> &P);
602 /// \brief Ensure that a label will be emitted before MI.
603 void requestLabelBeforeInsn(const MachineInstr *MI) {
604 LabelsBeforeInsn.insert(std::make_pair(MI, (MCSymbol*)0));
607 /// \brief Return Label preceding the instruction.
608 MCSymbol *getLabelBeforeInsn(const MachineInstr *MI);
610 /// \brief Ensure that a label will be emitted after MI.
611 void requestLabelAfterInsn(const MachineInstr *MI) {
612 LabelsAfterInsn.insert(std::make_pair(MI, (MCSymbol*)0));
615 /// \brief Return Label immediately following the instruction.
616 MCSymbol *getLabelAfterInsn(const MachineInstr *MI);
619 //===--------------------------------------------------------------------===//
620 // Main entry points.
622 DwarfDebug(AsmPrinter *A, Module *M);
625 /// \brief Emit all Dwarf sections that should come prior to the
629 /// \brief Emit all Dwarf sections that should come after the content.
632 /// \brief Gather pre-function debug information.
633 void beginFunction(const MachineFunction *MF);
635 /// \brief Gather and emit post-function debug information.
636 void endFunction(const MachineFunction *MF);
638 /// \brief Process beginning of an instruction.
639 void beginInstruction(const MachineInstr *MI);
641 /// \brief Process end of an instruction.
642 void endInstruction(const MachineInstr *MI);
644 /// \brief Look up the source id with the given directory and source file
645 /// names. If none currently exists, create a new id and insert it in the
647 unsigned getOrCreateSourceID(StringRef DirName, StringRef FullName,
650 /// \brief Recursively Emits a debug information entry.
651 void emitDIE(DIE *Die, std::vector<DIEAbbrev *> *Abbrevs);
653 /// \brief Returns whether or not to limit some of our debug
654 /// output to the limitations of darwin gdb.
655 bool useDarwinGDBCompat() { return IsDarwinGDBCompat; }
657 // Experimental DWARF5 features.
659 /// \brief Returns whether or not to emit tables that dwarf consumers can
660 /// use to accelerate lookup.
661 bool useDwarfAccelTables() { return HasDwarfAccelTables; }
663 /// \brief Returns whether or not to change the current debug info for the
664 /// split dwarf proposal support.
665 bool useSplitDwarf() { return HasSplitDwarf; }
667 /// Returns the Dwarf Version.
668 unsigned getDwarfVersion() const { return DwarfVersion; }
670 } // End of namespace llvm