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