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