1 //===-- DWARFDebugLine.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 #ifndef SymbolFileDWARF_DWARFDebugLine_h_ 11 #define SymbolFileDWARF_DWARFDebugLine_h_ 12 13 #include <map> 14 #include <string> 15 #include <vector> 16 17 #include "lldb/lldb-private.h" 18 19 #include "DWARFDataExtractor.h" 20 #include "DWARFDefines.h" 21 22 #include "llvm/Support/MD5.h" 23 24 class DWARFUnit; 25 class SymbolFileDWARF; 26 27 //---------------------------------------------------------------------- 28 // DWARFDebugLine 29 //---------------------------------------------------------------------- 30 class DWARFDebugLine { 31 public: 32 //------------------------------------------------------------------ 33 // FileNameEntry 34 //------------------------------------------------------------------ 35 struct FileNameEntry { FileNameEntryFileNameEntry36 FileNameEntry() : name(nullptr), dir_idx(0), mod_time(0), length(0) {} 37 38 const char *name; 39 dw_sleb128_t dir_idx; 40 dw_sleb128_t mod_time; 41 dw_sleb128_t length; 42 llvm::MD5::MD5Result checksum; 43 }; 44 45 //------------------------------------------------------------------ 46 // Prologue 47 //------------------------------------------------------------------ 48 struct Prologue { 49 ProloguePrologue50 Prologue() 51 : total_length(0), version(0), prologue_length(0), min_inst_length(0), 52 default_is_stmt(0), line_base(0), line_range(0), opcode_base(0), 53 standard_opcode_lengths(), include_directories(), file_names() {} 54 55 typedef std::shared_ptr<Prologue> shared_ptr; 56 57 uint32_t total_length; // The size in bytes of the statement information for 58 // this compilation unit (not including the 59 // total_length field itself). 60 uint16_t 61 version; // Version identifier for the statement information format. 62 63 uint8_t address_size; 64 uint8_t segment_selector_size; 65 66 uint32_t prologue_length; // The number of bytes following the 67 // prologue_length field to the beginning of the 68 // first byte of the statement program itself. 69 uint8_t min_inst_length; // The size in bytes of the smallest target machine 70 // instruction. Statement program opcodes that 71 // alter the address register first multiply their 72 // operands by this value. 73 uint8_t maximum_operations_per_instruction; // New in DWARF4. The maximum 74 // number of individual 75 // operations that may be 76 // encoded in an instruction. 77 uint8_t default_is_stmt; // The initial value of theis_stmtregister. 78 int8_t line_base; // This parameter affects the meaning of the special 79 // opcodes. See below. 80 uint8_t line_range; // This parameter affects the meaning of the special 81 // opcodes. See below. 82 uint8_t opcode_base; // The number assigned to the first special opcode. 83 std::vector<uint8_t> standard_opcode_lengths; 84 std::vector<const char *> include_directories; 85 std::vector<FileNameEntry> file_names; 86 MaxLineIncrementForSpecialOpcodePrologue87 int32_t MaxLineIncrementForSpecialOpcode() const { 88 return line_base + (int8_t)line_range - 1; 89 } 90 bool IsValid() const; 91 // void Append(BinaryStreamBuf& buff) const; 92 void Dump(lldb_private::Log *log); ClearPrologue93 void Clear() { 94 total_length = version = prologue_length = min_inst_length = line_base = 95 line_range = opcode_base = 0; 96 line_base = 0; 97 standard_opcode_lengths.clear(); 98 include_directories.clear(); 99 file_names.clear(); 100 } 101 bool GetFile(uint32_t file_idx, const lldb_private::FileSpec &cu_comp_dir, 102 lldb_private::FileSpec &file) const; 103 }; 104 105 // Standard .debug_line state machine structure 106 struct Row { 107 typedef std::vector<Row> collection; 108 typedef collection::iterator iterator; 109 typedef collection::const_iterator const_iterator; 110 111 Row(bool default_is_stmt = false); ~RowRow112 virtual ~Row() {} 113 void PostAppend(); 114 void Reset(bool default_is_stmt); 115 void Dump(lldb_private::Log *log) const; 116 static void Insert(Row::collection &state_coll, const Row &state); 117 static void Dump(lldb_private::Log *log, const Row::collection &state_coll); 118 119 dw_addr_t address; // The program-counter value corresponding to a machine 120 // instruction generated by the compiler. 121 uint32_t line; // An unsigned integer indicating a source line number. Lines 122 // are numbered beginning at 1. The compiler may emit the 123 // value 0 in cases where an instruction cannot be attributed 124 // to any source line. 125 uint16_t column; // An unsigned integer indicating a column number within a 126 // source line. Columns are numbered beginning at 1. The 127 // value 0 is reserved to indicate that a statement begins 128 // at the 'left edge' of the line. 129 uint16_t file; // An unsigned integer indicating the identity of the source 130 // file corresponding to a machine instruction. 131 uint8_t is_stmt : 1, // A boolean indicating that the current instruction is 132 // the beginning of a statement. 133 basic_block : 1, // A boolean indicating that the current instruction is 134 // the beginning of a basic block. 135 end_sequence : 1, // A boolean indicating that the current address is 136 // that of the first byte after the end of a sequence 137 // of target machine instructions. 138 prologue_end : 1, // A boolean indicating that the current address is 139 // one (of possibly many) where execution should be 140 // suspended for an entry breakpoint of a function. 141 epilogue_begin : 1; // A boolean indicating that the current address is 142 // one (of possibly many) where execution should be 143 // suspended for an exit breakpoint of a function. 144 uint32_t isa; // An unsigned integer whose value encodes the applicable 145 // instruction set architecture for the current instruction. 146 }; 147 148 //------------------------------------------------------------------ 149 // LineTable 150 //------------------------------------------------------------------ 151 struct LineTable { 152 typedef std::shared_ptr<LineTable> shared_ptr; 153 LineTableLineTable154 LineTable() : prologue(), rows() {} 155 156 void AppendRow(const DWARFDebugLine::Row &state); ClearLineTable157 void Clear() { 158 prologue.reset(); 159 rows.clear(); 160 } 161 162 uint32_t LookupAddress(dw_addr_t address, dw_addr_t cu_high_pc) const; 163 void Dump(lldb_private::Log *log) const; 164 165 Prologue::shared_ptr prologue; 166 Row::collection rows; 167 }; 168 169 //------------------------------------------------------------------ 170 // State 171 //------------------------------------------------------------------ 172 struct State : public Row { 173 typedef void (*Callback)(dw_offset_t offset, const State &state, 174 void *userData); 175 176 // Special row codes used when calling the callback 177 enum { StartParsingLineTable = 0, DoneParsingLineTable = -1 }; 178 179 State(Prologue::shared_ptr &prologue_sp, lldb_private::Log *log, 180 Callback callback, void *userData); 181 182 void AppendRowToMatrix(dw_offset_t offset); 183 184 void Finalize(dw_offset_t offset); 185 186 void Reset(); 187 188 Prologue::shared_ptr prologue; 189 lldb_private::Log *log; 190 Callback callback; // Callback function that gets called each time an entry 191 // is to be added to the matrix 192 void *callbackUserData; 193 int row; // The row number that starts at zero for the prologue, and 194 // increases for each row added to the matrix 195 private: 196 DISALLOW_COPY_AND_ASSIGN(State); 197 }; 198 199 static bool DumpOpcodes( 200 lldb_private::Log *log, SymbolFileDWARF *dwarf2Data, 201 dw_offset_t line_offset = DW_INVALID_OFFSET, 202 uint32_t dump_flags = 0); // If line_offset is invalid, dump everything 203 static bool DumpLineTableRows( 204 lldb_private::Log *log, SymbolFileDWARF *dwarf2Data, 205 dw_offset_t line_offset = 206 DW_INVALID_OFFSET); // If line_offset is invalid, dump everything 207 static bool 208 ParseSupportFiles(const lldb::ModuleSP &module_sp, 209 const lldb_private::DWARFDataExtractor &debug_line_data, 210 const lldb_private::FileSpec &cu_comp_dir, 211 dw_offset_t stmt_list, 212 lldb_private::FileSpecList &support_files, DWARFUnit *dwarf_cu); 213 static bool 214 ParsePrologue(const lldb_private::DWARFDataExtractor &debug_line_data, 215 lldb::offset_t *offset_ptr, Prologue *prologue, 216 DWARFUnit *dwarf_cu = nullptr); 217 static bool 218 ParseStatementTable(const lldb_private::DWARFDataExtractor &debug_line_data, 219 lldb::offset_t *offset_ptr, State::Callback callback, 220 void *userData, DWARFUnit *dwarf_cu); 221 static dw_offset_t 222 DumpStatementTable(lldb_private::Log *log, 223 const lldb_private::DWARFDataExtractor &debug_line_data, 224 const dw_offset_t line_offset); 225 static dw_offset_t 226 DumpStatementOpcodes(lldb_private::Log *log, 227 const lldb_private::DWARFDataExtractor &debug_line_data, 228 const dw_offset_t line_offset, uint32_t flags); 229 static bool 230 ParseStatementTable(const lldb_private::DWARFDataExtractor &debug_line_data, 231 lldb::offset_t *offset_ptr, LineTable *line_table, 232 DWARFUnit *dwarf_cu); 233 static void Parse(const lldb_private::DWARFDataExtractor &debug_line_data, 234 DWARFDebugLine::State::Callback callback, void *userData); 235 // static void AppendLineTableData(const DWARFDebugLine::Prologue* prologue, 236 // const DWARFDebugLine::Row::collection& state_coll, const uint32_t 237 // addr_size, BinaryStreamBuf &debug_line_data); 238 DWARFDebugLine()239 DWARFDebugLine() : m_lineTableMap() {} 240 241 void Parse(const lldb_private::DWARFDataExtractor &debug_line_data); 242 void ParseIfNeeded(const lldb_private::DWARFDataExtractor &debug_line_data); 243 LineTable::shared_ptr GetLineTable(const dw_offset_t offset) const; 244 245 protected: 246 typedef std::map<dw_offset_t, LineTable::shared_ptr> LineTableMap; 247 typedef LineTableMap::iterator LineTableIter; 248 typedef LineTableMap::const_iterator LineTableConstIter; 249 250 LineTableMap m_lineTableMap; 251 }; 252 253 #endif // SymbolFileDWARF_DWARFDebugLine_h_ 254