blob: 8ca7ec18d2405827f3b7fd0d9ea7aede0d3f97ff [file] [log] [blame]
Benjamin Kramer5acab502011-09-15 02:12:05 +00001//===-- 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
10#include "DWARFDebugLine.h"
11#include "llvm/Support/Dwarf.h"
12#include "llvm/Support/Format.h"
Alexey Samsonov45be7932012-08-30 07:49:50 +000013#include "llvm/Support/Path.h"
Benjamin Kramer5acab502011-09-15 02:12:05 +000014#include "llvm/Support/raw_ostream.h"
Benjamin Kramera57c46a2011-09-15 02:19:33 +000015#include <algorithm>
Benjamin Kramer5acab502011-09-15 02:12:05 +000016using namespace llvm;
17using namespace dwarf;
18
Alexey Samsonov836b1ae2014-04-29 21:28:13 +000019DWARFDebugLine::Prologue::Prologue() {
20 clear();
21}
22
23void DWARFDebugLine::Prologue::clear() {
24 TotalLength = Version = PrologueLength = 0;
25 MinInstLength = MaxOpsPerInst = DefaultIsStmt = LineBase = LineRange = 0;
26 OpcodeBase = 0;
27 StandardOpcodeLengths.clear();
28 IncludeDirectories.clear();
29 FileNames.clear();
30}
31
Benjamin Kramer5acab502011-09-15 02:12:05 +000032void DWARFDebugLine::Prologue::dump(raw_ostream &OS) const {
33 OS << "Line table prologue:\n"
David Blaikie1d4736e2014-02-24 23:58:54 +000034 << format(" total_length: 0x%8.8x\n", TotalLength)
35 << format(" version: %u\n", Version)
36 << format(" prologue_length: 0x%8.8x\n", PrologueLength)
37 << format(" min_inst_length: %u\n", MinInstLength)
38 << format(Version >= 4 ? "max_ops_per_inst: %u\n" : "", MaxOpsPerInst)
39 << format(" default_is_stmt: %u\n", DefaultIsStmt)
40 << format(" line_base: %i\n", LineBase)
41 << format(" line_range: %u\n", LineRange)
42 << format(" opcode_base: %u\n", OpcodeBase);
Benjamin Kramer5acab502011-09-15 02:12:05 +000043
44 for (uint32_t i = 0; i < StandardOpcodeLengths.size(); ++i)
45 OS << format("standard_opcode_lengths[%s] = %u\n", LNStandardString(i+1),
46 StandardOpcodeLengths[i]);
47
48 if (!IncludeDirectories.empty())
49 for (uint32_t i = 0; i < IncludeDirectories.size(); ++i)
50 OS << format("include_directories[%3u] = '", i+1)
51 << IncludeDirectories[i] << "'\n";
52
53 if (!FileNames.empty()) {
54 OS << " Dir Mod Time File Len File Name\n"
55 << " ---- ---------- ---------- -----------"
56 "----------------\n";
57 for (uint32_t i = 0; i < FileNames.size(); ++i) {
58 const FileNameEntry& fileEntry = FileNames[i];
Benjamin Kramer79730ad2011-11-05 16:01:13 +000059 OS << format("file_names[%3u] %4" PRIu64 " ", i+1, fileEntry.DirIdx)
60 << format("0x%8.8" PRIx64 " 0x%8.8" PRIx64 " ",
61 fileEntry.ModTime, fileEntry.Length)
Benjamin Kramer5acab502011-09-15 02:12:05 +000062 << fileEntry.Name << '\n';
63 }
64 }
65}
66
Alexey Samsonov836b1ae2014-04-29 21:28:13 +000067bool DWARFDebugLine::Prologue::parse(DataExtractor debug_line_data,
68 uint32_t *offset_ptr) {
69 const uint32_t prologue_offset = *offset_ptr;
70
71 clear();
72 TotalLength = debug_line_data.getU32(offset_ptr);
73 Version = debug_line_data.getU16(offset_ptr);
74 if (Version < 2)
75 return false;
76
77 PrologueLength = debug_line_data.getU32(offset_ptr);
78 const uint32_t end_prologue_offset = PrologueLength + *offset_ptr;
79 MinInstLength = debug_line_data.getU8(offset_ptr);
80 if (Version >= 4)
81 MaxOpsPerInst = debug_line_data.getU8(offset_ptr);
82 DefaultIsStmt = debug_line_data.getU8(offset_ptr);
83 LineBase = debug_line_data.getU8(offset_ptr);
84 LineRange = debug_line_data.getU8(offset_ptr);
85 OpcodeBase = debug_line_data.getU8(offset_ptr);
86
87 StandardOpcodeLengths.reserve(OpcodeBase - 1);
88 for (uint32_t i = 1; i < OpcodeBase; ++i) {
89 uint8_t op_len = debug_line_data.getU8(offset_ptr);
90 StandardOpcodeLengths.push_back(op_len);
91 }
92
93 while (*offset_ptr < end_prologue_offset) {
94 const char *s = debug_line_data.getCStr(offset_ptr);
95 if (s && s[0])
96 IncludeDirectories.push_back(s);
97 else
98 break;
99 }
100
101 while (*offset_ptr < end_prologue_offset) {
102 const char *name = debug_line_data.getCStr(offset_ptr);
103 if (name && name[0]) {
104 FileNameEntry fileEntry;
105 fileEntry.Name = name;
106 fileEntry.DirIdx = debug_line_data.getULEB128(offset_ptr);
107 fileEntry.ModTime = debug_line_data.getULEB128(offset_ptr);
108 fileEntry.Length = debug_line_data.getULEB128(offset_ptr);
109 FileNames.push_back(fileEntry);
110 } else {
111 break;
112 }
113 }
114
115 if (*offset_ptr != end_prologue_offset) {
116 fprintf(stderr, "warning: parsing line table prologue at 0x%8.8x should"
117 " have ended at 0x%8.8x but it ended at 0x%8.8x\n",
118 prologue_offset, end_prologue_offset, *offset_ptr);
119 return false;
120 }
121 return true;
122}
123
124DWARFDebugLine::Row::Row(bool default_is_stmt) {
125 reset(default_is_stmt);
126}
127
Benjamin Kramer5acab502011-09-15 02:12:05 +0000128void DWARFDebugLine::Row::postAppend() {
129 BasicBlock = false;
130 PrologueEnd = false;
131 EpilogueBegin = false;
132}
133
134void DWARFDebugLine::Row::reset(bool default_is_stmt) {
135 Address = 0;
136 Line = 1;
137 Column = 0;
138 File = 1;
139 Isa = 0;
Diego Novillo5b5cf502014-02-14 19:27:53 +0000140 Discriminator = 0;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000141 IsStmt = default_is_stmt;
142 BasicBlock = false;
143 EndSequence = false;
144 PrologueEnd = false;
145 EpilogueBegin = false;
146}
147
148void DWARFDebugLine::Row::dump(raw_ostream &OS) const {
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000149 OS << format("0x%16.16" PRIx64 " %6u %6u", Address, Line, Column)
Diego Novillo5b5cf502014-02-14 19:27:53 +0000150 << format(" %6u %3u %13u ", File, Isa, Discriminator)
Benjamin Kramer5acab502011-09-15 02:12:05 +0000151 << (IsStmt ? " is_stmt" : "")
152 << (BasicBlock ? " basic_block" : "")
153 << (PrologueEnd ? " prologue_end" : "")
154 << (EpilogueBegin ? " epilogue_begin" : "")
155 << (EndSequence ? " end_sequence" : "")
156 << '\n';
157}
158
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000159DWARFDebugLine::Sequence::Sequence() {
160 reset();
161}
162
163void DWARFDebugLine::Sequence::reset() {
164 LowPC = 0;
165 HighPC = 0;
166 FirstRowIndex = 0;
167 LastRowIndex = 0;
168 Empty = true;
169}
170
171DWARFDebugLine::LineTable::LineTable() {
172 clear();
173}
174
Benjamin Kramer5acab502011-09-15 02:12:05 +0000175void DWARFDebugLine::LineTable::dump(raw_ostream &OS) const {
176 Prologue.dump(OS);
177 OS << '\n';
178
179 if (!Rows.empty()) {
Diego Novillo5b5cf502014-02-14 19:27:53 +0000180 OS << "Address Line Column File ISA Discriminator Flags\n"
181 << "------------------ ------ ------ ------ --- ------------- "
182 "-------------\n";
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000183 for (const Row &R : Rows) {
184 R.dump(OS);
185 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000186 }
187}
188
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000189void DWARFDebugLine::LineTable::clear() {
190 Prologue.clear();
191 Rows.clear();
192 Sequences.clear();
193}
194
Nick Lewycky4d044922011-09-15 03:41:51 +0000195DWARFDebugLine::State::~State() {}
196
Benjamin Kramer5acab502011-09-15 02:12:05 +0000197void DWARFDebugLine::State::appendRowToMatrix(uint32_t offset) {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000198 if (Sequence::Empty) {
199 // Record the beginning of instruction sequence.
200 Sequence::Empty = false;
201 Sequence::LowPC = Address;
202 Sequence::FirstRowIndex = row;
203 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000204 ++row; // Increase the row number.
205 LineTable::appendRow(*this);
Alexey Samsonov947228c2012-08-07 11:46:57 +0000206 if (EndSequence) {
207 // Record the end of instruction sequence.
208 Sequence::HighPC = Address;
209 Sequence::LastRowIndex = row;
210 if (Sequence::isValid())
211 LineTable::appendSequence(*this);
212 Sequence::reset();
213 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000214 Row::postAppend();
215}
216
Alexey Samsonov947228c2012-08-07 11:46:57 +0000217void DWARFDebugLine::State::finalize() {
218 row = DoneParsingLineTable;
219 if (!Sequence::Empty) {
220 fprintf(stderr, "warning: last sequence in debug line table is not"
221 "terminated!\n");
222 }
223 // Sort all sequences so that address lookup will work faster.
224 if (!Sequences.empty()) {
225 std::sort(Sequences.begin(), Sequences.end(), Sequence::orderByLowPC);
226 // Note: actually, instruction address ranges of sequences should not
227 // overlap (in shared objects and executables). If they do, the address
228 // lookup would still work, though, but result would be ambiguous.
229 // We don't report warning in this case. For example,
230 // sometimes .so compiled from multiple object files contains a few
231 // rudimentary sequences for address ranges [0x0, 0xsomething).
232 }
233}
234
Nick Lewycky4d044922011-09-15 03:41:51 +0000235DWARFDebugLine::DumpingState::~DumpingState() {}
236
Alexey Samsonov947228c2012-08-07 11:46:57 +0000237void DWARFDebugLine::DumpingState::finalize() {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000238 LineTable::dump(OS);
239}
240
Benjamin Kramer5acab502011-09-15 02:12:05 +0000241const DWARFDebugLine::LineTable *
242DWARFDebugLine::getLineTable(uint32_t offset) const {
243 LineTableConstIter pos = LineTableMap.find(offset);
244 if (pos != LineTableMap.end())
245 return &pos->second;
Craig Topper2617dcc2014-04-15 06:32:26 +0000246 return nullptr;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000247}
248
Benjamin Kramer679e1752011-09-15 20:43:18 +0000249const DWARFDebugLine::LineTable *
250DWARFDebugLine::getOrParseLineTable(DataExtractor debug_line_data,
251 uint32_t offset) {
Benjamin Kramer2eeb4e52011-09-21 17:31:42 +0000252 std::pair<LineTableIter, bool> pos =
253 LineTableMap.insert(LineTableMapTy::value_type(offset, LineTable()));
254 if (pos.second) {
Benjamin Kramer679e1752011-09-15 20:43:18 +0000255 // Parse and cache the line table for at this offset.
256 State state;
Andrew Kaylord55d7012013-01-25 22:50:58 +0000257 if (!parseStatementTable(debug_line_data, RelocMap, &offset, state))
Craig Topper2617dcc2014-04-15 06:32:26 +0000258 return nullptr;
Benjamin Kramer2eeb4e52011-09-21 17:31:42 +0000259 pos.first->second = state;
Benjamin Kramer679e1752011-09-15 20:43:18 +0000260 }
Benjamin Kramer2eeb4e52011-09-21 17:31:42 +0000261 return &pos.first->second;
Benjamin Kramer679e1752011-09-15 20:43:18 +0000262}
263
David Blaikie1d4736e2014-02-24 23:58:54 +0000264bool DWARFDebugLine::parseStatementTable(DataExtractor debug_line_data,
265 const RelocAddrMap *RMap,
266 uint32_t *offset_ptr, State &state) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000267 const uint32_t debug_line_offset = *offset_ptr;
268
269 Prologue *prologue = &state.Prologue;
270
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000271 if (!prologue->parse(debug_line_data, offset_ptr)) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000272 // Restore our offset and return false to indicate failure!
273 *offset_ptr = debug_line_offset;
274 return false;
275 }
276
277 const uint32_t end_offset = debug_line_offset + prologue->TotalLength +
278 sizeof(prologue->TotalLength);
279
Benjamin Kramer112ec172011-09-15 21:59:13 +0000280 state.reset();
281
Benjamin Kramer5acab502011-09-15 02:12:05 +0000282 while (*offset_ptr < end_offset) {
283 uint8_t opcode = debug_line_data.getU8(offset_ptr);
284
285 if (opcode == 0) {
286 // Extended Opcodes always start with a zero opcode followed by
287 // a uleb128 length so you can skip ones you don't know about
288 uint32_t ext_offset = *offset_ptr;
289 uint64_t len = debug_line_data.getULEB128(offset_ptr);
290 uint32_t arg_size = len - (*offset_ptr - ext_offset);
291
292 uint8_t sub_opcode = debug_line_data.getU8(offset_ptr);
293 switch (sub_opcode) {
294 case DW_LNE_end_sequence:
295 // Set the end_sequence register of the state machine to true and
296 // append a row to the matrix using the current values of the
297 // state-machine registers. Then reset the registers to the initial
298 // values specified above. Every statement program sequence must end
299 // with a DW_LNE_end_sequence instruction which creates a row whose
300 // address is that of the byte after the last target machine instruction
301 // of the sequence.
302 state.EndSequence = true;
303 state.appendRowToMatrix(*offset_ptr);
304 state.reset();
305 break;
306
307 case DW_LNE_set_address:
308 // Takes a single relocatable address as an operand. The size of the
309 // operand is the size appropriate to hold an address on the target
310 // machine. Set the address register to the value given by the
311 // relocatable address. All of the other statement program opcodes
312 // that affect the address register add a delta to it. This instruction
313 // stores a relocatable value into it instead.
Andrew Kaylord55d7012013-01-25 22:50:58 +0000314 {
315 // If this address is in our relocation map, apply the relocation.
316 RelocAddrMap::const_iterator AI = RMap->find(*offset_ptr);
317 if (AI != RMap->end()) {
318 const std::pair<uint8_t, int64_t> &R = AI->second;
319 state.Address = debug_line_data.getAddress(offset_ptr) + R.second;
320 } else
321 state.Address = debug_line_data.getAddress(offset_ptr);
322 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000323 break;
324
325 case DW_LNE_define_file:
326 // Takes 4 arguments. The first is a null terminated string containing
327 // a source file name. The second is an unsigned LEB128 number
328 // representing the directory index of the directory in which the file
329 // was found. The third is an unsigned LEB128 number representing the
330 // time of last modification of the file. The fourth is an unsigned
331 // LEB128 number representing the length in bytes of the file. The time
332 // and length fields may contain LEB128(0) if the information is not
333 // available.
334 //
335 // The directory index represents an entry in the include_directories
336 // section of the statement program prologue. The index is LEB128(0)
337 // if the file was found in the current directory of the compilation,
338 // LEB128(1) if it was found in the first directory in the
339 // include_directories section, and so on. The directory index is
340 // ignored for file names that represent full path names.
341 //
342 // The files are numbered, starting at 1, in the order in which they
343 // appear; the names in the prologue come before names defined by
344 // the DW_LNE_define_file instruction. These numbers are used in the
345 // the file register of the state machine.
346 {
347 FileNameEntry fileEntry;
348 fileEntry.Name = debug_line_data.getCStr(offset_ptr);
349 fileEntry.DirIdx = debug_line_data.getULEB128(offset_ptr);
350 fileEntry.ModTime = debug_line_data.getULEB128(offset_ptr);
351 fileEntry.Length = debug_line_data.getULEB128(offset_ptr);
352 prologue->FileNames.push_back(fileEntry);
353 }
354 break;
355
Diego Novillo5b5cf502014-02-14 19:27:53 +0000356 case DW_LNE_set_discriminator:
357 state.Discriminator = debug_line_data.getULEB128(offset_ptr);
358 break;
359
Benjamin Kramer5acab502011-09-15 02:12:05 +0000360 default:
361 // Length doesn't include the zero opcode byte or the length itself, but
362 // it does include the sub_opcode, so we have to adjust for that below
363 (*offset_ptr) += arg_size;
364 break;
365 }
366 } else if (opcode < prologue->OpcodeBase) {
367 switch (opcode) {
368 // Standard Opcodes
369 case DW_LNS_copy:
370 // Takes no arguments. Append a row to the matrix using the
371 // current values of the state-machine registers. Then set
372 // the basic_block register to false.
373 state.appendRowToMatrix(*offset_ptr);
374 break;
375
376 case DW_LNS_advance_pc:
377 // Takes a single unsigned LEB128 operand, multiplies it by the
378 // min_inst_length field of the prologue, and adds the
379 // result to the address register of the state machine.
380 state.Address += debug_line_data.getULEB128(offset_ptr) *
381 prologue->MinInstLength;
382 break;
383
384 case DW_LNS_advance_line:
385 // Takes a single signed LEB128 operand and adds that value to
386 // the line register of the state machine.
387 state.Line += debug_line_data.getSLEB128(offset_ptr);
388 break;
389
390 case DW_LNS_set_file:
391 // Takes a single unsigned LEB128 operand and stores it in the file
392 // register of the state machine.
393 state.File = debug_line_data.getULEB128(offset_ptr);
394 break;
395
396 case DW_LNS_set_column:
397 // Takes a single unsigned LEB128 operand and stores it in the
398 // column register of the state machine.
399 state.Column = debug_line_data.getULEB128(offset_ptr);
400 break;
401
402 case DW_LNS_negate_stmt:
403 // Takes no arguments. Set the is_stmt register of the state
404 // machine to the logical negation of its current value.
405 state.IsStmt = !state.IsStmt;
406 break;
407
408 case DW_LNS_set_basic_block:
409 // Takes no arguments. Set the basic_block register of the
410 // state machine to true
411 state.BasicBlock = true;
412 break;
413
414 case DW_LNS_const_add_pc:
415 // Takes no arguments. Add to the address register of the state
416 // machine the address increment value corresponding to special
417 // opcode 255. The motivation for DW_LNS_const_add_pc is this:
418 // when the statement program needs to advance the address by a
419 // small amount, it can use a single special opcode, which occupies
420 // a single byte. When it needs to advance the address by up to
421 // twice the range of the last special opcode, it can use
422 // DW_LNS_const_add_pc followed by a special opcode, for a total
423 // of two bytes. Only if it needs to advance the address by more
424 // than twice that range will it need to use both DW_LNS_advance_pc
425 // and a special opcode, requiring three or more bytes.
426 {
427 uint8_t adjust_opcode = 255 - prologue->OpcodeBase;
428 uint64_t addr_offset = (adjust_opcode / prologue->LineRange) *
429 prologue->MinInstLength;
430 state.Address += addr_offset;
431 }
432 break;
433
434 case DW_LNS_fixed_advance_pc:
435 // Takes a single uhalf operand. Add to the address register of
436 // the state machine the value of the (unencoded) operand. This
437 // is the only extended opcode that takes an argument that is not
438 // a variable length number. The motivation for DW_LNS_fixed_advance_pc
439 // is this: existing assemblers cannot emit DW_LNS_advance_pc or
440 // special opcodes because they cannot encode LEB128 numbers or
441 // judge when the computation of a special opcode overflows and
442 // requires the use of DW_LNS_advance_pc. Such assemblers, however,
443 // can use DW_LNS_fixed_advance_pc instead, sacrificing compression.
444 state.Address += debug_line_data.getU16(offset_ptr);
445 break;
446
447 case DW_LNS_set_prologue_end:
448 // Takes no arguments. Set the prologue_end register of the
449 // state machine to true
450 state.PrologueEnd = true;
451 break;
452
453 case DW_LNS_set_epilogue_begin:
454 // Takes no arguments. Set the basic_block register of the
455 // state machine to true
456 state.EpilogueBegin = true;
457 break;
458
459 case DW_LNS_set_isa:
460 // Takes a single unsigned LEB128 operand and stores it in the
461 // column register of the state machine.
462 state.Isa = debug_line_data.getULEB128(offset_ptr);
463 break;
464
465 default:
466 // Handle any unknown standard opcodes here. We know the lengths
467 // of such opcodes because they are specified in the prologue
468 // as a multiple of LEB128 operands for each opcode.
469 {
Benjamin Kramer21b6f112011-09-15 03:20:04 +0000470 assert(opcode - 1U < prologue->StandardOpcodeLengths.size());
Benjamin Kramer5acab502011-09-15 02:12:05 +0000471 uint8_t opcode_length = prologue->StandardOpcodeLengths[opcode - 1];
472 for (uint8_t i=0; i<opcode_length; ++i)
473 debug_line_data.getULEB128(offset_ptr);
474 }
475 break;
476 }
477 } else {
478 // Special Opcodes
479
480 // A special opcode value is chosen based on the amount that needs
481 // to be added to the line and address registers. The maximum line
482 // increment for a special opcode is the value of the line_base
483 // field in the header, plus the value of the line_range field,
484 // minus 1 (line base + line range - 1). If the desired line
485 // increment is greater than the maximum line increment, a standard
NAKAMURA Takumif9959852011-10-08 11:22:47 +0000486 // opcode must be used instead of a special opcode. The "address
487 // advance" is calculated by dividing the desired address increment
Benjamin Kramer5acab502011-09-15 02:12:05 +0000488 // by the minimum_instruction_length field from the header. The
489 // special opcode is then calculated using the following formula:
490 //
491 // opcode = (desired line increment - line_base) +
492 // (line_range * address advance) + opcode_base
493 //
494 // If the resulting opcode is greater than 255, a standard opcode
495 // must be used instead.
496 //
497 // To decode a special opcode, subtract the opcode_base from the
498 // opcode itself to give the adjusted opcode. The amount to
499 // increment the address register is the result of the adjusted
500 // opcode divided by the line_range multiplied by the
501 // minimum_instruction_length field from the header. That is:
502 //
503 // address increment = (adjusted opcode / line_range) *
504 // minimum_instruction_length
505 //
506 // The amount to increment the line register is the line_base plus
507 // the result of the adjusted opcode modulo the line_range. That is:
508 //
509 // line increment = line_base + (adjusted opcode % line_range)
510
511 uint8_t adjust_opcode = opcode - prologue->OpcodeBase;
512 uint64_t addr_offset = (adjust_opcode / prologue->LineRange) *
513 prologue->MinInstLength;
514 int32_t line_offset = prologue->LineBase +
515 (adjust_opcode % prologue->LineRange);
516 state.Line += line_offset;
517 state.Address += addr_offset;
518 state.appendRowToMatrix(*offset_ptr);
519 }
520 }
521
Alexey Samsonov947228c2012-08-07 11:46:57 +0000522 state.finalize();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000523
524 return end_offset;
525}
526
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000527uint32_t DWARFDebugLine::LineTable::lookupAddress(uint64_t address) const {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000528 uint32_t unknown_index = UINT32_MAX;
529 if (Sequences.empty())
530 return unknown_index;
531 // First, find an instruction sequence containing the given address.
532 DWARFDebugLine::Sequence sequence;
533 sequence.LowPC = address;
534 SequenceIter first_seq = Sequences.begin();
535 SequenceIter last_seq = Sequences.end();
536 SequenceIter seq_pos = std::lower_bound(first_seq, last_seq, sequence,
537 DWARFDebugLine::Sequence::orderByLowPC);
538 DWARFDebugLine::Sequence found_seq;
539 if (seq_pos == last_seq) {
540 found_seq = Sequences.back();
541 } else if (seq_pos->LowPC == address) {
542 found_seq = *seq_pos;
543 } else {
544 if (seq_pos == first_seq)
545 return unknown_index;
546 found_seq = *(seq_pos - 1);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000547 }
Alexey Samsonov947228c2012-08-07 11:46:57 +0000548 if (!found_seq.containsPC(address))
549 return unknown_index;
550 // Search for instruction address in the rows describing the sequence.
551 // Rows are stored in a vector, so we may use arithmetical operations with
552 // iterators.
553 DWARFDebugLine::Row row;
554 row.Address = address;
555 RowIter first_row = Rows.begin() + found_seq.FirstRowIndex;
556 RowIter last_row = Rows.begin() + found_seq.LastRowIndex;
557 RowIter row_pos = std::lower_bound(first_row, last_row, row,
558 DWARFDebugLine::Row::orderByAddress);
559 if (row_pos == last_row) {
560 return found_seq.LastRowIndex - 1;
561 }
562 uint32_t index = found_seq.FirstRowIndex + (row_pos - first_row);
563 if (row_pos->Address > address) {
564 if (row_pos == first_row)
565 return unknown_index;
566 else
567 index--;
568 }
569 return index;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000570}
Alexey Samsonov45be7932012-08-30 07:49:50 +0000571
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000572bool DWARFDebugLine::LineTable::lookupAddressRange(
573 uint64_t address, uint64_t size, std::vector<uint32_t> &result) const {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000574 if (Sequences.empty())
575 return false;
576 uint64_t end_addr = address + size;
577 // First, find an instruction sequence containing the given address.
578 DWARFDebugLine::Sequence sequence;
579 sequence.LowPC = address;
580 SequenceIter first_seq = Sequences.begin();
581 SequenceIter last_seq = Sequences.end();
582 SequenceIter seq_pos = std::lower_bound(first_seq, last_seq, sequence,
583 DWARFDebugLine::Sequence::orderByLowPC);
584 if (seq_pos == last_seq || seq_pos->LowPC != address) {
585 if (seq_pos == first_seq)
586 return false;
587 seq_pos--;
588 }
589 if (!seq_pos->containsPC(address))
590 return false;
591
592 SequenceIter start_pos = seq_pos;
593
594 // Add the rows from the first sequence to the vector, starting with the
595 // index we just calculated
596
597 while (seq_pos != last_seq && seq_pos->LowPC < end_addr) {
598 DWARFDebugLine::Sequence cur_seq = *seq_pos;
599 uint32_t first_row_index;
600 uint32_t last_row_index;
601 if (seq_pos == start_pos) {
602 // For the first sequence, we need to find which row in the sequence is the
603 // first in our range. Rows are stored in a vector, so we may use
604 // arithmetical operations with iterators.
605 DWARFDebugLine::Row row;
606 row.Address = address;
607 RowIter first_row = Rows.begin() + cur_seq.FirstRowIndex;
608 RowIter last_row = Rows.begin() + cur_seq.LastRowIndex;
609 RowIter row_pos = std::upper_bound(first_row, last_row, row,
610 DWARFDebugLine::Row::orderByAddress);
611 // The 'row_pos' iterator references the first row that is greater than
612 // our start address. Unless that's the first row, we want to start at
613 // the row before that.
614 first_row_index = cur_seq.FirstRowIndex + (row_pos - first_row);
615 if (row_pos != first_row)
616 --first_row_index;
617 } else
618 first_row_index = cur_seq.FirstRowIndex;
619
620 // For the last sequence in our range, we need to figure out the last row in
621 // range. For all other sequences we can go to the end of the sequence.
622 if (cur_seq.HighPC > end_addr) {
623 DWARFDebugLine::Row row;
624 row.Address = end_addr;
625 RowIter first_row = Rows.begin() + cur_seq.FirstRowIndex;
626 RowIter last_row = Rows.begin() + cur_seq.LastRowIndex;
627 RowIter row_pos = std::upper_bound(first_row, last_row, row,
628 DWARFDebugLine::Row::orderByAddress);
629 // The 'row_pos' iterator references the first row that is greater than
630 // our end address. The row before that is the last row we want.
631 last_row_index = cur_seq.FirstRowIndex + (row_pos - first_row) - 1;
632 } else
633 // Contrary to what you might expect, DWARFDebugLine::SequenceLastRowIndex
634 // isn't a valid index within the current sequence. It's that plus one.
635 last_row_index = cur_seq.LastRowIndex - 1;
636
637 for (uint32_t i = first_row_index; i <= last_row_index; ++i) {
638 result.push_back(i);
639 }
640
641 ++seq_pos;
642 }
NAKAMURA Takumi4b86cdb2013-01-26 01:45:06 +0000643
644 return true;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000645}
646
647bool
Alexey Samsonov45be7932012-08-30 07:49:50 +0000648DWARFDebugLine::LineTable::getFileNameByIndex(uint64_t FileIndex,
649 bool NeedsAbsoluteFilePath,
650 std::string &Result) const {
651 if (FileIndex == 0 || FileIndex > Prologue.FileNames.size())
652 return false;
653 const FileNameEntry &Entry = Prologue.FileNames[FileIndex - 1];
654 const char *FileName = Entry.Name;
655 if (!NeedsAbsoluteFilePath ||
656 sys::path::is_absolute(FileName)) {
657 Result = FileName;
658 return true;
659 }
660 SmallString<16> FilePath;
661 uint64_t IncludeDirIndex = Entry.DirIdx;
662 // Be defensive about the contents of Entry.
663 if (IncludeDirIndex > 0 &&
664 IncludeDirIndex <= Prologue.IncludeDirectories.size()) {
665 const char *IncludeDir = Prologue.IncludeDirectories[IncludeDirIndex - 1];
666 sys::path::append(FilePath, IncludeDir);
667 }
668 sys::path::append(FilePath, FileName);
669 Result = FilePath.str();
670 return true;
671}