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__
17 #include "llvm/CodeGen/AsmPrinter.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/FoldingSet.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/ADT/StringMap.h"
23 #include "llvm/ADT/UniqueVector.h"
24 #include "llvm/Support/Allocator.h"
29 class DbgConcreteScope;
32 class MachineFrameInfo;
33 class MachineLocation;
34 class MachineModuleInfo;
45 class DIGlobalVariable;
52 class DICompositeType;
54 //===----------------------------------------------------------------------===//
55 /// SrcLineInfo - This class is used to record source line correspondence.
58 unsigned Line; // Source line number.
59 unsigned Column; // Source column.
60 unsigned SourceID; // Source ID number.
61 MCSymbol *Label; // Label in code ID number.
63 SrcLineInfo(unsigned L, unsigned C, unsigned S, MCSymbol *label)
64 : Line(L), Column(C), SourceID(S), Label(label) {}
67 unsigned getLine() const { return Line; }
68 unsigned getColumn() const { return Column; }
69 unsigned getSourceID() const { return SourceID; }
70 MCSymbol *getLabel() const { return Label; }
74 /// Asm - Target of Dwarf emission.
77 /// MMI - Collected machine module information.
78 MachineModuleInfo *MMI;
80 //===--------------------------------------------------------------------===//
81 // Attributes used to construct specific Dwarf sections.
84 /// ModuleCU - All DIEs are inserted in ModuleCU.
85 CompileUnit *ModuleCU;
87 /// AbbreviationsSet - Used to uniquely define abbreviations.
89 FoldingSet<DIEAbbrev> AbbreviationsSet;
91 /// Abbreviations - A list of all the unique abbreviations in use.
93 std::vector<DIEAbbrev *> Abbreviations;
95 /// DirectoryIdMap - Directory name to directory id map.
97 StringMap<unsigned> DirectoryIdMap;
99 /// DirectoryNames - A list of directory names.
100 SmallVector<std::string, 8> DirectoryNames;
102 /// SourceFileIdMap - Source file name to source file id map.
104 StringMap<unsigned> SourceFileIdMap;
106 /// SourceFileNames - A list of source file names.
107 SmallVector<std::string, 8> SourceFileNames;
109 /// SourceIdMap - Source id map, i.e. pair of directory id and source file
110 /// id mapped to a unique id.
111 DenseMap<std::pair<unsigned, unsigned>, unsigned> SourceIdMap;
113 /// SourceIds - Reverse map from source id to directory id + file id pair.
115 SmallVector<std::pair<unsigned, unsigned>, 8> SourceIds;
117 /// Lines - List of source line correspondence.
118 std::vector<SrcLineInfo> Lines;
120 /// DIEBlocks - A list of all the DIEBlocks in use.
121 std::vector<DIEBlock *> DIEBlocks;
123 // DIEValueAllocator - All DIEValues are allocated through this allocator.
124 BumpPtrAllocator DIEValueAllocator;
126 /// StringPool - A String->Symbol mapping of strings used by indirect
128 StringMap<std::pair<MCSymbol*, unsigned> > StringPool;
129 unsigned NextStringPoolNumber;
131 MCSymbol *getStringPoolEntry(StringRef Str);
133 /// SectionMap - Provides a unique id per text section.
135 UniqueVector<const MCSection*> SectionMap;
137 /// SectionSourceLines - Tracks line numbers per text section.
139 std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
141 // CurrentFnDbgScope - Top level scope for the current function.
143 DbgScope *CurrentFnDbgScope;
145 /// DbgScopeMap - Tracks the scopes in the current function. Owns the
146 /// contained DbgScope*s.
148 DenseMap<MDNode *, DbgScope *> DbgScopeMap;
150 /// ConcreteScopes - Tracks the concrete scopees in the current function.
151 /// These scopes are also included in DbgScopeMap.
152 DenseMap<MDNode *, DbgScope *> ConcreteScopes;
154 /// AbstractScopes - Tracks the abstract scopes a module. These scopes are
155 /// not included DbgScopeMap. AbstractScopes owns its DbgScope*s.
156 DenseMap<MDNode *, DbgScope *> AbstractScopes;
158 /// AbstractScopesList - Tracks abstract scopes constructed while processing
159 /// a function. This list is cleared during endFunction().
160 SmallVector<DbgScope *, 4>AbstractScopesList;
162 /// AbstractVariables - Collection on abstract variables. Owned by the
163 /// DbgScopes in AbstractScopes.
164 DenseMap<MDNode *, DbgVariable *> AbstractVariables;
166 /// DbgValueStartMap - Tracks starting scope of variable DIEs.
167 /// If the scope of an object begins sometime after the low pc value for the
168 /// scope most closely enclosing the object, the object entry may have a
169 /// DW_AT_start_scope attribute.
170 DenseMap<const MachineInstr *, DbgVariable *> DbgValueStartMap;
172 /// InliendSubprogramDIEs - Collection of subprgram DIEs that are marked
173 /// (at the end of the module) as DW_AT_inline.
174 SmallPtrSet<DIE *, 4> InlinedSubprogramDIEs;
176 /// ContainingTypeMap - This map is used to keep track of subprogram DIEs that
177 /// need DW_AT_containing_type attribute. This attribute points to a DIE that
178 /// corresponds to the MDNode mapped with the subprogram DIE.
179 DenseMap<DIE *, MDNode *> ContainingTypeMap;
181 typedef SmallVector<DbgScope *, 2> ScopeVector;
182 SmallPtrSet<const MachineInstr *, 8> InsnsBeginScopeSet;
183 SmallPtrSet<const MachineInstr *, 8> InsnsEndScopeSet;
185 /// InlineInfo - Keep track of inlined functions and their location. This
186 /// information is used to populate debug_inlined section.
187 typedef std::pair<MCSymbol*, DIE *> InlineInfoLabels;
188 DenseMap<MDNode*, SmallVector<InlineInfoLabels, 4> > InlineInfo;
189 SmallVector<MDNode *, 4> InlinedSPNodes;
191 /// InsnBeforeLabelMap - Maps instruction with label emitted before
193 DenseMap<const MachineInstr *, MCSymbol *> InsnBeforeLabelMap;
195 /// InsnAfterLabelMap - Maps instruction with label emitted after
197 DenseMap<const MachineInstr *, MCSymbol *> InsnAfterLabelMap;
199 SmallVector<const MCSymbol *, 8> DebugRangeSymbols;
201 /// Previous instruction's location information. This is used to determine
202 /// label location to indicate scope boundries in dwarf debug info.
203 DebugLoc PrevInstLoc;
206 struct FunctionDebugFrameInfo {
208 std::vector<MachineMove> Moves;
210 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M)
211 : Number(Num), Moves(M) {}
214 std::vector<FunctionDebugFrameInfo> DebugFrames;
216 // Section Symbols: these are assembler temporary labels that are emitted at
217 // the beginning of each supported dwarf section. These are used to form
218 // section offsets and are created by EmitSectionLabels.
219 MCSymbol *DwarfFrameSectionSym, *DwarfInfoSectionSym, *DwarfAbbrevSectionSym;
220 MCSymbol *DwarfStrSectionSym, *TextSectionSym, *DwarfDebugRangeSectionSym;
224 /// getSourceDirectoryAndFileIds - Return the directory and file ids that
225 /// maps to the source id. Source id starts at 1.
226 std::pair<unsigned, unsigned>
227 getSourceDirectoryAndFileIds(unsigned SId) const {
228 return SourceIds[SId-1];
231 /// getNumSourceDirectories - Return the number of source directories in the
233 unsigned getNumSourceDirectories() const {
234 return DirectoryNames.size();
237 /// getSourceDirectoryName - Return the name of the directory corresponding
239 const std::string &getSourceDirectoryName(unsigned Id) const {
240 return DirectoryNames[Id - 1];
243 /// getSourceFileName - Return the name of the source file corresponding
245 const std::string &getSourceFileName(unsigned Id) const {
246 return SourceFileNames[Id - 1];
249 /// getNumSourceIds - Return the number of unique source ids.
250 unsigned getNumSourceIds() const {
251 return SourceIds.size();
254 /// assignAbbrevNumber - Define a unique number for the abbreviation.
256 void assignAbbrevNumber(DIEAbbrev &Abbrev);
258 /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
259 /// information entry.
260 DIEEntry *createDIEEntry(DIE *Entry);
262 /// addUInt - Add an unsigned integer attribute data and value.
264 void addUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer);
266 /// addSInt - Add an signed integer attribute data and value.
268 void addSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer);
270 /// addString - Add a string attribute data and value.
272 void addString(DIE *Die, unsigned Attribute, unsigned Form,
273 const StringRef Str);
275 /// addLabel - Add a Dwarf label attribute data and value.
277 void addLabel(DIE *Die, unsigned Attribute, unsigned Form,
278 const MCSymbol *Label);
280 /// addDelta - Add a label delta attribute data and value.
282 void addDelta(DIE *Die, unsigned Attribute, unsigned Form,
283 const MCSymbol *Hi, const MCSymbol *Lo);
285 /// addDIEEntry - Add a DIE attribute data and value.
287 void addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry);
289 /// addBlock - Add block data.
291 void addBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block);
293 /// addSourceLine - Add location information to specified debug information
295 void addSourceLine(DIE *Die, const DIVariable *V);
296 void addSourceLine(DIE *Die, const DIGlobal *G);
297 void addSourceLine(DIE *Die, const DISubprogram *SP);
298 void addSourceLine(DIE *Die, const DIType *Ty);
299 void addSourceLine(DIE *Die, const DINameSpace *NS);
301 /// addAddress - Add an address attribute to a die based on the location
303 void addAddress(DIE *Die, unsigned Attribute,
304 const MachineLocation &Location);
306 /// addComplexAddress - Start with the address based on the location provided,
307 /// and generate the DWARF information necessary to find the actual variable
308 /// (navigating the extra location information encoded in the type) based on
309 /// the starting location. Add the DWARF information to the die.
311 void addComplexAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
312 const MachineLocation &Location);
314 // FIXME: Should be reformulated in terms of addComplexAddress.
315 /// addBlockByrefAddress - Start with the address based on the location
316 /// provided, and generate the DWARF information necessary to find the
317 /// actual Block variable (navigating the Block struct) based on the
318 /// starting location. Add the DWARF information to the die. Obsolete,
319 /// please use addComplexAddress instead.
321 void addBlockByrefAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
322 const MachineLocation &Location);
324 /// addToContextOwner - Add Die into the list of its context owner's children.
325 void addToContextOwner(DIE *Die, DIDescriptor Context);
327 /// addType - Add a new type attribute to the specified entity.
328 void addType(DIE *Entity, DIType Ty);
331 /// getOrCreateNameSpace - Create a DIE for DINameSpace.
332 DIE *getOrCreateNameSpace(DINameSpace NS);
334 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
336 DIE *getOrCreateTypeDIE(DIType Ty);
338 void addPubTypes(DISubprogram SP);
340 /// constructTypeDIE - Construct basic type die from DIBasicType.
341 void constructTypeDIE(DIE &Buffer,
344 /// constructTypeDIE - Construct derived type die from DIDerivedType.
345 void constructTypeDIE(DIE &Buffer,
348 /// constructTypeDIE - Construct type DIE from DICompositeType.
349 void constructTypeDIE(DIE &Buffer,
350 DICompositeType CTy);
352 /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
353 void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
355 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
356 void constructArrayTypeDIE(DIE &Buffer,
357 DICompositeType *CTy);
359 /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
360 DIE *constructEnumTypeDIE(DIEnumerator ETy);
362 /// createGlobalVariableDIE - Create new DIE using GV.
363 DIE *createGlobalVariableDIE(const DIGlobalVariable &GV);
365 /// createMemberDIE - Create new member DIE.
366 DIE *createMemberDIE(const DIDerivedType &DT);
368 /// createSubprogramDIE - Create new DIE using SP.
369 DIE *createSubprogramDIE(const DISubprogram &SP, bool MakeDecl = false);
371 /// getUpdatedDbgScope - Find or create DbgScope assicated with
372 /// the instruction. Initialize scope and update scope hierarchy.
373 DbgScope *getUpdatedDbgScope(MDNode *N, const MachineInstr *MI,
376 /// createDbgScope - Create DbgScope for the scope.
377 void createDbgScope(MDNode *Scope, MDNode *InlinedAt);
379 DbgScope *getOrCreateAbstractScope(MDNode *N);
381 /// findAbstractVariable - Find abstract variable associated with Var.
382 DbgVariable *findAbstractVariable(DIVariable &Var, unsigned FrameIdx,
384 DbgVariable *findAbstractVariable(DIVariable &Var, const MachineInstr *MI,
387 /// updateSubprogramScopeDIE - Find DIE for the given subprogram and
388 /// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
389 /// If there are global variables in this scope then create and insert
390 /// DIEs for these variables.
391 DIE *updateSubprogramScopeDIE(MDNode *SPNode);
393 /// constructLexicalScope - Construct new DW_TAG_lexical_block
394 /// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
395 DIE *constructLexicalScopeDIE(DbgScope *Scope);
397 /// constructInlinedScopeDIE - This scope represents inlined body of
398 /// a function. Construct DIE to represent this concrete inlined copy
400 DIE *constructInlinedScopeDIE(DbgScope *Scope);
402 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
403 DIE *constructVariableDIE(DbgVariable *DV, DbgScope *S);
405 /// constructScopeDIE - Construct a DIE for this scope.
406 DIE *constructScopeDIE(DbgScope *Scope);
408 /// EmitSectionLabels - Emit initial Dwarf sections with a label at
409 /// the start of each one.
410 void EmitSectionLabels();
412 /// emitDIE - Recusively Emits a debug information entry.
414 void emitDIE(DIE *Die);
416 /// computeSizeAndOffset - Compute the size and offset of a DIE.
418 unsigned computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last);
420 /// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
422 void computeSizeAndOffsets();
424 /// EmitDebugInfo - Emit the debug info section.
426 void emitDebugInfo();
428 /// emitAbbreviations - Emit the abbreviation section.
430 void emitAbbreviations() const;
432 /// emitEndOfLineMatrix - Emit the last address of the section and the end of
435 void emitEndOfLineMatrix(unsigned SectionEnd);
437 /// emitDebugLines - Emit source line information.
439 void emitDebugLines();
441 /// emitCommonDebugFrame - Emit common frame info into a debug frame section.
443 void emitCommonDebugFrame();
445 /// emitFunctionDebugFrame - Emit per function frame info into a debug frame
447 void emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo);
449 /// emitDebugPubNames - Emit visible names into a debug pubnames section.
451 void emitDebugPubNames();
453 /// emitDebugPubTypes - Emit visible types into a debug pubtypes section.
455 void emitDebugPubTypes();
457 /// emitDebugStr - Emit visible names into a debug str section.
461 /// emitDebugLoc - Emit visible names into a debug loc section.
465 /// EmitDebugARanges - Emit visible names into a debug aranges section.
467 void EmitDebugARanges();
469 /// emitDebugRanges - Emit visible names into a debug ranges section.
471 void emitDebugRanges();
473 /// emitDebugMacInfo - Emit visible names into a debug macinfo section.
475 void emitDebugMacInfo();
477 /// emitDebugInlineInfo - Emit inline info using following format.
479 /// 1. length of section
480 /// 2. Dwarf version number
483 /// Entries (one "entry" for each function that was inlined):
485 /// 1. offset into __debug_str section for MIPS linkage name, if exists;
486 /// otherwise offset into __debug_str for regular function name.
487 /// 2. offset into __debug_str section for regular function name.
488 /// 3. an unsigned LEB128 number indicating the number of distinct inlining
489 /// instances for the function.
491 /// The rest of the entry consists of a {die_offset, low_pc} pair for each
492 /// inlined instance; the die_offset points to the inlined_subroutine die in
493 /// the __debug_info section, and the low_pc is the starting address for the
494 /// inlining instance.
495 void emitDebugInlineInfo();
497 /// GetOrCreateSourceID - Look up the source id with the given directory and
498 /// source file names. If none currently exists, create a new id and insert it
499 /// in the SourceIds map. This can update DirectoryNames and SourceFileNames
501 unsigned GetOrCreateSourceID(StringRef DirName, StringRef FileName);
503 void constructCompileUnit(MDNode *N);
505 void constructGlobalVariableDIE(MDNode *N);
507 void constructSubprogramDIE(MDNode *N);
509 // FIXME: This should go away in favor of complex addresses.
510 /// Find the type the programmer originally declared the variable to be
511 /// and return that type. Obsolete, use GetComplexAddrType instead.
513 DIType getBlockByrefType(DIType Ty, std::string Name);
515 /// recordSourceLine - Register a source line with debug info. Returns the
516 /// unique label that was emitted and which provides correspondence to
517 /// the source line list.
518 MCSymbol *recordSourceLine(unsigned Line, unsigned Col, MDNode *Scope);
520 /// getSourceLineCount - Return the number of source lines in the debug
522 unsigned getSourceLineCount() const {
526 /// identifyScopeMarkers() - Indentify instructions that are marking
527 /// beginning of or end of a scope.
528 void identifyScopeMarkers();
530 /// extractScopeInformation - Scan machine instructions in this function
531 /// and collect DbgScopes. Return true, if atleast one scope was found.
532 bool extractScopeInformation();
534 /// collectVariableInfo - Populate DbgScope entries with variables' info.
535 void collectVariableInfo();
538 //===--------------------------------------------------------------------===//
539 // Main entry points.
541 DwarfDebug(AsmPrinter *A, Module *M);
544 /// beginModule - Emit all Dwarf sections that should come prior to the
546 void beginModule(Module *M);
548 /// endModule - Emit all Dwarf sections that should come after the content.
552 /// beginFunction - Gather pre-function debug information. Assumes being
553 /// emitted immediately after the function entry point.
554 void beginFunction(const MachineFunction *MF);
556 /// endFunction - Gather and emit post-function debug information.
558 void endFunction(const MachineFunction *MF);
560 /// beginScope - Process beginning of a scope.
561 void beginScope(const MachineInstr *MI);
563 /// endScope - Prcess end of a scope.
564 void endScope(const MachineInstr *MI);
566 } // End of namespace llvm