LLVM API Documentation

DWARFDebugLine.cpp
Go to the documentation of this file.
00001 //===-- DWARFDebugLine.cpp ------------------------------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #include "DWARFDebugLine.h"
00011 #include "llvm/Support/Dwarf.h"
00012 #include "llvm/Support/Format.h"
00013 #include "llvm/Support/Path.h"
00014 #include "llvm/Support/raw_ostream.h"
00015 #include <algorithm>
00016 using namespace llvm;
00017 using namespace dwarf;
00018 
00019 void DWARFDebugLine::Prologue::dump(raw_ostream &OS) const {
00020   OS << "Line table prologue:\n"
00021      << format("    total_length: 0x%8.8x\n", TotalLength)
00022      << format("         version: %u\n", Version)
00023      << format(" prologue_length: 0x%8.8x\n", PrologueLength)
00024      << format(" min_inst_length: %u\n", MinInstLength)
00025      << format(Version >= 4 ? "max_ops_per_inst: %u\n" : "", MaxOpsPerInst)
00026      << format(" default_is_stmt: %u\n", DefaultIsStmt)
00027      << format("       line_base: %i\n", LineBase)
00028      << format("      line_range: %u\n", LineRange)
00029      << format("     opcode_base: %u\n", OpcodeBase);
00030 
00031   for (uint32_t i = 0; i < StandardOpcodeLengths.size(); ++i)
00032     OS << format("standard_opcode_lengths[%s] = %u\n", LNStandardString(i+1),
00033                  StandardOpcodeLengths[i]);
00034 
00035   if (!IncludeDirectories.empty())
00036     for (uint32_t i = 0; i < IncludeDirectories.size(); ++i)
00037       OS << format("include_directories[%3u] = '", i+1)
00038          << IncludeDirectories[i] << "'\n";
00039 
00040   if (!FileNames.empty()) {
00041     OS << "                Dir  Mod Time   File Len   File Name\n"
00042        << "                ---- ---------- ---------- -----------"
00043           "----------------\n";
00044     for (uint32_t i = 0; i < FileNames.size(); ++i) {
00045       const FileNameEntry& fileEntry = FileNames[i];
00046       OS << format("file_names[%3u] %4" PRIu64 " ", i+1, fileEntry.DirIdx)
00047          << format("0x%8.8" PRIx64 " 0x%8.8" PRIx64 " ",
00048                    fileEntry.ModTime, fileEntry.Length)
00049          << fileEntry.Name << '\n';
00050     }
00051   }
00052 }
00053 
00054 void DWARFDebugLine::Row::postAppend() {
00055   BasicBlock = false;
00056   PrologueEnd = false;
00057   EpilogueBegin = false;
00058 }
00059 
00060 void DWARFDebugLine::Row::reset(bool default_is_stmt) {
00061   Address = 0;
00062   Line = 1;
00063   Column = 0;
00064   File = 1;
00065   Isa = 0;
00066   Discriminator = 0;
00067   IsStmt = default_is_stmt;
00068   BasicBlock = false;
00069   EndSequence = false;
00070   PrologueEnd = false;
00071   EpilogueBegin = false;
00072 }
00073 
00074 void DWARFDebugLine::Row::dump(raw_ostream &OS) const {
00075   OS << format("0x%16.16" PRIx64 " %6u %6u", Address, Line, Column)
00076      << format(" %6u %3u %13u ", File, Isa, Discriminator)
00077      << (IsStmt ? " is_stmt" : "")
00078      << (BasicBlock ? " basic_block" : "")
00079      << (PrologueEnd ? " prologue_end" : "")
00080      << (EpilogueBegin ? " epilogue_begin" : "")
00081      << (EndSequence ? " end_sequence" : "")
00082      << '\n';
00083 }
00084 
00085 void DWARFDebugLine::LineTable::dump(raw_ostream &OS) const {
00086   Prologue.dump(OS);
00087   OS << '\n';
00088 
00089   if (!Rows.empty()) {
00090     OS << "Address            Line   Column File   ISA Discriminator Flags\n"
00091        << "------------------ ------ ------ ------ --- ------------- "
00092           "-------------\n";
00093     for (const Row &R : Rows) {
00094       R.dump(OS);
00095     }
00096   }
00097 }
00098 
00099 DWARFDebugLine::State::~State() {}
00100 
00101 void DWARFDebugLine::State::appendRowToMatrix(uint32_t offset) {
00102   if (Sequence::Empty) {
00103     // Record the beginning of instruction sequence.
00104     Sequence::Empty = false;
00105     Sequence::LowPC = Address;
00106     Sequence::FirstRowIndex = row;
00107   }
00108   ++row;  // Increase the row number.
00109   LineTable::appendRow(*this);
00110   if (EndSequence) {
00111     // Record the end of instruction sequence.
00112     Sequence::HighPC = Address;
00113     Sequence::LastRowIndex = row;
00114     if (Sequence::isValid())
00115       LineTable::appendSequence(*this);
00116     Sequence::reset();
00117   }
00118   Row::postAppend();
00119 }
00120 
00121 void DWARFDebugLine::State::finalize() {
00122   row = DoneParsingLineTable;
00123   if (!Sequence::Empty) {
00124     fprintf(stderr, "warning: last sequence in debug line table is not"
00125                     "terminated!\n");
00126   }
00127   // Sort all sequences so that address lookup will work faster.
00128   if (!Sequences.empty()) {
00129     std::sort(Sequences.begin(), Sequences.end(), Sequence::orderByLowPC);
00130     // Note: actually, instruction address ranges of sequences should not
00131     // overlap (in shared objects and executables). If they do, the address
00132     // lookup would still work, though, but result would be ambiguous.
00133     // We don't report warning in this case. For example,
00134     // sometimes .so compiled from multiple object files contains a few
00135     // rudimentary sequences for address ranges [0x0, 0xsomething).
00136   }
00137 }
00138 
00139 DWARFDebugLine::DumpingState::~DumpingState() {}
00140 
00141 void DWARFDebugLine::DumpingState::finalize() {
00142   LineTable::dump(OS);
00143 }
00144 
00145 const DWARFDebugLine::LineTable *
00146 DWARFDebugLine::getLineTable(uint32_t offset) const {
00147   LineTableConstIter pos = LineTableMap.find(offset);
00148   if (pos != LineTableMap.end())
00149     return &pos->second;
00150   return nullptr;
00151 }
00152 
00153 const DWARFDebugLine::LineTable *
00154 DWARFDebugLine::getOrParseLineTable(DataExtractor debug_line_data,
00155                                     uint32_t offset) {
00156   std::pair<LineTableIter, bool> pos =
00157     LineTableMap.insert(LineTableMapTy::value_type(offset, LineTable()));
00158   if (pos.second) {
00159     // Parse and cache the line table for at this offset.
00160     State state;
00161     if (!parseStatementTable(debug_line_data, RelocMap, &offset, state))
00162       return nullptr;
00163     pos.first->second = state;
00164   }
00165   return &pos.first->second;
00166 }
00167 
00168 bool
00169 DWARFDebugLine::parsePrologue(DataExtractor debug_line_data,
00170                               uint32_t *offset_ptr, Prologue *prologue) {
00171   const uint32_t prologue_offset = *offset_ptr;
00172 
00173   prologue->clear();
00174   prologue->TotalLength = debug_line_data.getU32(offset_ptr);
00175   prologue->Version = debug_line_data.getU16(offset_ptr);
00176   if (prologue->Version < 2)
00177     return false;
00178 
00179   prologue->PrologueLength = debug_line_data.getU32(offset_ptr);
00180   const uint32_t end_prologue_offset = prologue->PrologueLength + *offset_ptr;
00181   prologue->MinInstLength = debug_line_data.getU8(offset_ptr);
00182   if (prologue->Version >= 4)
00183     prologue->MaxOpsPerInst = debug_line_data.getU8(offset_ptr);
00184   prologue->DefaultIsStmt = debug_line_data.getU8(offset_ptr);
00185   prologue->LineBase = debug_line_data.getU8(offset_ptr);
00186   prologue->LineRange = debug_line_data.getU8(offset_ptr);
00187   prologue->OpcodeBase = debug_line_data.getU8(offset_ptr);
00188 
00189   prologue->StandardOpcodeLengths.reserve(prologue->OpcodeBase-1);
00190   for (uint32_t i = 1; i < prologue->OpcodeBase; ++i) {
00191     uint8_t op_len = debug_line_data.getU8(offset_ptr);
00192     prologue->StandardOpcodeLengths.push_back(op_len);
00193   }
00194 
00195   while (*offset_ptr < end_prologue_offset) {
00196     const char *s = debug_line_data.getCStr(offset_ptr);
00197     if (s && s[0])
00198       prologue->IncludeDirectories.push_back(s);
00199     else
00200       break;
00201   }
00202 
00203   while (*offset_ptr < end_prologue_offset) {
00204     const char *name = debug_line_data.getCStr(offset_ptr);
00205     if (name && name[0]) {
00206       FileNameEntry fileEntry;
00207       fileEntry.Name = name;
00208       fileEntry.DirIdx = debug_line_data.getULEB128(offset_ptr);
00209       fileEntry.ModTime = debug_line_data.getULEB128(offset_ptr);
00210       fileEntry.Length = debug_line_data.getULEB128(offset_ptr);
00211       prologue->FileNames.push_back(fileEntry);
00212     } else {
00213       break;
00214     }
00215   }
00216 
00217   if (*offset_ptr != end_prologue_offset) {
00218     fprintf(stderr, "warning: parsing line table prologue at 0x%8.8x should"
00219                     " have ended at 0x%8.8x but it ended at 0x%8.8x\n",
00220             prologue_offset, end_prologue_offset, *offset_ptr);
00221     return false;
00222   }
00223   return true;
00224 }
00225 
00226 bool DWARFDebugLine::parseStatementTable(DataExtractor debug_line_data,
00227                                          const RelocAddrMap *RMap,
00228                                          uint32_t *offset_ptr, State &state) {
00229   const uint32_t debug_line_offset = *offset_ptr;
00230 
00231   Prologue *prologue = &state.Prologue;
00232 
00233   if (!parsePrologue(debug_line_data, offset_ptr, prologue)) {
00234     // Restore our offset and return false to indicate failure!
00235     *offset_ptr = debug_line_offset;
00236     return false;
00237   }
00238 
00239   const uint32_t end_offset = debug_line_offset + prologue->TotalLength +
00240                               sizeof(prologue->TotalLength);
00241 
00242   state.reset();
00243 
00244   while (*offset_ptr < end_offset) {
00245     uint8_t opcode = debug_line_data.getU8(offset_ptr);
00246 
00247     if (opcode == 0) {
00248       // Extended Opcodes always start with a zero opcode followed by
00249       // a uleb128 length so you can skip ones you don't know about
00250       uint32_t ext_offset = *offset_ptr;
00251       uint64_t len = debug_line_data.getULEB128(offset_ptr);
00252       uint32_t arg_size = len - (*offset_ptr - ext_offset);
00253 
00254       uint8_t sub_opcode = debug_line_data.getU8(offset_ptr);
00255       switch (sub_opcode) {
00256       case DW_LNE_end_sequence:
00257         // Set the end_sequence register of the state machine to true and
00258         // append a row to the matrix using the current values of the
00259         // state-machine registers. Then reset the registers to the initial
00260         // values specified above. Every statement program sequence must end
00261         // with a DW_LNE_end_sequence instruction which creates a row whose
00262         // address is that of the byte after the last target machine instruction
00263         // of the sequence.
00264         state.EndSequence = true;
00265         state.appendRowToMatrix(*offset_ptr);
00266         state.reset();
00267         break;
00268 
00269       case DW_LNE_set_address:
00270         // Takes a single relocatable address as an operand. The size of the
00271         // operand is the size appropriate to hold an address on the target
00272         // machine. Set the address register to the value given by the
00273         // relocatable address. All of the other statement program opcodes
00274         // that affect the address register add a delta to it. This instruction
00275         // stores a relocatable value into it instead.
00276         {
00277           // If this address is in our relocation map, apply the relocation.
00278           RelocAddrMap::const_iterator AI = RMap->find(*offset_ptr);
00279           if (AI != RMap->end()) {
00280              const std::pair<uint8_t, int64_t> &R = AI->second;
00281              state.Address = debug_line_data.getAddress(offset_ptr) + R.second;
00282           } else
00283             state.Address = debug_line_data.getAddress(offset_ptr);
00284         }
00285         break;
00286 
00287       case DW_LNE_define_file:
00288         // Takes 4 arguments. The first is a null terminated string containing
00289         // a source file name. The second is an unsigned LEB128 number
00290         // representing the directory index of the directory in which the file
00291         // was found. The third is an unsigned LEB128 number representing the
00292         // time of last modification of the file. The fourth is an unsigned
00293         // LEB128 number representing the length in bytes of the file. The time
00294         // and length fields may contain LEB128(0) if the information is not
00295         // available.
00296         //
00297         // The directory index represents an entry in the include_directories
00298         // section of the statement program prologue. The index is LEB128(0)
00299         // if the file was found in the current directory of the compilation,
00300         // LEB128(1) if it was found in the first directory in the
00301         // include_directories section, and so on. The directory index is
00302         // ignored for file names that represent full path names.
00303         //
00304         // The files are numbered, starting at 1, in the order in which they
00305         // appear; the names in the prologue come before names defined by
00306         // the DW_LNE_define_file instruction. These numbers are used in the
00307         // the file register of the state machine.
00308         {
00309           FileNameEntry fileEntry;
00310           fileEntry.Name = debug_line_data.getCStr(offset_ptr);
00311           fileEntry.DirIdx = debug_line_data.getULEB128(offset_ptr);
00312           fileEntry.ModTime = debug_line_data.getULEB128(offset_ptr);
00313           fileEntry.Length = debug_line_data.getULEB128(offset_ptr);
00314           prologue->FileNames.push_back(fileEntry);
00315         }
00316         break;
00317 
00318       case DW_LNE_set_discriminator:
00319         state.Discriminator = debug_line_data.getULEB128(offset_ptr);
00320         break;
00321 
00322       default:
00323         // Length doesn't include the zero opcode byte or the length itself, but
00324         // it does include the sub_opcode, so we have to adjust for that below
00325         (*offset_ptr) += arg_size;
00326         break;
00327       }
00328     } else if (opcode < prologue->OpcodeBase) {
00329       switch (opcode) {
00330       // Standard Opcodes
00331       case DW_LNS_copy:
00332         // Takes no arguments. Append a row to the matrix using the
00333         // current values of the state-machine registers. Then set
00334         // the basic_block register to false.
00335         state.appendRowToMatrix(*offset_ptr);
00336         break;
00337 
00338       case DW_LNS_advance_pc:
00339         // Takes a single unsigned LEB128 operand, multiplies it by the
00340         // min_inst_length field of the prologue, and adds the
00341         // result to the address register of the state machine.
00342         state.Address += debug_line_data.getULEB128(offset_ptr) *
00343                          prologue->MinInstLength;
00344         break;
00345 
00346       case DW_LNS_advance_line:
00347         // Takes a single signed LEB128 operand and adds that value to
00348         // the line register of the state machine.
00349         state.Line += debug_line_data.getSLEB128(offset_ptr);
00350         break;
00351 
00352       case DW_LNS_set_file:
00353         // Takes a single unsigned LEB128 operand and stores it in the file
00354         // register of the state machine.
00355         state.File = debug_line_data.getULEB128(offset_ptr);
00356         break;
00357 
00358       case DW_LNS_set_column:
00359         // Takes a single unsigned LEB128 operand and stores it in the
00360         // column register of the state machine.
00361         state.Column = debug_line_data.getULEB128(offset_ptr);
00362         break;
00363 
00364       case DW_LNS_negate_stmt:
00365         // Takes no arguments. Set the is_stmt register of the state
00366         // machine to the logical negation of its current value.
00367         state.IsStmt = !state.IsStmt;
00368         break;
00369 
00370       case DW_LNS_set_basic_block:
00371         // Takes no arguments. Set the basic_block register of the
00372         // state machine to true
00373         state.BasicBlock = true;
00374         break;
00375 
00376       case DW_LNS_const_add_pc:
00377         // Takes no arguments. Add to the address register of the state
00378         // machine the address increment value corresponding to special
00379         // opcode 255. The motivation for DW_LNS_const_add_pc is this:
00380         // when the statement program needs to advance the address by a
00381         // small amount, it can use a single special opcode, which occupies
00382         // a single byte. When it needs to advance the address by up to
00383         // twice the range of the last special opcode, it can use
00384         // DW_LNS_const_add_pc followed by a special opcode, for a total
00385         // of two bytes. Only if it needs to advance the address by more
00386         // than twice that range will it need to use both DW_LNS_advance_pc
00387         // and a special opcode, requiring three or more bytes.
00388         {
00389           uint8_t adjust_opcode = 255 - prologue->OpcodeBase;
00390           uint64_t addr_offset = (adjust_opcode / prologue->LineRange) *
00391                                  prologue->MinInstLength;
00392           state.Address += addr_offset;
00393         }
00394         break;
00395 
00396       case DW_LNS_fixed_advance_pc:
00397         // Takes a single uhalf operand. Add to the address register of
00398         // the state machine the value of the (unencoded) operand. This
00399         // is the only extended opcode that takes an argument that is not
00400         // a variable length number. The motivation for DW_LNS_fixed_advance_pc
00401         // is this: existing assemblers cannot emit DW_LNS_advance_pc or
00402         // special opcodes because they cannot encode LEB128 numbers or
00403         // judge when the computation of a special opcode overflows and
00404         // requires the use of DW_LNS_advance_pc. Such assemblers, however,
00405         // can use DW_LNS_fixed_advance_pc instead, sacrificing compression.
00406         state.Address += debug_line_data.getU16(offset_ptr);
00407         break;
00408 
00409       case DW_LNS_set_prologue_end:
00410         // Takes no arguments. Set the prologue_end register of the
00411         // state machine to true
00412         state.PrologueEnd = true;
00413         break;
00414 
00415       case DW_LNS_set_epilogue_begin:
00416         // Takes no arguments. Set the basic_block register of the
00417         // state machine to true
00418         state.EpilogueBegin = true;
00419         break;
00420 
00421       case DW_LNS_set_isa:
00422         // Takes a single unsigned LEB128 operand and stores it in the
00423         // column register of the state machine.
00424         state.Isa = debug_line_data.getULEB128(offset_ptr);
00425         break;
00426 
00427       default:
00428         // Handle any unknown standard opcodes here. We know the lengths
00429         // of such opcodes because they are specified in the prologue
00430         // as a multiple of LEB128 operands for each opcode.
00431         {
00432           assert(opcode - 1U < prologue->StandardOpcodeLengths.size());
00433           uint8_t opcode_length = prologue->StandardOpcodeLengths[opcode - 1];
00434           for (uint8_t i=0; i<opcode_length; ++i)
00435             debug_line_data.getULEB128(offset_ptr);
00436         }
00437         break;
00438       }
00439     } else {
00440       // Special Opcodes
00441 
00442       // A special opcode value is chosen based on the amount that needs
00443       // to be added to the line and address registers. The maximum line
00444       // increment for a special opcode is the value of the line_base
00445       // field in the header, plus the value of the line_range field,
00446       // minus 1 (line base + line range - 1). If the desired line
00447       // increment is greater than the maximum line increment, a standard
00448       // opcode must be used instead of a special opcode. The "address
00449       // advance" is calculated by dividing the desired address increment
00450       // by the minimum_instruction_length field from the header. The
00451       // special opcode is then calculated using the following formula:
00452       //
00453       //  opcode = (desired line increment - line_base) +
00454       //           (line_range * address advance) + opcode_base
00455       //
00456       // If the resulting opcode is greater than 255, a standard opcode
00457       // must be used instead.
00458       //
00459       // To decode a special opcode, subtract the opcode_base from the
00460       // opcode itself to give the adjusted opcode. The amount to
00461       // increment the address register is the result of the adjusted
00462       // opcode divided by the line_range multiplied by the
00463       // minimum_instruction_length field from the header. That is:
00464       //
00465       //  address increment = (adjusted opcode / line_range) *
00466       //                      minimum_instruction_length
00467       //
00468       // The amount to increment the line register is the line_base plus
00469       // the result of the adjusted opcode modulo the line_range. That is:
00470       //
00471       // line increment = line_base + (adjusted opcode % line_range)
00472 
00473       uint8_t adjust_opcode = opcode - prologue->OpcodeBase;
00474       uint64_t addr_offset = (adjust_opcode / prologue->LineRange) *
00475                              prologue->MinInstLength;
00476       int32_t line_offset = prologue->LineBase +
00477                             (adjust_opcode % prologue->LineRange);
00478       state.Line += line_offset;
00479       state.Address += addr_offset;
00480       state.appendRowToMatrix(*offset_ptr);
00481     }
00482   }
00483 
00484   state.finalize();
00485 
00486   return end_offset;
00487 }
00488 
00489 uint32_t
00490 DWARFDebugLine::LineTable::lookupAddress(uint64_t address) const {
00491   uint32_t unknown_index = UINT32_MAX;
00492   if (Sequences.empty())
00493     return unknown_index;
00494   // First, find an instruction sequence containing the given address.
00495   DWARFDebugLine::Sequence sequence;
00496   sequence.LowPC = address;
00497   SequenceIter first_seq = Sequences.begin();
00498   SequenceIter last_seq = Sequences.end();
00499   SequenceIter seq_pos = std::lower_bound(first_seq, last_seq, sequence,
00500       DWARFDebugLine::Sequence::orderByLowPC);
00501   DWARFDebugLine::Sequence found_seq;
00502   if (seq_pos == last_seq) {
00503     found_seq = Sequences.back();
00504   } else if (seq_pos->LowPC == address) {
00505     found_seq = *seq_pos;
00506   } else {
00507     if (seq_pos == first_seq)
00508       return unknown_index;
00509     found_seq = *(seq_pos - 1);
00510   }
00511   if (!found_seq.containsPC(address))
00512     return unknown_index;
00513   // Search for instruction address in the rows describing the sequence.
00514   // Rows are stored in a vector, so we may use arithmetical operations with
00515   // iterators.
00516   DWARFDebugLine::Row row;
00517   row.Address = address;
00518   RowIter first_row = Rows.begin() + found_seq.FirstRowIndex;
00519   RowIter last_row = Rows.begin() + found_seq.LastRowIndex;
00520   RowIter row_pos = std::lower_bound(first_row, last_row, row,
00521       DWARFDebugLine::Row::orderByAddress);
00522   if (row_pos == last_row) {
00523     return found_seq.LastRowIndex - 1;
00524   }
00525   uint32_t index = found_seq.FirstRowIndex + (row_pos - first_row);
00526   if (row_pos->Address > address) {
00527     if (row_pos == first_row)
00528       return unknown_index;
00529     else
00530       index--;
00531   }
00532   return index;
00533 }
00534 
00535 bool
00536 DWARFDebugLine::LineTable::lookupAddressRange(uint64_t address,
00537                                        uint64_t size, 
00538                                        std::vector<uint32_t>& result) const {
00539   if (Sequences.empty())
00540     return false;
00541   uint64_t end_addr = address + size;
00542   // First, find an instruction sequence containing the given address.
00543   DWARFDebugLine::Sequence sequence;
00544   sequence.LowPC = address;
00545   SequenceIter first_seq = Sequences.begin();
00546   SequenceIter last_seq = Sequences.end();
00547   SequenceIter seq_pos = std::lower_bound(first_seq, last_seq, sequence,
00548       DWARFDebugLine::Sequence::orderByLowPC);
00549   if (seq_pos == last_seq || seq_pos->LowPC != address) {
00550     if (seq_pos == first_seq)
00551       return false;
00552     seq_pos--;
00553   }
00554   if (!seq_pos->containsPC(address))
00555     return false;
00556 
00557   SequenceIter start_pos = seq_pos;
00558 
00559   // Add the rows from the first sequence to the vector, starting with the
00560   // index we just calculated
00561 
00562   while (seq_pos != last_seq && seq_pos->LowPC < end_addr) {
00563     DWARFDebugLine::Sequence cur_seq = *seq_pos;
00564     uint32_t first_row_index;
00565     uint32_t last_row_index;
00566     if (seq_pos == start_pos) {
00567       // For the first sequence, we need to find which row in the sequence is the
00568       // first in our range. Rows are stored in a vector, so we may use
00569       // arithmetical operations with iterators.
00570       DWARFDebugLine::Row row;
00571       row.Address = address;
00572       RowIter first_row = Rows.begin() + cur_seq.FirstRowIndex;
00573       RowIter last_row = Rows.begin() + cur_seq.LastRowIndex;
00574       RowIter row_pos = std::upper_bound(first_row, last_row, row,
00575                                          DWARFDebugLine::Row::orderByAddress);
00576       // The 'row_pos' iterator references the first row that is greater than
00577       // our start address. Unless that's the first row, we want to start at
00578       // the row before that.
00579       first_row_index = cur_seq.FirstRowIndex + (row_pos - first_row);
00580       if (row_pos != first_row)
00581         --first_row_index;
00582     } else
00583       first_row_index = cur_seq.FirstRowIndex;
00584 
00585     // For the last sequence in our range, we need to figure out the last row in
00586     // range.  For all other sequences we can go to the end of the sequence.
00587     if (cur_seq.HighPC > end_addr) {
00588       DWARFDebugLine::Row row;
00589       row.Address = end_addr;
00590       RowIter first_row = Rows.begin() + cur_seq.FirstRowIndex;
00591       RowIter last_row = Rows.begin() + cur_seq.LastRowIndex;
00592       RowIter row_pos = std::upper_bound(first_row, last_row, row,
00593                                          DWARFDebugLine::Row::orderByAddress);
00594       // The 'row_pos' iterator references the first row that is greater than
00595       // our end address.  The row before that is the last row we want.
00596       last_row_index = cur_seq.FirstRowIndex + (row_pos - first_row) - 1;
00597     } else
00598       // Contrary to what you might expect, DWARFDebugLine::SequenceLastRowIndex
00599       // isn't a valid index within the current sequence.  It's that plus one.
00600       last_row_index = cur_seq.LastRowIndex - 1;
00601 
00602     for (uint32_t i = first_row_index; i <= last_row_index; ++i) {
00603       result.push_back(i);
00604     }
00605 
00606     ++seq_pos;
00607   }
00608 
00609   return true;
00610 }
00611 
00612 bool
00613 DWARFDebugLine::LineTable::getFileNameByIndex(uint64_t FileIndex,
00614                                               bool NeedsAbsoluteFilePath,
00615                                               std::string &Result) const {
00616   if (FileIndex == 0 || FileIndex > Prologue.FileNames.size())
00617     return false;
00618   const FileNameEntry &Entry = Prologue.FileNames[FileIndex - 1];
00619   const char *FileName = Entry.Name;
00620   if (!NeedsAbsoluteFilePath ||
00621       sys::path::is_absolute(FileName)) {
00622     Result = FileName;
00623     return true;
00624   }
00625   SmallString<16> FilePath;
00626   uint64_t IncludeDirIndex = Entry.DirIdx;
00627   // Be defensive about the contents of Entry.
00628   if (IncludeDirIndex > 0 &&
00629       IncludeDirIndex <= Prologue.IncludeDirectories.size()) {
00630     const char *IncludeDir = Prologue.IncludeDirectories[IncludeDirIndex - 1];
00631     sys::path::append(FilePath, IncludeDir);
00632   }
00633   sys::path::append(FilePath, FileName);
00634   Result = FilePath.str();
00635   return true;
00636 }