Home | History | Annotate | Download | only in DebugInfo
      1 //===-- DIContext.h ---------------------------------------------*- 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 defines DIContext, an abstract data structure that holds
     11 // debug information data.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #ifndef LLVM_DEBUGINFO_DICONTEXT_H
     16 #define LLVM_DEBUGINFO_DICONTEXT_H
     17 
     18 #include "llvm/ADT/DenseMap.h"
     19 #include "llvm/ADT/SmallString.h"
     20 #include "llvm/ADT/SmallVector.h"
     21 #include "llvm/ADT/StringRef.h"
     22 #include "llvm/Object/ObjectFile.h"
     23 #include "llvm/Object/RelocVisitor.h"
     24 #include "llvm/Support/Casting.h"
     25 #include "llvm/Support/DataTypes.h"
     26 
     27 namespace llvm {
     28 
     29 class raw_ostream;
     30 
     31 /// DILineInfo - a format-neutral container for source line information.
     32 class DILineInfo {
     33   SmallString<16> FileName;
     34   SmallString<16> FunctionName;
     35   uint32_t Line;
     36   uint32_t Column;
     37 public:
     38   DILineInfo()
     39     : FileName("<invalid>"), FunctionName("<invalid>"),
     40       Line(0), Column(0) {}
     41   DILineInfo(const SmallString<16> &fileName,
     42              const SmallString<16> &functionName,
     43              uint32_t line, uint32_t column)
     44     : FileName(fileName), FunctionName(functionName),
     45       Line(line), Column(column) {}
     46 
     47   const char *getFileName() { return FileName.c_str(); }
     48   const char *getFunctionName() { return FunctionName.c_str(); }
     49   uint32_t getLine() const { return Line; }
     50   uint32_t getColumn() const { return Column; }
     51 
     52   bool operator==(const DILineInfo &RHS) const {
     53     return Line == RHS.Line && Column == RHS.Column &&
     54            FileName.equals(RHS.FileName) &&
     55            FunctionName.equals(RHS.FunctionName);
     56   }
     57   bool operator!=(const DILineInfo &RHS) const {
     58     return !(*this == RHS);
     59   }
     60 };
     61 
     62 typedef SmallVector<std::pair<uint64_t, DILineInfo>, 16> DILineInfoTable;
     63 
     64 /// DIInliningInfo - a format-neutral container for inlined code description.
     65 class DIInliningInfo {
     66   SmallVector<DILineInfo, 4> Frames;
     67  public:
     68   DIInliningInfo() {}
     69   DILineInfo getFrame(unsigned Index) const {
     70     assert(Index < Frames.size());
     71     return Frames[Index];
     72   }
     73   uint32_t getNumberOfFrames() const {
     74     return Frames.size();
     75   }
     76   void addFrame(const DILineInfo &Frame) {
     77     Frames.push_back(Frame);
     78   }
     79 };
     80 
     81 /// DILineInfoSpecifier - controls which fields of DILineInfo container
     82 /// should be filled with data.
     83 class DILineInfoSpecifier {
     84   const uint32_t Flags;  // Or'ed flags that set the info we want to fetch.
     85 public:
     86   enum Specification {
     87     FileLineInfo = 1 << 0,
     88     AbsoluteFilePath = 1 << 1,
     89     FunctionName = 1 << 2
     90   };
     91   // Use file/line info by default.
     92   DILineInfoSpecifier(uint32_t flags = FileLineInfo) : Flags(flags) {}
     93   bool needs(Specification spec) const {
     94     return (Flags & spec) > 0;
     95   }
     96 };
     97 
     98 /// Selects which debug sections get dumped.
     99 enum DIDumpType {
    100   DIDT_Null,
    101   DIDT_All,
    102   DIDT_Abbrev,
    103   DIDT_AbbrevDwo,
    104   DIDT_Aranges,
    105   DIDT_Frames,
    106   DIDT_Info,
    107   DIDT_InfoDwo,
    108   DIDT_Line,
    109   DIDT_Loc,
    110   DIDT_Ranges,
    111   DIDT_Pubnames,
    112   DIDT_Str,
    113   DIDT_StrDwo,
    114   DIDT_StrOffsetsDwo
    115 };
    116 
    117 // In place of applying the relocations to the data we've read from disk we use
    118 // a separate mapping table to the side and checking that at locations in the
    119 // dwarf where we expect relocated values. This adds a bit of complexity to the
    120 // dwarf parsing/extraction at the benefit of not allocating memory for the
    121 // entire size of the debug info sections.
    122 typedef DenseMap<uint64_t, std::pair<uint8_t, int64_t> > RelocAddrMap;
    123 
    124 class DIContext {
    125 public:
    126   enum DIContextKind {
    127     CK_DWARF
    128   };
    129   DIContextKind getKind() const { return Kind; }
    130 
    131   DIContext(DIContextKind K) : Kind(K) {}
    132   virtual ~DIContext();
    133 
    134   /// getDWARFContext - get a context for binary DWARF data.
    135   static DIContext *getDWARFContext(object::ObjectFile *);
    136 
    137   virtual void dump(raw_ostream &OS, DIDumpType DumpType = DIDT_All) = 0;
    138 
    139   virtual DILineInfo getLineInfoForAddress(uint64_t Address,
    140       DILineInfoSpecifier Specifier = DILineInfoSpecifier()) = 0;
    141   virtual DILineInfoTable getLineInfoForAddressRange(uint64_t Address,
    142       uint64_t Size, DILineInfoSpecifier Specifier = DILineInfoSpecifier()) = 0;
    143   virtual DIInliningInfo getInliningInfoForAddress(uint64_t Address,
    144       DILineInfoSpecifier Specifier = DILineInfoSpecifier()) = 0;
    145 private:
    146   const DIContextKind Kind;
    147 };
    148 
    149 }
    150 
    151 #endif
    152