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