DwarfDebug.h revision 62060a802b8ba875bf7ed22eee05f77c55581f56
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  DenseMap<DIE *, MDNode *> ContainingTypeMap;
177
178  /// AbstractSubprogramDIEs - Collection of abstruct subprogram DIEs.
179  SmallPtrSet<DIE *, 4> AbstractSubprogramDIEs;
180
181  /// TopLevelDIEs - Collection of top level DIEs.
182  SmallPtrSet<DIE *, 4> TopLevelDIEs;
183  SmallVector<DIE *, 4> TopLevelDIEsVector;
184
185  typedef SmallVector<DbgScope *, 2> ScopeVector;
186  typedef DenseMap<const MachineInstr *, ScopeVector>
187    InsnToDbgScopeMapTy;
188
189  /// DbgScopeBeginMap - Maps instruction with a list of DbgScopes it starts.
190  InsnToDbgScopeMapTy DbgScopeBeginMap;
191
192  /// DbgScopeEndMap - Maps instruction with a list DbgScopes it ends.
193  InsnToDbgScopeMapTy DbgScopeEndMap;
194
195  /// InlineInfo - Keep track of inlined functions and their location.  This
196  /// information is used to populate debug_inlined section.
197  typedef std::pair<MCSymbol*, DIE *> InlineInfoLabels;
198  DenseMap<MDNode*, SmallVector<InlineInfoLabels, 4> > InlineInfo;
199  SmallVector<MDNode *, 4> InlinedSPNodes;
200
201  /// CompileUnitOffsets - A vector of the offsets of the compile units. This is
202  /// used when calculating the "origin" of a concrete instance of an inlined
203  /// function.
204  DenseMap<CompileUnit *, unsigned> CompileUnitOffsets;
205
206  /// Previous instruction's location information. This is used to determine
207  /// label location to indicate scope boundries in dwarf debug info.
208  DebugLoc PrevInstLoc;
209
210  struct FunctionDebugFrameInfo {
211    unsigned Number;
212    std::vector<MachineMove> Moves;
213
214    FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M)
215      : Number(Num), Moves(M) {}
216  };
217
218  std::vector<FunctionDebugFrameInfo> DebugFrames;
219
220  // Section Symbols: these are assembler temporary labels that are emitted at
221  // the beginning of each supported dwarf section.  These are used to form
222  // section offsets and are created by EmitSectionLabels.
223  MCSymbol *DwarfFrameSectionSym, *DwarfInfoSectionSym, *DwarfAbbrevSectionSym;
224  MCSymbol *DwarfStrSectionSym, *TextSectionSym;
225
226private:
227
228  /// getSourceDirectoryAndFileIds - Return the directory and file ids that
229  /// maps to the source id. Source id starts at 1.
230  std::pair<unsigned, unsigned>
231  getSourceDirectoryAndFileIds(unsigned SId) const {
232    return SourceIds[SId-1];
233  }
234
235  /// getNumSourceDirectories - Return the number of source directories in the
236  /// debug info.
237  unsigned getNumSourceDirectories() const {
238    return DirectoryNames.size();
239  }
240
241  /// getSourceDirectoryName - Return the name of the directory corresponding
242  /// to the id.
243  const std::string &getSourceDirectoryName(unsigned Id) const {
244    return DirectoryNames[Id - 1];
245  }
246
247  /// getSourceFileName - Return the name of the source file corresponding
248  /// to the id.
249  const std::string &getSourceFileName(unsigned Id) const {
250    return SourceFileNames[Id - 1];
251  }
252
253  /// getNumSourceIds - Return the number of unique source ids.
254  unsigned getNumSourceIds() const {
255    return SourceIds.size();
256  }
257
258  /// assignAbbrevNumber - Define a unique number for the abbreviation.
259  ///
260  void assignAbbrevNumber(DIEAbbrev &Abbrev);
261
262  /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
263  /// information entry.
264  DIEEntry *createDIEEntry(DIE *Entry);
265
266  /// addUInt - Add an unsigned integer attribute data and value.
267  ///
268  void addUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer);
269
270  /// addSInt - Add an signed integer attribute data and value.
271  ///
272  void addSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer);
273
274  /// addString - Add a string attribute data and value.
275  ///
276  void addString(DIE *Die, unsigned Attribute, unsigned Form,
277                 const StringRef Str);
278
279  /// addLabel - Add a Dwarf label attribute data and value.
280  ///
281  void addLabel(DIE *Die, unsigned Attribute, unsigned Form,
282                const MCSymbol *Label);
283
284  /// addDelta - Add a label delta attribute data and value.
285  ///
286  void addDelta(DIE *Die, unsigned Attribute, unsigned Form,
287                const MCSymbol *Hi, const MCSymbol *Lo);
288
289  /// addDIEEntry - Add a DIE attribute data and value.
290  ///
291  void addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry);
292
293  /// addBlock - Add block data.
294  ///
295  void addBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block);
296
297  /// addSourceLine - Add location information to specified debug information
298  /// entry.
299  void addSourceLine(DIE *Die, const DIVariable *V);
300  void addSourceLine(DIE *Die, const DIGlobal *G);
301  void addSourceLine(DIE *Die, const DISubprogram *SP);
302  void addSourceLine(DIE *Die, const DIType *Ty);
303  void addSourceLine(DIE *Die, const DINameSpace *NS);
304
305  /// addAddress - Add an address attribute to a die based on the location
306  /// provided.
307  void addAddress(DIE *Die, unsigned Attribute,
308                  const MachineLocation &Location);
309
310  /// addComplexAddress - Start with the address based on the location provided,
311  /// and generate the DWARF information necessary to find the actual variable
312  /// (navigating the extra location information encoded in the type) based on
313  /// the starting location.  Add the DWARF information to the die.
314  ///
315  void addComplexAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
316                         const MachineLocation &Location);
317
318  // FIXME: Should be reformulated in terms of addComplexAddress.
319  /// addBlockByrefAddress - Start with the address based on the location
320  /// provided, and generate the DWARF information necessary to find the
321  /// actual Block variable (navigating the Block struct) based on the
322  /// starting location.  Add the DWARF information to the die.  Obsolete,
323  /// please use addComplexAddress instead.
324  ///
325  void addBlockByrefAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
326                            const MachineLocation &Location);
327
328  /// addToContextOwner - Add Die into the list of its context owner's children.
329  void addToContextOwner(DIE *Die, DIDescriptor Context);
330
331  /// addType - Add a new type attribute to the specified entity.
332  void addType(DIE *Entity, DIType Ty);
333
334
335  /// getOrCreateNameSpace - Create a DIE for DINameSpace.
336  DIE *getOrCreateNameSpace(DINameSpace NS);
337
338  /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
339  /// given DIType.
340  DIE *getOrCreateTypeDIE(DIType Ty);
341
342  void addPubTypes(DISubprogram SP);
343
344  /// constructTypeDIE - Construct basic type die from DIBasicType.
345  void constructTypeDIE(DIE &Buffer,
346                        DIBasicType BTy);
347
348  /// constructTypeDIE - Construct derived type die from DIDerivedType.
349  void constructTypeDIE(DIE &Buffer,
350                        DIDerivedType DTy);
351
352  /// constructTypeDIE - Construct type DIE from DICompositeType.
353  void constructTypeDIE(DIE &Buffer,
354                        DICompositeType CTy);
355
356  /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
357  void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
358
359  /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
360  void constructArrayTypeDIE(DIE &Buffer,
361                             DICompositeType *CTy);
362
363  /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
364  DIE *constructEnumTypeDIE(DIEnumerator ETy);
365
366  /// createGlobalVariableDIE - Create new DIE using GV.
367  DIE *createGlobalVariableDIE(const DIGlobalVariable &GV);
368
369  /// createMemberDIE - Create new member DIE.
370  DIE *createMemberDIE(const DIDerivedType &DT);
371
372  /// createSubprogramDIE - Create new DIE using SP.
373  DIE *createSubprogramDIE(const DISubprogram &SP, bool MakeDecl = false);
374
375  /// getUpdatedDbgScope - Find or create DbgScope assicated with
376  /// the instruction. Initialize scope and update scope hierarchy.
377  DbgScope *getUpdatedDbgScope(MDNode *N, const MachineInstr *MI,
378                               MDNode *InlinedAt);
379
380  /// createDbgScope - Create DbgScope for the scope.
381  void createDbgScope(MDNode *Scope, MDNode *InlinedAt);
382
383  DbgScope *getOrCreateAbstractScope(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(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  void constructCompileUnit(MDNode *N);
508
509  void constructGlobalVariableDIE(MDNode *N);
510
511  void constructSubprogramDIE(MDNode *N);
512
513  // FIXME: This should go away in favor of complex addresses.
514  /// Find the type the programmer originally declared the variable to be
515  /// and return that type.  Obsolete, use GetComplexAddrType instead.
516  ///
517  DIType getBlockByrefType(DIType Ty, std::string Name);
518
519  /// recordSourceLine - Register a source line with debug info. Returns the
520  /// unique label that was emitted and which provides correspondence to
521  /// the source line list.
522  MCSymbol *recordSourceLine(unsigned Line, unsigned Col, MDNode *Scope);
523
524  /// getSourceLineCount - Return the number of source lines in the debug
525  /// info.
526  unsigned getSourceLineCount() const {
527    return Lines.size();
528  }
529
530  /// extractScopeInformation - Scan machine instructions in this function
531  /// and collect DbgScopes. Return true, if atleast one scope was found.
532  bool extractScopeInformation();
533
534  /// collectVariableInfo - Populate DbgScope entries with variables' info.
535  void collectVariableInfo();
536
537public:
538  //===--------------------------------------------------------------------===//
539  // Main entry points.
540  //
541  DwarfDebug(AsmPrinter *A, Module *M);
542  ~DwarfDebug();
543
544  /// beginModule - Emit all Dwarf sections that should come prior to the
545  /// content.
546  void beginModule(Module *M);
547
548  /// endModule - Emit all Dwarf sections that should come after the content.
549  ///
550  void endModule();
551
552  /// beginFunction - Gather pre-function debug information.  Assumes being
553  /// emitted immediately after the function entry point.
554  void beginFunction(const MachineFunction *MF);
555
556  /// endFunction - Gather and emit post-function debug information.
557  ///
558  void endFunction(const MachineFunction *MF);
559
560  /// beginScope - Process beginning of a scope.
561  void beginScope(const MachineInstr *MI);
562
563  /// endScope - Prcess end of a scope.
564  void endScope(const MachineInstr *MI);
565};
566} // End of namespace llvm
567
568#endif
569