DwarfDebug.h revision a5db75c20bb3cb31b8fdb4c710159297c2d3c65f
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  CompileUnit *FirstCU;
86  DenseMap <const MDNode *, CompileUnit *> CUMap;
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  SmallPtrSet<const MachineInstr *, 8> InsnsEndScopeSet;
183
184  /// InlineInfo - Keep track of inlined functions and their location.  This
185  /// information is used to populate debug_inlined section.
186  typedef std::pair<MCSymbol *, DIE *> InlineInfoLabels;
187  DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> > InlineInfo;
188  SmallVector<const MDNode *, 4> InlinedSPNodes;
189
190  /// LabelsBeforeInsn - Maps instruction with label emitted before
191  /// instruction.
192  DenseMap<const MachineInstr *, MCSymbol *> LabelsBeforeInsn;
193
194  /// LabelsAfterInsn - Maps instruction with label emitted after
195  /// instruction.
196  DenseMap<const MachineInstr *, MCSymbol *> LabelsAfterInsn;
197
198  SmallVector<const MCSymbol *, 8> DebugRangeSymbols;
199
200  /// Previous instruction's location information. This is used to determine
201  /// label location to indicate scope boundries in dwarf debug info.
202  DebugLoc PrevInstLoc;
203  MCSymbol *PrevLabel;
204
205  struct FunctionDebugFrameInfo {
206    unsigned Number;
207    std::vector<MachineMove> Moves;
208
209    FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M)
210      : Number(Num), Moves(M) {}
211  };
212
213  std::vector<FunctionDebugFrameInfo> DebugFrames;
214
215  // Section Symbols: these are assembler temporary labels that are emitted at
216  // the beginning of each supported dwarf section.  These are used to form
217  // section offsets and are created by EmitSectionLabels.
218  MCSymbol *DwarfFrameSectionSym, *DwarfInfoSectionSym, *DwarfAbbrevSectionSym;
219  MCSymbol *DwarfStrSectionSym, *TextSectionSym, *DwarfDebugRangeSectionSym;
220
221  MCSymbol *FunctionBeginSym;
222private:
223
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];
229  }
230
231  /// getNumSourceDirectories - Return the number of source directories in the
232  /// debug info.
233  unsigned getNumSourceDirectories() const {
234    return DirectoryNames.size();
235  }
236
237  /// getSourceDirectoryName - Return the name of the directory corresponding
238  /// to the id.
239  const std::string &getSourceDirectoryName(unsigned Id) const {
240    return DirectoryNames[Id - 1];
241  }
242
243  /// getSourceFileName - Return the name of the source file corresponding
244  /// to the id.
245  const std::string &getSourceFileName(unsigned Id) const {
246    return SourceFileNames[Id - 1];
247  }
248
249  /// getNumSourceIds - Return the number of unique source ids.
250  unsigned getNumSourceIds() const {
251    return SourceIds.size();
252  }
253
254  /// assignAbbrevNumber - Define a unique number for the abbreviation.
255  ///
256  void assignAbbrevNumber(DIEAbbrev &Abbrev);
257
258  /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
259  /// information entry.
260  DIEEntry *createDIEEntry(DIE *Entry);
261
262  /// addUInt - Add an unsigned integer attribute data and value.
263  ///
264  void addUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer);
265
266  /// addSInt - Add an signed integer attribute data and value.
267  ///
268  void addSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer);
269
270  /// addString - Add a string attribute data and value.
271  ///
272  void addString(DIE *Die, unsigned Attribute, unsigned Form,
273                 const StringRef Str);
274
275  /// addLabel - Add a Dwarf label attribute data and value.
276  ///
277  void addLabel(DIE *Die, unsigned Attribute, unsigned Form,
278                const MCSymbol *Label);
279
280  /// addDelta - Add a label delta attribute data and value.
281  ///
282  void addDelta(DIE *Die, unsigned Attribute, unsigned Form,
283                const MCSymbol *Hi, const MCSymbol *Lo);
284
285  /// addDIEEntry - Add a DIE attribute data and value.
286  ///
287  void addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry);
288
289  /// addBlock - Add block data.
290  ///
291  void addBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block);
292
293  /// addSourceLine - Add location information to specified debug information
294  /// entry.
295  void addSourceLine(DIE *Die, const DIVariable *V);
296  void addSourceLine(DIE *Die, const DIGlobalVariable *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);
300
301  /// addAddress - Add an address attribute to a die based on the location
302  /// provided.
303  void addAddress(DIE *Die, unsigned Attribute,
304                  const MachineLocation &Location);
305
306  /// addRegisterAddress - Add register location entry in variable DIE.
307  bool addRegisterAddress(DIE *Die, DbgVariable *DV, const MachineOperand &MO);
308
309  /// addConstantValue - Add constant value entry in variable DIE.
310  bool addConstantValue(DIE *Die, DbgVariable *DV, const MachineOperand &MO);
311
312  /// addConstantFPValue - Add constant value entry in variable DIE.
313  bool addConstantFPValue(DIE *Die, DbgVariable *DV, const MachineOperand &MO);
314
315  /// addComplexAddress - Start with the address based on the location provided,
316  /// and generate the DWARF information necessary to find the actual variable
317  /// (navigating the extra location information encoded in the type) based on
318  /// the starting location.  Add the DWARF information to the die.
319  ///
320  void addComplexAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
321                         const MachineLocation &Location);
322
323  // FIXME: Should be reformulated in terms of addComplexAddress.
324  /// addBlockByrefAddress - Start with the address based on the location
325  /// provided, and generate the DWARF information necessary to find the
326  /// actual Block variable (navigating the Block struct) based on the
327  /// starting location.  Add the DWARF information to the die.  Obsolete,
328  /// please use addComplexAddress instead.
329  ///
330  void addBlockByrefAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
331                            const MachineLocation &Location);
332
333  /// addToContextOwner - Add Die into the list of its context owner's children.
334  void addToContextOwner(DIE *Die, DIDescriptor Context);
335
336  /// addType - Add a new type attribute to the specified entity.
337  void addType(DIE *Entity, DIType Ty);
338
339
340  /// getOrCreateNameSpace - Create a DIE for DINameSpace.
341  DIE *getOrCreateNameSpace(DINameSpace NS);
342
343  /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
344  /// given DIType.
345  DIE *getOrCreateTypeDIE(DIType Ty);
346
347  void addPubTypes(DISubprogram SP);
348
349  /// constructTypeDIE - Construct basic type die from DIBasicType.
350  void constructTypeDIE(DIE &Buffer,
351                        DIBasicType BTy);
352
353  /// constructTypeDIE - Construct derived type die from DIDerivedType.
354  void constructTypeDIE(DIE &Buffer,
355                        DIDerivedType DTy);
356
357  /// constructTypeDIE - Construct type DIE from DICompositeType.
358  void constructTypeDIE(DIE &Buffer,
359                        DICompositeType CTy);
360
361  /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
362  void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
363
364  /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
365  void constructArrayTypeDIE(DIE &Buffer,
366                             DICompositeType *CTy);
367
368  /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
369  DIE *constructEnumTypeDIE(DIEnumerator ETy);
370
371  /// createGlobalVariableDIE - Create new DIE using GV.
372  DIE *createGlobalVariableDIE(const DIGlobalVariable &GV);
373
374  /// createMemberDIE - Create new member DIE.
375  DIE *createMemberDIE(const DIDerivedType &DT);
376
377  /// createSubprogramDIE - Create new DIE using SP.
378  DIE *createSubprogramDIE(const DISubprogram &SP, bool MakeDecl = false);
379
380  /// getOrCreateDbgScope - Create DbgScope for the scope.
381  DbgScope *getOrCreateDbgScope(const MDNode *Scope, const MDNode *InlinedAt);
382
383  DbgScope *getOrCreateAbstractScope(const MDNode *N);
384
385  /// findAbstractVariable - Find abstract variable associated with Var.
386  DbgVariable *findAbstractVariable(DIVariable &Var, unsigned FrameIdx,
387                                    DebugLoc Loc);
388  DbgVariable *findAbstractVariable(DIVariable &Var, const MachineInstr *MI,
389                                    DebugLoc Loc);
390
391  /// updateSubprogramScopeDIE - Find DIE for the given subprogram and
392  /// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
393  /// If there are global variables in this scope then create and insert
394  /// DIEs for these variables.
395  DIE *updateSubprogramScopeDIE(const MDNode *SPNode);
396
397  /// constructLexicalScope - Construct new DW_TAG_lexical_block
398  /// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
399  DIE *constructLexicalScopeDIE(DbgScope *Scope);
400
401  /// constructInlinedScopeDIE - This scope represents inlined body of
402  /// a function. Construct DIE to represent this concrete inlined copy
403  /// of the function.
404  DIE *constructInlinedScopeDIE(DbgScope *Scope);
405
406  /// constructVariableDIE - Construct a DIE for the given DbgVariable.
407  DIE *constructVariableDIE(DbgVariable *DV, DbgScope *S);
408
409  /// constructScopeDIE - Construct a DIE for this scope.
410  DIE *constructScopeDIE(DbgScope *Scope);
411
412  /// EmitSectionLabels - Emit initial Dwarf sections with a label at
413  /// the start of each one.
414  void EmitSectionLabels();
415
416  /// emitDIE - Recusively Emits a debug information entry.
417  ///
418  void emitDIE(DIE *Die);
419
420  /// computeSizeAndOffset - Compute the size and offset of a DIE.
421  ///
422  unsigned computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last);
423
424  /// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
425  ///
426  void computeSizeAndOffsets();
427
428  /// EmitDebugInfo - Emit the debug info section.
429  ///
430  void emitDebugInfo();
431
432  /// emitAbbreviations - Emit the abbreviation section.
433  ///
434  void emitAbbreviations() const;
435
436  /// emitEndOfLineMatrix - Emit the last address of the section and the end of
437  /// the line matrix.
438  ///
439  void emitEndOfLineMatrix(unsigned SectionEnd);
440
441  /// emitDebugLines - Emit source line information.
442  ///
443  void emitDebugLines();
444
445  /// emitCommonDebugFrame - Emit common frame info into a debug frame section.
446  ///
447  void emitCommonDebugFrame();
448
449  /// emitFunctionDebugFrame - Emit per function frame info into a debug frame
450  /// section.
451  void emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo);
452
453  /// emitDebugPubNames - Emit visible names into a debug pubnames section.
454  ///
455  void emitDebugPubNames();
456
457  /// emitDebugPubTypes - Emit visible types into a debug pubtypes section.
458  ///
459  void emitDebugPubTypes();
460
461  /// emitDebugStr - Emit visible names into a debug str section.
462  ///
463  void emitDebugStr();
464
465  /// emitDebugLoc - Emit visible names into a debug loc section.
466  ///
467  void emitDebugLoc();
468
469  /// EmitDebugARanges - Emit visible names into a debug aranges section.
470  ///
471  void EmitDebugARanges();
472
473  /// emitDebugRanges - Emit visible names into a debug ranges section.
474  ///
475  void emitDebugRanges();
476
477  /// emitDebugMacInfo - Emit visible names into a debug macinfo section.
478  ///
479  void emitDebugMacInfo();
480
481  /// emitDebugInlineInfo - Emit inline info using following format.
482  /// Section Header:
483  /// 1. length of section
484  /// 2. Dwarf version number
485  /// 3. address size.
486  ///
487  /// Entries (one "entry" for each function that was inlined):
488  ///
489  /// 1. offset into __debug_str section for MIPS linkage name, if exists;
490  ///   otherwise offset into __debug_str for regular function name.
491  /// 2. offset into __debug_str section for regular function name.
492  /// 3. an unsigned LEB128 number indicating the number of distinct inlining
493  /// instances for the function.
494  ///
495  /// The rest of the entry consists of a {die_offset, low_pc}  pair for each
496  /// inlined instance; the die_offset points to the inlined_subroutine die in
497  /// the __debug_info section, and the low_pc is the starting address  for the
498  ///  inlining instance.
499  void emitDebugInlineInfo();
500
501  /// GetOrCreateSourceID - Look up the source id with the given directory and
502  /// source file names. If none currently exists, create a new id and insert it
503  /// in the SourceIds map. This can update DirectoryNames and SourceFileNames
504  /// maps as well.
505  unsigned GetOrCreateSourceID(StringRef DirName, StringRef FileName);
506
507  /// constructCompileUnit - Create new CompileUnit for the given
508  /// metadata node with tag DW_TAG_compile_unit.
509  void constructCompileUnit(const MDNode *N);
510
511  /// getCompielUnit - Get CompileUnit DIE.
512  CompileUnit *getCompileUnit(const MDNode *N) const;
513
514  /// constructGlobalVariableDIE - Construct global variable DIE.
515  void constructGlobalVariableDIE(const MDNode *N);
516
517  /// construct SubprogramDIE - Construct subprogram DIE.
518  void constructSubprogramDIE(const MDNode *N);
519
520  // FIXME: This should go away in favor of complex addresses.
521  /// Find the type the programmer originally declared the variable to be
522  /// and return that type.  Obsolete, use GetComplexAddrType instead.
523  ///
524  DIType getBlockByrefType(DIType Ty, std::string Name);
525
526  /// recordSourceLine - Register a source line with debug info. Returns the
527  /// unique label that was emitted and which provides correspondence to
528  /// the source line list.
529  MCSymbol *recordSourceLine(unsigned Line, unsigned Col, const MDNode *Scope);
530
531  /// getSourceLineCount - Return the number of source lines in the debug
532  /// info.
533  unsigned getSourceLineCount() const {
534    return Lines.size();
535  }
536
537  /// identifyScopeMarkers() - Indentify instructions that are marking
538  /// beginning of or end of a scope.
539  void identifyScopeMarkers();
540
541  /// extractScopeInformation - Scan machine instructions in this function
542  /// and collect DbgScopes. Return true, if atleast one scope was found.
543  bool extractScopeInformation();
544
545  /// collectVariableInfo - Populate DbgScope entries with variables' info.
546  void collectVariableInfo(const MachineFunction *);
547
548public:
549  //===--------------------------------------------------------------------===//
550  // Main entry points.
551  //
552  DwarfDebug(AsmPrinter *A, Module *M);
553  ~DwarfDebug();
554
555  /// beginModule - Emit all Dwarf sections that should come prior to the
556  /// content.
557  void beginModule(Module *M);
558
559  /// endModule - Emit all Dwarf sections that should come after the content.
560  ///
561  void endModule();
562
563  /// beginFunction - Gather pre-function debug information.  Assumes being
564  /// emitted immediately after the function entry point.
565  void beginFunction(const MachineFunction *MF);
566
567  /// endFunction - Gather and emit post-function debug information.
568  ///
569  void endFunction(const MachineFunction *MF);
570
571  /// beginScope - Process beginning of a scope.
572  void beginScope(const MachineInstr *MI);
573
574  /// endScope - Prcess end of a scope.
575  void endScope(const MachineInstr *MI);
576};
577} // End of namespace llvm
578
579#endif
580