DwarfDebug.h revision bbf90b91a1424e74b007b78316ebc3f5d8a2838b
1//===-- llvm/CodeGen/DwarfDebug.h - Dwarf Debug Framework ------*- C++ -*--===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing dwarf debug info into asm files.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef CODEGEN_ASMPRINTER_DWARFDEBUG_H__
15#define CODEGEN_ASMPRINTER_DWARFDEBUG_H__
16
17#include "llvm/CodeGen/AsmPrinter.h"
18#include "DIE.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"
25
26namespace llvm {
27
28class CompileUnit;
29class DbgConcreteScope;
30class DbgScope;
31class DbgVariable;
32class MachineFrameInfo;
33class MachineLocation;
34class MachineModuleInfo;
35class MCAsmInfo;
36class DIEAbbrev;
37class DIE;
38class DIEBlock;
39class DIEEntry;
40
41class DIEnumerator;
42class DIDescriptor;
43class DIVariable;
44class DIGlobal;
45class DIGlobalVariable;
46class DISubprogram;
47class DIBasicType;
48class DIDerivedType;
49class DIType;
50class DINameSpace;
51class DISubrange;
52class DICompositeType;
53
54//===----------------------------------------------------------------------===//
55/// SrcLineInfo - This class is used to record source line correspondence.
56///
57class SrcLineInfo {
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.
62public:
63  SrcLineInfo(unsigned L, unsigned C, unsigned S, MCSymbol *label)
64    : Line(L), Column(C), SourceID(S), Label(label) {}
65
66  // Accessors
67  unsigned getLine() const { return Line; }
68  unsigned getColumn() const { return Column; }
69  unsigned getSourceID() const { return SourceID; }
70  MCSymbol *getLabel() const { return Label; }
71};
72
73class DwarfDebug {
74  /// Asm - Target of Dwarf emission.
75  AsmPrinter *Asm;
76
77  /// MMI - Collected machine module information.
78  MachineModuleInfo *MMI;
79
80  //===--------------------------------------------------------------------===//
81  // Attributes used to construct specific Dwarf sections.
82  //
83
84  /// ModuleCU - All DIEs are inserted in ModuleCU.
85  CompileUnit *ModuleCU;
86
87  /// AbbreviationsSet - Used to uniquely define abbreviations.
88  ///
89  FoldingSet<DIEAbbrev> AbbreviationsSet;
90
91  /// Abbreviations - A list of all the unique abbreviations in use.
92  ///
93  std::vector<DIEAbbrev *> Abbreviations;
94
95  /// DirectoryIdMap - Directory name to directory id map.
96  ///
97  StringMap<unsigned> DirectoryIdMap;
98
99  /// DirectoryNames - A list of directory names.
100  SmallVector<std::string, 8> DirectoryNames;
101
102  /// SourceFileIdMap - Source file name to source file id map.
103  ///
104  StringMap<unsigned> SourceFileIdMap;
105
106  /// SourceFileNames - A list of source file names.
107  SmallVector<std::string, 8> SourceFileNames;
108
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;
112
113  /// SourceIds - Reverse map from source id to directory id + file id pair.
114  ///
115  SmallVector<std::pair<unsigned, unsigned>, 8> SourceIds;
116
117  /// Lines - List of source line correspondence.
118  std::vector<SrcLineInfo> Lines;
119
120  /// DIEBlocks - A list of all the DIEBlocks in use.
121  std::vector<DIEBlock *> DIEBlocks;
122
123  // DIEValueAllocator - All DIEValues are allocated through this allocator.
124  BumpPtrAllocator DIEValueAllocator;
125
126  /// StringPool - A String->Symbol mapping of strings used by indirect
127  /// references.
128  StringMap<std::pair<MCSymbol*, unsigned> > StringPool;
129  unsigned NextStringPoolNumber;
130
131  MCSymbol *getStringPoolEntry(StringRef Str);
132
133  /// SectionMap - Provides a unique id per text section.
134  ///
135  UniqueVector<const MCSection*> SectionMap;
136
137  /// SectionSourceLines - Tracks line numbers per text section.
138  ///
139  std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
140
141  // CurrentFnDbgScope - Top level scope for the current function.
142  //
143  DbgScope *CurrentFnDbgScope;
144
145  /// DbgScopeMap - Tracks the scopes in the current function.  Owns the
146  /// contained DbgScope*s.
147  ///
148  DenseMap<MDNode *, DbgScope *> DbgScopeMap;
149
150  /// ConcreteScopes - Tracks the concrete scopees in the current function.
151  /// These scopes are also included in DbgScopeMap.
152  DenseMap<MDNode *, DbgScope *> ConcreteScopes;
153
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;
157
158  /// AbstractScopesList - Tracks abstract scopes constructed while processing
159  /// a function. This list is cleared during endFunction().
160  SmallVector<DbgScope *, 4>AbstractScopesList;
161
162  /// AbstractVariables - Collection on abstract variables.  Owned by the
163  /// DbgScopes in AbstractScopes.
164  DenseMap<MDNode *, DbgVariable *> AbstractVariables;
165
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;
171
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;
175
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;
180
181  typedef SmallVector<DbgScope *, 2> ScopeVector;
182  SmallPtrSet<const MachineInstr *, 8> InsnsBeginScopeSet;
183  SmallPtrSet<const MachineInstr *, 8> InsnsEndScopeSet;
184
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;
190
191  /// LabelsBeforeInsn - Maps instruction with label emitted before
192  /// instruction.
193  DenseMap<const MachineInstr *, MCSymbol *> LabelsBeforeInsn;
194
195  /// LabelsAfterInsn - Maps instruction with label emitted after
196  /// instruction.
197  DenseMap<const MachineInstr *, MCSymbol *> LabelsAfterInsn;
198
199  SmallVector<const MCSymbol *, 8> DebugRangeSymbols;
200  DenseMap<const MCSymbol *, const MCSymbol *> WeakDebugRangeSymbols;
201
202  /// Previous instruction's location information. This is used to determine
203  /// label location to indicate scope boundries in dwarf debug info.
204  DebugLoc PrevInstLoc;
205  MCSymbol *PrevLabel;
206
207  struct FunctionDebugFrameInfo {
208    unsigned Number;
209    std::vector<MachineMove> Moves;
210
211    FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M)
212      : Number(Num), Moves(M) {}
213  };
214
215  std::vector<FunctionDebugFrameInfo> DebugFrames;
216
217  // Section Symbols: these are assembler temporary labels that are emitted at
218  // the beginning of each supported dwarf section.  These are used to form
219  // section offsets and are created by EmitSectionLabels.
220  MCSymbol *DwarfFrameSectionSym, *DwarfInfoSectionSym, *DwarfAbbrevSectionSym;
221  MCSymbol *DwarfStrSectionSym, *TextSectionSym, *DwarfDebugRangeSectionSym;
222
223  MCSymbol *FunctionBeginSym;
224private:
225
226  /// getSourceDirectoryAndFileIds - Return the directory and file ids that
227  /// maps to the source id. Source id starts at 1.
228  std::pair<unsigned, unsigned>
229  getSourceDirectoryAndFileIds(unsigned SId) const {
230    return SourceIds[SId-1];
231  }
232
233  /// getNumSourceDirectories - Return the number of source directories in the
234  /// debug info.
235  unsigned getNumSourceDirectories() const {
236    return DirectoryNames.size();
237  }
238
239  /// getSourceDirectoryName - Return the name of the directory corresponding
240  /// to the id.
241  const std::string &getSourceDirectoryName(unsigned Id) const {
242    return DirectoryNames[Id - 1];
243  }
244
245  /// getSourceFileName - Return the name of the source file corresponding
246  /// to the id.
247  const std::string &getSourceFileName(unsigned Id) const {
248    return SourceFileNames[Id - 1];
249  }
250
251  /// getNumSourceIds - Return the number of unique source ids.
252  unsigned getNumSourceIds() const {
253    return SourceIds.size();
254  }
255
256  /// assignAbbrevNumber - Define a unique number for the abbreviation.
257  ///
258  void assignAbbrevNumber(DIEAbbrev &Abbrev);
259
260  /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
261  /// information entry.
262  DIEEntry *createDIEEntry(DIE *Entry);
263
264  /// addUInt - Add an unsigned integer attribute data and value.
265  ///
266  void addUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer);
267
268  /// addSInt - Add an signed integer attribute data and value.
269  ///
270  void addSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer);
271
272  /// addString - Add a string attribute data and value.
273  ///
274  void addString(DIE *Die, unsigned Attribute, unsigned Form,
275                 const StringRef Str);
276
277  /// addLabel - Add a Dwarf label attribute data and value.
278  ///
279  void addLabel(DIE *Die, unsigned Attribute, unsigned Form,
280                const MCSymbol *Label);
281
282  /// addDelta - Add a label delta attribute data and value.
283  ///
284  void addDelta(DIE *Die, unsigned Attribute, unsigned Form,
285                const MCSymbol *Hi, const MCSymbol *Lo);
286
287  /// addDIEEntry - Add a DIE attribute data and value.
288  ///
289  void addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry);
290
291  /// addBlock - Add block data.
292  ///
293  void addBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block);
294
295  /// addSourceLine - Add location information to specified debug information
296  /// entry.
297  void addSourceLine(DIE *Die, const DIVariable *V);
298  void addSourceLine(DIE *Die, const DIGlobal *G);
299  void addSourceLine(DIE *Die, const DISubprogram *SP);
300  void addSourceLine(DIE *Die, const DIType *Ty);
301  void addSourceLine(DIE *Die, const DINameSpace *NS);
302
303  /// addAddress - Add an address attribute to a die based on the location
304  /// provided.
305  void addAddress(DIE *Die, unsigned Attribute,
306                  const MachineLocation &Location);
307
308  /// addComplexAddress - Start with the address based on the location provided,
309  /// and generate the DWARF information necessary to find the actual variable
310  /// (navigating the extra location information encoded in the type) based on
311  /// the starting location.  Add the DWARF information to the die.
312  ///
313  void addComplexAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
314                         const MachineLocation &Location);
315
316  // FIXME: Should be reformulated in terms of addComplexAddress.
317  /// addBlockByrefAddress - Start with the address based on the location
318  /// provided, and generate the DWARF information necessary to find the
319  /// actual Block variable (navigating the Block struct) based on the
320  /// starting location.  Add the DWARF information to the die.  Obsolete,
321  /// please use addComplexAddress instead.
322  ///
323  void addBlockByrefAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
324                            const MachineLocation &Location);
325
326  /// addToContextOwner - Add Die into the list of its context owner's children.
327  void addToContextOwner(DIE *Die, DIDescriptor Context);
328
329  /// addType - Add a new type attribute to the specified entity.
330  void addType(DIE *Entity, DIType Ty);
331
332
333  /// getOrCreateNameSpace - Create a DIE for DINameSpace.
334  DIE *getOrCreateNameSpace(DINameSpace NS);
335
336  /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
337  /// given DIType.
338  DIE *getOrCreateTypeDIE(DIType Ty);
339
340  void addPubTypes(DISubprogram SP);
341
342  /// constructTypeDIE - Construct basic type die from DIBasicType.
343  void constructTypeDIE(DIE &Buffer,
344                        DIBasicType BTy);
345
346  /// constructTypeDIE - Construct derived type die from DIDerivedType.
347  void constructTypeDIE(DIE &Buffer,
348                        DIDerivedType DTy);
349
350  /// constructTypeDIE - Construct type DIE from DICompositeType.
351  void constructTypeDIE(DIE &Buffer,
352                        DICompositeType CTy);
353
354  /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
355  void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
356
357  /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
358  void constructArrayTypeDIE(DIE &Buffer,
359                             DICompositeType *CTy);
360
361  /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
362  DIE *constructEnumTypeDIE(DIEnumerator ETy);
363
364  /// createGlobalVariableDIE - Create new DIE using GV.
365  DIE *createGlobalVariableDIE(const DIGlobalVariable &GV);
366
367  /// createMemberDIE - Create new member DIE.
368  DIE *createMemberDIE(const DIDerivedType &DT);
369
370  /// createSubprogramDIE - Create new DIE using SP.
371  DIE *createSubprogramDIE(const DISubprogram &SP, bool MakeDecl = false);
372
373  /// getOrCreateDbgScope - Create DbgScope for the scope.
374  DbgScope *getOrCreateDbgScope(MDNode *Scope, MDNode *InlinedAt);
375
376  DbgScope *getOrCreateAbstractScope(MDNode *N);
377
378  /// findAbstractVariable - Find abstract variable associated with Var.
379  DbgVariable *findAbstractVariable(DIVariable &Var, unsigned FrameIdx,
380                                    DebugLoc Loc);
381  DbgVariable *findAbstractVariable(DIVariable &Var, const MachineInstr *MI,
382                                    DebugLoc Loc);
383
384  /// updateSubprogramScopeDIE - Find DIE for the given subprogram and
385  /// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
386  /// If there are global variables in this scope then create and insert
387  /// DIEs for these variables.
388  DIE *updateSubprogramScopeDIE(MDNode *SPNode);
389
390  /// constructLexicalScope - Construct new DW_TAG_lexical_block
391  /// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
392  DIE *constructLexicalScopeDIE(DbgScope *Scope);
393
394  /// constructInlinedScopeDIE - This scope represents inlined body of
395  /// a function. Construct DIE to represent this concrete inlined copy
396  /// of the function.
397  DIE *constructInlinedScopeDIE(DbgScope *Scope);
398
399  /// constructVariableDIE - Construct a DIE for the given DbgVariable.
400  DIE *constructVariableDIE(DbgVariable *DV, DbgScope *S);
401
402  /// constructScopeDIE - Construct a DIE for this scope.
403  DIE *constructScopeDIE(DbgScope *Scope);
404
405  /// EmitSectionLabels - Emit initial Dwarf sections with a label at
406  /// the start of each one.
407  void EmitSectionLabels();
408
409  /// emitDIE - Recusively Emits a debug information entry.
410  ///
411  void emitDIE(DIE *Die);
412
413  /// computeSizeAndOffset - Compute the size and offset of a DIE.
414  ///
415  unsigned computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last);
416
417  /// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
418  ///
419  void computeSizeAndOffsets();
420
421  /// EmitDebugInfo - Emit the debug info section.
422  ///
423  void emitDebugInfo();
424
425  /// emitAbbreviations - Emit the abbreviation section.
426  ///
427  void emitAbbreviations() const;
428
429  /// emitEndOfLineMatrix - Emit the last address of the section and the end of
430  /// the line matrix.
431  ///
432  void emitEndOfLineMatrix(unsigned SectionEnd);
433
434  /// emitDebugLines - Emit source line information.
435  ///
436  void emitDebugLines();
437
438  /// emitCommonDebugFrame - Emit common frame info into a debug frame section.
439  ///
440  void emitCommonDebugFrame();
441
442  /// emitFunctionDebugFrame - Emit per function frame info into a debug frame
443  /// section.
444  void emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo);
445
446  /// emitDebugPubNames - Emit visible names into a debug pubnames section.
447  ///
448  void emitDebugPubNames();
449
450  /// emitDebugPubTypes - Emit visible types into a debug pubtypes section.
451  ///
452  void emitDebugPubTypes();
453
454  /// emitDebugStr - Emit visible names into a debug str section.
455  ///
456  void emitDebugStr();
457
458  /// emitDebugLoc - Emit visible names into a debug loc section.
459  ///
460  void emitDebugLoc();
461
462  /// EmitDebugARanges - Emit visible names into a debug aranges section.
463  ///
464  void EmitDebugARanges();
465
466  /// emitDebugRanges - Emit visible names into a debug ranges section.
467  ///
468  void emitDebugRanges();
469
470  /// emitDebugMacInfo - Emit visible names into a debug macinfo section.
471  ///
472  void emitDebugMacInfo();
473
474  /// emitDebugInlineInfo - Emit inline info using following format.
475  /// Section Header:
476  /// 1. length of section
477  /// 2. Dwarf version number
478  /// 3. address size.
479  ///
480  /// Entries (one "entry" for each function that was inlined):
481  ///
482  /// 1. offset into __debug_str section for MIPS linkage name, if exists;
483  ///   otherwise offset into __debug_str for regular function name.
484  /// 2. offset into __debug_str section for regular function name.
485  /// 3. an unsigned LEB128 number indicating the number of distinct inlining
486  /// instances for the function.
487  ///
488  /// The rest of the entry consists of a {die_offset, low_pc}  pair for each
489  /// inlined instance; the die_offset points to the inlined_subroutine die in
490  /// the __debug_info section, and the low_pc is the starting address  for the
491  ///  inlining instance.
492  void emitDebugInlineInfo();
493
494  /// GetOrCreateSourceID - Look up the source id with the given directory and
495  /// source file names. If none currently exists, create a new id and insert it
496  /// in the SourceIds map. This can update DirectoryNames and SourceFileNames
497  /// maps as well.
498  unsigned GetOrCreateSourceID(StringRef DirName, StringRef FileName);
499
500  void constructCompileUnit(MDNode *N);
501
502  void constructGlobalVariableDIE(MDNode *N);
503
504  void constructSubprogramDIE(MDNode *N);
505
506  // FIXME: This should go away in favor of complex addresses.
507  /// Find the type the programmer originally declared the variable to be
508  /// and return that type.  Obsolete, use GetComplexAddrType instead.
509  ///
510  DIType getBlockByrefType(DIType Ty, std::string Name);
511
512  /// recordSourceLine - Register a source line with debug info. Returns the
513  /// unique label that was emitted and which provides correspondence to
514  /// the source line list.
515  MCSymbol *recordSourceLine(unsigned Line, unsigned Col, MDNode *Scope);
516
517  /// getSourceLineCount - Return the number of source lines in the debug
518  /// info.
519  unsigned getSourceLineCount() const {
520    return Lines.size();
521  }
522
523  /// identifyScopeMarkers() - Indentify instructions that are marking
524  /// beginning of or end of a scope.
525  void identifyScopeMarkers();
526
527  /// extractScopeInformation - Scan machine instructions in this function
528  /// and collect DbgScopes. Return true, if atleast one scope was found.
529  bool extractScopeInformation();
530
531  /// collectVariableInfo - Populate DbgScope entries with variables' info.
532  void collectVariableInfo();
533
534public:
535  //===--------------------------------------------------------------------===//
536  // Main entry points.
537  //
538  DwarfDebug(AsmPrinter *A, Module *M);
539  ~DwarfDebug();
540
541  /// beginModule - Emit all Dwarf sections that should come prior to the
542  /// content.
543  void beginModule(Module *M);
544
545  /// endModule - Emit all Dwarf sections that should come after the content.
546  ///
547  void endModule();
548
549  /// beginFunction - Gather pre-function debug information.  Assumes being
550  /// emitted immediately after the function entry point.
551  void beginFunction(const MachineFunction *MF);
552
553  /// endFunction - Gather and emit post-function debug information.
554  ///
555  void endFunction(const MachineFunction *MF);
556
557  /// beginScope - Process beginning of a scope.
558  void beginScope(const MachineInstr *MI);
559
560  /// endScope - Prcess end of a scope.
561  void endScope(const MachineInstr *MI);
562};
563} // End of namespace llvm
564
565#endif
566