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