DwarfDebug.h revision f2548caaa8b290aa598bf49c27dff72f7751ba5c
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  /// InsnBeforeLabelMap - Maps instruction with label emitted before
192  /// instruction.
193  DenseMap<const MachineInstr *, MCSymbol *> InsnBeforeLabelMap;
194
195  /// InsnAfterLabelMap - Maps instruction with label emitted after
196  /// instruction.
197  DenseMap<const MachineInstr *, MCSymbol *> InsnAfterLabelMap;
198
199  SmallVector<const MCSymbol *, 8> DebugRangeSymbols;
200
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;
204  MCSymbol *PrevLabel;
205
206  struct FunctionDebugFrameInfo {
207    unsigned Number;
208    std::vector<MachineMove> Moves;
209
210    FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M)
211      : Number(Num), Moves(M) {}
212  };
213
214  std::vector<FunctionDebugFrameInfo> DebugFrames;
215
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;
221
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 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);
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  /// 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.
310  ///
311  void addComplexAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
312                         const MachineLocation &Location);
313
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.
320  ///
321  void addBlockByrefAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
322                            const MachineLocation &Location);
323
324  /// addToContextOwner - Add Die into the list of its context owner's children.
325  void addToContextOwner(DIE *Die, DIDescriptor Context);
326
327  /// addType - Add a new type attribute to the specified entity.
328  void addType(DIE *Entity, DIType Ty);
329
330
331  /// getOrCreateNameSpace - Create a DIE for DINameSpace.
332  DIE *getOrCreateNameSpace(DINameSpace NS);
333
334  /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
335  /// given DIType.
336  DIE *getOrCreateTypeDIE(DIType Ty);
337
338  void addPubTypes(DISubprogram SP);
339
340  /// constructTypeDIE - Construct basic type die from DIBasicType.
341  void constructTypeDIE(DIE &Buffer,
342                        DIBasicType BTy);
343
344  /// constructTypeDIE - Construct derived type die from DIDerivedType.
345  void constructTypeDIE(DIE &Buffer,
346                        DIDerivedType DTy);
347
348  /// constructTypeDIE - Construct type DIE from DICompositeType.
349  void constructTypeDIE(DIE &Buffer,
350                        DICompositeType CTy);
351
352  /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
353  void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
354
355  /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
356  void constructArrayTypeDIE(DIE &Buffer,
357                             DICompositeType *CTy);
358
359  /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
360  DIE *constructEnumTypeDIE(DIEnumerator ETy);
361
362  /// createGlobalVariableDIE - Create new DIE using GV.
363  DIE *createGlobalVariableDIE(const DIGlobalVariable &GV);
364
365  /// createMemberDIE - Create new member DIE.
366  DIE *createMemberDIE(const DIDerivedType &DT);
367
368  /// createSubprogramDIE - Create new DIE using SP.
369  DIE *createSubprogramDIE(const DISubprogram &SP, bool MakeDecl = false);
370
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,
374                               MDNode *InlinedAt);
375
376  /// createDbgScope - Create DbgScope for the scope.
377  void createDbgScope(MDNode *Scope, MDNode *InlinedAt);
378
379  DbgScope *getOrCreateAbstractScope(MDNode *N);
380
381  /// findAbstractVariable - Find abstract variable associated with Var.
382  DbgVariable *findAbstractVariable(DIVariable &Var, unsigned FrameIdx,
383                                    DebugLoc Loc);
384  DbgVariable *findAbstractVariable(DIVariable &Var, const MachineInstr *MI,
385                                    DebugLoc Loc);
386
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);
392
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);
396
397  /// constructInlinedScopeDIE - This scope represents inlined body of
398  /// a function. Construct DIE to represent this concrete inlined copy
399  /// of the function.
400  DIE *constructInlinedScopeDIE(DbgScope *Scope);
401
402  /// constructVariableDIE - Construct a DIE for the given DbgVariable.
403  DIE *constructVariableDIE(DbgVariable *DV, DbgScope *S);
404
405  /// constructScopeDIE - Construct a DIE for this scope.
406  DIE *constructScopeDIE(DbgScope *Scope);
407
408  /// EmitSectionLabels - Emit initial Dwarf sections with a label at
409  /// the start of each one.
410  void EmitSectionLabels();
411
412  /// emitDIE - Recusively Emits a debug information entry.
413  ///
414  void emitDIE(DIE *Die);
415
416  /// computeSizeAndOffset - Compute the size and offset of a DIE.
417  ///
418  unsigned computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last);
419
420  /// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
421  ///
422  void computeSizeAndOffsets();
423
424  /// EmitDebugInfo - Emit the debug info section.
425  ///
426  void emitDebugInfo();
427
428  /// emitAbbreviations - Emit the abbreviation section.
429  ///
430  void emitAbbreviations() const;
431
432  /// emitEndOfLineMatrix - Emit the last address of the section and the end of
433  /// the line matrix.
434  ///
435  void emitEndOfLineMatrix(unsigned SectionEnd);
436
437  /// emitDebugLines - Emit source line information.
438  ///
439  void emitDebugLines();
440
441  /// emitCommonDebugFrame - Emit common frame info into a debug frame section.
442  ///
443  void emitCommonDebugFrame();
444
445  /// emitFunctionDebugFrame - Emit per function frame info into a debug frame
446  /// section.
447  void emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo);
448
449  /// emitDebugPubNames - Emit visible names into a debug pubnames section.
450  ///
451  void emitDebugPubNames();
452
453  /// emitDebugPubTypes - Emit visible types into a debug pubtypes section.
454  ///
455  void emitDebugPubTypes();
456
457  /// emitDebugStr - Emit visible names into a debug str section.
458  ///
459  void emitDebugStr();
460
461  /// emitDebugLoc - Emit visible names into a debug loc section.
462  ///
463  void emitDebugLoc();
464
465  /// EmitDebugARanges - Emit visible names into a debug aranges section.
466  ///
467  void EmitDebugARanges();
468
469  /// emitDebugRanges - Emit visible names into a debug ranges section.
470  ///
471  void emitDebugRanges();
472
473  /// emitDebugMacInfo - Emit visible names into a debug macinfo section.
474  ///
475  void emitDebugMacInfo();
476
477  /// emitDebugInlineInfo - Emit inline info using following format.
478  /// Section Header:
479  /// 1. length of section
480  /// 2. Dwarf version number
481  /// 3. address size.
482  ///
483  /// Entries (one "entry" for each function that was inlined):
484  ///
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.
490  ///
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();
496
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
500  /// maps as well.
501  unsigned GetOrCreateSourceID(StringRef DirName, StringRef FileName);
502
503  void constructCompileUnit(MDNode *N);
504
505  void constructGlobalVariableDIE(MDNode *N);
506
507  void constructSubprogramDIE(MDNode *N);
508
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.
512  ///
513  DIType getBlockByrefType(DIType Ty, std::string Name);
514
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);
519
520  /// getSourceLineCount - Return the number of source lines in the debug
521  /// info.
522  unsigned getSourceLineCount() const {
523    return Lines.size();
524  }
525
526  /// identifyScopeMarkers() - Indentify instructions that are marking
527  /// beginning of or end of a scope.
528  void identifyScopeMarkers();
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