DWARFDebugLine.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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 "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
  10. #include "llvm/Support/Dwarf.h"
  11. #include "llvm/Support/Format.h"
  12. #include "llvm/Support/Path.h"
  13. #include "llvm/Support/raw_ostream.h"
  14. #include <algorithm>
  15. using namespace llvm;
  16. using namespace dwarf;
  17. typedef DILineInfoSpecifier::FileLineInfoKind FileLineInfoKind;
  18. DWARFDebugLine::Prologue::Prologue() {
  19. clear();
  20. }
  21. void DWARFDebugLine::Prologue::clear() {
  22. TotalLength = Version = PrologueLength = 0;
  23. MinInstLength = MaxOpsPerInst = DefaultIsStmt = LineBase = LineRange = 0;
  24. OpcodeBase = 0;
  25. IsDWARF64 = false;
  26. StandardOpcodeLengths.clear();
  27. IncludeDirectories.clear();
  28. FileNames.clear();
  29. }
  30. void DWARFDebugLine::Prologue::dump(raw_ostream &OS) const {
  31. OS << "Line table prologue:\n"
  32. << format(" total_length: 0x%8.8" PRIx64 "\n", TotalLength)
  33. << format(" version: %u\n", Version)
  34. << format(" prologue_length: 0x%8.8" PRIx64 "\n", PrologueLength)
  35. << format(" min_inst_length: %u\n", MinInstLength)
  36. << format(Version >= 4 ? "max_ops_per_inst: %u\n" : "", MaxOpsPerInst)
  37. << format(" default_is_stmt: %u\n", DefaultIsStmt)
  38. << format(" line_base: %i\n", LineBase)
  39. << format(" line_range: %u\n", LineRange)
  40. << format(" opcode_base: %u\n", OpcodeBase);
  41. for (uint32_t i = 0; i < StandardOpcodeLengths.size(); ++i)
  42. OS << format("standard_opcode_lengths[%s] = %u\n", LNStandardString(i+1),
  43. StandardOpcodeLengths[i]);
  44. if (!IncludeDirectories.empty())
  45. for (uint32_t i = 0; i < IncludeDirectories.size(); ++i)
  46. OS << format("include_directories[%3u] = '", i+1)
  47. << IncludeDirectories[i] << "'\n";
  48. if (!FileNames.empty()) {
  49. OS << " Dir Mod Time File Len File Name\n"
  50. << " ---- ---------- ---------- -----------"
  51. "----------------\n";
  52. for (uint32_t i = 0; i < FileNames.size(); ++i) {
  53. const FileNameEntry& fileEntry = FileNames[i];
  54. OS << format("file_names[%3u] %4" PRIu64 " ", i+1, fileEntry.DirIdx)
  55. << format("0x%8.8" PRIx64 " 0x%8.8" PRIx64 " ",
  56. fileEntry.ModTime, fileEntry.Length)
  57. << fileEntry.Name << '\n';
  58. }
  59. }
  60. }
  61. bool DWARFDebugLine::Prologue::parse(DataExtractor debug_line_data,
  62. uint32_t *offset_ptr) {
  63. const uint64_t prologue_offset = *offset_ptr;
  64. clear();
  65. TotalLength = debug_line_data.getU32(offset_ptr);
  66. if (TotalLength == UINT32_MAX) {
  67. IsDWARF64 = true;
  68. TotalLength = debug_line_data.getU64(offset_ptr);
  69. } else if (TotalLength > 0xffffff00) {
  70. return false;
  71. }
  72. Version = debug_line_data.getU16(offset_ptr);
  73. if (Version < 2)
  74. return false;
  75. PrologueLength = debug_line_data.getUnsigned(offset_ptr,
  76. sizeofPrologueLength());
  77. const uint64_t end_prologue_offset = PrologueLength + *offset_ptr;
  78. MinInstLength = debug_line_data.getU8(offset_ptr);
  79. if (Version >= 4)
  80. MaxOpsPerInst = debug_line_data.getU8(offset_ptr);
  81. DefaultIsStmt = debug_line_data.getU8(offset_ptr);
  82. LineBase = debug_line_data.getU8(offset_ptr);
  83. LineRange = debug_line_data.getU8(offset_ptr);
  84. OpcodeBase = debug_line_data.getU8(offset_ptr);
  85. StandardOpcodeLengths.reserve(OpcodeBase - 1);
  86. for (uint32_t i = 1; i < OpcodeBase; ++i) {
  87. uint8_t op_len = debug_line_data.getU8(offset_ptr);
  88. StandardOpcodeLengths.push_back(op_len);
  89. }
  90. while (*offset_ptr < end_prologue_offset) {
  91. const char *s = debug_line_data.getCStr(offset_ptr);
  92. if (s && s[0])
  93. IncludeDirectories.push_back(s);
  94. else
  95. break;
  96. }
  97. while (*offset_ptr < end_prologue_offset) {
  98. const char *name = debug_line_data.getCStr(offset_ptr);
  99. if (name && name[0]) {
  100. FileNameEntry fileEntry;
  101. fileEntry.Name = name;
  102. fileEntry.DirIdx = debug_line_data.getULEB128(offset_ptr);
  103. fileEntry.ModTime = debug_line_data.getULEB128(offset_ptr);
  104. fileEntry.Length = debug_line_data.getULEB128(offset_ptr);
  105. FileNames.push_back(fileEntry);
  106. } else {
  107. break;
  108. }
  109. }
  110. if (*offset_ptr != end_prologue_offset) {
  111. fprintf(stderr, "warning: parsing line table prologue at 0x%8.8" PRIx64
  112. " should have ended at 0x%8.8" PRIx64
  113. " but it ended at 0x%8.8" PRIx64 "\n",
  114. prologue_offset, end_prologue_offset, (uint64_t)*offset_ptr);
  115. return false;
  116. }
  117. return true;
  118. }
  119. DWARFDebugLine::Row::Row(bool default_is_stmt) {
  120. reset(default_is_stmt);
  121. }
  122. void DWARFDebugLine::Row::postAppend() {
  123. BasicBlock = false;
  124. PrologueEnd = false;
  125. EpilogueBegin = false;
  126. }
  127. void DWARFDebugLine::Row::reset(bool default_is_stmt) {
  128. Address = 0;
  129. Line = 1;
  130. Column = 0;
  131. File = 1;
  132. Isa = 0;
  133. Discriminator = 0;
  134. IsStmt = default_is_stmt;
  135. BasicBlock = false;
  136. EndSequence = false;
  137. PrologueEnd = false;
  138. EpilogueBegin = false;
  139. }
  140. void DWARFDebugLine::Row::dump(raw_ostream &OS) const {
  141. OS << format("0x%16.16" PRIx64 " %6u %6u", Address, Line, Column)
  142. << format(" %6u %3u %13u ", File, Isa, Discriminator)
  143. << (IsStmt ? " is_stmt" : "")
  144. << (BasicBlock ? " basic_block" : "")
  145. << (PrologueEnd ? " prologue_end" : "")
  146. << (EpilogueBegin ? " epilogue_begin" : "")
  147. << (EndSequence ? " end_sequence" : "")
  148. << '\n';
  149. }
  150. DWARFDebugLine::Sequence::Sequence() {
  151. reset();
  152. }
  153. void DWARFDebugLine::Sequence::reset() {
  154. LowPC = 0;
  155. HighPC = 0;
  156. FirstRowIndex = 0;
  157. LastRowIndex = 0;
  158. Empty = true;
  159. }
  160. DWARFDebugLine::LineTable::LineTable() {
  161. clear();
  162. }
  163. void DWARFDebugLine::LineTable::dump(raw_ostream &OS) const {
  164. Prologue.dump(OS);
  165. OS << '\n';
  166. if (!Rows.empty()) {
  167. OS << "Address Line Column File ISA Discriminator Flags\n"
  168. << "------------------ ------ ------ ------ --- ------------- "
  169. "-------------\n";
  170. for (const Row &R : Rows) {
  171. R.dump(OS);
  172. }
  173. }
  174. }
  175. void DWARFDebugLine::LineTable::clear() {
  176. Prologue.clear();
  177. Rows.clear();
  178. Sequences.clear();
  179. }
  180. DWARFDebugLine::ParsingState::ParsingState(struct LineTable *LT)
  181. : LineTable(LT), RowNumber(0) {
  182. resetRowAndSequence();
  183. }
  184. void DWARFDebugLine::ParsingState::resetRowAndSequence() {
  185. Row.reset(LineTable->Prologue.DefaultIsStmt);
  186. Sequence.reset();
  187. }
  188. void DWARFDebugLine::ParsingState::appendRowToMatrix(uint32_t offset) {
  189. if (Sequence.Empty) {
  190. // Record the beginning of instruction sequence.
  191. Sequence.Empty = false;
  192. Sequence.LowPC = Row.Address;
  193. Sequence.FirstRowIndex = RowNumber;
  194. }
  195. ++RowNumber;
  196. LineTable->appendRow(Row);
  197. if (Row.EndSequence) {
  198. // Record the end of instruction sequence.
  199. Sequence.HighPC = Row.Address;
  200. Sequence.LastRowIndex = RowNumber;
  201. if (Sequence.isValid())
  202. LineTable->appendSequence(Sequence);
  203. Sequence.reset();
  204. }
  205. Row.postAppend();
  206. }
  207. const DWARFDebugLine::LineTable *
  208. DWARFDebugLine::getLineTable(uint32_t offset) const {
  209. LineTableConstIter pos = LineTableMap.find(offset);
  210. if (pos != LineTableMap.end())
  211. return &pos->second;
  212. return nullptr;
  213. }
  214. const DWARFDebugLine::LineTable *
  215. DWARFDebugLine::getOrParseLineTable(DataExtractor debug_line_data,
  216. uint32_t offset) {
  217. std::pair<LineTableIter, bool> pos =
  218. LineTableMap.insert(LineTableMapTy::value_type(offset, LineTable()));
  219. LineTable *LT = &pos.first->second;
  220. if (pos.second) {
  221. if (!LT->parse(debug_line_data, RelocMap, &offset))
  222. return nullptr;
  223. }
  224. return LT;
  225. }
  226. bool DWARFDebugLine::LineTable::parse(DataExtractor debug_line_data,
  227. const RelocAddrMap *RMap,
  228. uint32_t *offset_ptr) {
  229. const uint32_t debug_line_offset = *offset_ptr;
  230. clear();
  231. if (!Prologue.parse(debug_line_data, offset_ptr)) {
  232. // Restore our offset and return false to indicate failure!
  233. *offset_ptr = debug_line_offset;
  234. return false;
  235. }
  236. const uint32_t end_offset = debug_line_offset + Prologue.TotalLength +
  237. Prologue.sizeofTotalLength();
  238. ParsingState State(this);
  239. while (*offset_ptr < end_offset) {
  240. uint8_t opcode = debug_line_data.getU8(offset_ptr);
  241. if (opcode == 0) {
  242. // Extended Opcodes always start with a zero opcode followed by
  243. // a uleb128 length so you can skip ones you don't know about
  244. uint32_t ext_offset = *offset_ptr;
  245. uint64_t len = debug_line_data.getULEB128(offset_ptr);
  246. uint32_t arg_size = len - (*offset_ptr - ext_offset);
  247. uint8_t sub_opcode = debug_line_data.getU8(offset_ptr);
  248. switch (sub_opcode) {
  249. case DW_LNE_end_sequence:
  250. // Set the end_sequence register of the state machine to true and
  251. // append a row to the matrix using the current values of the
  252. // state-machine registers. Then reset the registers to the initial
  253. // values specified above. Every statement program sequence must end
  254. // with a DW_LNE_end_sequence instruction which creates a row whose
  255. // address is that of the byte after the last target machine instruction
  256. // of the sequence.
  257. State.Row.EndSequence = true;
  258. State.appendRowToMatrix(*offset_ptr);
  259. State.resetRowAndSequence();
  260. break;
  261. case DW_LNE_set_address:
  262. // Takes a single relocatable address as an operand. The size of the
  263. // operand is the size appropriate to hold an address on the target
  264. // machine. Set the address register to the value given by the
  265. // relocatable address. All of the other statement program opcodes
  266. // that affect the address register add a delta to it. This instruction
  267. // stores a relocatable value into it instead.
  268. {
  269. // If this address is in our relocation map, apply the relocation.
  270. RelocAddrMap::const_iterator AI = RMap->find(*offset_ptr);
  271. if (AI != RMap->end()) {
  272. const std::pair<uint8_t, int64_t> &R = AI->second;
  273. State.Row.Address =
  274. debug_line_data.getAddress(offset_ptr) + R.second;
  275. } else
  276. State.Row.Address = debug_line_data.getAddress(offset_ptr);
  277. }
  278. break;
  279. case DW_LNE_define_file:
  280. // Takes 4 arguments. The first is a null terminated string containing
  281. // a source file name. The second is an unsigned LEB128 number
  282. // representing the directory index of the directory in which the file
  283. // was found. The third is an unsigned LEB128 number representing the
  284. // time of last modification of the file. The fourth is an unsigned
  285. // LEB128 number representing the length in bytes of the file. The time
  286. // and length fields may contain LEB128(0) if the information is not
  287. // available.
  288. //
  289. // The directory index represents an entry in the include_directories
  290. // section of the statement program prologue. The index is LEB128(0)
  291. // if the file was found in the current directory of the compilation,
  292. // LEB128(1) if it was found in the first directory in the
  293. // include_directories section, and so on. The directory index is
  294. // ignored for file names that represent full path names.
  295. //
  296. // The files are numbered, starting at 1, in the order in which they
  297. // appear; the names in the prologue come before names defined by
  298. // the DW_LNE_define_file instruction. These numbers are used in the
  299. // the file register of the state machine.
  300. {
  301. FileNameEntry fileEntry;
  302. fileEntry.Name = debug_line_data.getCStr(offset_ptr);
  303. fileEntry.DirIdx = debug_line_data.getULEB128(offset_ptr);
  304. fileEntry.ModTime = debug_line_data.getULEB128(offset_ptr);
  305. fileEntry.Length = debug_line_data.getULEB128(offset_ptr);
  306. Prologue.FileNames.push_back(fileEntry);
  307. }
  308. break;
  309. case DW_LNE_set_discriminator:
  310. State.Row.Discriminator = debug_line_data.getULEB128(offset_ptr);
  311. break;
  312. default:
  313. // Length doesn't include the zero opcode byte or the length itself, but
  314. // it does include the sub_opcode, so we have to adjust for that below
  315. (*offset_ptr) += arg_size;
  316. break;
  317. }
  318. } else if (opcode < Prologue.OpcodeBase) {
  319. switch (opcode) {
  320. // Standard Opcodes
  321. case DW_LNS_copy:
  322. // Takes no arguments. Append a row to the matrix using the
  323. // current values of the state-machine registers. Then set
  324. // the basic_block register to false.
  325. State.appendRowToMatrix(*offset_ptr);
  326. break;
  327. case DW_LNS_advance_pc:
  328. // Takes a single unsigned LEB128 operand, multiplies it by the
  329. // min_inst_length field of the prologue, and adds the
  330. // result to the address register of the state machine.
  331. State.Row.Address +=
  332. debug_line_data.getULEB128(offset_ptr) * Prologue.MinInstLength;
  333. break;
  334. case DW_LNS_advance_line:
  335. // Takes a single signed LEB128 operand and adds that value to
  336. // the line register of the state machine.
  337. State.Row.Line += debug_line_data.getSLEB128(offset_ptr);
  338. break;
  339. case DW_LNS_set_file:
  340. // Takes a single unsigned LEB128 operand and stores it in the file
  341. // register of the state machine.
  342. State.Row.File = debug_line_data.getULEB128(offset_ptr);
  343. break;
  344. case DW_LNS_set_column:
  345. // Takes a single unsigned LEB128 operand and stores it in the
  346. // column register of the state machine.
  347. State.Row.Column = debug_line_data.getULEB128(offset_ptr);
  348. break;
  349. case DW_LNS_negate_stmt:
  350. // Takes no arguments. Set the is_stmt register of the state
  351. // machine to the logical negation of its current value.
  352. State.Row.IsStmt = !State.Row.IsStmt;
  353. break;
  354. case DW_LNS_set_basic_block:
  355. // Takes no arguments. Set the basic_block register of the
  356. // state machine to true
  357. State.Row.BasicBlock = true;
  358. break;
  359. case DW_LNS_const_add_pc:
  360. // Takes no arguments. Add to the address register of the state
  361. // machine the address increment value corresponding to special
  362. // opcode 255. The motivation for DW_LNS_const_add_pc is this:
  363. // when the statement program needs to advance the address by a
  364. // small amount, it can use a single special opcode, which occupies
  365. // a single byte. When it needs to advance the address by up to
  366. // twice the range of the last special opcode, it can use
  367. // DW_LNS_const_add_pc followed by a special opcode, for a total
  368. // of two bytes. Only if it needs to advance the address by more
  369. // than twice that range will it need to use both DW_LNS_advance_pc
  370. // and a special opcode, requiring three or more bytes.
  371. {
  372. uint8_t adjust_opcode = 255 - Prologue.OpcodeBase;
  373. uint64_t addr_offset =
  374. (adjust_opcode / Prologue.LineRange) * Prologue.MinInstLength;
  375. State.Row.Address += addr_offset;
  376. }
  377. break;
  378. case DW_LNS_fixed_advance_pc:
  379. // Takes a single uhalf operand. Add to the address register of
  380. // the state machine the value of the (unencoded) operand. This
  381. // is the only extended opcode that takes an argument that is not
  382. // a variable length number. The motivation for DW_LNS_fixed_advance_pc
  383. // is this: existing assemblers cannot emit DW_LNS_advance_pc or
  384. // special opcodes because they cannot encode LEB128 numbers or
  385. // judge when the computation of a special opcode overflows and
  386. // requires the use of DW_LNS_advance_pc. Such assemblers, however,
  387. // can use DW_LNS_fixed_advance_pc instead, sacrificing compression.
  388. State.Row.Address += debug_line_data.getU16(offset_ptr);
  389. break;
  390. case DW_LNS_set_prologue_end:
  391. // Takes no arguments. Set the prologue_end register of the
  392. // state machine to true
  393. State.Row.PrologueEnd = true;
  394. break;
  395. case DW_LNS_set_epilogue_begin:
  396. // Takes no arguments. Set the basic_block register of the
  397. // state machine to true
  398. State.Row.EpilogueBegin = true;
  399. break;
  400. case DW_LNS_set_isa:
  401. // Takes a single unsigned LEB128 operand and stores it in the
  402. // column register of the state machine.
  403. State.Row.Isa = debug_line_data.getULEB128(offset_ptr);
  404. break;
  405. default:
  406. // Handle any unknown standard opcodes here. We know the lengths
  407. // of such opcodes because they are specified in the prologue
  408. // as a multiple of LEB128 operands for each opcode.
  409. {
  410. assert(opcode - 1U < Prologue.StandardOpcodeLengths.size());
  411. uint8_t opcode_length = Prologue.StandardOpcodeLengths[opcode - 1];
  412. for (uint8_t i = 0; i < opcode_length; ++i)
  413. debug_line_data.getULEB128(offset_ptr);
  414. }
  415. break;
  416. }
  417. } else {
  418. // Special Opcodes
  419. // A special opcode value is chosen based on the amount that needs
  420. // to be added to the line and address registers. The maximum line
  421. // increment for a special opcode is the value of the line_base
  422. // field in the header, plus the value of the line_range field,
  423. // minus 1 (line base + line range - 1). If the desired line
  424. // increment is greater than the maximum line increment, a standard
  425. // opcode must be used instead of a special opcode. The "address
  426. // advance" is calculated by dividing the desired address increment
  427. // by the minimum_instruction_length field from the header. The
  428. // special opcode is then calculated using the following formula:
  429. //
  430. // opcode = (desired line increment - line_base) +
  431. // (line_range * address advance) + opcode_base
  432. //
  433. // If the resulting opcode is greater than 255, a standard opcode
  434. // must be used instead.
  435. //
  436. // To decode a special opcode, subtract the opcode_base from the
  437. // opcode itself to give the adjusted opcode. The amount to
  438. // increment the address register is the result of the adjusted
  439. // opcode divided by the line_range multiplied by the
  440. // minimum_instruction_length field from the header. That is:
  441. //
  442. // address increment = (adjusted opcode / line_range) *
  443. // minimum_instruction_length
  444. //
  445. // The amount to increment the line register is the line_base plus
  446. // the result of the adjusted opcode modulo the line_range. That is:
  447. //
  448. // line increment = line_base + (adjusted opcode % line_range)
  449. uint8_t adjust_opcode = opcode - Prologue.OpcodeBase;
  450. uint64_t addr_offset =
  451. (adjust_opcode / Prologue.LineRange) * Prologue.MinInstLength;
  452. int32_t line_offset =
  453. Prologue.LineBase + (adjust_opcode % Prologue.LineRange);
  454. State.Row.Line += line_offset;
  455. State.Row.Address += addr_offset;
  456. State.appendRowToMatrix(*offset_ptr);
  457. }
  458. }
  459. if (!State.Sequence.Empty) {
  460. fprintf(stderr, "warning: last sequence in debug line table is not"
  461. "terminated!\n");
  462. }
  463. // Sort all sequences so that address lookup will work faster.
  464. if (!Sequences.empty()) {
  465. std::sort(Sequences.begin(), Sequences.end(), Sequence::orderByLowPC);
  466. // Note: actually, instruction address ranges of sequences should not
  467. // overlap (in shared objects and executables). If they do, the address
  468. // lookup would still work, though, but result would be ambiguous.
  469. // We don't report warning in this case. For example,
  470. // sometimes .so compiled from multiple object files contains a few
  471. // rudimentary sequences for address ranges [0x0, 0xsomething).
  472. }
  473. return end_offset;
  474. }
  475. uint32_t
  476. DWARFDebugLine::LineTable::findRowInSeq(const DWARFDebugLine::Sequence &seq,
  477. uint64_t address) const {
  478. if (!seq.containsPC(address))
  479. return UnknownRowIndex;
  480. // Search for instruction address in the rows describing the sequence.
  481. // Rows are stored in a vector, so we may use arithmetical operations with
  482. // iterators.
  483. DWARFDebugLine::Row row;
  484. row.Address = address;
  485. RowIter first_row = Rows.begin() + seq.FirstRowIndex;
  486. RowIter last_row = Rows.begin() + seq.LastRowIndex;
  487. LineTable::RowIter row_pos = std::lower_bound(
  488. first_row, last_row, row, DWARFDebugLine::Row::orderByAddress);
  489. if (row_pos == last_row) {
  490. return seq.LastRowIndex - 1;
  491. }
  492. uint32_t index = seq.FirstRowIndex + (row_pos - first_row);
  493. if (row_pos->Address > address) {
  494. if (row_pos == first_row)
  495. return UnknownRowIndex;
  496. else
  497. index--;
  498. }
  499. return index;
  500. }
  501. uint32_t DWARFDebugLine::LineTable::lookupAddress(uint64_t address) const {
  502. if (Sequences.empty())
  503. return UnknownRowIndex;
  504. // First, find an instruction sequence containing the given address.
  505. DWARFDebugLine::Sequence sequence;
  506. sequence.LowPC = address;
  507. SequenceIter first_seq = Sequences.begin();
  508. SequenceIter last_seq = Sequences.end();
  509. SequenceIter seq_pos = std::lower_bound(first_seq, last_seq, sequence,
  510. DWARFDebugLine::Sequence::orderByLowPC);
  511. DWARFDebugLine::Sequence found_seq;
  512. if (seq_pos == last_seq) {
  513. found_seq = Sequences.back();
  514. } else if (seq_pos->LowPC == address) {
  515. found_seq = *seq_pos;
  516. } else {
  517. if (seq_pos == first_seq)
  518. return UnknownRowIndex;
  519. found_seq = *(seq_pos - 1);
  520. }
  521. return findRowInSeq(found_seq, address);
  522. }
  523. bool DWARFDebugLine::LineTable::lookupAddressRange(
  524. uint64_t address, uint64_t size, std::vector<uint32_t> &result) const {
  525. if (Sequences.empty())
  526. return false;
  527. uint64_t end_addr = address + size;
  528. // First, find an instruction sequence containing the given address.
  529. DWARFDebugLine::Sequence sequence;
  530. sequence.LowPC = address;
  531. SequenceIter first_seq = Sequences.begin();
  532. SequenceIter last_seq = Sequences.end();
  533. SequenceIter seq_pos = std::lower_bound(first_seq, last_seq, sequence,
  534. DWARFDebugLine::Sequence::orderByLowPC);
  535. if (seq_pos == last_seq || seq_pos->LowPC != address) {
  536. if (seq_pos == first_seq)
  537. return false;
  538. seq_pos--;
  539. }
  540. if (!seq_pos->containsPC(address))
  541. return false;
  542. SequenceIter start_pos = seq_pos;
  543. // Add the rows from the first sequence to the vector, starting with the
  544. // index we just calculated
  545. while (seq_pos != last_seq && seq_pos->LowPC < end_addr) {
  546. const DWARFDebugLine::Sequence &cur_seq = *seq_pos;
  547. // For the first sequence, we need to find which row in the sequence is the
  548. // first in our range.
  549. uint32_t first_row_index = cur_seq.FirstRowIndex;
  550. if (seq_pos == start_pos)
  551. first_row_index = findRowInSeq(cur_seq, address);
  552. // Figure out the last row in the range.
  553. uint32_t last_row_index = findRowInSeq(cur_seq, end_addr - 1);
  554. if (last_row_index == UnknownRowIndex)
  555. last_row_index = cur_seq.LastRowIndex - 1;
  556. assert(first_row_index != UnknownRowIndex);
  557. assert(last_row_index != UnknownRowIndex);
  558. for (uint32_t i = first_row_index; i <= last_row_index; ++i) {
  559. result.push_back(i);
  560. }
  561. ++seq_pos;
  562. }
  563. return true;
  564. }
  565. bool
  566. DWARFDebugLine::LineTable::getFileNameByIndex(uint64_t FileIndex,
  567. const char *CompDir,
  568. FileLineInfoKind Kind,
  569. std::string &Result) const {
  570. if (FileIndex == 0 || FileIndex > Prologue.FileNames.size() ||
  571. Kind == FileLineInfoKind::None)
  572. return false;
  573. const FileNameEntry &Entry = Prologue.FileNames[FileIndex - 1];
  574. const char *FileName = Entry.Name;
  575. if (Kind != FileLineInfoKind::AbsoluteFilePath ||
  576. sys::path::is_absolute(FileName)) {
  577. Result = FileName;
  578. return true;
  579. }
  580. SmallString<16> FilePath;
  581. uint64_t IncludeDirIndex = Entry.DirIdx;
  582. const char *IncludeDir = "";
  583. // Be defensive about the contents of Entry.
  584. if (IncludeDirIndex > 0 &&
  585. IncludeDirIndex <= Prologue.IncludeDirectories.size())
  586. IncludeDir = Prologue.IncludeDirectories[IncludeDirIndex - 1];
  587. // We may still need to append compilation directory of compile unit.
  588. // We know that FileName is not absolute, the only way to have an
  589. // absolute path at this point would be if IncludeDir is absolute.
  590. if (CompDir && Kind == FileLineInfoKind::AbsoluteFilePath &&
  591. sys::path::is_relative(IncludeDir))
  592. sys::path::append(FilePath, CompDir);
  593. // sys::path::append skips empty strings.
  594. sys::path::append(FilePath, IncludeDir, FileName);
  595. Result = FilePath.str();
  596. return true;
  597. }
  598. bool
  599. DWARFDebugLine::LineTable::getFileLineInfoForAddress(uint64_t Address,
  600. const char *CompDir,
  601. FileLineInfoKind Kind,
  602. DILineInfo &Result) const {
  603. // Get the index of row we're looking for in the line table.
  604. uint32_t RowIndex = lookupAddress(Address);
  605. if (RowIndex == -1U)
  606. return false;
  607. // Take file number and line/column from the row.
  608. const auto &Row = Rows[RowIndex];
  609. if (!getFileNameByIndex(Row.File, CompDir, Kind, Result.FileName))
  610. return false;
  611. Result.Line = Row.Line;
  612. Result.Column = Row.Column;
  613. return true;
  614. }