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