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