blob: 72d8760751d808605506d11dd8ccc3d618826cc1 [file] [log] [blame]
Eugene Zelenkoe94042c2017-02-27 23:43:14 +00001//===- DWARFDebugLine.cpp -------------------------------------------------===//
Benjamin Kramer5acab502011-09-15 02:12:05 +00002//
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
Paul Robinson9d4eb692017-05-01 23:27:55 +000010#include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000011#include "llvm/ADT/SmallString.h"
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000012#include "llvm/ADT/SmallVector.h"
13#include "llvm/ADT/StringRef.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000014#include "llvm/BinaryFormat/Dwarf.h"
George Rimarf8a96422017-04-21 09:12:18 +000015#include "llvm/DebugInfo/DWARF/DWARFContext.h"
Paul Robinson2bc38732017-05-02 21:40:47 +000016#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000017#include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
Benjamin Kramer5acab502011-09-15 02:12:05 +000018#include "llvm/Support/Format.h"
Alexey Samsonov45be7932012-08-30 07:49:50 +000019#include "llvm/Support/Path.h"
Benjamin Kramer5acab502011-09-15 02:12:05 +000020#include "llvm/Support/raw_ostream.h"
Benjamin Kramera57c46a2011-09-15 02:19:33 +000021#include <algorithm>
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000022#include <cassert>
23#include <cinttypes>
24#include <cstdint>
25#include <cstdio>
26#include <utility>
27
Benjamin Kramer5acab502011-09-15 02:12:05 +000028using namespace llvm;
29using namespace dwarf;
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000030
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000031using FileLineInfoKind = DILineInfoSpecifier::FileLineInfoKind;
32
Paul Robinson2bc38732017-05-02 21:40:47 +000033namespace {
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000034
Paul Robinson2bc38732017-05-02 21:40:47 +000035struct ContentDescriptor {
36 dwarf::LineNumberEntryFormat Type;
37 dwarf::Form Form;
38};
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000039
40using ContentDescriptors = SmallVector<ContentDescriptor, 4>;
41
Paul Robinson2bc38732017-05-02 21:40:47 +000042} // end anonmyous namespace
Benjamin Kramer5acab502011-09-15 02:12:05 +000043
Dehao Chen1b54fce2016-04-28 22:09:37 +000044DWARFDebugLine::Prologue::Prologue() { clear(); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +000045
46void DWARFDebugLine::Prologue::clear() {
47 TotalLength = Version = PrologueLength = 0;
Paul Robinson2bc38732017-05-02 21:40:47 +000048 AddressSize = SegSelectorSize = 0;
Alexey Samsonov836b1ae2014-04-29 21:28:13 +000049 MinInstLength = MaxOpsPerInst = DefaultIsStmt = LineBase = LineRange = 0;
50 OpcodeBase = 0;
Ed Maste6d0bee52015-05-28 15:38:17 +000051 IsDWARF64 = false;
Alexey Samsonov836b1ae2014-04-29 21:28:13 +000052 StandardOpcodeLengths.clear();
53 IncludeDirectories.clear();
54 FileNames.clear();
55}
56
Benjamin Kramer5acab502011-09-15 02:12:05 +000057void DWARFDebugLine::Prologue::dump(raw_ostream &OS) const {
58 OS << "Line table prologue:\n"
Ed Maste6d0bee52015-05-28 15:38:17 +000059 << format(" total_length: 0x%8.8" PRIx64 "\n", TotalLength)
David Blaikie1d4736e2014-02-24 23:58:54 +000060 << format(" version: %u\n", Version)
Paul Robinson2bc38732017-05-02 21:40:47 +000061 << format(Version >= 5 ? " address_size: %u\n" : "", AddressSize)
62 << format(Version >= 5 ? " seg_select_size: %u\n" : "", SegSelectorSize)
Ed Maste6d0bee52015-05-28 15:38:17 +000063 << format(" prologue_length: 0x%8.8" PRIx64 "\n", PrologueLength)
David Blaikie1d4736e2014-02-24 23:58:54 +000064 << format(" min_inst_length: %u\n", MinInstLength)
65 << format(Version >= 4 ? "max_ops_per_inst: %u\n" : "", MaxOpsPerInst)
66 << format(" default_is_stmt: %u\n", DefaultIsStmt)
67 << format(" line_base: %i\n", LineBase)
68 << format(" line_range: %u\n", LineRange)
69 << format(" opcode_base: %u\n", OpcodeBase);
Benjamin Kramer5acab502011-09-15 02:12:05 +000070
Paul Robinson9d4eb692017-05-01 23:27:55 +000071 for (uint32_t I = 0; I != StandardOpcodeLengths.size(); ++I)
Mehdi Amini149f6ea2016-10-05 05:59:29 +000072 OS << format("standard_opcode_lengths[%s] = %u\n",
Paul Robinson9d4eb692017-05-01 23:27:55 +000073 LNStandardString(I + 1).data(), StandardOpcodeLengths[I]);
Benjamin Kramer5acab502011-09-15 02:12:05 +000074
75 if (!IncludeDirectories.empty())
Paul Robinson9d4eb692017-05-01 23:27:55 +000076 for (uint32_t I = 0; I != IncludeDirectories.size(); ++I)
77 OS << format("include_directories[%3u] = '", I + 1)
78 << IncludeDirectories[I] << "'\n";
Benjamin Kramer5acab502011-09-15 02:12:05 +000079
80 if (!FileNames.empty()) {
81 OS << " Dir Mod Time File Len File Name\n"
82 << " ---- ---------- ---------- -----------"
83 "----------------\n";
Paul Robinson9d4eb692017-05-01 23:27:55 +000084 for (uint32_t I = 0; I != FileNames.size(); ++I) {
85 const FileNameEntry &FileEntry = FileNames[I];
86 OS << format("file_names[%3u] %4" PRIu64 " ", I + 1, FileEntry.DirIdx)
87 << format("0x%8.8" PRIx64 " 0x%8.8" PRIx64 " ", FileEntry.ModTime,
88 FileEntry.Length)
89 << FileEntry.Name << '\n';
Benjamin Kramer5acab502011-09-15 02:12:05 +000090 }
91 }
92}
93
Paul Robinson2bc38732017-05-02 21:40:47 +000094// Parse v2-v4 directory and file tables.
95static void
96parseV2DirFileTables(DataExtractor DebugLineData, uint32_t *OffsetPtr,
97 uint64_t EndPrologueOffset,
98 std::vector<StringRef> &IncludeDirectories,
99 std::vector<DWARFDebugLine::FileNameEntry> &FileNames) {
100 while (*OffsetPtr < EndPrologueOffset) {
101 StringRef S = DebugLineData.getCStrRef(OffsetPtr);
102 if (S.empty())
103 break;
104 IncludeDirectories.push_back(S);
105 }
106
107 while (*OffsetPtr < EndPrologueOffset) {
108 StringRef Name = DebugLineData.getCStrRef(OffsetPtr);
109 if (Name.empty())
110 break;
111 DWARFDebugLine::FileNameEntry FileEntry;
112 FileEntry.Name = Name;
113 FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr);
114 FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr);
115 FileEntry.Length = DebugLineData.getULEB128(OffsetPtr);
116 FileNames.push_back(FileEntry);
117 }
118}
119
120// Parse v5 directory/file entry content descriptions.
121// Returns the descriptors, or an empty vector if we did not find a path or
122// ran off the end of the prologue.
123static ContentDescriptors
124parseV5EntryFormat(DataExtractor DebugLineData, uint32_t *OffsetPtr,
125 uint64_t EndPrologueOffset) {
126 ContentDescriptors Descriptors;
127 int FormatCount = DebugLineData.getU8(OffsetPtr);
128 bool HasPath = false;
129 for (int I = 0; I != FormatCount; ++I) {
130 if (*OffsetPtr >= EndPrologueOffset)
131 return ContentDescriptors();
132 ContentDescriptor Descriptor;
133 Descriptor.Type =
134 dwarf::LineNumberEntryFormat(DebugLineData.getULEB128(OffsetPtr));
135 Descriptor.Form = dwarf::Form(DebugLineData.getULEB128(OffsetPtr));
136 if (Descriptor.Type == dwarf::DW_LNCT_path)
137 HasPath = true;
138 Descriptors.push_back(Descriptor);
139 }
140 return HasPath ? Descriptors : ContentDescriptors();
141}
142
143static bool
144parseV5DirFileTables(DataExtractor DebugLineData, uint32_t *OffsetPtr,
145 uint64_t EndPrologueOffset,
146 std::vector<StringRef> &IncludeDirectories,
147 std::vector<DWARFDebugLine::FileNameEntry> &FileNames) {
148 // Get the directory entry description.
149 ContentDescriptors DirDescriptors =
150 parseV5EntryFormat(DebugLineData, OffsetPtr, EndPrologueOffset);
151 if (DirDescriptors.empty())
152 return false;
153
154 // Get the directory entries, according to the format described above.
155 int DirEntryCount = DebugLineData.getU8(OffsetPtr);
156 for (int I = 0; I != DirEntryCount; ++I) {
157 if (*OffsetPtr >= EndPrologueOffset)
158 return false;
159 for (auto Descriptor : DirDescriptors) {
160 DWARFFormValue Value(Descriptor.Form);
161 switch (Descriptor.Type) {
162 case DW_LNCT_path:
163 if (!Value.extractValue(DebugLineData, OffsetPtr, nullptr))
164 return false;
165 IncludeDirectories.push_back(Value.getAsCString().getValue());
166 break;
167 default:
168 if (!Value.skipValue(DebugLineData, OffsetPtr, nullptr))
169 return false;
170 }
171 }
172 }
173
174 // Get the file entry description.
175 ContentDescriptors FileDescriptors =
176 parseV5EntryFormat(DebugLineData, OffsetPtr, EndPrologueOffset);
177 if (FileDescriptors.empty())
178 return false;
179
180 // Get the file entries, according to the format described above.
181 int FileEntryCount = DebugLineData.getU8(OffsetPtr);
182 for (int I = 0; I != FileEntryCount; ++I) {
183 if (*OffsetPtr >= EndPrologueOffset)
184 return false;
185 DWARFDebugLine::FileNameEntry FileEntry;
186 for (auto Descriptor : FileDescriptors) {
187 DWARFFormValue Value(Descriptor.Form);
188 if (!Value.extractValue(DebugLineData, OffsetPtr, nullptr))
189 return false;
190 switch (Descriptor.Type) {
191 case DW_LNCT_path:
192 FileEntry.Name = Value.getAsCString().getValue();
193 break;
194 case DW_LNCT_directory_index:
195 FileEntry.DirIdx = Value.getAsUnsignedConstant().getValue();
196 break;
197 case DW_LNCT_timestamp:
198 FileEntry.ModTime = Value.getAsUnsignedConstant().getValue();
199 break;
200 case DW_LNCT_size:
201 FileEntry.Length = Value.getAsUnsignedConstant().getValue();
202 break;
203 // FIXME: Add MD5
204 default:
205 break;
206 }
207 }
208 FileNames.push_back(FileEntry);
209 }
210 return true;
211}
212
Paul Robinson9d4eb692017-05-01 23:27:55 +0000213bool DWARFDebugLine::Prologue::parse(DataExtractor DebugLineData,
214 uint32_t *OffsetPtr) {
215 const uint64_t PrologueOffset = *OffsetPtr;
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000216
217 clear();
Paul Robinson9d4eb692017-05-01 23:27:55 +0000218 TotalLength = DebugLineData.getU32(OffsetPtr);
Ed Maste6d0bee52015-05-28 15:38:17 +0000219 if (TotalLength == UINT32_MAX) {
220 IsDWARF64 = true;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000221 TotalLength = DebugLineData.getU64(OffsetPtr);
Ed Maste6d0bee52015-05-28 15:38:17 +0000222 } else if (TotalLength > 0xffffff00) {
223 return false;
224 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000225 Version = DebugLineData.getU16(OffsetPtr);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000226 if (Version < 2)
227 return false;
228
Paul Robinson2bc38732017-05-02 21:40:47 +0000229 if (Version >= 5) {
230 AddressSize = DebugLineData.getU8(OffsetPtr);
231 SegSelectorSize = DebugLineData.getU8(OffsetPtr);
232 }
233
Paul Robinson9d4eb692017-05-01 23:27:55 +0000234 PrologueLength = DebugLineData.getUnsigned(OffsetPtr, sizeofPrologueLength());
235 const uint64_t EndPrologueOffset = PrologueLength + *OffsetPtr;
236 MinInstLength = DebugLineData.getU8(OffsetPtr);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000237 if (Version >= 4)
Paul Robinson9d4eb692017-05-01 23:27:55 +0000238 MaxOpsPerInst = DebugLineData.getU8(OffsetPtr);
239 DefaultIsStmt = DebugLineData.getU8(OffsetPtr);
240 LineBase = DebugLineData.getU8(OffsetPtr);
241 LineRange = DebugLineData.getU8(OffsetPtr);
242 OpcodeBase = DebugLineData.getU8(OffsetPtr);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000243
244 StandardOpcodeLengths.reserve(OpcodeBase - 1);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000245 for (uint32_t I = 1; I < OpcodeBase; ++I) {
246 uint8_t OpLen = DebugLineData.getU8(OffsetPtr);
247 StandardOpcodeLengths.push_back(OpLen);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000248 }
249
Paul Robinson2bc38732017-05-02 21:40:47 +0000250 if (Version >= 5) {
251 if (!parseV5DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset,
252 IncludeDirectories, FileNames)) {
253 fprintf(stderr,
254 "warning: parsing line table prologue at 0x%8.8" PRIx64
255 " found an invalid directory or file table description at"
256 " 0x%8.8" PRIx64 "\n", PrologueOffset, (uint64_t)*OffsetPtr);
257 return false;
258 }
259 } else
260 parseV2DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset,
261 IncludeDirectories, FileNames);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000262
Paul Robinson9d4eb692017-05-01 23:27:55 +0000263 if (*OffsetPtr != EndPrologueOffset) {
264 fprintf(stderr,
265 "warning: parsing line table prologue at 0x%8.8" PRIx64
266 " should have ended at 0x%8.8" PRIx64
267 " but it ended at 0x%8.8" PRIx64 "\n",
268 PrologueOffset, EndPrologueOffset, (uint64_t)*OffsetPtr);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000269 return false;
270 }
271 return true;
272}
273
Paul Robinson9d4eb692017-05-01 23:27:55 +0000274DWARFDebugLine::Row::Row(bool DefaultIsStmt) { reset(DefaultIsStmt); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000275
Benjamin Kramer5acab502011-09-15 02:12:05 +0000276void DWARFDebugLine::Row::postAppend() {
277 BasicBlock = false;
278 PrologueEnd = false;
279 EpilogueBegin = false;
280}
281
Paul Robinson9d4eb692017-05-01 23:27:55 +0000282void DWARFDebugLine::Row::reset(bool DefaultIsStmt) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000283 Address = 0;
284 Line = 1;
285 Column = 0;
286 File = 1;
287 Isa = 0;
Diego Novillo5b5cf502014-02-14 19:27:53 +0000288 Discriminator = 0;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000289 IsStmt = DefaultIsStmt;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000290 BasicBlock = false;
291 EndSequence = false;
292 PrologueEnd = false;
293 EpilogueBegin = false;
294}
295
Greg Clayton67070462017-05-02 22:48:52 +0000296void DWARFDebugLine::Row::dumpTableHeader(raw_ostream &OS) {
297 OS << "Address Line Column File ISA Discriminator Flags\n"
298 << "------------------ ------ ------ ------ --- ------------- "
299 "-------------\n";
300}
301
Benjamin Kramer5acab502011-09-15 02:12:05 +0000302void DWARFDebugLine::Row::dump(raw_ostream &OS) const {
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000303 OS << format("0x%16.16" PRIx64 " %6u %6u", Address, Line, Column)
Diego Novillo5b5cf502014-02-14 19:27:53 +0000304 << format(" %6u %3u %13u ", File, Isa, Discriminator)
Dehao Chen1b54fce2016-04-28 22:09:37 +0000305 << (IsStmt ? " is_stmt" : "") << (BasicBlock ? " basic_block" : "")
Benjamin Kramer5acab502011-09-15 02:12:05 +0000306 << (PrologueEnd ? " prologue_end" : "")
307 << (EpilogueBegin ? " epilogue_begin" : "")
Dehao Chen1b54fce2016-04-28 22:09:37 +0000308 << (EndSequence ? " end_sequence" : "") << '\n';
Benjamin Kramer5acab502011-09-15 02:12:05 +0000309}
310
Dehao Chen1b54fce2016-04-28 22:09:37 +0000311DWARFDebugLine::Sequence::Sequence() { reset(); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000312
313void DWARFDebugLine::Sequence::reset() {
314 LowPC = 0;
315 HighPC = 0;
316 FirstRowIndex = 0;
317 LastRowIndex = 0;
318 Empty = true;
319}
320
Dehao Chen1b54fce2016-04-28 22:09:37 +0000321DWARFDebugLine::LineTable::LineTable() { clear(); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000322
Benjamin Kramer5acab502011-09-15 02:12:05 +0000323void DWARFDebugLine::LineTable::dump(raw_ostream &OS) const {
324 Prologue.dump(OS);
325 OS << '\n';
326
327 if (!Rows.empty()) {
Greg Clayton67070462017-05-02 22:48:52 +0000328 Row::dumpTableHeader(OS);
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000329 for (const Row &R : Rows) {
330 R.dump(OS);
331 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000332 }
333}
334
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000335void DWARFDebugLine::LineTable::clear() {
336 Prologue.clear();
337 Rows.clear();
338 Sequences.clear();
339}
340
Alexey Samsonov110d5952014-04-30 00:09:19 +0000341DWARFDebugLine::ParsingState::ParsingState(struct LineTable *LT)
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +0000342 : LineTable(LT) {
Alexey Samsonov110d5952014-04-30 00:09:19 +0000343 resetRowAndSequence();
344}
Nick Lewycky4d044922011-09-15 03:41:51 +0000345
Alexey Samsonov110d5952014-04-30 00:09:19 +0000346void DWARFDebugLine::ParsingState::resetRowAndSequence() {
347 Row.reset(LineTable->Prologue.DefaultIsStmt);
348 Sequence.reset();
349}
350
Paul Robinson9d4eb692017-05-01 23:27:55 +0000351void DWARFDebugLine::ParsingState::appendRowToMatrix(uint32_t Offset) {
Alexey Samsonov110d5952014-04-30 00:09:19 +0000352 if (Sequence.Empty) {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000353 // Record the beginning of instruction sequence.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000354 Sequence.Empty = false;
355 Sequence.LowPC = Row.Address;
356 Sequence.FirstRowIndex = RowNumber;
Alexey Samsonov947228c2012-08-07 11:46:57 +0000357 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000358 ++RowNumber;
359 LineTable->appendRow(Row);
360 if (Row.EndSequence) {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000361 // Record the end of instruction sequence.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000362 Sequence.HighPC = Row.Address;
363 Sequence.LastRowIndex = RowNumber;
364 if (Sequence.isValid())
365 LineTable->appendSequence(Sequence);
366 Sequence.reset();
Alexey Samsonov947228c2012-08-07 11:46:57 +0000367 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000368 Row.postAppend();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000369}
370
Benjamin Kramer5acab502011-09-15 02:12:05 +0000371const DWARFDebugLine::LineTable *
Paul Robinson9d4eb692017-05-01 23:27:55 +0000372DWARFDebugLine::getLineTable(uint32_t Offset) const {
373 LineTableConstIter Pos = LineTableMap.find(Offset);
374 if (Pos != LineTableMap.end())
375 return &Pos->second;
Craig Topper2617dcc2014-04-15 06:32:26 +0000376 return nullptr;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000377}
378
Benjamin Kramer679e1752011-09-15 20:43:18 +0000379const DWARFDebugLine::LineTable *
Paul Robinson9d4eb692017-05-01 23:27:55 +0000380DWARFDebugLine::getOrParseLineTable(DataExtractor DebugLineData,
381 uint32_t Offset) {
382 std::pair<LineTableIter, bool> Pos =
383 LineTableMap.insert(LineTableMapTy::value_type(Offset, LineTable()));
384 LineTable *LT = &Pos.first->second;
385 if (Pos.second) {
386 if (!LT->parse(DebugLineData, RelocMap, &Offset))
Craig Topper2617dcc2014-04-15 06:32:26 +0000387 return nullptr;
Benjamin Kramer679e1752011-09-15 20:43:18 +0000388 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000389 return LT;
Benjamin Kramer679e1752011-09-15 20:43:18 +0000390}
391
Paul Robinson9d4eb692017-05-01 23:27:55 +0000392bool DWARFDebugLine::LineTable::parse(DataExtractor DebugLineData,
Alexey Samsonov110d5952014-04-30 00:09:19 +0000393 const RelocAddrMap *RMap,
Paul Robinson9d4eb692017-05-01 23:27:55 +0000394 uint32_t *OffsetPtr) {
395 const uint32_t DebugLineOffset = *OffsetPtr;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000396
Alexey Samsonov110d5952014-04-30 00:09:19 +0000397 clear();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000398
Paul Robinson9d4eb692017-05-01 23:27:55 +0000399 if (!Prologue.parse(DebugLineData, OffsetPtr)) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000400 // Restore our offset and return false to indicate failure!
Paul Robinson9d4eb692017-05-01 23:27:55 +0000401 *OffsetPtr = DebugLineOffset;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000402 return false;
403 }
404
Paul Robinson9d4eb692017-05-01 23:27:55 +0000405 const uint32_t EndOffset =
406 DebugLineOffset + Prologue.TotalLength + Prologue.sizeofTotalLength();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000407
Alexey Samsonov110d5952014-04-30 00:09:19 +0000408 ParsingState State(this);
Benjamin Kramer112ec172011-09-15 21:59:13 +0000409
Paul Robinson9d4eb692017-05-01 23:27:55 +0000410 while (*OffsetPtr < EndOffset) {
411 uint8_t Opcode = DebugLineData.getU8(OffsetPtr);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000412
Paul Robinson9d4eb692017-05-01 23:27:55 +0000413 if (Opcode == 0) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000414 // Extended Opcodes always start with a zero opcode followed by
415 // a uleb128 length so you can skip ones you don't know about
Paul Robinson9d4eb692017-05-01 23:27:55 +0000416 uint32_t ExtOffset = *OffsetPtr;
417 uint64_t Len = DebugLineData.getULEB128(OffsetPtr);
418 uint32_t ArgSize = Len - (*OffsetPtr - ExtOffset);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000419
Paul Robinson9d4eb692017-05-01 23:27:55 +0000420 uint8_t SubOpcode = DebugLineData.getU8(OffsetPtr);
421 switch (SubOpcode) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000422 case DW_LNE_end_sequence:
423 // Set the end_sequence register of the state machine to true and
424 // append a row to the matrix using the current values of the
425 // state-machine registers. Then reset the registers to the initial
426 // values specified above. Every statement program sequence must end
427 // with a DW_LNE_end_sequence instruction which creates a row whose
428 // address is that of the byte after the last target machine instruction
429 // of the sequence.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000430 State.Row.EndSequence = true;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000431 State.appendRowToMatrix(*OffsetPtr);
Alexey Samsonov110d5952014-04-30 00:09:19 +0000432 State.resetRowAndSequence();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000433 break;
434
435 case DW_LNE_set_address:
436 // Takes a single relocatable address as an operand. The size of the
437 // operand is the size appropriate to hold an address on the target
438 // machine. Set the address register to the value given by the
439 // relocatable address. All of the other statement program opcodes
440 // that affect the address register add a delta to it. This instruction
441 // stores a relocatable value into it instead.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000442 State.Row.Address = getRelocatedValue(
443 DebugLineData, DebugLineData.getAddressSize(), OffsetPtr, RMap);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000444 break;
445
446 case DW_LNE_define_file:
447 // Takes 4 arguments. The first is a null terminated string containing
448 // a source file name. The second is an unsigned LEB128 number
449 // representing the directory index of the directory in which the file
450 // was found. The third is an unsigned LEB128 number representing the
451 // time of last modification of the file. The fourth is an unsigned
452 // LEB128 number representing the length in bytes of the file. The time
453 // and length fields may contain LEB128(0) if the information is not
454 // available.
455 //
456 // The directory index represents an entry in the include_directories
457 // section of the statement program prologue. The index is LEB128(0)
458 // if the file was found in the current directory of the compilation,
459 // LEB128(1) if it was found in the first directory in the
460 // include_directories section, and so on. The directory index is
461 // ignored for file names that represent full path names.
462 //
463 // The files are numbered, starting at 1, in the order in which they
464 // appear; the names in the prologue come before names defined by
465 // the DW_LNE_define_file instruction. These numbers are used in the
466 // the file register of the state machine.
467 {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000468 FileNameEntry FileEntry;
469 FileEntry.Name = DebugLineData.getCStr(OffsetPtr);
470 FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr);
471 FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr);
472 FileEntry.Length = DebugLineData.getULEB128(OffsetPtr);
473 Prologue.FileNames.push_back(FileEntry);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000474 }
475 break;
476
Diego Novillo5b5cf502014-02-14 19:27:53 +0000477 case DW_LNE_set_discriminator:
Paul Robinson9d4eb692017-05-01 23:27:55 +0000478 State.Row.Discriminator = DebugLineData.getULEB128(OffsetPtr);
Diego Novillo5b5cf502014-02-14 19:27:53 +0000479 break;
480
Benjamin Kramer5acab502011-09-15 02:12:05 +0000481 default:
482 // Length doesn't include the zero opcode byte or the length itself, but
483 // it does include the sub_opcode, so we have to adjust for that below
Paul Robinson9d4eb692017-05-01 23:27:55 +0000484 (*OffsetPtr) += ArgSize;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000485 break;
486 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000487 } else if (Opcode < Prologue.OpcodeBase) {
488 switch (Opcode) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000489 // Standard Opcodes
490 case DW_LNS_copy:
491 // Takes no arguments. Append a row to the matrix using the
492 // current values of the state-machine registers. Then set
493 // the basic_block register to false.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000494 State.appendRowToMatrix(*OffsetPtr);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000495 break;
496
497 case DW_LNS_advance_pc:
498 // Takes a single unsigned LEB128 operand, multiplies it by the
499 // min_inst_length field of the prologue, and adds the
500 // result to the address register of the state machine.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000501 State.Row.Address +=
Paul Robinson9d4eb692017-05-01 23:27:55 +0000502 DebugLineData.getULEB128(OffsetPtr) * Prologue.MinInstLength;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000503 break;
504
505 case DW_LNS_advance_line:
506 // Takes a single signed LEB128 operand and adds that value to
507 // the line register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000508 State.Row.Line += DebugLineData.getSLEB128(OffsetPtr);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000509 break;
510
511 case DW_LNS_set_file:
512 // Takes a single unsigned LEB128 operand and stores it in the file
513 // register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000514 State.Row.File = DebugLineData.getULEB128(OffsetPtr);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000515 break;
516
517 case DW_LNS_set_column:
518 // Takes a single unsigned LEB128 operand and stores it in the
519 // column register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000520 State.Row.Column = DebugLineData.getULEB128(OffsetPtr);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000521 break;
522
523 case DW_LNS_negate_stmt:
524 // Takes no arguments. Set the is_stmt register of the state
525 // machine to the logical negation of its current value.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000526 State.Row.IsStmt = !State.Row.IsStmt;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000527 break;
528
529 case DW_LNS_set_basic_block:
530 // Takes no arguments. Set the basic_block register of the
531 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000532 State.Row.BasicBlock = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000533 break;
534
535 case DW_LNS_const_add_pc:
536 // Takes no arguments. Add to the address register of the state
537 // machine the address increment value corresponding to special
538 // opcode 255. The motivation for DW_LNS_const_add_pc is this:
539 // when the statement program needs to advance the address by a
540 // small amount, it can use a single special opcode, which occupies
541 // a single byte. When it needs to advance the address by up to
542 // twice the range of the last special opcode, it can use
543 // DW_LNS_const_add_pc followed by a special opcode, for a total
544 // of two bytes. Only if it needs to advance the address by more
545 // than twice that range will it need to use both DW_LNS_advance_pc
546 // and a special opcode, requiring three or more bytes.
547 {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000548 uint8_t AdjustOpcode = 255 - Prologue.OpcodeBase;
549 uint64_t AddrOffset =
550 (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength;
551 State.Row.Address += AddrOffset;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000552 }
553 break;
554
555 case DW_LNS_fixed_advance_pc:
556 // Takes a single uhalf operand. Add to the address register of
557 // the state machine the value of the (unencoded) operand. This
558 // is the only extended opcode that takes an argument that is not
559 // a variable length number. The motivation for DW_LNS_fixed_advance_pc
560 // is this: existing assemblers cannot emit DW_LNS_advance_pc or
561 // special opcodes because they cannot encode LEB128 numbers or
562 // judge when the computation of a special opcode overflows and
563 // requires the use of DW_LNS_advance_pc. Such assemblers, however,
564 // can use DW_LNS_fixed_advance_pc instead, sacrificing compression.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000565 State.Row.Address += DebugLineData.getU16(OffsetPtr);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000566 break;
567
568 case DW_LNS_set_prologue_end:
569 // Takes no arguments. Set the prologue_end register of the
570 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000571 State.Row.PrologueEnd = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000572 break;
573
574 case DW_LNS_set_epilogue_begin:
575 // Takes no arguments. Set the basic_block register of the
576 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000577 State.Row.EpilogueBegin = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000578 break;
579
580 case DW_LNS_set_isa:
581 // Takes a single unsigned LEB128 operand and stores it in the
582 // column register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000583 State.Row.Isa = DebugLineData.getULEB128(OffsetPtr);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000584 break;
585
586 default:
587 // Handle any unknown standard opcodes here. We know the lengths
588 // of such opcodes because they are specified in the prologue
589 // as a multiple of LEB128 operands for each opcode.
590 {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000591 assert(Opcode - 1U < Prologue.StandardOpcodeLengths.size());
592 uint8_t OpcodeLength = Prologue.StandardOpcodeLengths[Opcode - 1];
593 for (uint8_t I = 0; I < OpcodeLength; ++I)
594 DebugLineData.getULEB128(OffsetPtr);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000595 }
596 break;
597 }
598 } else {
599 // Special Opcodes
600
601 // A special opcode value is chosen based on the amount that needs
602 // to be added to the line and address registers. The maximum line
603 // increment for a special opcode is the value of the line_base
604 // field in the header, plus the value of the line_range field,
605 // minus 1 (line base + line range - 1). If the desired line
606 // increment is greater than the maximum line increment, a standard
NAKAMURA Takumif9959852011-10-08 11:22:47 +0000607 // opcode must be used instead of a special opcode. The "address
608 // advance" is calculated by dividing the desired address increment
Benjamin Kramer5acab502011-09-15 02:12:05 +0000609 // by the minimum_instruction_length field from the header. The
610 // special opcode is then calculated using the following formula:
611 //
612 // opcode = (desired line increment - line_base) +
613 // (line_range * address advance) + opcode_base
614 //
615 // If the resulting opcode is greater than 255, a standard opcode
616 // must be used instead.
617 //
618 // To decode a special opcode, subtract the opcode_base from the
619 // opcode itself to give the adjusted opcode. The amount to
620 // increment the address register is the result of the adjusted
621 // opcode divided by the line_range multiplied by the
622 // minimum_instruction_length field from the header. That is:
623 //
624 // address increment = (adjusted opcode / line_range) *
625 // minimum_instruction_length
626 //
627 // The amount to increment the line register is the line_base plus
628 // the result of the adjusted opcode modulo the line_range. That is:
629 //
630 // line increment = line_base + (adjusted opcode % line_range)
631
Paul Robinson9d4eb692017-05-01 23:27:55 +0000632 uint8_t AdjustOpcode = Opcode - Prologue.OpcodeBase;
633 uint64_t AddrOffset =
634 (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength;
635 int32_t LineOffset =
636 Prologue.LineBase + (AdjustOpcode % Prologue.LineRange);
637 State.Row.Line += LineOffset;
638 State.Row.Address += AddrOffset;
639 State.appendRowToMatrix(*OffsetPtr);
Dehao Chen1b54fce2016-04-28 22:09:37 +0000640 // Reset discriminator to 0.
641 State.Row.Discriminator = 0;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000642 }
643 }
644
Alexey Samsonov110d5952014-04-30 00:09:19 +0000645 if (!State.Sequence.Empty) {
646 fprintf(stderr, "warning: last sequence in debug line table is not"
647 "terminated!\n");
648 }
649
650 // Sort all sequences so that address lookup will work faster.
651 if (!Sequences.empty()) {
652 std::sort(Sequences.begin(), Sequences.end(), Sequence::orderByLowPC);
653 // Note: actually, instruction address ranges of sequences should not
654 // overlap (in shared objects and executables). If they do, the address
655 // lookup would still work, though, but result would be ambiguous.
656 // We don't report warning in this case. For example,
657 // sometimes .so compiled from multiple object files contains a few
658 // rudimentary sequences for address ranges [0x0, 0xsomething).
659 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000660
Paul Robinson9d4eb692017-05-01 23:27:55 +0000661 return EndOffset;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000662}
663
Keno Fischerc2c60182015-05-31 23:37:04 +0000664uint32_t
Paul Robinson9d4eb692017-05-01 23:27:55 +0000665DWARFDebugLine::LineTable::findRowInSeq(const DWARFDebugLine::Sequence &Seq,
666 uint64_t Address) const {
667 if (!Seq.containsPC(Address))
Keno Fischerc2c60182015-05-31 23:37:04 +0000668 return UnknownRowIndex;
669 // Search for instruction address in the rows describing the sequence.
670 // Rows are stored in a vector, so we may use arithmetical operations with
671 // iterators.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000672 DWARFDebugLine::Row Row;
673 Row.Address = Address;
674 RowIter FirstRow = Rows.begin() + Seq.FirstRowIndex;
675 RowIter LastRow = Rows.begin() + Seq.LastRowIndex;
676 LineTable::RowIter RowPos = std::lower_bound(
677 FirstRow, LastRow, Row, DWARFDebugLine::Row::orderByAddress);
678 if (RowPos == LastRow) {
679 return Seq.LastRowIndex - 1;
Keno Fischerc2c60182015-05-31 23:37:04 +0000680 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000681 uint32_t Index = Seq.FirstRowIndex + (RowPos - FirstRow);
682 if (RowPos->Address > Address) {
683 if (RowPos == FirstRow)
Keno Fischerc2c60182015-05-31 23:37:04 +0000684 return UnknownRowIndex;
685 else
Paul Robinson9d4eb692017-05-01 23:27:55 +0000686 Index--;
Keno Fischerc2c60182015-05-31 23:37:04 +0000687 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000688 return Index;
Keno Fischerc2c60182015-05-31 23:37:04 +0000689}
690
Paul Robinson9d4eb692017-05-01 23:27:55 +0000691uint32_t DWARFDebugLine::LineTable::lookupAddress(uint64_t Address) const {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000692 if (Sequences.empty())
Keno Fischerc2c60182015-05-31 23:37:04 +0000693 return UnknownRowIndex;
Alexey Samsonov947228c2012-08-07 11:46:57 +0000694 // First, find an instruction sequence containing the given address.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000695 DWARFDebugLine::Sequence Sequence;
696 Sequence.LowPC = Address;
697 SequenceIter FirstSeq = Sequences.begin();
698 SequenceIter LastSeq = Sequences.end();
699 SequenceIter SeqPos = std::lower_bound(
700 FirstSeq, LastSeq, Sequence, DWARFDebugLine::Sequence::orderByLowPC);
701 DWARFDebugLine::Sequence FoundSeq;
702 if (SeqPos == LastSeq) {
703 FoundSeq = Sequences.back();
704 } else if (SeqPos->LowPC == Address) {
705 FoundSeq = *SeqPos;
Alexey Samsonov947228c2012-08-07 11:46:57 +0000706 } else {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000707 if (SeqPos == FirstSeq)
Keno Fischerc2c60182015-05-31 23:37:04 +0000708 return UnknownRowIndex;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000709 FoundSeq = *(SeqPos - 1);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000710 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000711 return findRowInSeq(FoundSeq, Address);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000712}
Alexey Samsonov45be7932012-08-30 07:49:50 +0000713
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000714bool DWARFDebugLine::LineTable::lookupAddressRange(
Paul Robinson9d4eb692017-05-01 23:27:55 +0000715 uint64_t Address, uint64_t Size, std::vector<uint32_t> &Result) const {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000716 if (Sequences.empty())
717 return false;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000718 uint64_t EndAddr = Address + Size;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000719 // First, find an instruction sequence containing the given address.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000720 DWARFDebugLine::Sequence Sequence;
721 Sequence.LowPC = Address;
722 SequenceIter FirstSeq = Sequences.begin();
723 SequenceIter LastSeq = Sequences.end();
724 SequenceIter SeqPos = std::lower_bound(
725 FirstSeq, LastSeq, Sequence, DWARFDebugLine::Sequence::orderByLowPC);
726 if (SeqPos == LastSeq || SeqPos->LowPC != Address) {
727 if (SeqPos == FirstSeq)
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000728 return false;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000729 SeqPos--;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000730 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000731 if (!SeqPos->containsPC(Address))
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000732 return false;
733
Paul Robinson9d4eb692017-05-01 23:27:55 +0000734 SequenceIter StartPos = SeqPos;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000735
736 // Add the rows from the first sequence to the vector, starting with the
737 // index we just calculated
738
Paul Robinson9d4eb692017-05-01 23:27:55 +0000739 while (SeqPos != LastSeq && SeqPos->LowPC < EndAddr) {
740 const DWARFDebugLine::Sequence &CurSeq = *SeqPos;
Keno Fischerc2c60182015-05-31 23:37:04 +0000741 // For the first sequence, we need to find which row in the sequence is the
742 // first in our range.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000743 uint32_t FirstRowIndex = CurSeq.FirstRowIndex;
744 if (SeqPos == StartPos)
745 FirstRowIndex = findRowInSeq(CurSeq, Address);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000746
Keno Fischerc2c60182015-05-31 23:37:04 +0000747 // Figure out the last row in the range.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000748 uint32_t LastRowIndex = findRowInSeq(CurSeq, EndAddr - 1);
749 if (LastRowIndex == UnknownRowIndex)
750 LastRowIndex = CurSeq.LastRowIndex - 1;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000751
Paul Robinson9d4eb692017-05-01 23:27:55 +0000752 assert(FirstRowIndex != UnknownRowIndex);
753 assert(LastRowIndex != UnknownRowIndex);
Keno Fischerc2c60182015-05-31 23:37:04 +0000754
Paul Robinson9d4eb692017-05-01 23:27:55 +0000755 for (uint32_t I = FirstRowIndex; I <= LastRowIndex; ++I) {
756 Result.push_back(I);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000757 }
758
Paul Robinson9d4eb692017-05-01 23:27:55 +0000759 ++SeqPos;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000760 }
NAKAMURA Takumi4b86cdb2013-01-26 01:45:06 +0000761
762 return true;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000763}
764
Paul Robinson9d4eb692017-05-01 23:27:55 +0000765bool DWARFDebugLine::LineTable::hasFileAtIndex(uint64_t FileIndex) const {
Pete Cooperb2ba7762016-07-22 01:41:32 +0000766 return FileIndex != 0 && FileIndex <= Prologue.FileNames.size();
767}
768
Paul Robinson9d4eb692017-05-01 23:27:55 +0000769bool DWARFDebugLine::LineTable::getFileNameByIndex(uint64_t FileIndex,
770 const char *CompDir,
771 FileLineInfoKind Kind,
772 std::string &Result) const {
Pete Cooperb2ba7762016-07-22 01:41:32 +0000773 if (Kind == FileLineInfoKind::None || !hasFileAtIndex(FileIndex))
Alexey Samsonov45be7932012-08-30 07:49:50 +0000774 return false;
775 const FileNameEntry &Entry = Prologue.FileNames[FileIndex - 1];
Paul Robinsonba1c9152017-05-02 17:37:32 +0000776 StringRef FileName = Entry.Name;
Alexey Samsonovdce67342014-05-15 21:24:32 +0000777 if (Kind != FileLineInfoKind::AbsoluteFilePath ||
Alexey Samsonov45be7932012-08-30 07:49:50 +0000778 sys::path::is_absolute(FileName)) {
779 Result = FileName;
780 return true;
781 }
Frederic Riss101b5e22014-09-19 15:11:51 +0000782
Alexey Samsonov45be7932012-08-30 07:49:50 +0000783 SmallString<16> FilePath;
784 uint64_t IncludeDirIndex = Entry.DirIdx;
Paul Robinsonba1c9152017-05-02 17:37:32 +0000785 StringRef IncludeDir;
Alexey Samsonov45be7932012-08-30 07:49:50 +0000786 // Be defensive about the contents of Entry.
787 if (IncludeDirIndex > 0 &&
Frederic Riss101b5e22014-09-19 15:11:51 +0000788 IncludeDirIndex <= Prologue.IncludeDirectories.size())
789 IncludeDir = Prologue.IncludeDirectories[IncludeDirIndex - 1];
790
791 // We may still need to append compilation directory of compile unit.
792 // We know that FileName is not absolute, the only way to have an
793 // absolute path at this point would be if IncludeDir is absolute.
794 if (CompDir && Kind == FileLineInfoKind::AbsoluteFilePath &&
795 sys::path::is_relative(IncludeDir))
796 sys::path::append(FilePath, CompDir);
797
798 // sys::path::append skips empty strings.
799 sys::path::append(FilePath, IncludeDir, FileName);
Alexey Samsonov45be7932012-08-30 07:49:50 +0000800 Result = FilePath.str();
801 return true;
802}
Frederic Riss101b5e22014-09-19 15:11:51 +0000803
Dehao Chen1b54fce2016-04-28 22:09:37 +0000804bool DWARFDebugLine::LineTable::getFileLineInfoForAddress(
805 uint64_t Address, const char *CompDir, FileLineInfoKind Kind,
806 DILineInfo &Result) const {
Frederic Riss101b5e22014-09-19 15:11:51 +0000807 // Get the index of row we're looking for in the line table.
808 uint32_t RowIndex = lookupAddress(Address);
809 if (RowIndex == -1U)
810 return false;
811 // Take file number and line/column from the row.
812 const auto &Row = Rows[RowIndex];
813 if (!getFileNameByIndex(Row.File, CompDir, Kind, Result.FileName))
814 return false;
815 Result.Line = Row.Line;
816 Result.Column = Row.Column;
Eric Christopherba1024c2016-12-14 18:29:39 +0000817 Result.Discriminator = Row.Discriminator;
Frederic Riss101b5e22014-09-19 15:11:51 +0000818 return true;
819}