LLVM API Documentation

DWARFDebugLine.h
Go to the documentation of this file.
00001 //===-- DWARFDebugLine.h ----------------------------------------*- 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 #ifndef LLVM_LIB_DEBUGINFO_DWARFDEBUGLINE_H
00011 #define LLVM_LIB_DEBUGINFO_DWARFDEBUGLINE_H
00012 
00013 #include "DWARFRelocMap.h"
00014 #include "llvm/DebugInfo/DIContext.h"
00015 #include "llvm/Support/DataExtractor.h"
00016 #include <map>
00017 #include <string>
00018 #include <vector>
00019 
00020 namespace llvm {
00021 
00022 class raw_ostream;
00023 
00024 class DWARFDebugLine {
00025 public:
00026   DWARFDebugLine(const RelocAddrMap* LineInfoRelocMap) : RelocMap(LineInfoRelocMap) {}
00027   struct FileNameEntry {
00028     FileNameEntry() : Name(nullptr), DirIdx(0), ModTime(0), Length(0) {}
00029 
00030     const char *Name;
00031     uint64_t DirIdx;
00032     uint64_t ModTime;
00033     uint64_t Length;
00034   };
00035 
00036   struct Prologue {
00037     Prologue();
00038 
00039     // The size in bytes of the statement information for this compilation unit
00040     // (not including the total_length field itself).
00041     uint32_t TotalLength;
00042     // Version identifier for the statement information format.
00043     uint16_t Version;
00044     // The number of bytes following the prologue_length field to the beginning
00045     // of the first byte of the statement program itself.
00046     uint32_t PrologueLength;
00047     // The size in bytes of the smallest target machine instruction. Statement
00048     // program opcodes that alter the address register first multiply their
00049     // operands by this value.
00050     uint8_t MinInstLength;
00051     // The maximum number of individual operations that may be encoded in an
00052     // instruction.
00053     uint8_t MaxOpsPerInst;
00054     // The initial value of theis_stmtregister.
00055     uint8_t DefaultIsStmt;
00056     // This parameter affects the meaning of the special opcodes. See below.
00057     int8_t LineBase;
00058     // This parameter affects the meaning of the special opcodes. See below.
00059     uint8_t LineRange;
00060     // The number assigned to the first special opcode.
00061     uint8_t OpcodeBase;
00062     std::vector<uint8_t> StandardOpcodeLengths;
00063     std::vector<const char*> IncludeDirectories;
00064     std::vector<FileNameEntry> FileNames;
00065 
00066     // Length of the prologue in bytes.
00067     uint32_t getLength() const {
00068       return PrologueLength + sizeof(TotalLength) + sizeof(Version) +
00069              sizeof(PrologueLength);
00070     }
00071     // Length of the line table data in bytes (not including the prologue).
00072     uint32_t getStatementTableLength() const {
00073       return TotalLength + sizeof(TotalLength) - getLength();
00074     }
00075     int32_t getMaxLineIncrementForSpecialOpcode() const {
00076       return LineBase + (int8_t)LineRange - 1;
00077     }
00078 
00079     void clear();
00080     void dump(raw_ostream &OS) const;
00081     bool parse(DataExtractor debug_line_data, uint32_t *offset_ptr);
00082   };
00083 
00084   // Standard .debug_line state machine structure.
00085   struct Row {
00086     explicit Row(bool default_is_stmt = false);
00087 
00088     /// Called after a row is appended to the matrix.
00089     void postAppend();
00090     void reset(bool default_is_stmt);
00091     void dump(raw_ostream &OS) const;
00092 
00093     static bool orderByAddress(const Row& LHS, const Row& RHS) {
00094       return LHS.Address < RHS.Address;
00095     }
00096 
00097     // The program-counter value corresponding to a machine instruction
00098     // generated by the compiler.
00099     uint64_t Address;
00100     // An unsigned integer indicating a source line number. Lines are numbered
00101     // beginning at 1. The compiler may emit the value 0 in cases where an
00102     // instruction cannot be attributed to any source line.
00103     uint32_t Line;
00104     // An unsigned integer indicating a column number within a source line.
00105     // Columns are numbered beginning at 1. The value 0 is reserved to indicate
00106     // that a statement begins at the 'left edge' of the line.
00107     uint16_t Column;
00108     // An unsigned integer indicating the identity of the source file
00109     // corresponding to a machine instruction.
00110     uint16_t File;
00111     // An unsigned integer whose value encodes the applicable instruction set
00112     // architecture for the current instruction.
00113     uint8_t Isa;
00114     // An unsigned integer representing the DWARF path discriminator value
00115     // for this location.
00116     uint32_t Discriminator;
00117     // A boolean indicating that the current instruction is the beginning of a
00118     // statement.
00119     uint8_t IsStmt:1,
00120             // A boolean indicating that the current instruction is the
00121             // beginning of a basic block.
00122             BasicBlock:1,
00123             // A boolean indicating that the current address is that of the
00124             // first byte after the end of a sequence of target machine
00125             // instructions.
00126             EndSequence:1,
00127             // A boolean indicating that the current address is one (of possibly
00128             // many) where execution should be suspended for an entry breakpoint
00129             // of a function.
00130             PrologueEnd:1,
00131             // A boolean indicating that the current address is one (of possibly
00132             // many) where execution should be suspended for an exit breakpoint
00133             // of a function.
00134             EpilogueBegin:1;
00135   };
00136 
00137   // Represents a series of contiguous machine instructions. Line table for each
00138   // compilation unit may consist of multiple sequences, which are not
00139   // guaranteed to be in the order of ascending instruction address.
00140   struct Sequence {
00141     // Sequence describes instructions at address range [LowPC, HighPC)
00142     // and is described by line table rows [FirstRowIndex, LastRowIndex).
00143     uint64_t LowPC;
00144     uint64_t HighPC;
00145     unsigned FirstRowIndex;
00146     unsigned LastRowIndex;
00147     bool Empty;
00148 
00149     Sequence();
00150     void reset();
00151 
00152     static bool orderByLowPC(const Sequence& LHS, const Sequence& RHS) {
00153       return LHS.LowPC < RHS.LowPC;
00154     }
00155     bool isValid() const {
00156       return !Empty && (LowPC < HighPC) && (FirstRowIndex < LastRowIndex);
00157     }
00158     bool containsPC(uint64_t pc) const {
00159       return (LowPC <= pc && pc < HighPC);
00160     }
00161   };
00162 
00163   struct LineTable {
00164     LineTable();
00165 
00166     void appendRow(const DWARFDebugLine::Row &R) {
00167       Rows.push_back(R);
00168     }
00169     void appendSequence(const DWARFDebugLine::Sequence &S) {
00170       Sequences.push_back(S);
00171     }
00172 
00173     // Returns the index of the row with file/line info for a given address,
00174     // or -1 if there is no such row.
00175     uint32_t lookupAddress(uint64_t address) const;
00176 
00177     bool lookupAddressRange(uint64_t address, uint64_t size,
00178                             std::vector<uint32_t> &result) const;
00179 
00180     // Extracts filename by its index in filename table in prologue.
00181     // Returns true on success.
00182     bool getFileNameByIndex(uint64_t FileIndex,
00183                             DILineInfoSpecifier::FileLineInfoKind Kind,
00184                             std::string &Result) const;
00185 
00186     void dump(raw_ostream &OS) const;
00187     void clear();
00188 
00189     /// Parse prologue and all rows.
00190     bool parse(DataExtractor debug_line_data, const RelocAddrMap *RMap,
00191                uint32_t *offset_ptr);
00192 
00193     struct Prologue Prologue;
00194     typedef std::vector<Row> RowVector;
00195     typedef RowVector::const_iterator RowIter;
00196     typedef std::vector<Sequence> SequenceVector;
00197     typedef SequenceVector::const_iterator SequenceIter;
00198     RowVector Rows;
00199     SequenceVector Sequences;
00200   };
00201 
00202   const LineTable *getLineTable(uint32_t offset) const;
00203   const LineTable *getOrParseLineTable(DataExtractor debug_line_data,
00204                                        uint32_t offset);
00205 
00206 private:
00207   struct ParsingState {
00208     ParsingState(struct LineTable *LT);
00209 
00210     void resetRowAndSequence();
00211     void appendRowToMatrix(uint32_t offset);
00212 
00213     // Line table we're currently parsing.
00214     struct LineTable *LineTable;
00215     // The row number that starts at zero for the prologue, and increases for
00216     // each row added to the matrix.
00217     unsigned RowNumber;
00218     struct Row Row;
00219     struct Sequence Sequence;
00220   };
00221 
00222   typedef std::map<uint32_t, LineTable> LineTableMapTy;
00223   typedef LineTableMapTy::iterator LineTableIter;
00224   typedef LineTableMapTy::const_iterator LineTableConstIter;
00225 
00226   const RelocAddrMap *RelocMap;
00227   LineTableMapTy LineTableMap;
00228 };
00229 
00230 }
00231 
00232 #endif