DwarfDebug.h revision 4942c4b48d0756df697bf4e488b95dc498170d6e
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 MachineOperand;
36class MCAsmInfo;
37class DIEAbbrev;
38class DIE;
39class DIEBlock;
40class DIEEntry;
41
42class DIEnumerator;
43class DIDescriptor;
44class DIVariable;
45class DIGlobal;
46class DIGlobalVariable;
47class DISubprogram;
48class DIBasicType;
49class DIDerivedType;
50class DIType;
51class DINameSpace;
52class DISubrange;
53class DICompositeType;
54
55//===----------------------------------------------------------------------===//
56/// SrcLineInfo - This class is used to record source line correspondence.
57///
58class SrcLineInfo {
59  unsigned Line;                     // Source line number.
60  unsigned Column;                   // Source column.
61  unsigned SourceID;                 // Source ID number.
62  MCSymbol *Label;                   // Label in code ID number.
63public:
64  SrcLineInfo(unsigned L, unsigned C, unsigned S, MCSymbol *label)
65    : Line(L), Column(C), SourceID(S), Label(label) {}
66
67  // Accessors
68  unsigned getLine() const { return Line; }
69  unsigned getColumn() const { return Column; }
70  unsigned getSourceID() const { return SourceID; }
71  MCSymbol *getLabel() const { return Label; }
72};
73
74class DwarfDebug {
75  /// Asm - Target of Dwarf emission.
76  AsmPrinter *Asm;
77
78  /// MMI - Collected machine module information.
79  MachineModuleInfo *MMI;
80
81  //===--------------------------------------------------------------------===//
82  // Attributes used to construct specific Dwarf sections.
83  //
84
85  /// ModuleCU - All DIEs are inserted in ModuleCU.
86  CompileUnit *ModuleCU;
87
88  /// AbbreviationsSet - Used to uniquely define abbreviations.
89  ///
90  FoldingSet<DIEAbbrev> AbbreviationsSet;
91
92  /// Abbreviations - A list of all the unique abbreviations in use.
93  ///
94  std::vector<DIEAbbrev *> Abbreviations;
95
96  /// DirectoryIdMap - Directory name to directory id map.
97  ///
98  StringMap<unsigned> DirectoryIdMap;
99
100  /// DirectoryNames - A list of directory names.
101  SmallVector<std::string, 8> DirectoryNames;
102
103  /// SourceFileIdMap - Source file name to source file id map.
104  ///
105  StringMap<unsigned> SourceFileIdMap;
106
107  /// SourceFileNames - A list of source file names.
108  SmallVector<std::string, 8> SourceFileNames;
109
110  /// SourceIdMap - Source id map, i.e. pair of directory id and source file
111  /// id mapped to a unique id.
112  DenseMap<std::pair<unsigned, unsigned>, unsigned> SourceIdMap;
113
114  /// SourceIds - Reverse map from source id to directory id + file id pair.
115  ///
116  SmallVector<std::pair<unsigned, unsigned>, 8> SourceIds;
117
118  /// Lines - List of source line correspondence.
119  std::vector<SrcLineInfo> Lines;
120
121  /// DIEBlocks - A list of all the DIEBlocks in use.
122  std::vector<DIEBlock *> DIEBlocks;
123
124  // DIEValueAllocator - All DIEValues are allocated through this allocator.
125  BumpPtrAllocator DIEValueAllocator;
126
127  /// StringPool - A String->Symbol mapping of strings used by indirect
128  /// references.
129  StringMap<std::pair<MCSymbol*, unsigned> > StringPool;
130  unsigned NextStringPoolNumber;
131
132  MCSymbol *getStringPoolEntry(StringRef Str);
133
134  /// SectionMap - Provides a unique id per text section.
135  ///
136  UniqueVector<const MCSection*> SectionMap;
137
138  /// SectionSourceLines - Tracks line numbers per text section.
139  ///
140  std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
141
142  // CurrentFnDbgScope - Top level scope for the current function.
143  //
144  DbgScope *CurrentFnDbgScope;
145
146  /// DbgScopeMap - Tracks the scopes in the current function.  Owns the
147  /// contained DbgScope*s.
148  ///
149  DenseMap<const MDNode *, DbgScope *> DbgScopeMap;
150
151  /// ConcreteScopes - Tracks the concrete scopees in the current function.
152  /// These scopes are also included in DbgScopeMap.
153  DenseMap<const MDNode *, DbgScope *> ConcreteScopes;
154
155  /// AbstractScopes - Tracks the abstract scopes a module. These scopes are
156  /// not included DbgScopeMap.  AbstractScopes owns its DbgScope*s.
157  DenseMap<const MDNode *, DbgScope *> AbstractScopes;
158
159  /// AbstractScopesList - Tracks abstract scopes constructed while processing
160  /// a function. This list is cleared during endFunction().
161  SmallVector<DbgScope *, 4>AbstractScopesList;
162
163  /// AbstractVariables - Collection on abstract variables.  Owned by the
164  /// DbgScopes in AbstractScopes.
165  DenseMap<const MDNode *, DbgVariable *> AbstractVariables;
166
167  /// DbgValueStartMap - Tracks starting scope of variable DIEs.
168  /// If the scope of an object begins sometime after the low pc value for the
169  /// scope most closely enclosing the object, the object entry may have a
170  /// DW_AT_start_scope attribute.
171  DenseMap<const MachineInstr *, DbgVariable *> DbgValueStartMap;
172
173  /// InliendSubprogramDIEs - Collection of subprgram DIEs that are marked
174  /// (at the end of the module) as DW_AT_inline.
175  SmallPtrSet<DIE *, 4> InlinedSubprogramDIEs;
176
177  /// ContainingTypeMap - This map is used to keep track of subprogram DIEs that
178  /// need DW_AT_containing_type attribute. This attribute points to a DIE that
179  /// corresponds to the MDNode mapped with the subprogram DIE.
180  DenseMap<DIE *, const MDNode *> ContainingTypeMap;
181
182  typedef SmallVector<DbgScope *, 2> ScopeVector;
183  SmallPtrSet<const MachineInstr *, 8> InsnsBeginScopeSet;
184  SmallPtrSet<const MachineInstr *, 8> InsnsEndScopeSet;
185
186  /// InlineInfo - Keep track of inlined functions and their location.  This
187  /// information is used to populate debug_inlined section.
188  typedef std::pair<MCSymbol *, DIE *> InlineInfoLabels;
189  DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> > InlineInfo;
190  SmallVector<const MDNode *, 4> InlinedSPNodes;
191
192  /// LabelsBeforeInsn - Maps instruction with label emitted before
193  /// instruction.
194  DenseMap<const MachineInstr *, MCSymbol *> LabelsBeforeInsn;
195
196  /// LabelsAfterInsn - Maps instruction with label emitted after
197  /// instruction.
198  DenseMap<const MachineInstr *, MCSymbol *> LabelsAfterInsn;
199
200  SmallVector<const MCSymbol *, 8> DebugRangeSymbols;
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 DIGlobalVariable *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  /// addRegisterAddress - Add register location entry in variable DIE.
309  bool addRegisterAddress(DIE *Die, DbgVariable *DV, const MachineOperand &MO);
310
311  /// addConstantValue - Add constant value entry in variable DIE.
312  bool addConstantValue(DIE *Die, DbgVariable *DV, const MachineOperand &MO);
313
314  /// addConstantFPValue - Add constant value entry in variable DIE.
315  bool addConstantFPValue(DIE *Die, DbgVariable *DV, const MachineOperand &MO);
316
317  /// addComplexAddress - Start with the address based on the location provided,
318  /// and generate the DWARF information necessary to find the actual variable
319  /// (navigating the extra location information encoded in the type) based on
320  /// the starting location.  Add the DWARF information to the die.
321  ///
322  void addComplexAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
323                         const MachineLocation &Location);
324
325  // FIXME: Should be reformulated in terms of addComplexAddress.
326  /// addBlockByrefAddress - Start with the address based on the location
327  /// provided, and generate the DWARF information necessary to find the
328  /// actual Block variable (navigating the Block struct) based on the
329  /// starting location.  Add the DWARF information to the die.  Obsolete,
330  /// please use addComplexAddress instead.
331  ///
332  void addBlockByrefAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
333                            const MachineLocation &Location);
334
335  /// addToContextOwner - Add Die into the list of its context owner's children.
336  void addToContextOwner(DIE *Die, DIDescriptor Context);
337
338  /// addType - Add a new type attribute to the specified entity.
339  void addType(DIE *Entity, DIType Ty);
340
341
342  /// getOrCreateNameSpace - Create a DIE for DINameSpace.
343  DIE *getOrCreateNameSpace(DINameSpace NS);
344
345  /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
346  /// given DIType.
347  DIE *getOrCreateTypeDIE(DIType Ty);
348
349  void addPubTypes(DISubprogram SP);
350
351  /// constructTypeDIE - Construct basic type die from DIBasicType.
352  void constructTypeDIE(DIE &Buffer,
353                        DIBasicType BTy);
354
355  /// constructTypeDIE - Construct derived type die from DIDerivedType.
356  void constructTypeDIE(DIE &Buffer,
357                        DIDerivedType DTy);
358
359  /// constructTypeDIE - Construct type DIE from DICompositeType.
360  void constructTypeDIE(DIE &Buffer,
361                        DICompositeType CTy);
362
363  /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
364  void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
365
366  /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
367  void constructArrayTypeDIE(DIE &Buffer,
368                             DICompositeType *CTy);
369
370  /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
371  DIE *constructEnumTypeDIE(DIEnumerator ETy);
372
373  /// createGlobalVariableDIE - Create new DIE using GV.
374  DIE *createGlobalVariableDIE(const DIGlobalVariable &GV);
375
376  /// createMemberDIE - Create new member DIE.
377  DIE *createMemberDIE(const DIDerivedType &DT);
378
379  /// createSubprogramDIE - Create new DIE using SP.
380  DIE *createSubprogramDIE(const DISubprogram &SP, bool MakeDecl = false);
381
382  /// getOrCreateDbgScope - Create DbgScope for the scope.
383  DbgScope *getOrCreateDbgScope(const MDNode *Scope, const MDNode *InlinedAt);
384
385  DbgScope *getOrCreateAbstractScope(const MDNode *N);
386
387  /// findAbstractVariable - Find abstract variable associated with Var.
388  DbgVariable *findAbstractVariable(DIVariable &Var, unsigned FrameIdx,
389                                    DebugLoc Loc);
390  DbgVariable *findAbstractVariable(DIVariable &Var, const MachineInstr *MI,
391                                    DebugLoc Loc);
392
393  /// updateSubprogramScopeDIE - Find DIE for the given subprogram and
394  /// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
395  /// If there are global variables in this scope then create and insert
396  /// DIEs for these variables.
397  DIE *updateSubprogramScopeDIE(const MDNode *SPNode);
398
399  /// constructLexicalScope - Construct new DW_TAG_lexical_block
400  /// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
401  DIE *constructLexicalScopeDIE(DbgScope *Scope);
402
403  /// constructInlinedScopeDIE - This scope represents inlined body of
404  /// a function. Construct DIE to represent this concrete inlined copy
405  /// of the function.
406  DIE *constructInlinedScopeDIE(DbgScope *Scope);
407
408  /// constructVariableDIE - Construct a DIE for the given DbgVariable.
409  DIE *constructVariableDIE(DbgVariable *DV, DbgScope *S);
410
411  /// constructScopeDIE - Construct a DIE for this scope.
412  DIE *constructScopeDIE(DbgScope *Scope);
413
414  /// EmitSectionLabels - Emit initial Dwarf sections with a label at
415  /// the start of each one.
416  void EmitSectionLabels();
417
418  /// emitDIE - Recusively Emits a debug information entry.
419  ///
420  void emitDIE(DIE *Die);
421
422  /// computeSizeAndOffset - Compute the size and offset of a DIE.
423  ///
424  unsigned computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last);
425
426  /// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
427  ///
428  void computeSizeAndOffsets();
429
430  /// EmitDebugInfo - Emit the debug info section.
431  ///
432  void emitDebugInfo();
433
434  /// emitAbbreviations - Emit the abbreviation section.
435  ///
436  void emitAbbreviations() const;
437
438  /// emitEndOfLineMatrix - Emit the last address of the section and the end of
439  /// the line matrix.
440  ///
441  void emitEndOfLineMatrix(unsigned SectionEnd);
442
443  /// emitDebugLines - Emit source line information.
444  ///
445  void emitDebugLines();
446
447  /// emitCommonDebugFrame - Emit common frame info into a debug frame section.
448  ///
449  void emitCommonDebugFrame();
450
451  /// emitFunctionDebugFrame - Emit per function frame info into a debug frame
452  /// section.
453  void emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo);
454
455  /// emitDebugPubNames - Emit visible names into a debug pubnames section.
456  ///
457  void emitDebugPubNames();
458
459  /// emitDebugPubTypes - Emit visible types into a debug pubtypes section.
460  ///
461  void emitDebugPubTypes();
462
463  /// emitDebugStr - Emit visible names into a debug str section.
464  ///
465  void emitDebugStr();
466
467  /// emitDebugLoc - Emit visible names into a debug loc section.
468  ///
469  void emitDebugLoc();
470
471  /// EmitDebugARanges - Emit visible names into a debug aranges section.
472  ///
473  void EmitDebugARanges();
474
475  /// emitDebugRanges - Emit visible names into a debug ranges section.
476  ///
477  void emitDebugRanges();
478
479  /// emitDebugMacInfo - Emit visible names into a debug macinfo section.
480  ///
481  void emitDebugMacInfo();
482
483  /// emitDebugInlineInfo - Emit inline info using following format.
484  /// Section Header:
485  /// 1. length of section
486  /// 2. Dwarf version number
487  /// 3. address size.
488  ///
489  /// Entries (one "entry" for each function that was inlined):
490  ///
491  /// 1. offset into __debug_str section for MIPS linkage name, if exists;
492  ///   otherwise offset into __debug_str for regular function name.
493  /// 2. offset into __debug_str section for regular function name.
494  /// 3. an unsigned LEB128 number indicating the number of distinct inlining
495  /// instances for the function.
496  ///
497  /// The rest of the entry consists of a {die_offset, low_pc}  pair for each
498  /// inlined instance; the die_offset points to the inlined_subroutine die in
499  /// the __debug_info section, and the low_pc is the starting address  for the
500  ///  inlining instance.
501  void emitDebugInlineInfo();
502
503  /// GetOrCreateSourceID - Look up the source id with the given directory and
504  /// source file names. If none currently exists, create a new id and insert it
505  /// in the SourceIds map. This can update DirectoryNames and SourceFileNames
506  /// maps as well.
507  unsigned GetOrCreateSourceID(StringRef DirName, StringRef FileName);
508
509  void constructCompileUnit(const MDNode *N);
510
511  void constructGlobalVariableDIE(const MDNode *N);
512
513  void constructSubprogramDIE(const MDNode *N);
514
515  // FIXME: This should go away in favor of complex addresses.
516  /// Find the type the programmer originally declared the variable to be
517  /// and return that type.  Obsolete, use GetComplexAddrType instead.
518  ///
519  DIType getBlockByrefType(DIType Ty, std::string Name);
520
521  /// recordSourceLine - Register a source line with debug info. Returns the
522  /// unique label that was emitted and which provides correspondence to
523  /// the source line list.
524  MCSymbol *recordSourceLine(unsigned Line, unsigned Col, const MDNode *Scope);
525
526  /// getSourceLineCount - Return the number of source lines in the debug
527  /// info.
528  unsigned getSourceLineCount() const {
529    return Lines.size();
530  }
531
532  /// identifyScopeMarkers() - Indentify instructions that are marking
533  /// beginning of or end of a scope.
534  void identifyScopeMarkers();
535
536  /// extractScopeInformation - Scan machine instructions in this function
537  /// and collect DbgScopes. Return true, if atleast one scope was found.
538  bool extractScopeInformation();
539
540  /// collectVariableInfo - Populate DbgScope entries with variables' info.
541  void collectVariableInfo();
542
543public:
544  //===--------------------------------------------------------------------===//
545  // Main entry points.
546  //
547  DwarfDebug(AsmPrinter *A, Module *M);
548  ~DwarfDebug();
549
550  /// beginModule - Emit all Dwarf sections that should come prior to the
551  /// content.
552  void beginModule(Module *M);
553
554  /// endModule - Emit all Dwarf sections that should come after the content.
555  ///
556  void endModule();
557
558  /// beginFunction - Gather pre-function debug information.  Assumes being
559  /// emitted immediately after the function entry point.
560  void beginFunction(const MachineFunction *MF);
561
562  /// endFunction - Gather and emit post-function debug information.
563  ///
564  void endFunction(const MachineFunction *MF);
565
566  /// beginScope - Process beginning of a scope.
567  void beginScope(const MachineInstr *MI);
568
569  /// endScope - Prcess end of a scope.
570  void endScope(const MachineInstr *MI);
571};
572} // End of namespace llvm
573
574#endif
575