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