LCOV - code coverage report
Current view: top level - include/llvm/DebugInfo - DIContext.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 35 46 76.1 %
Date: 2018-07-13 00:08:38 Functions: 15 20 75.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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/SmallVector.h"
      19             : #include "llvm/Object/ObjectFile.h"
      20             : #include "llvm/Support/raw_ostream.h"
      21             : #include <cassert>
      22             : #include <cstdint>
      23             : #include <memory>
      24             : #include <string>
      25             : #include <tuple>
      26             : #include <utility>
      27             : 
      28             : namespace llvm {
      29             : 
      30             : /// A format-neutral container for source line information.
      31       38924 : struct DILineInfo {
      32             :   std::string FileName;
      33             :   std::string FunctionName;
      34             :   Optional<StringRef> Source;
      35             :   uint32_t Line = 0;
      36             :   uint32_t Column = 0;
      37             :   uint32_t StartLine = 0;
      38             : 
      39             :   // DWARF-specific.
      40             :   uint32_t Discriminator = 0;
      41             : 
      42       12002 :   DILineInfo() : FileName("<invalid>"), FunctionName("<invalid>") {}
      43             : 
      44           3 :   bool operator==(const DILineInfo &RHS) const {
      45           3 :     return Line == RHS.Line && Column == RHS.Column &&
      46           0 :            FileName == RHS.FileName && FunctionName == RHS.FunctionName &&
      47           3 :            StartLine == RHS.StartLine && Discriminator == RHS.Discriminator;
      48             :   }
      49             : 
      50             :   bool operator!=(const DILineInfo &RHS) const {
      51           3 :     return !(*this == RHS);
      52             :   }
      53             : 
      54         200 :   bool operator<(const DILineInfo &RHS) const {
      55         400 :     return std::tie(FileName, FunctionName, Line, Column, StartLine,
      56             :                     Discriminator) <
      57         200 :            std::tie(RHS.FileName, RHS.FunctionName, RHS.Line, RHS.Column,
      58         400 :                     RHS.StartLine, RHS.Discriminator);
      59             :   }
      60             : 
      61           6 :   explicit operator bool() const { return *this != DILineInfo(); }
      62             : 
      63           3 :   void dump(raw_ostream &OS) {
      64           3 :     OS << "Line info: ";
      65           6 :     if (FileName != "<invalid>")
      66           6 :       OS << "file '" << FileName << "', ";
      67           6 :     if (FunctionName != "<invalid>")
      68           0 :       OS << "function '" << FunctionName << "', ";
      69           6 :     OS << "line " << Line << ", ";
      70           6 :     OS << "column " << Column << ", ";
      71           3 :     OS << "start line " << StartLine << '\n';
      72           3 :   }
      73             : };
      74             : 
      75             : using DILineInfoTable = SmallVector<std::pair<uint64_t, DILineInfo>, 16>;
      76             : 
      77             : /// A format-neutral container for inlined code description.
      78        2509 : class DIInliningInfo {
      79             :   SmallVector<DILineInfo, 4> Frames;
      80             : 
      81             : public:
      82             :   DIInliningInfo() = default;
      83             : 
      84             :   DILineInfo getFrame(unsigned Index) const {
      85             :     assert(Index < Frames.size());
      86        1453 :     return Frames[Index];
      87             :   }
      88             : 
      89             :   DILineInfo *getMutableFrame(unsigned Index) {
      90             :     assert(Index < Frames.size());
      91        1237 :     return &Frames[Index];
      92             :   }
      93             : 
      94             :   uint32_t getNumberOfFrames() const {
      95        2182 :     return Frames.size();
      96             :   }
      97             : 
      98             :   void addFrame(const DILineInfo &Frame) {
      99         724 :     Frames.push_back(Frame);
     100             :   }
     101             : };
     102             : 
     103             : /// Container for description of a global variable.
     104           0 : struct DIGlobal {
     105             :   std::string Name;
     106             :   uint64_t Start = 0;
     107             :   uint64_t Size = 0;
     108             : 
     109           0 :   DIGlobal() : Name("<invalid>") {}
     110             : };
     111             : 
     112             : /// A DINameKind is passed to name search methods to specify a
     113             : /// preference regarding the type of name resolution the caller wants.
     114             : enum class DINameKind { None, ShortName, LinkageName };
     115             : 
     116             : /// Controls which fields of DILineInfo container should be filled
     117             : /// with data.
     118             : struct DILineInfoSpecifier {
     119             :   enum class FileLineInfoKind { None, Default, AbsoluteFilePath };
     120             :   using FunctionNameKind = DINameKind;
     121             : 
     122             :   FileLineInfoKind FLIKind;
     123             :   FunctionNameKind FNKind;
     124             : 
     125             :   DILineInfoSpecifier(FileLineInfoKind FLIKind = FileLineInfoKind::Default,
     126             :                       FunctionNameKind FNKind = FunctionNameKind::None)
     127          29 :       : FLIKind(FLIKind), FNKind(FNKind) {}
     128             : };
     129             : 
     130             : /// This is just a helper to programmatically construct DIDumpType.
     131             : enum DIDumpTypeCounter {
     132             : #define HANDLE_DWARF_SECTION(ENUM_NAME, ELF_NAME, CMDLINE_NAME) \
     133             :   DIDT_ID_##ENUM_NAME,
     134             : #include "llvm/BinaryFormat/Dwarf.def"
     135             : #undef HANDLE_DWARF_SECTION
     136             :   DIDT_ID_UUID,
     137             :   DIDT_ID_Count
     138             : };
     139             : static_assert(DIDT_ID_Count <= 32, "section types overflow storage");
     140             : 
     141             : /// Selects which debug sections get dumped.
     142             : enum DIDumpType : unsigned {
     143             :   DIDT_Null,
     144             :   DIDT_All             = ~0U,
     145             : #define HANDLE_DWARF_SECTION(ENUM_NAME, ELF_NAME, CMDLINE_NAME) \
     146             :   DIDT_##ENUM_NAME = 1U << DIDT_ID_##ENUM_NAME,
     147             : #include "llvm/BinaryFormat/Dwarf.def"
     148             : #undef HANDLE_DWARF_SECTION
     149             :   DIDT_UUID = 1 << DIDT_ID_UUID,
     150             : };
     151             : 
     152             : /// Container for dump options that control which debug information will be
     153             : /// dumped.
     154        1955 : struct DIDumpOptions {
     155             :   unsigned DumpType = DIDT_All;
     156             :   unsigned RecurseDepth = -1U;
     157             :   bool ShowAddresses = true;
     158             :   bool ShowChildren = false;
     159             :   bool ShowParents = false;
     160             :   bool ShowForm = false;
     161             :   bool SummarizeTypes = false;
     162             :   bool Verbose = false;
     163             :   bool DisplayRawContents = false;
     164             : 
     165             :   /// Return default option set for printing a single DIE without children.
     166             :   static DIDumpOptions getForSingleDIE() {
     167             :     DIDumpOptions Opts;
     168             :     Opts.RecurseDepth = 0;
     169             :     return Opts;
     170             :   }
     171             : 
     172             :   /// Return the options with RecurseDepth set to 0 unless explicitly required.
     173          64 :   DIDumpOptions noImplicitRecursion() const {
     174          64 :     DIDumpOptions Opts = *this;
     175          64 :     if (RecurseDepth == -1U && !ShowChildren)
     176             :       Opts.RecurseDepth = 0;
     177          64 :     return Opts;
     178             :   }
     179             : };
     180             : 
     181             : class DIContext {
     182             : public:
     183             :   enum DIContextKind {
     184             :     CK_DWARF,
     185             :     CK_PDB
     186             :   };
     187             : 
     188        1442 :   DIContext(DIContextKind K) : Kind(K) {}
     189             :   virtual ~DIContext() = default;
     190             : 
     191             :   DIContextKind getKind() const { return Kind; }
     192             : 
     193             :   virtual void dump(raw_ostream &OS, DIDumpOptions DumpOpts) = 0;
     194             : 
     195           0 :   virtual bool verify(raw_ostream &OS, DIDumpOptions DumpOpts = {}) {
     196             :     // No verifier? Just say things went well.
     197           0 :     return true;
     198             :   }
     199             : 
     200             :   virtual DILineInfo getLineInfoForAddress(uint64_t Address,
     201             :       DILineInfoSpecifier Specifier = DILineInfoSpecifier()) = 0;
     202             :   virtual DILineInfoTable getLineInfoForAddressRange(uint64_t Address,
     203             :       uint64_t Size, DILineInfoSpecifier Specifier = DILineInfoSpecifier()) = 0;
     204             :   virtual DIInliningInfo getInliningInfoForAddress(uint64_t Address,
     205             :       DILineInfoSpecifier Specifier = DILineInfoSpecifier()) = 0;
     206             : 
     207             : private:
     208             :   const DIContextKind Kind;
     209             : };
     210             : 
     211             : /// An inferface for inquiring the load address of a loaded object file
     212             : /// to be used by the DIContext implementations when applying relocations
     213             : /// on the fly.
     214             : class LoadedObjectInfo {
     215             : protected:
     216             :   LoadedObjectInfo() = default;
     217           0 :   LoadedObjectInfo(const LoadedObjectInfo &) = default;
     218             : 
     219             : public:
     220             :   virtual ~LoadedObjectInfo() = default;
     221             : 
     222             :   /// Obtain the Load Address of a section by SectionRef.
     223             :   ///
     224             :   /// Calculate the address of the given section.
     225             :   /// The section need not be present in the local address space. The addresses
     226             :   /// need to be consistent with the addresses used to query the DIContext and
     227             :   /// the output of this function should be deterministic, i.e. repeated calls
     228             :   /// with the same Sec should give the same address.
     229           0 :   virtual uint64_t getSectionLoadAddress(const object::SectionRef &Sec) const {
     230           0 :     return 0;
     231             :   }
     232             : 
     233             :   /// If conveniently available, return the content of the given Section.
     234             :   ///
     235             :   /// When the section is available in the local address space, in relocated
     236             :   /// (loaded) form, e.g. because it was relocated by a JIT for execution, this
     237             :   /// function should provide the contents of said section in `Data`. If the
     238             :   /// loaded section is not available, or the cost of retrieving it would be
     239             :   /// prohibitive, this function should return false. In that case, relocations
     240             :   /// will be read from the local (unrelocated) object file and applied on the
     241             :   /// fly. Note that this method is used purely for optimzation purposes in the
     242             :   /// common case of JITting in the local address space, so returning false
     243             :   /// should always be correct.
     244         134 :   virtual bool getLoadedSectionContents(const object::SectionRef &Sec,
     245             :                                         StringRef &Data) const {
     246         134 :     return false;
     247             :   }
     248             : 
     249             :   // FIXME: This is untested and unused anywhere in the LLVM project, it's
     250             :   // used/needed by Julia (an external project). It should have some coverage
     251             :   // (at least tests, but ideally example functionality).
     252             :   /// Obtain a copy of this LoadedObjectInfo.
     253             :   virtual std::unique_ptr<LoadedObjectInfo> clone() const = 0;
     254             : };
     255             : 
     256             : template <typename Derived, typename Base = LoadedObjectInfo>
     257             : struct LoadedObjectInfoHelper : Base {
     258             : protected:
     259             :   LoadedObjectInfoHelper(const LoadedObjectInfoHelper &) = default;
     260             :   LoadedObjectInfoHelper() = default;
     261             : 
     262             : public:
     263             :   template <typename... Ts>
     264         682 :   LoadedObjectInfoHelper(Ts &&... Args) : Base(std::forward<Ts>(Args)...) {}
     265             : 
     266           0 :   std::unique_ptr<llvm::LoadedObjectInfo> clone() const override {
     267           0 :     return llvm::make_unique<Derived>(static_cast<const Derived &>(*this));
     268             :   }
     269             : };
     270             : 
     271             : } // end namespace llvm
     272             : 
     273             : #endif // LLVM_DEBUGINFO_DICONTEXT_H

Generated by: LCOV version 1.13