DWARFDebugLine.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. //===-- DWARFDebugLine.cpp ------------------------------------------------===//
  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. #include "DWARFDebugLine.h"
  10. #include "llvm/Support/Dwarf.h"
  11. #include "llvm/Support/Format.h"
  12. #include "llvm/Support/raw_ostream.h"
  13. #include <algorithm>
  14. using namespace llvm;
  15. using namespace dwarf;
  16. void DWARFDebugLine::Prologue::dump(raw_ostream &OS) const {
  17. OS << "Line table prologue:\n"
  18. << format(" total_length: 0x%8.8x\n", TotalLength)
  19. << format(" version: %u\n", Version)
  20. << format("prologue_length: 0x%8.8x\n", PrologueLength)
  21. << format("min_inst_length: %u\n", MinInstLength)
  22. << format("default_is_stmt: %u\n", DefaultIsStmt)
  23. << format(" line_base: %i\n", LineBase)
  24. << format(" line_range: %u\n", LineRange)
  25. << format(" opcode_base: %u\n", OpcodeBase);
  26. for (uint32_t i = 0; i < StandardOpcodeLengths.size(); ++i)
  27. OS << format("standard_opcode_lengths[%s] = %u\n", LNStandardString(i+1),
  28. StandardOpcodeLengths[i]);
  29. if (!IncludeDirectories.empty())
  30. for (uint32_t i = 0; i < IncludeDirectories.size(); ++i)
  31. OS << format("include_directories[%3u] = '", i+1)
  32. << IncludeDirectories[i] << "'\n";
  33. if (!FileNames.empty()) {
  34. OS << " Dir Mod Time File Len File Name\n"
  35. << " ---- ---------- ---------- -----------"
  36. "----------------\n";
  37. for (uint32_t i = 0; i < FileNames.size(); ++i) {
  38. const FileNameEntry& fileEntry = FileNames[i];
  39. OS << format("file_names[%3u] %4u ", i+1, fileEntry.DirIdx)
  40. << format("0x%8.8x 0x%8.8x ", fileEntry.ModTime, fileEntry.Length)
  41. << fileEntry.Name << '\n';
  42. }
  43. }
  44. }
  45. void DWARFDebugLine::Row::postAppend() {
  46. BasicBlock = false;
  47. PrologueEnd = false;
  48. EpilogueBegin = false;
  49. }
  50. void DWARFDebugLine::Row::reset(bool default_is_stmt) {
  51. Address = 0;
  52. Line = 1;
  53. Column = 0;
  54. File = 1;
  55. Isa = 0;
  56. IsStmt = default_is_stmt;
  57. BasicBlock = false;
  58. EndSequence = false;
  59. PrologueEnd = false;
  60. EpilogueBegin = false;
  61. }
  62. void DWARFDebugLine::Row::dump(raw_ostream &OS) const {
  63. OS << format("0x%16.16" PRIx64 " %6u %6u", Address, Line, Column)
  64. << format(" %6u %3u ", File, Isa)
  65. << (IsStmt ? " is_stmt" : "")
  66. << (BasicBlock ? " basic_block" : "")
  67. << (PrologueEnd ? " prologue_end" : "")
  68. << (EpilogueBegin ? " epilogue_begin" : "")
  69. << (EndSequence ? " end_sequence" : "")
  70. << '\n';
  71. }
  72. void DWARFDebugLine::LineTable::dump(raw_ostream &OS) const {
  73. Prologue.dump(OS);
  74. OS << '\n';
  75. if (!Rows.empty()) {
  76. OS << "Address Line Column File ISA Flags\n"
  77. << "------------------ ------ ------ ------ --- -------------\n";
  78. for (std::vector<Row>::const_iterator pos = Rows.begin(),
  79. end = Rows.end(); pos != end; ++pos)
  80. pos->dump(OS);
  81. }
  82. }
  83. DWARFDebugLine::State::~State() {}
  84. void DWARFDebugLine::State::appendRowToMatrix(uint32_t offset) {
  85. ++row; // Increase the row number.
  86. LineTable::appendRow(*this);
  87. Row::postAppend();
  88. }
  89. DWARFDebugLine::DumpingState::~DumpingState() {}
  90. void DWARFDebugLine::DumpingState::finalize(uint32_t offset) {
  91. LineTable::dump(OS);
  92. }
  93. const DWARFDebugLine::LineTable *
  94. DWARFDebugLine::getLineTable(uint32_t offset) const {
  95. LineTableConstIter pos = LineTableMap.find(offset);
  96. if (pos != LineTableMap.end())
  97. return &pos->second;
  98. return 0;
  99. }
  100. const DWARFDebugLine::LineTable *
  101. DWARFDebugLine::getOrParseLineTable(DataExtractor debug_line_data,
  102. uint32_t offset) {
  103. std::pair<LineTableIter, bool> pos =
  104. LineTableMap.insert(LineTableMapTy::value_type(offset, LineTable()));
  105. if (pos.second) {
  106. // Parse and cache the line table for at this offset.
  107. State state;
  108. if (!parseStatementTable(debug_line_data, &offset, state))
  109. return 0;
  110. pos.first->second = state;
  111. }
  112. return &pos.first->second;
  113. }
  114. bool
  115. DWARFDebugLine::parsePrologue(DataExtractor debug_line_data,
  116. uint32_t *offset_ptr, Prologue *prologue) {
  117. const uint32_t prologue_offset = *offset_ptr;
  118. prologue->clear();
  119. prologue->TotalLength = debug_line_data.getU32(offset_ptr);
  120. prologue->Version = debug_line_data.getU16(offset_ptr);
  121. if (prologue->Version != 2)
  122. return false;
  123. prologue->PrologueLength = debug_line_data.getU32(offset_ptr);
  124. const uint32_t end_prologue_offset = prologue->PrologueLength + *offset_ptr;
  125. prologue->MinInstLength = debug_line_data.getU8(offset_ptr);
  126. prologue->DefaultIsStmt = debug_line_data.getU8(offset_ptr);
  127. prologue->LineBase = debug_line_data.getU8(offset_ptr);
  128. prologue->LineRange = debug_line_data.getU8(offset_ptr);
  129. prologue->OpcodeBase = debug_line_data.getU8(offset_ptr);
  130. prologue->StandardOpcodeLengths.reserve(prologue->OpcodeBase-1);
  131. for (uint32_t i = 1; i < prologue->OpcodeBase; ++i) {
  132. uint8_t op_len = debug_line_data.getU8(offset_ptr);
  133. prologue->StandardOpcodeLengths.push_back(op_len);
  134. }
  135. while (*offset_ptr < end_prologue_offset) {
  136. const char *s = debug_line_data.getCStr(offset_ptr);
  137. if (s && s[0])
  138. prologue->IncludeDirectories.push_back(s);
  139. else
  140. break;
  141. }
  142. while (*offset_ptr < end_prologue_offset) {
  143. const char *name = debug_line_data.getCStr(offset_ptr);
  144. if (name && name[0]) {
  145. FileNameEntry fileEntry;
  146. fileEntry.Name = name;
  147. fileEntry.DirIdx = debug_line_data.getULEB128(offset_ptr);
  148. fileEntry.ModTime = debug_line_data.getULEB128(offset_ptr);
  149. fileEntry.Length = debug_line_data.getULEB128(offset_ptr);
  150. prologue->FileNames.push_back(fileEntry);
  151. } else {
  152. break;
  153. }
  154. }
  155. if (*offset_ptr != end_prologue_offset) {
  156. fprintf(stderr, "warning: parsing line table prologue at 0x%8.8x should"
  157. " have ended at 0x%8.8x but it ended ad 0x%8.8x\n",
  158. prologue_offset, end_prologue_offset, *offset_ptr);
  159. }
  160. return end_prologue_offset;
  161. }
  162. bool
  163. DWARFDebugLine::parseStatementTable(DataExtractor debug_line_data,
  164. uint32_t *offset_ptr, State &state) {
  165. const uint32_t debug_line_offset = *offset_ptr;
  166. Prologue *prologue = &state.Prologue;
  167. if (!parsePrologue(debug_line_data, offset_ptr, prologue)) {
  168. // Restore our offset and return false to indicate failure!
  169. *offset_ptr = debug_line_offset;
  170. return false;
  171. }
  172. const uint32_t end_offset = debug_line_offset + prologue->TotalLength +
  173. sizeof(prologue->TotalLength);
  174. state.reset();
  175. while (*offset_ptr < end_offset) {
  176. uint8_t opcode = debug_line_data.getU8(offset_ptr);
  177. if (opcode == 0) {
  178. // Extended Opcodes always start with a zero opcode followed by
  179. // a uleb128 length so you can skip ones you don't know about
  180. uint32_t ext_offset = *offset_ptr;
  181. uint64_t len = debug_line_data.getULEB128(offset_ptr);
  182. uint32_t arg_size = len - (*offset_ptr - ext_offset);
  183. uint8_t sub_opcode = debug_line_data.getU8(offset_ptr);
  184. switch (sub_opcode) {
  185. case DW_LNE_end_sequence:
  186. // Set the end_sequence register of the state machine to true and
  187. // append a row to the matrix using the current values of the
  188. // state-machine registers. Then reset the registers to the initial
  189. // values specified above. Every statement program sequence must end
  190. // with a DW_LNE_end_sequence instruction which creates a row whose
  191. // address is that of the byte after the last target machine instruction
  192. // of the sequence.
  193. state.EndSequence = true;
  194. state.appendRowToMatrix(*offset_ptr);
  195. state.reset();
  196. break;
  197. case DW_LNE_set_address:
  198. // Takes a single relocatable address as an operand. The size of the
  199. // operand is the size appropriate to hold an address on the target
  200. // machine. Set the address register to the value given by the
  201. // relocatable address. All of the other statement program opcodes
  202. // that affect the address register add a delta to it. This instruction
  203. // stores a relocatable value into it instead.
  204. state.Address = debug_line_data.getAddress(offset_ptr);
  205. break;
  206. case DW_LNE_define_file:
  207. // Takes 4 arguments. The first is a null terminated string containing
  208. // a source file name. The second is an unsigned LEB128 number
  209. // representing the directory index of the directory in which the file
  210. // was found. The third is an unsigned LEB128 number representing the
  211. // time of last modification of the file. The fourth is an unsigned
  212. // LEB128 number representing the length in bytes of the file. The time
  213. // and length fields may contain LEB128(0) if the information is not
  214. // available.
  215. //
  216. // The directory index represents an entry in the include_directories
  217. // section of the statement program prologue. The index is LEB128(0)
  218. // if the file was found in the current directory of the compilation,
  219. // LEB128(1) if it was found in the first directory in the
  220. // include_directories section, and so on. The directory index is
  221. // ignored for file names that represent full path names.
  222. //
  223. // The files are numbered, starting at 1, in the order in which they
  224. // appear; the names in the prologue come before names defined by
  225. // the DW_LNE_define_file instruction. These numbers are used in the
  226. // the file register of the state machine.
  227. {
  228. FileNameEntry fileEntry;
  229. fileEntry.Name = debug_line_data.getCStr(offset_ptr);
  230. fileEntry.DirIdx = debug_line_data.getULEB128(offset_ptr);
  231. fileEntry.ModTime = debug_line_data.getULEB128(offset_ptr);
  232. fileEntry.Length = debug_line_data.getULEB128(offset_ptr);
  233. prologue->FileNames.push_back(fileEntry);
  234. }
  235. break;
  236. default:
  237. // Length doesn't include the zero opcode byte or the length itself, but
  238. // it does include the sub_opcode, so we have to adjust for that below
  239. (*offset_ptr) += arg_size;
  240. break;
  241. }
  242. } else if (opcode < prologue->OpcodeBase) {
  243. switch (opcode) {
  244. // Standard Opcodes
  245. case DW_LNS_copy:
  246. // Takes no arguments. Append a row to the matrix using the
  247. // current values of the state-machine registers. Then set
  248. // the basic_block register to false.
  249. state.appendRowToMatrix(*offset_ptr);
  250. break;
  251. case DW_LNS_advance_pc:
  252. // Takes a single unsigned LEB128 operand, multiplies it by the
  253. // min_inst_length field of the prologue, and adds the
  254. // result to the address register of the state machine.
  255. state.Address += debug_line_data.getULEB128(offset_ptr) *
  256. prologue->MinInstLength;
  257. break;
  258. case DW_LNS_advance_line:
  259. // Takes a single signed LEB128 operand and adds that value to
  260. // the line register of the state machine.
  261. state.Line += debug_line_data.getSLEB128(offset_ptr);
  262. break;
  263. case DW_LNS_set_file:
  264. // Takes a single unsigned LEB128 operand and stores it in the file
  265. // register of the state machine.
  266. state.File = debug_line_data.getULEB128(offset_ptr);
  267. break;
  268. case DW_LNS_set_column:
  269. // Takes a single unsigned LEB128 operand and stores it in the
  270. // column register of the state machine.
  271. state.Column = debug_line_data.getULEB128(offset_ptr);
  272. break;
  273. case DW_LNS_negate_stmt:
  274. // Takes no arguments. Set the is_stmt register of the state
  275. // machine to the logical negation of its current value.
  276. state.IsStmt = !state.IsStmt;
  277. break;
  278. case DW_LNS_set_basic_block:
  279. // Takes no arguments. Set the basic_block register of the
  280. // state machine to true
  281. state.BasicBlock = true;
  282. break;
  283. case DW_LNS_const_add_pc:
  284. // Takes no arguments. Add to the address register of the state
  285. // machine the address increment value corresponding to special
  286. // opcode 255. The motivation for DW_LNS_const_add_pc is this:
  287. // when the statement program needs to advance the address by a
  288. // small amount, it can use a single special opcode, which occupies
  289. // a single byte. When it needs to advance the address by up to
  290. // twice the range of the last special opcode, it can use
  291. // DW_LNS_const_add_pc followed by a special opcode, for a total
  292. // of two bytes. Only if it needs to advance the address by more
  293. // than twice that range will it need to use both DW_LNS_advance_pc
  294. // and a special opcode, requiring three or more bytes.
  295. {
  296. uint8_t adjust_opcode = 255 - prologue->OpcodeBase;
  297. uint64_t addr_offset = (adjust_opcode / prologue->LineRange) *
  298. prologue->MinInstLength;
  299. state.Address += addr_offset;
  300. }
  301. break;
  302. case DW_LNS_fixed_advance_pc:
  303. // Takes a single uhalf operand. Add to the address register of
  304. // the state machine the value of the (unencoded) operand. This
  305. // is the only extended opcode that takes an argument that is not
  306. // a variable length number. The motivation for DW_LNS_fixed_advance_pc
  307. // is this: existing assemblers cannot emit DW_LNS_advance_pc or
  308. // special opcodes because they cannot encode LEB128 numbers or
  309. // judge when the computation of a special opcode overflows and
  310. // requires the use of DW_LNS_advance_pc. Such assemblers, however,
  311. // can use DW_LNS_fixed_advance_pc instead, sacrificing compression.
  312. state.Address += debug_line_data.getU16(offset_ptr);
  313. break;
  314. case DW_LNS_set_prologue_end:
  315. // Takes no arguments. Set the prologue_end register of the
  316. // state machine to true
  317. state.PrologueEnd = true;
  318. break;
  319. case DW_LNS_set_epilogue_begin:
  320. // Takes no arguments. Set the basic_block register of the
  321. // state machine to true
  322. state.EpilogueBegin = true;
  323. break;
  324. case DW_LNS_set_isa:
  325. // Takes a single unsigned LEB128 operand and stores it in the
  326. // column register of the state machine.
  327. state.Isa = debug_line_data.getULEB128(offset_ptr);
  328. break;
  329. default:
  330. // Handle any unknown standard opcodes here. We know the lengths
  331. // of such opcodes because they are specified in the prologue
  332. // as a multiple of LEB128 operands for each opcode.
  333. {
  334. assert(opcode - 1U < prologue->StandardOpcodeLengths.size());
  335. uint8_t opcode_length = prologue->StandardOpcodeLengths[opcode - 1];
  336. for (uint8_t i=0; i<opcode_length; ++i)
  337. debug_line_data.getULEB128(offset_ptr);
  338. }
  339. break;
  340. }
  341. } else {
  342. // Special Opcodes
  343. // A special opcode value is chosen based on the amount that needs
  344. // to be added to the line and address registers. The maximum line
  345. // increment for a special opcode is the value of the line_base
  346. // field in the header, plus the value of the line_range field,
  347. // minus 1 (line base + line range - 1). If the desired line
  348. // increment is greater than the maximum line increment, a standard
  349. // opcode must be used instead of a special opcode. The "address
  350. // advance" is calculated by dividing the desired address increment
  351. // by the minimum_instruction_length field from the header. The
  352. // special opcode is then calculated using the following formula:
  353. //
  354. // opcode = (desired line increment - line_base) +
  355. // (line_range * address advance) + opcode_base
  356. //
  357. // If the resulting opcode is greater than 255, a standard opcode
  358. // must be used instead.
  359. //
  360. // To decode a special opcode, subtract the opcode_base from the
  361. // opcode itself to give the adjusted opcode. The amount to
  362. // increment the address register is the result of the adjusted
  363. // opcode divided by the line_range multiplied by the
  364. // minimum_instruction_length field from the header. That is:
  365. //
  366. // address increment = (adjusted opcode / line_range) *
  367. // minimum_instruction_length
  368. //
  369. // The amount to increment the line register is the line_base plus
  370. // the result of the adjusted opcode modulo the line_range. That is:
  371. //
  372. // line increment = line_base + (adjusted opcode % line_range)
  373. uint8_t adjust_opcode = opcode - prologue->OpcodeBase;
  374. uint64_t addr_offset = (adjust_opcode / prologue->LineRange) *
  375. prologue->MinInstLength;
  376. int32_t line_offset = prologue->LineBase +
  377. (adjust_opcode % prologue->LineRange);
  378. state.Line += line_offset;
  379. state.Address += addr_offset;
  380. state.appendRowToMatrix(*offset_ptr);
  381. }
  382. }
  383. state.finalize(*offset_ptr);
  384. return end_offset;
  385. }
  386. static bool findMatchingAddress(const DWARFDebugLine::Row& row1,
  387. const DWARFDebugLine::Row& row2) {
  388. return row1.Address < row2.Address;
  389. }
  390. uint32_t
  391. DWARFDebugLine::LineTable::lookupAddress(uint64_t address,
  392. uint64_t cu_high_pc) const {
  393. uint32_t index = UINT32_MAX;
  394. if (!Rows.empty()) {
  395. // Use the lower_bound algorithm to perform a binary search since we know
  396. // that our line table data is ordered by address.
  397. DWARFDebugLine::Row row;
  398. row.Address = address;
  399. typedef std::vector<Row>::const_iterator iterator;
  400. iterator begin_pos = Rows.begin();
  401. iterator end_pos = Rows.end();
  402. iterator pos = std::lower_bound(begin_pos, end_pos, row,
  403. findMatchingAddress);
  404. if (pos == end_pos) {
  405. if (address < cu_high_pc)
  406. return Rows.size()-1;
  407. } else {
  408. // Rely on fact that we are using a std::vector and we can do
  409. // pointer arithmetic to find the row index (which will be one less
  410. // that what we found since it will find the first position after
  411. // the current address) since std::vector iterators are just
  412. // pointers to the container type.
  413. index = pos - begin_pos;
  414. if (pos->Address > address) {
  415. if (index > 0)
  416. --index;
  417. else
  418. index = UINT32_MAX;
  419. }
  420. }
  421. }
  422. return index; // Failed to find address.
  423. }