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/DebugInfo.h"
19 #include "llvm/CodeGen/AsmPrinter.h"
20 #include "llvm/CodeGen/LexicalScopes.h"
21 #include "llvm/MC/MachineLocation.h"
22 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/ADT/FoldingSet.h"
24 #include "llvm/ADT/SmallPtrSet.h"
25 #include "llvm/ADT/StringMap.h"
26 #include "llvm/ADT/UniqueVector.h"
27 #include "llvm/Support/Allocator.h"
28 #include "llvm/Support/DebugLoc.h"
36 class MachineFrameInfo;
37 class MachineModuleInfo;
45 //===----------------------------------------------------------------------===//
46 /// SrcLineInfo - 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 /// DotDebugLocEntry - This struct describes location entries emitted in
65 /// .debug_loc section.
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 /// 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 /// DbgVariable - This class is used to track local variable information.
127 DIVariable Var; // Variable Descriptor.
128 DIE *TheDIE; // Variable DIE.
129 unsigned DotDebugLocOffset; // Offset in DotDebugLocEntries.
130 DbgVariable *AbsVar; // Corresponding Abstract variable, if any.
131 const MachineInstr *MInsn; // DBG_VALUE instruction of the variable.
134 // AbsVar may be NULL.
135 DbgVariable(DIVariable V, DbgVariable *AV)
136 : Var(V), TheDIE(0), DotDebugLocOffset(~0U), AbsVar(AV), MInsn(0),
140 DIVariable getVariable() const { return Var; }
141 void setDIE(DIE *D) { TheDIE = D; }
142 DIE *getDIE() const { return TheDIE; }
143 void setDotDebugLocOffset(unsigned O) { DotDebugLocOffset = O; }
144 unsigned getDotDebugLocOffset() const { return DotDebugLocOffset; }
145 StringRef getName() const { return Var.getName(); }
146 DbgVariable *getAbstractVariable() const { return AbsVar; }
147 const MachineInstr *getMInsn() const { return MInsn; }
148 void setMInsn(const MachineInstr *M) { MInsn = M; }
149 int getFrameIndex() const { return FrameIndex; }
150 void setFrameIndex(int FI) { FrameIndex = FI; }
151 // Translate tag to proper Dwarf tag.
152 unsigned getTag() const {
153 if (Var.getTag() == dwarf::DW_TAG_arg_variable)
154 return dwarf::DW_TAG_formal_parameter;
156 return dwarf::DW_TAG_variable;
158 /// isArtificial - Return true if DbgVariable is artificial.
159 bool isArtificial() const {
160 if (Var.isArtificial())
162 if (Var.getTag() == dwarf::DW_TAG_arg_variable
163 && getType().isArtificial())
168 bool isObjectPointer() const {
169 if (Var.isObjectPointer())
171 if (Var.getTag() == dwarf::DW_TAG_arg_variable
172 && getType().isObjectPointer())
177 bool variableHasComplexAddress() const {
178 assert(Var.Verify() && "Invalid complex DbgVariable!");
179 return Var.hasComplexAddress();
181 bool isBlockByrefVariable() const {
182 assert(Var.Verify() && "Invalid complex DbgVariable!");
183 return Var.isBlockByrefVariable();
185 unsigned getNumAddrElements() const {
186 assert(Var.Verify() && "Invalid complex DbgVariable!");
187 return Var.getNumAddrElements();
189 uint64_t getAddrElement(unsigned i) const {
190 return Var.getAddrElement(i);
192 DIType getType() const;
196 /// Asm - Target of Dwarf emission.
199 /// MMI - Collected machine module information.
200 MachineModuleInfo *MMI;
202 /// DIEValueAllocator - All DIEValues are allocated through this allocator.
203 BumpPtrAllocator DIEValueAllocator;
205 //===--------------------------------------------------------------------===//
206 // Attributes used to construct specific Dwarf sections.
209 CompileUnit *FirstCU;
211 /// Maps MDNode with its corresponding CompileUnit.
212 DenseMap <const MDNode *, CompileUnit *> CUMap;
214 /// Maps subprogram MDNode with its corresponding CompileUnit.
215 DenseMap <const MDNode *, CompileUnit *> SPMap;
217 /// AbbreviationsSet - Used to uniquely define abbreviations.
219 FoldingSet<DIEAbbrev> AbbreviationsSet;
221 /// Abbreviations - A list of all the unique abbreviations in use.
223 std::vector<DIEAbbrev *> Abbreviations;
225 /// SourceIdMap - Source id map, i.e. pair of source filename and directory,
226 /// separated by a zero byte, mapped to a unique id.
227 StringMap<unsigned, BumpPtrAllocator&> SourceIdMap;
229 /// StringPool - A String->Symbol mapping of strings used by indirect
231 StringMap<std::pair<MCSymbol*, unsigned>, BumpPtrAllocator&> StringPool;
232 unsigned NextStringPoolNumber;
234 /// SectionMap - Provides a unique id per text section.
236 UniqueVector<const MCSection*> SectionMap;
238 /// CurrentFnArguments - List of Arguments (DbgValues) for current function.
239 SmallVector<DbgVariable *, 8> CurrentFnArguments;
241 LexicalScopes LScopes;
243 /// AbstractSPDies - Collection of abstract subprogram DIEs.
244 DenseMap<const MDNode *, DIE *> AbstractSPDies;
246 /// ScopeVariables - Collection of dbg variables of a scope.
247 DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> > ScopeVariables;
249 /// AbstractVariables - Collection of abstract variables.
250 DenseMap<const MDNode *, DbgVariable *> AbstractVariables;
252 /// DotDebugLocEntries - Collection of DotDebugLocEntry.
253 SmallVector<DotDebugLocEntry, 4> DotDebugLocEntries;
255 /// InlinedSubprogramDIEs - Collection of subprogram DIEs that are marked
256 /// (at the end of the module) as DW_AT_inline.
257 SmallPtrSet<DIE *, 4> InlinedSubprogramDIEs;
259 /// InlineInfo - Keep track of inlined functions and their location. This
260 /// information is used to populate the debug_inlined section.
261 typedef std::pair<const MCSymbol *, DIE *> InlineInfoLabels;
262 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> > InlineInfo;
263 SmallVector<const MDNode *, 4> InlinedSPNodes;
265 // ProcessedSPNodes - This is a collection of subprogram MDNodes that
266 // are processed to create DIEs.
267 SmallPtrSet<const MDNode *, 16> ProcessedSPNodes;
269 /// LabelsBeforeInsn - Maps instruction with label emitted before
271 DenseMap<const MachineInstr *, MCSymbol *> LabelsBeforeInsn;
273 /// LabelsAfterInsn - Maps instruction with label emitted after
275 DenseMap<const MachineInstr *, MCSymbol *> LabelsAfterInsn;
277 /// UserVariables - Every user variable mentioned by a DBG_VALUE instruction
278 /// in order of appearance.
279 SmallVector<const MDNode*, 8> UserVariables;
281 /// DbgValues - For each user variable, keep a list of DBG_VALUE
282 /// instructions in order. The list can also contain normal instructions that
283 /// clobber the previous DBG_VALUE.
284 typedef DenseMap<const MDNode*, SmallVector<const MachineInstr*, 4> >
286 DbgValueHistoryMap DbgValues;
288 SmallVector<const MCSymbol *, 8> DebugRangeSymbols;
290 /// Previous instruction's location information. This is used to determine
291 /// label location to indicate scope boundries in dwarf debug info.
292 DebugLoc PrevInstLoc;
295 /// PrologEndLoc - This location indicates end of function prologue and
296 /// beginning of function body.
297 DebugLoc PrologEndLoc;
299 struct FunctionDebugFrameInfo {
301 std::vector<MachineMove> Moves;
303 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M)
304 : Number(Num), Moves(M) {}
307 std::vector<FunctionDebugFrameInfo> DebugFrames;
309 // Section Symbols: these are assembler temporary labels that are emitted at
310 // the beginning of each supported dwarf section. These are used to form
311 // section offsets and are created by EmitSectionLabels.
312 MCSymbol *DwarfInfoSectionSym, *DwarfAbbrevSectionSym;
313 MCSymbol *DwarfStrSectionSym, *TextSectionSym, *DwarfDebugRangeSectionSym;
314 MCSymbol *DwarfDebugLocSectionSym;
315 MCSymbol *FunctionBeginSym, *FunctionEndSym;
317 // As an optimization, there is no need to emit an entry in the directory
318 // table for the same directory as DW_at_comp_dir.
319 StringRef CompilationDir;
321 // A holder for the DarwinGDBCompat flag so that the compile unit can use it.
322 bool isDarwinGDBCompat;
323 bool hasDwarfAccelTables;
326 /// assignAbbrevNumber - Define a unique number for the abbreviation.
328 void assignAbbrevNumber(DIEAbbrev &Abbrev);
330 void addScopeVariable(LexicalScope *LS, DbgVariable *Var);
332 /// findAbstractVariable - Find abstract variable associated with Var.
333 DbgVariable *findAbstractVariable(DIVariable &Var, DebugLoc Loc);
335 /// updateSubprogramScopeDIE - Find DIE for the given subprogram and
336 /// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
337 /// If there are global variables in this scope then create and insert
338 /// DIEs for these variables.
339 DIE *updateSubprogramScopeDIE(CompileUnit *SPCU, const MDNode *SPNode);
341 /// constructLexicalScope - Construct new DW_TAG_lexical_block
342 /// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
343 DIE *constructLexicalScopeDIE(CompileUnit *TheCU, LexicalScope *Scope);
345 /// constructInlinedScopeDIE - This scope represents inlined body of
346 /// a function. Construct DIE to represent this concrete inlined copy
348 DIE *constructInlinedScopeDIE(CompileUnit *TheCU, LexicalScope *Scope);
350 /// constructScopeDIE - Construct a DIE for this scope.
351 DIE *constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope);
353 /// EmitSectionLabels - Emit initial Dwarf sections with a label at
354 /// the start of each one.
355 void EmitSectionLabels();
357 /// emitDIE - Recursively Emits a debug information entry.
359 void emitDIE(DIE *Die);
361 /// computeSizeAndOffset - Compute the size and offset of a DIE.
363 unsigned computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last);
365 /// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
367 void computeSizeAndOffsets();
369 /// EmitDebugInfo - Emit the debug info section.
371 void emitDebugInfo();
373 /// emitAbbreviations - Emit the abbreviation section.
375 void emitAbbreviations() const;
377 /// emitEndOfLineMatrix - Emit the last address of the section and the end of
380 void emitEndOfLineMatrix(unsigned SectionEnd);
382 /// emitAccelNames - Emit visible names into a hashed accelerator table
384 void emitAccelNames();
386 /// emitAccelObjC - Emit objective C classes and categories into a hashed
387 /// accelerator table section.
388 void emitAccelObjC();
390 /// emitAccelNamespace - Emit namespace dies into a hashed accelerator
392 void emitAccelNamespaces();
394 /// emitAccelTypes() - Emit type dies into a hashed accelerator table.
396 void emitAccelTypes();
398 /// emitDebugPubTypes - Emit visible types into a debug pubtypes section.
400 void emitDebugPubTypes();
402 /// emitDebugStr - Emit visible names into a debug str section.
406 /// emitDebugLoc - Emit visible names into a debug loc section.
410 /// EmitDebugARanges - Emit visible names into a debug aranges section.
412 void EmitDebugARanges();
414 /// emitDebugRanges - Emit visible names into a debug ranges section.
416 void emitDebugRanges();
418 /// emitDebugMacInfo - Emit visible names into a debug macinfo section.
420 void emitDebugMacInfo();
422 /// emitDebugInlineInfo - Emit inline info using following format.
424 /// 1. length of section
425 /// 2. Dwarf version number
428 /// Entries (one "entry" for each function that was inlined):
430 /// 1. offset into __debug_str section for MIPS linkage name, if exists;
431 /// otherwise offset into __debug_str for regular function name.
432 /// 2. offset into __debug_str section for regular function name.
433 /// 3. an unsigned LEB128 number indicating the number of distinct inlining
434 /// instances for the function.
436 /// The rest of the entry consists of a {die_offset, low_pc} pair for each
437 /// inlined instance; the die_offset points to the inlined_subroutine die in
438 /// the __debug_info section, and the low_pc is the starting address for the
439 /// inlining instance.
440 void emitDebugInlineInfo();
442 /// constructCompileUnit - Create new CompileUnit for the given
443 /// metadata node with tag DW_TAG_compile_unit.
444 CompileUnit *constructCompileUnit(const MDNode *N);
446 /// construct SubprogramDIE - Construct subprogram DIE.
447 void constructSubprogramDIE(CompileUnit *TheCU, const MDNode *N);
449 /// recordSourceLine - Register a source line with debug info. Returns the
450 /// unique label that was emitted and which provides correspondence to
451 /// the source line list.
452 void recordSourceLine(unsigned Line, unsigned Col, const MDNode *Scope,
455 /// identifyScopeMarkers() - Indentify instructions that are marking the
456 /// beginning of or ending of a scope.
457 void identifyScopeMarkers();
459 /// addCurrentFnArgument - If Var is an current function argument that add
460 /// it in CurrentFnArguments list.
461 bool addCurrentFnArgument(const MachineFunction *MF,
462 DbgVariable *Var, LexicalScope *Scope);
464 /// collectVariableInfo - Populate LexicalScope entries with variables' info.
465 void collectVariableInfo(const MachineFunction *,
466 SmallPtrSet<const MDNode *, 16> &ProcessedVars);
468 /// collectVariableInfoFromMMITable - Collect variable information from
469 /// side table maintained by MMI.
470 void collectVariableInfoFromMMITable(const MachineFunction * MF,
471 SmallPtrSet<const MDNode *, 16> &P);
473 /// requestLabelBeforeInsn - Ensure that a label will be emitted before MI.
474 void requestLabelBeforeInsn(const MachineInstr *MI) {
475 LabelsBeforeInsn.insert(std::make_pair(MI, (MCSymbol*)0));
478 /// getLabelBeforeInsn - Return Label preceding the instruction.
479 const MCSymbol *getLabelBeforeInsn(const MachineInstr *MI);
481 /// requestLabelAfterInsn - Ensure that a label will be emitted after MI.
482 void requestLabelAfterInsn(const MachineInstr *MI) {
483 LabelsAfterInsn.insert(std::make_pair(MI, (MCSymbol*)0));
486 /// getLabelAfterInsn - Return Label immediately following the instruction.
487 const MCSymbol *getLabelAfterInsn(const MachineInstr *MI);
490 //===--------------------------------------------------------------------===//
491 // Main entry points.
493 DwarfDebug(AsmPrinter *A, Module *M);
496 /// collectInfoFromNamedMDNodes - Collect debug info from named mdnodes such
497 /// as llvm.dbg.enum and llvm.dbg.ty
498 void collectInfoFromNamedMDNodes(Module *M);
500 /// collectLegacyDebugInfo - Collect debug info using DebugInfoFinder.
501 /// FIXME - Remove this when DragonEgg switches to DIBuilder.
502 bool collectLegacyDebugInfo(Module *M);
504 /// beginModule - Emit all Dwarf sections that should come prior to the
506 void beginModule(Module *M);
508 /// endModule - Emit all Dwarf sections that should come after the content.
512 /// beginFunction - Gather pre-function debug information. Assumes being
513 /// emitted immediately after the function entry point.
514 void beginFunction(const MachineFunction *MF);
516 /// endFunction - Gather and emit post-function debug information.
518 void endFunction(const MachineFunction *MF);
520 /// beginInstruction - Process beginning of an instruction.
521 void beginInstruction(const MachineInstr *MI);
523 /// endInstruction - Prcess end of an instruction.
524 void endInstruction(const MachineInstr *MI);
526 /// GetOrCreateSourceID - Look up the source id with the given directory and
527 /// source file names. If none currently exists, create a new id and insert it
528 /// in the SourceIds map.
529 unsigned GetOrCreateSourceID(StringRef DirName, StringRef FullName);
531 /// getStringPool - returns the entry into the start of the pool.
532 MCSymbol *getStringPool();
534 /// getStringPoolEntry - returns an entry into the string pool with the given
536 MCSymbol *getStringPoolEntry(StringRef Str);
538 /// useDarwinGDBCompat - returns whether or not to limit some of our debug
539 /// output to the limitations of darwin gdb.
540 bool useDarwinGDBCompat() { return isDarwinGDBCompat; }
541 bool useDwarfAccelTables() { return hasDwarfAccelTables; }
543 } // End of namespace llvm