LLVM API Documentation

DwarfDebug.h

Go to the documentation of this file.
00001 //===-- llvm/CodeGen/DwarfDebug.h - Dwarf Debug Framework ------*- C++ -*--===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains support for writing dwarf debug info into asm files.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef CODEGEN_ASMPRINTER_DWARFDEBUG_H__
00015 #define CODEGEN_ASMPRINTER_DWARFDEBUG_H__
00016 
00017 #include "DIE.h"
00018 #include "DwarfPrinter.h"
00019 #include "llvm/CodeGen/AsmPrinter.h"
00020 #include "llvm/CodeGen/MachineLocation.h"
00021 #include "llvm/Analysis/DebugInfo.h"
00022 #include "llvm/Support/raw_ostream.h"
00023 #include "llvm/ADT/DenseMap.h"
00024 #include "llvm/ADT/FoldingSet.h"
00025 #include "llvm/ADT/SmallSet.h"
00026 #include "llvm/ADT/StringMap.h"
00027 #include "llvm/ADT/UniqueVector.h"
00028 #include <string>
00029 
00030 namespace llvm {
00031 
00032 class CompileUnit;
00033 class DbgVariable;
00034 class DbgScope;
00035 class DbgConcreteScope;
00036 class MachineFrameInfo;
00037 class MachineModuleInfo;
00038 class TargetAsmInfo;
00039 class Timer;
00040 
00041 //===----------------------------------------------------------------------===//
00042 /// SrcLineInfo - This class is used to record source line correspondence.
00043 ///
00044 class VISIBILITY_HIDDEN SrcLineInfo {
00045   unsigned Line;                     // Source line number.
00046   unsigned Column;                   // Source column.
00047   unsigned SourceID;                 // Source ID number.
00048   unsigned LabelID;                  // Label in code ID number.
00049 public:
00050   SrcLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
00051     : Line(L), Column(C), SourceID(S), LabelID(I) {}
00052 
00053   // Accessors
00054   unsigned getLine() const { return Line; }
00055   unsigned getColumn() const { return Column; }
00056   unsigned getSourceID() const { return SourceID; }
00057   unsigned getLabelID() const { return LabelID; }
00058 };
00059 
00060 class VISIBILITY_HIDDEN DwarfDebug : public Dwarf {
00061   //===--------------------------------------------------------------------===//
00062   // Attributes used to construct specific Dwarf sections.
00063   //
00064 
00065   /// CompileUnitMap - A map of global variables representing compile units to
00066   /// compile units.
00067   DenseMap<Value *, CompileUnit *> CompileUnitMap;
00068 
00069   /// CompileUnits - All the compile units in this module.
00070   ///
00071   SmallVector<CompileUnit *, 8> CompileUnits;
00072 
00073   /// ModuleCU - All DIEs are inserted in ModuleCU.
00074   CompileUnit *ModuleCU;
00075 
00076   /// AbbreviationsSet - Used to uniquely define abbreviations.
00077   ///
00078   FoldingSet<DIEAbbrev> AbbreviationsSet;
00079 
00080   /// Abbreviations - A list of all the unique abbreviations in use.
00081   ///
00082   std::vector<DIEAbbrev *> Abbreviations;
00083 
00084   /// DirectoryIdMap - Directory name to directory id map.
00085   ///
00086   StringMap<unsigned> DirectoryIdMap;
00087 
00088   /// DirectoryNames - A list of directory names.
00089   SmallVector<std::string, 8> DirectoryNames;
00090 
00091   /// SourceFileIdMap - Source file name to source file id map.
00092   ///
00093   StringMap<unsigned> SourceFileIdMap;
00094 
00095   /// SourceFileNames - A list of source file names.
00096   SmallVector<std::string, 8> SourceFileNames;
00097 
00098   /// SourceIdMap - Source id map, i.e. pair of directory id and source file
00099   /// id mapped to a unique id.
00100   DenseMap<std::pair<unsigned, unsigned>, unsigned> SourceIdMap;
00101 
00102   /// SourceIds - Reverse map from source id to directory id + file id pair.
00103   ///
00104   SmallVector<std::pair<unsigned, unsigned>, 8> SourceIds;
00105 
00106   /// Lines - List of of source line correspondence.
00107   std::vector<SrcLineInfo> Lines;
00108 
00109   /// ValuesSet - Used to uniquely define values.
00110   ///
00111   FoldingSet<DIEValue> ValuesSet;
00112 
00113   /// Values - A list of all the unique values in use.
00114   ///
00115   std::vector<DIEValue *> Values;
00116 
00117   /// StringPool - A UniqueVector of strings used by indirect references.
00118   ///
00119   UniqueVector<std::string> StringPool;
00120 
00121   /// SectionMap - Provides a unique id per text section.
00122   ///
00123   UniqueVector<const MCSection*> SectionMap;
00124 
00125   /// SectionSourceLines - Tracks line numbers per text section.
00126   ///
00127   std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
00128 
00129   /// didInitial - Flag to indicate if initial emission has been done.
00130   ///
00131   bool didInitial;
00132 
00133   /// shouldEmit - Flag to indicate if debug information should be emitted.
00134   ///
00135   bool shouldEmit;
00136 
00137   // FunctionDbgScope - Top level scope for the current function.
00138   //
00139   DbgScope *FunctionDbgScope;
00140   
00141   /// DbgScopeMap - Tracks the scopes in the current function.
00142   DenseMap<GlobalVariable *, DbgScope *> DbgScopeMap;
00143 
00144   /// DbgAbstractScopeMap - Tracks abstract instance scopes in the current
00145   /// function.
00146   DenseMap<GlobalVariable *, DbgScope *> DbgAbstractScopeMap;
00147 
00148   /// DbgConcreteScopeMap - Tracks concrete instance scopes in the current
00149   /// function.
00150   DenseMap<GlobalVariable *,
00151            SmallVector<DbgScope *, 8> > DbgConcreteScopeMap;
00152 
00153   /// InlineInfo - Keep track of inlined functions and their location.  This
00154   /// information is used to populate debug_inlined section.
00155   DenseMap<GlobalVariable *, SmallVector<unsigned, 4> > InlineInfo;
00156 
00157   /// InlinedVariableScopes - Scopes information for the inlined subroutine
00158   /// variables.
00159   DenseMap<const MachineInstr *, DbgScope *> InlinedVariableScopes;
00160 
00161   /// AbstractInstanceRootMap - Map of abstract instance roots of inlined
00162   /// functions. These are subroutine entries that contain a DW_AT_inline
00163   /// attribute.
00164   DenseMap<const GlobalVariable *, DbgScope *> AbstractInstanceRootMap;
00165 
00166   /// AbstractInstanceRootList - List of abstract instance roots of inlined
00167   /// functions. These are subroutine entries that contain a DW_AT_inline
00168   /// attribute.
00169   SmallVector<DbgScope *, 32> AbstractInstanceRootList;
00170 
00171   /// LexicalScopeStack - A stack of lexical scopes. The top one is the current
00172   /// scope.
00173   SmallVector<DbgScope *, 16> LexicalScopeStack;
00174 
00175   /// CompileUnitOffsets - A vector of the offsets of the compile units. This is
00176   /// used when calculating the "origin" of a concrete instance of an inlined
00177   /// function.
00178   DenseMap<CompileUnit *, unsigned> CompileUnitOffsets;
00179 
00180   /// DebugTimer - Timer for the Dwarf debug writer.
00181   Timer *DebugTimer;
00182   
00183   struct FunctionDebugFrameInfo {
00184     unsigned Number;
00185     std::vector<MachineMove> Moves;
00186 
00187     FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M)
00188       : Number(Num), Moves(M) {}
00189   };
00190 
00191   std::vector<FunctionDebugFrameInfo> DebugFrames;
00192 
00193   /// getSourceDirectoryAndFileIds - Return the directory and file ids that
00194   /// maps to the source id. Source id starts at 1.
00195   std::pair<unsigned, unsigned>
00196   getSourceDirectoryAndFileIds(unsigned SId) const {
00197     return SourceIds[SId-1];
00198   }
00199 
00200   /// getNumSourceDirectories - Return the number of source directories in the
00201   /// debug info.
00202   unsigned getNumSourceDirectories() const {
00203     return DirectoryNames.size();
00204   }
00205 
00206   /// getSourceDirectoryName - Return the name of the directory corresponding
00207   /// to the id.
00208   const std::string &getSourceDirectoryName(unsigned Id) const {
00209     return DirectoryNames[Id - 1];
00210   }
00211 
00212   /// getSourceFileName - Return the name of the source file corresponding
00213   /// to the id.
00214   const std::string &getSourceFileName(unsigned Id) const {
00215     return SourceFileNames[Id - 1];
00216   }
00217 
00218   /// getNumSourceIds - Return the number of unique source ids.
00219   unsigned getNumSourceIds() const {
00220     return SourceIds.size();
00221   }
00222 
00223   /// AssignAbbrevNumber - Define a unique number for the abbreviation.
00224   ///
00225   void AssignAbbrevNumber(DIEAbbrev &Abbrev);
00226 
00227   /// CreateDIEEntry - Creates a new DIEEntry to be a proxy for a debug
00228   /// information entry.
00229   DIEEntry *CreateDIEEntry(DIE *Entry = NULL);
00230 
00231   /// SetDIEEntry - Set a DIEEntry once the debug information entry is defined.
00232   ///
00233   void SetDIEEntry(DIEEntry *Value, DIE *Entry);
00234 
00235   /// AddUInt - Add an unsigned integer attribute data and value.
00236   ///
00237   void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer);
00238 
00239   /// AddSInt - Add an signed integer attribute data and value.
00240   ///
00241   void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer);
00242 
00243   /// AddString - Add a string attribute data and value.
00244   ///
00245   void AddString(DIE *Die, unsigned Attribute, unsigned Form,
00246                  const std::string &String);
00247 
00248   /// AddLabel - Add a Dwarf label attribute data and value.
00249   ///
00250   void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
00251                 const DWLabel &Label);
00252 
00253   /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
00254   ///
00255   void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
00256                       const std::string &Label);
00257 
00258   /// AddSectionOffset - Add a section offset label attribute data and value.
00259   ///
00260   void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
00261                         const DWLabel &Label, const DWLabel &Section,
00262                         bool isEH = false, bool useSet = true);
00263 
00264   /// AddDelta - Add a label delta attribute data and value.
00265   ///
00266   void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
00267                 const DWLabel &Hi, const DWLabel &Lo);
00268 
00269   /// AddDIEEntry - Add a DIE attribute data and value.
00270   ///
00271   void AddDIEEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
00272     Die->AddValue(Attribute, Form, CreateDIEEntry(Entry));
00273   }
00274 
00275   /// AddBlock - Add block data.
00276   ///
00277   void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block);
00278 
00279   /// AddSourceLine - Add location information to specified debug information
00280   /// entry.
00281   void AddSourceLine(DIE *Die, const DIVariable *V);
00282 
00283   /// AddSourceLine - Add location information to specified debug information
00284   /// entry.
00285   void AddSourceLine(DIE *Die, const DIGlobal *G);
00286 
00287   void AddSourceLine(DIE *Die, const DIType *Ty);
00288 
00289   /// AddAddress - Add an address attribute to a die based on the location
00290   /// provided.
00291   void AddAddress(DIE *Die, unsigned Attribute,
00292                   const MachineLocation &Location);
00293 
00294   /// AddType - Add a new type attribute to the specified entity.
00295   void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty);
00296 
00297   /// ConstructTypeDIE - Construct basic type die from DIBasicType.
00298   void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
00299                         DIBasicType BTy);
00300 
00301   /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
00302   void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
00303                         DIDerivedType DTy);
00304 
00305   /// ConstructTypeDIE - Construct type DIE from DICompositeType.
00306   void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
00307                         DICompositeType CTy);
00308 
00309   /// ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
00310   void ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
00311 
00312   /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
00313   void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer, 
00314                              DICompositeType *CTy);
00315 
00316   /// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
00317   DIE *ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy);
00318 
00319   /// CreateGlobalVariableDIE - Create new DIE using GV.
00320   DIE *CreateGlobalVariableDIE(CompileUnit *DW_Unit,
00321                                const DIGlobalVariable &GV);
00322 
00323   /// CreateMemberDIE - Create new member DIE.
00324   DIE *CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT);
00325 
00326   /// CreateSubprogramDIE - Create new DIE using SP.
00327   DIE *CreateSubprogramDIE(CompileUnit *DW_Unit,
00328                            const DISubprogram &SP,
00329                            bool IsConstructor = false,
00330                            bool IsInlined = false);
00331 
00332   /// FindCompileUnit - Get the compile unit for the given descriptor. 
00333   ///
00334   CompileUnit &FindCompileUnit(DICompileUnit Unit) const;
00335 
00336   /// CreateDbgScopeVariable - Create a new scope variable.
00337   ///
00338   DIE *CreateDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit);
00339 
00340   /// getOrCreateScope - Returns the scope associated with the given descriptor.
00341   ///
00342   DbgScope *getOrCreateScope(GlobalVariable *V);
00343 
00344   /// ConstructDbgScope - Construct the components of a scope.
00345   ///
00346   void ConstructDbgScope(DbgScope *ParentScope,
00347                          unsigned ParentStartID, unsigned ParentEndID,
00348                          DIE *ParentDie, CompileUnit *Unit);
00349 
00350   /// ConstructFunctionDbgScope - Construct the scope for the subprogram.
00351   ///
00352   void ConstructFunctionDbgScope(DbgScope *RootScope,
00353                                  bool AbstractScope = false);
00354 
00355   /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
00356   ///
00357   void ConstructDefaultDbgScope(MachineFunction *MF);
00358 
00359   /// EmitInitial - Emit initial Dwarf declarations.  This is necessary for cc
00360   /// tools to recognize the object file contains Dwarf information.
00361   void EmitInitial();
00362 
00363   /// EmitDIE - Recusively Emits a debug information entry.
00364   ///
00365   void EmitDIE(DIE *Die);
00366 
00367   /// SizeAndOffsetDie - Compute the size and offset of a DIE.
00368   ///
00369   unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last);
00370 
00371   /// SizeAndOffsets - Compute the size and offset of all the DIEs.
00372   ///
00373   void SizeAndOffsets();
00374 
00375   /// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section.
00376   ///
00377   void EmitDebugInfoPerCU(CompileUnit *Unit);
00378 
00379   void EmitDebugInfo();
00380 
00381   /// EmitAbbreviations - Emit the abbreviation section.
00382   ///
00383   void EmitAbbreviations() const;
00384 
00385   /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
00386   /// the line matrix.
00387   ///
00388   void EmitEndOfLineMatrix(unsigned SectionEnd);
00389 
00390   /// EmitDebugLines - Emit source line information.
00391   ///
00392   void EmitDebugLines();
00393 
00394   /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
00395   ///
00396   void EmitCommonDebugFrame();
00397 
00398   /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
00399   /// section.
00400   void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo);
00401 
00402   void EmitDebugPubNamesPerCU(CompileUnit *Unit);
00403 
00404   /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
00405   ///
00406   void EmitDebugPubNames();
00407 
00408   /// EmitDebugStr - Emit visible names into a debug str section.
00409   ///
00410   void EmitDebugStr();
00411 
00412   /// EmitDebugLoc - Emit visible names into a debug loc section.
00413   ///
00414   void EmitDebugLoc();
00415 
00416   /// EmitDebugARanges - Emit visible names into a debug aranges section.
00417   ///
00418   void EmitDebugARanges();
00419 
00420   /// EmitDebugRanges - Emit visible names into a debug ranges section.
00421   ///
00422   void EmitDebugRanges();
00423 
00424   /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
00425   ///
00426   void EmitDebugMacInfo();
00427 
00428   /// EmitDebugInlineInfo - Emit inline info using following format.
00429   /// Section Header:
00430   /// 1. length of section
00431   /// 2. Dwarf version number
00432   /// 3. address size.
00433   ///
00434   /// Entries (one "entry" for each function that was inlined):
00435   ///
00436   /// 1. offset into __debug_str section for MIPS linkage name, if exists; 
00437   ///   otherwise offset into __debug_str for regular function name.
00438   /// 2. offset into __debug_str section for regular function name.
00439   /// 3. an unsigned LEB128 number indicating the number of distinct inlining 
00440   /// instances for the function.
00441   /// 
00442   /// The rest of the entry consists of a {die_offset, low_pc}  pair for each 
00443   /// inlined instance; the die_offset points to the inlined_subroutine die in
00444   /// the __debug_info section, and the low_pc is the starting address  for the
00445   ///  inlining instance.
00446   void EmitDebugInlineInfo();
00447 
00448   /// GetOrCreateSourceID - Look up the source id with the given directory and
00449   /// source file names. If none currently exists, create a new id and insert it
00450   /// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
00451   /// as well.
00452   unsigned GetOrCreateSourceID(const std::string &DirName,
00453                                const std::string &FileName);
00454 
00455   void ConstructCompileUnit(GlobalVariable *GV);
00456 
00457   void ConstructGlobalVariableDIE(GlobalVariable *GV);
00458 
00459   void ConstructSubprogram(GlobalVariable *GV);
00460 
00461 public:
00462   //===--------------------------------------------------------------------===//
00463   // Main entry points.
00464   //
00465   DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T);
00466   virtual ~DwarfDebug();
00467 
00468   /// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
00469   /// be emitted.
00470   bool ShouldEmitDwarfDebug() const { return shouldEmit; }
00471 
00472   /// BeginModule - Emit all Dwarf sections that should come prior to the
00473   /// content.
00474   void BeginModule(Module *M, MachineModuleInfo *MMI);
00475 
00476   /// EndModule - Emit all Dwarf sections that should come after the content.
00477   ///
00478   void EndModule();
00479 
00480   /// BeginFunction - Gather pre-function debug information.  Assumes being
00481   /// emitted immediately after the function entry point.
00482   void BeginFunction(MachineFunction *MF);
00483 
00484   /// EndFunction - Gather and emit post-function debug information.
00485   ///
00486   void EndFunction(MachineFunction *MF);
00487 
00488   /// RecordSourceLine - Records location information and associates it with a 
00489   /// label. Returns a unique label ID used to generate a label and provide
00490   /// correspondence to the source line list.
00491   unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col);
00492   
00493   /// RecordSourceLine - Records location information and associates it with a 
00494   /// label. Returns a unique label ID used to generate a label and provide
00495   /// correspondence to the source line list.
00496   unsigned RecordSourceLine(unsigned Line, unsigned Col, DICompileUnit CU);
00497 
00498   /// getRecordSourceLineCount - Return the number of source lines in the debug
00499   /// info.
00500   unsigned getRecordSourceLineCount() const {
00501     return Lines.size();
00502   }
00503                             
00504   /// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
00505   /// timed. Look up the source id with the given directory and source file
00506   /// names. If none currently exists, create a new id and insert it in the
00507   /// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
00508   /// well.
00509   unsigned getOrCreateSourceID(const std::string &DirName,
00510                                const std::string &FileName);
00511 
00512   /// RecordRegionStart - Indicate the start of a region.
00513   unsigned RecordRegionStart(GlobalVariable *V);
00514 
00515   /// RecordRegionEnd - Indicate the end of a region.
00516   unsigned RecordRegionEnd(GlobalVariable *V);
00517 
00518   /// RecordVariable - Indicate the declaration of  a local variable.
00519   void RecordVariable(GlobalVariable *GV, unsigned FrameIndex,
00520                       const MachineInstr *MI);
00521 
00522   //// RecordInlinedFnStart - Indicate the start of inlined subroutine.
00523   unsigned RecordInlinedFnStart(DISubprogram &SP, DICompileUnit CU,
00524                                 unsigned Line, unsigned Col);
00525 
00526   /// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
00527   unsigned RecordInlinedFnEnd(DISubprogram &SP);
00528 
00529   /// RecordVariableScope - Record scope for the variable declared by
00530   /// DeclareMI. DeclareMI must describe TargetInstrInfo::DECLARE. Record scopes
00531   /// for only inlined subroutine variables. Other variables's scopes are
00532   /// determined during RecordVariable().
00533   void RecordVariableScope(DIVariable &DV, const MachineInstr *DeclareMI);
00534 };
00535 
00536 } // End of namespace llvm
00537 
00538 #endif