blob: 3e7f3c59c30b7ce69e71b15b288889895b471c18 [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() {
Paul Robinson75c068c2017-06-26 18:43:01 +000047 TotalLength = PrologueLength = 0;
48 SegSelectorSize = 0;
Alexey Samsonov836b1ae2014-04-29 21:28:13 +000049 MinInstLength = MaxOpsPerInst = DefaultIsStmt = LineBase = LineRange = 0;
50 OpcodeBase = 0;
Paul Robinson75c068c2017-06-26 18:43:01 +000051 FormParams = DWARFFormParams({0, 0, DWARF32});
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)
Paul Robinson75c068c2017-06-26 18:43:01 +000060 << format(" version: %u\n", getVersion());
61 if (getVersion() >= 5)
62 OS << format(" address_size: %u\n", getAddressSize())
63 << format(" seg_select_size: %u\n", SegSelectorSize);
64 OS << format(" prologue_length: 0x%8.8" PRIx64 "\n", PrologueLength)
David Blaikie1d4736e2014-02-24 23:58:54 +000065 << format(" min_inst_length: %u\n", MinInstLength)
Paul Robinson75c068c2017-06-26 18:43:01 +000066 << format(getVersion() >= 4 ? "max_ops_per_inst: %u\n" : "", MaxOpsPerInst)
David Blaikie1d4736e2014-02-24 23:58:54 +000067 << format(" default_is_stmt: %u\n", DefaultIsStmt)
68 << format(" line_base: %i\n", LineBase)
69 << format(" line_range: %u\n", LineRange)
70 << format(" opcode_base: %u\n", OpcodeBase);
Benjamin Kramer5acab502011-09-15 02:12:05 +000071
Paul Robinson9d4eb692017-05-01 23:27:55 +000072 for (uint32_t I = 0; I != StandardOpcodeLengths.size(); ++I)
Mehdi Amini149f6ea2016-10-05 05:59:29 +000073 OS << format("standard_opcode_lengths[%s] = %u\n",
Paul Robinson9d4eb692017-05-01 23:27:55 +000074 LNStandardString(I + 1).data(), StandardOpcodeLengths[I]);
Benjamin Kramer5acab502011-09-15 02:12:05 +000075
76 if (!IncludeDirectories.empty())
Paul Robinson9d4eb692017-05-01 23:27:55 +000077 for (uint32_t I = 0; I != IncludeDirectories.size(); ++I)
78 OS << format("include_directories[%3u] = '", I + 1)
79 << IncludeDirectories[I] << "'\n";
Benjamin Kramer5acab502011-09-15 02:12:05 +000080
81 if (!FileNames.empty()) {
82 OS << " Dir Mod Time File Len File Name\n"
83 << " ---- ---------- ---------- -----------"
84 "----------------\n";
Paul Robinson9d4eb692017-05-01 23:27:55 +000085 for (uint32_t I = 0; I != FileNames.size(); ++I) {
86 const FileNameEntry &FileEntry = FileNames[I];
87 OS << format("file_names[%3u] %4" PRIu64 " ", I + 1, FileEntry.DirIdx)
88 << format("0x%8.8" PRIx64 " 0x%8.8" PRIx64 " ", FileEntry.ModTime,
89 FileEntry.Length)
90 << FileEntry.Name << '\n';
Benjamin Kramer5acab502011-09-15 02:12:05 +000091 }
92 }
93}
94
Paul Robinson2bc38732017-05-02 21:40:47 +000095// Parse v2-v4 directory and file tables.
96static void
Paul Robinson17536b92017-06-29 16:52:08 +000097parseV2DirFileTables(const DWARFDataExtractor &DebugLineData,
98 uint32_t *OffsetPtr, uint64_t EndPrologueOffset,
Paul Robinson2bc38732017-05-02 21:40:47 +000099 std::vector<StringRef> &IncludeDirectories,
100 std::vector<DWARFDebugLine::FileNameEntry> &FileNames) {
101 while (*OffsetPtr < EndPrologueOffset) {
102 StringRef S = DebugLineData.getCStrRef(OffsetPtr);
103 if (S.empty())
104 break;
105 IncludeDirectories.push_back(S);
106 }
107
108 while (*OffsetPtr < EndPrologueOffset) {
109 StringRef Name = DebugLineData.getCStrRef(OffsetPtr);
110 if (Name.empty())
111 break;
112 DWARFDebugLine::FileNameEntry FileEntry;
113 FileEntry.Name = Name;
114 FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr);
115 FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr);
116 FileEntry.Length = DebugLineData.getULEB128(OffsetPtr);
117 FileNames.push_back(FileEntry);
118 }
119}
120
121// Parse v5 directory/file entry content descriptions.
122// Returns the descriptors, or an empty vector if we did not find a path or
123// ran off the end of the prologue.
124static ContentDescriptors
Paul Robinson17536b92017-06-29 16:52:08 +0000125parseV5EntryFormat(const DWARFDataExtractor &DebugLineData, uint32_t *OffsetPtr,
Paul Robinson2bc38732017-05-02 21:40:47 +0000126 uint64_t EndPrologueOffset) {
127 ContentDescriptors Descriptors;
128 int FormatCount = DebugLineData.getU8(OffsetPtr);
129 bool HasPath = false;
130 for (int I = 0; I != FormatCount; ++I) {
131 if (*OffsetPtr >= EndPrologueOffset)
132 return ContentDescriptors();
133 ContentDescriptor Descriptor;
134 Descriptor.Type =
135 dwarf::LineNumberEntryFormat(DebugLineData.getULEB128(OffsetPtr));
136 Descriptor.Form = dwarf::Form(DebugLineData.getULEB128(OffsetPtr));
137 if (Descriptor.Type == dwarf::DW_LNCT_path)
138 HasPath = true;
139 Descriptors.push_back(Descriptor);
140 }
141 return HasPath ? Descriptors : ContentDescriptors();
142}
143
144static bool
Paul Robinson17536b92017-06-29 16:52:08 +0000145parseV5DirFileTables(const DWARFDataExtractor &DebugLineData,
146 uint32_t *OffsetPtr, uint64_t EndPrologueOffset,
Paul Robinsone5400f82017-11-07 19:57:12 +0000147 const DWARFFormParams &FormParams, const DWARFUnit *U,
Paul Robinson2bc38732017-05-02 21:40:47 +0000148 std::vector<StringRef> &IncludeDirectories,
149 std::vector<DWARFDebugLine::FileNameEntry> &FileNames) {
150 // Get the directory entry description.
151 ContentDescriptors DirDescriptors =
152 parseV5EntryFormat(DebugLineData, OffsetPtr, EndPrologueOffset);
153 if (DirDescriptors.empty())
154 return false;
155
156 // Get the directory entries, according to the format described above.
157 int DirEntryCount = DebugLineData.getU8(OffsetPtr);
158 for (int I = 0; I != DirEntryCount; ++I) {
159 if (*OffsetPtr >= EndPrologueOffset)
160 return false;
161 for (auto Descriptor : DirDescriptors) {
162 DWARFFormValue Value(Descriptor.Form);
163 switch (Descriptor.Type) {
164 case DW_LNCT_path:
Paul Robinsone5400f82017-11-07 19:57:12 +0000165 if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, U))
Paul Robinson2bc38732017-05-02 21:40:47 +0000166 return false;
167 IncludeDirectories.push_back(Value.getAsCString().getValue());
168 break;
169 default:
Paul Robinson75c068c2017-06-26 18:43:01 +0000170 if (!Value.skipValue(DebugLineData, OffsetPtr, FormParams))
Paul Robinson2bc38732017-05-02 21:40:47 +0000171 return false;
172 }
173 }
174 }
175
176 // Get the file entry description.
177 ContentDescriptors FileDescriptors =
178 parseV5EntryFormat(DebugLineData, OffsetPtr, EndPrologueOffset);
179 if (FileDescriptors.empty())
180 return false;
181
182 // Get the file entries, according to the format described above.
183 int FileEntryCount = DebugLineData.getU8(OffsetPtr);
184 for (int I = 0; I != FileEntryCount; ++I) {
185 if (*OffsetPtr >= EndPrologueOffset)
186 return false;
187 DWARFDebugLine::FileNameEntry FileEntry;
188 for (auto Descriptor : FileDescriptors) {
189 DWARFFormValue Value(Descriptor.Form);
Paul Robinsone5400f82017-11-07 19:57:12 +0000190 if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, U))
Paul Robinson2bc38732017-05-02 21:40:47 +0000191 return false;
192 switch (Descriptor.Type) {
193 case DW_LNCT_path:
194 FileEntry.Name = Value.getAsCString().getValue();
195 break;
196 case DW_LNCT_directory_index:
197 FileEntry.DirIdx = Value.getAsUnsignedConstant().getValue();
198 break;
199 case DW_LNCT_timestamp:
200 FileEntry.ModTime = Value.getAsUnsignedConstant().getValue();
201 break;
202 case DW_LNCT_size:
203 FileEntry.Length = Value.getAsUnsignedConstant().getValue();
204 break;
205 // FIXME: Add MD5
206 default:
207 break;
208 }
209 }
210 FileNames.push_back(FileEntry);
211 }
212 return true;
213}
214
Paul Robinson17536b92017-06-29 16:52:08 +0000215bool DWARFDebugLine::Prologue::parse(const DWARFDataExtractor &DebugLineData,
Paul Robinsone5400f82017-11-07 19:57:12 +0000216 uint32_t *OffsetPtr, const DWARFUnit *U) {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000217 const uint64_t PrologueOffset = *OffsetPtr;
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000218
219 clear();
Paul Robinson9d4eb692017-05-01 23:27:55 +0000220 TotalLength = DebugLineData.getU32(OffsetPtr);
Ed Maste6d0bee52015-05-28 15:38:17 +0000221 if (TotalLength == UINT32_MAX) {
Paul Robinson75c068c2017-06-26 18:43:01 +0000222 FormParams.Format = dwarf::DWARF64;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000223 TotalLength = DebugLineData.getU64(OffsetPtr);
Paul Robinson75c068c2017-06-26 18:43:01 +0000224 } else if (TotalLength >= 0xffffff00) {
Ed Maste6d0bee52015-05-28 15:38:17 +0000225 return false;
226 }
Paul Robinson75c068c2017-06-26 18:43:01 +0000227 FormParams.Version = DebugLineData.getU16(OffsetPtr);
228 if (getVersion() < 2)
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000229 return false;
230
Paul Robinson75c068c2017-06-26 18:43:01 +0000231 if (getVersion() >= 5) {
232 FormParams.AddrSize = DebugLineData.getU8(OffsetPtr);
Paul Robinson63811a42017-11-22 15:33:17 +0000233 assert((DebugLineData.getAddressSize() == 0 ||
234 DebugLineData.getAddressSize() == getAddressSize()) &&
Paul Robinson75c068c2017-06-26 18:43:01 +0000235 "Line table header and data extractor disagree");
Paul Robinson2bc38732017-05-02 21:40:47 +0000236 SegSelectorSize = DebugLineData.getU8(OffsetPtr);
237 }
238
Paul Robinson9d4eb692017-05-01 23:27:55 +0000239 PrologueLength = DebugLineData.getUnsigned(OffsetPtr, sizeofPrologueLength());
240 const uint64_t EndPrologueOffset = PrologueLength + *OffsetPtr;
241 MinInstLength = DebugLineData.getU8(OffsetPtr);
Paul Robinson75c068c2017-06-26 18:43:01 +0000242 if (getVersion() >= 4)
Paul Robinson9d4eb692017-05-01 23:27:55 +0000243 MaxOpsPerInst = DebugLineData.getU8(OffsetPtr);
244 DefaultIsStmt = DebugLineData.getU8(OffsetPtr);
245 LineBase = DebugLineData.getU8(OffsetPtr);
246 LineRange = DebugLineData.getU8(OffsetPtr);
247 OpcodeBase = DebugLineData.getU8(OffsetPtr);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000248
249 StandardOpcodeLengths.reserve(OpcodeBase - 1);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000250 for (uint32_t I = 1; I < OpcodeBase; ++I) {
251 uint8_t OpLen = DebugLineData.getU8(OffsetPtr);
252 StandardOpcodeLengths.push_back(OpLen);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000253 }
254
Paul Robinson75c068c2017-06-26 18:43:01 +0000255 if (getVersion() >= 5) {
Paul Robinson2bc38732017-05-02 21:40:47 +0000256 if (!parseV5DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset,
Paul Robinsone5400f82017-11-07 19:57:12 +0000257 getFormParams(), U, IncludeDirectories,
258 FileNames)) {
Paul Robinson2bc38732017-05-02 21:40:47 +0000259 fprintf(stderr,
260 "warning: parsing line table prologue at 0x%8.8" PRIx64
261 " found an invalid directory or file table description at"
262 " 0x%8.8" PRIx64 "\n", PrologueOffset, (uint64_t)*OffsetPtr);
263 return false;
264 }
265 } else
266 parseV2DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset,
267 IncludeDirectories, FileNames);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000268
Paul Robinson9d4eb692017-05-01 23:27:55 +0000269 if (*OffsetPtr != EndPrologueOffset) {
270 fprintf(stderr,
271 "warning: parsing line table prologue at 0x%8.8" PRIx64
272 " should have ended at 0x%8.8" PRIx64
273 " but it ended at 0x%8.8" PRIx64 "\n",
274 PrologueOffset, EndPrologueOffset, (uint64_t)*OffsetPtr);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000275 return false;
276 }
277 return true;
278}
279
Paul Robinson9d4eb692017-05-01 23:27:55 +0000280DWARFDebugLine::Row::Row(bool DefaultIsStmt) { reset(DefaultIsStmt); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000281
Benjamin Kramer5acab502011-09-15 02:12:05 +0000282void DWARFDebugLine::Row::postAppend() {
283 BasicBlock = false;
284 PrologueEnd = false;
285 EpilogueBegin = false;
286}
287
Paul Robinson9d4eb692017-05-01 23:27:55 +0000288void DWARFDebugLine::Row::reset(bool DefaultIsStmt) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000289 Address = 0;
290 Line = 1;
291 Column = 0;
292 File = 1;
293 Isa = 0;
Diego Novillo5b5cf502014-02-14 19:27:53 +0000294 Discriminator = 0;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000295 IsStmt = DefaultIsStmt;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000296 BasicBlock = false;
297 EndSequence = false;
298 PrologueEnd = false;
299 EpilogueBegin = false;
300}
301
Greg Clayton67070462017-05-02 22:48:52 +0000302void DWARFDebugLine::Row::dumpTableHeader(raw_ostream &OS) {
303 OS << "Address Line Column File ISA Discriminator Flags\n"
304 << "------------------ ------ ------ ------ --- ------------- "
305 "-------------\n";
306}
307
Benjamin Kramer5acab502011-09-15 02:12:05 +0000308void DWARFDebugLine::Row::dump(raw_ostream &OS) const {
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000309 OS << format("0x%16.16" PRIx64 " %6u %6u", Address, Line, Column)
Diego Novillo5b5cf502014-02-14 19:27:53 +0000310 << format(" %6u %3u %13u ", File, Isa, Discriminator)
Dehao Chen1b54fce2016-04-28 22:09:37 +0000311 << (IsStmt ? " is_stmt" : "") << (BasicBlock ? " basic_block" : "")
Benjamin Kramer5acab502011-09-15 02:12:05 +0000312 << (PrologueEnd ? " prologue_end" : "")
313 << (EpilogueBegin ? " epilogue_begin" : "")
Dehao Chen1b54fce2016-04-28 22:09:37 +0000314 << (EndSequence ? " end_sequence" : "") << '\n';
Benjamin Kramer5acab502011-09-15 02:12:05 +0000315}
316
Dehao Chen1b54fce2016-04-28 22:09:37 +0000317DWARFDebugLine::Sequence::Sequence() { reset(); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000318
319void DWARFDebugLine::Sequence::reset() {
320 LowPC = 0;
321 HighPC = 0;
322 FirstRowIndex = 0;
323 LastRowIndex = 0;
324 Empty = true;
325}
326
Dehao Chen1b54fce2016-04-28 22:09:37 +0000327DWARFDebugLine::LineTable::LineTable() { clear(); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000328
Benjamin Kramer5acab502011-09-15 02:12:05 +0000329void DWARFDebugLine::LineTable::dump(raw_ostream &OS) const {
330 Prologue.dump(OS);
331 OS << '\n';
332
333 if (!Rows.empty()) {
Greg Clayton67070462017-05-02 22:48:52 +0000334 Row::dumpTableHeader(OS);
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000335 for (const Row &R : Rows) {
336 R.dump(OS);
337 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000338 }
339}
340
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000341void DWARFDebugLine::LineTable::clear() {
342 Prologue.clear();
343 Rows.clear();
344 Sequences.clear();
345}
346
Alexey Samsonov110d5952014-04-30 00:09:19 +0000347DWARFDebugLine::ParsingState::ParsingState(struct LineTable *LT)
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +0000348 : LineTable(LT) {
Alexey Samsonov110d5952014-04-30 00:09:19 +0000349 resetRowAndSequence();
350}
Nick Lewycky4d044922011-09-15 03:41:51 +0000351
Alexey Samsonov110d5952014-04-30 00:09:19 +0000352void DWARFDebugLine::ParsingState::resetRowAndSequence() {
353 Row.reset(LineTable->Prologue.DefaultIsStmt);
354 Sequence.reset();
355}
356
Paul Robinson9d4eb692017-05-01 23:27:55 +0000357void DWARFDebugLine::ParsingState::appendRowToMatrix(uint32_t Offset) {
Alexey Samsonov110d5952014-04-30 00:09:19 +0000358 if (Sequence.Empty) {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000359 // Record the beginning of instruction sequence.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000360 Sequence.Empty = false;
361 Sequence.LowPC = Row.Address;
362 Sequence.FirstRowIndex = RowNumber;
Alexey Samsonov947228c2012-08-07 11:46:57 +0000363 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000364 ++RowNumber;
365 LineTable->appendRow(Row);
366 if (Row.EndSequence) {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000367 // Record the end of instruction sequence.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000368 Sequence.HighPC = Row.Address;
369 Sequence.LastRowIndex = RowNumber;
370 if (Sequence.isValid())
371 LineTable->appendSequence(Sequence);
372 Sequence.reset();
Alexey Samsonov947228c2012-08-07 11:46:57 +0000373 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000374 Row.postAppend();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000375}
376
Benjamin Kramer5acab502011-09-15 02:12:05 +0000377const DWARFDebugLine::LineTable *
Paul Robinson9d4eb692017-05-01 23:27:55 +0000378DWARFDebugLine::getLineTable(uint32_t Offset) const {
379 LineTableConstIter Pos = LineTableMap.find(Offset);
380 if (Pos != LineTableMap.end())
381 return &Pos->second;
Craig Topper2617dcc2014-04-15 06:32:26 +0000382 return nullptr;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000383}
384
Benjamin Kramer679e1752011-09-15 20:43:18 +0000385const DWARFDebugLine::LineTable *
Paul Robinson511b54c2017-11-22 15:48:30 +0000386DWARFDebugLine::getOrParseLineTable(DWARFDataExtractor &DebugLineData,
Paul Robinsone5400f82017-11-07 19:57:12 +0000387 uint32_t Offset, const DWARFUnit *U) {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000388 std::pair<LineTableIter, bool> Pos =
389 LineTableMap.insert(LineTableMapTy::value_type(Offset, LineTable()));
390 LineTable *LT = &Pos.first->second;
391 if (Pos.second) {
Paul Robinsone5400f82017-11-07 19:57:12 +0000392 if (!LT->parse(DebugLineData, &Offset, U))
Craig Topper2617dcc2014-04-15 06:32:26 +0000393 return nullptr;
Benjamin Kramer679e1752011-09-15 20:43:18 +0000394 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000395 return LT;
Benjamin Kramer679e1752011-09-15 20:43:18 +0000396}
397
Paul Robinson511b54c2017-11-22 15:48:30 +0000398bool DWARFDebugLine::LineTable::parse(DWARFDataExtractor &DebugLineData,
Paul Robinsone5400f82017-11-07 19:57:12 +0000399 uint32_t *OffsetPtr, const DWARFUnit *U,
400 raw_ostream *OS) {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000401 const uint32_t DebugLineOffset = *OffsetPtr;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000402
Alexey Samsonov110d5952014-04-30 00:09:19 +0000403 clear();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000404
Paul Robinsone5400f82017-11-07 19:57:12 +0000405 if (!Prologue.parse(DebugLineData, OffsetPtr, U)) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000406 // Restore our offset and return false to indicate failure!
Paul Robinson9d4eb692017-05-01 23:27:55 +0000407 *OffsetPtr = DebugLineOffset;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000408 return false;
409 }
410
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000411 if (OS)
412 Prologue.dump(*OS);
413
Paul Robinson9d4eb692017-05-01 23:27:55 +0000414 const uint32_t EndOffset =
415 DebugLineOffset + Prologue.TotalLength + Prologue.sizeofTotalLength();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000416
Paul Robinson511b54c2017-11-22 15:48:30 +0000417 // See if we should tell the data extractor the address size.
418 if (DebugLineData.getAddressSize() == 0)
419 DebugLineData.setAddressSize(Prologue.getAddressSize());
420 else
421 assert(Prologue.getAddressSize() == 0 ||
422 Prologue.getAddressSize() == DebugLineData.getAddressSize());
423
Alexey Samsonov110d5952014-04-30 00:09:19 +0000424 ParsingState State(this);
Benjamin Kramer112ec172011-09-15 21:59:13 +0000425
Paul Robinson9d4eb692017-05-01 23:27:55 +0000426 while (*OffsetPtr < EndOffset) {
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000427 if (OS)
428 *OS << format("0x%08.08" PRIx32 ": ", *OffsetPtr);
429
Paul Robinson9d4eb692017-05-01 23:27:55 +0000430 uint8_t Opcode = DebugLineData.getU8(OffsetPtr);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000431
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000432 if (OS)
433 *OS << format("%02.02" PRIx8 " ", Opcode);
434
Paul Robinson9d4eb692017-05-01 23:27:55 +0000435 if (Opcode == 0) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000436 // Extended Opcodes always start with a zero opcode followed by
437 // a uleb128 length so you can skip ones you don't know about
Paul Robinson9d4eb692017-05-01 23:27:55 +0000438 uint64_t Len = DebugLineData.getULEB128(OffsetPtr);
Paul Robinsone0833342017-11-22 15:14:49 +0000439 uint32_t ExtOffset = *OffsetPtr;
440
441 // Tolerate zero-length; assume length is correct and soldier on.
442 if (Len == 0) {
443 if (OS)
444 *OS << "Badly formed extended line op (length 0)\n";
445 continue;
446 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000447
Paul Robinson9d4eb692017-05-01 23:27:55 +0000448 uint8_t SubOpcode = DebugLineData.getU8(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000449 if (OS)
450 *OS << LNExtendedString(SubOpcode);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000451 switch (SubOpcode) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000452 case DW_LNE_end_sequence:
453 // Set the end_sequence register of the state machine to true and
454 // append a row to the matrix using the current values of the
455 // state-machine registers. Then reset the registers to the initial
456 // values specified above. Every statement program sequence must end
457 // with a DW_LNE_end_sequence instruction which creates a row whose
458 // address is that of the byte after the last target machine instruction
459 // of the sequence.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000460 State.Row.EndSequence = true;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000461 State.appendRowToMatrix(*OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000462 if (OS) {
463 *OS << "\n";
464 OS->indent(12);
465 State.Row.dump(*OS);
466 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000467 State.resetRowAndSequence();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000468 break;
469
470 case DW_LNE_set_address:
471 // Takes a single relocatable address as an operand. The size of the
472 // operand is the size appropriate to hold an address on the target
473 // machine. Set the address register to the value given by the
474 // relocatable address. All of the other statement program opcodes
475 // that affect the address register add a delta to it. This instruction
476 // stores a relocatable value into it instead.
Paul Robinson511b54c2017-11-22 15:48:30 +0000477 //
478 // Make sure the extractor knows the address size. If not, infer it
479 // from the size of the operand.
480 if (DebugLineData.getAddressSize() == 0)
481 DebugLineData.setAddressSize(Len - 1);
482 else
483 assert(DebugLineData.getAddressSize() == Len - 1);
Paul Robinson17536b92017-06-29 16:52:08 +0000484 State.Row.Address = DebugLineData.getRelocatedAddress(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000485 if (OS)
486 *OS << format(" (0x%16.16" PRIx64 ")", State.Row.Address);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000487 break;
488
489 case DW_LNE_define_file:
490 // Takes 4 arguments. The first is a null terminated string containing
491 // a source file name. The second is an unsigned LEB128 number
492 // representing the directory index of the directory in which the file
493 // was found. The third is an unsigned LEB128 number representing the
494 // time of last modification of the file. The fourth is an unsigned
495 // LEB128 number representing the length in bytes of the file. The time
496 // and length fields may contain LEB128(0) if the information is not
497 // available.
498 //
499 // The directory index represents an entry in the include_directories
500 // section of the statement program prologue. The index is LEB128(0)
501 // if the file was found in the current directory of the compilation,
502 // LEB128(1) if it was found in the first directory in the
503 // include_directories section, and so on. The directory index is
504 // ignored for file names that represent full path names.
505 //
506 // The files are numbered, starting at 1, in the order in which they
507 // appear; the names in the prologue come before names defined by
508 // the DW_LNE_define_file instruction. These numbers are used in the
509 // the file register of the state machine.
510 {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000511 FileNameEntry FileEntry;
512 FileEntry.Name = DebugLineData.getCStr(OffsetPtr);
513 FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr);
514 FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr);
515 FileEntry.Length = DebugLineData.getULEB128(OffsetPtr);
516 Prologue.FileNames.push_back(FileEntry);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000517 if (OS)
518 *OS << " (" << FileEntry.Name.str()
519 << ", dir=" << FileEntry.DirIdx << ", mod_time="
520 << format("(0x%16.16" PRIx64 ")", FileEntry.ModTime)
521 << ", length=" << FileEntry.Length << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000522 }
523 break;
524
Diego Novillo5b5cf502014-02-14 19:27:53 +0000525 case DW_LNE_set_discriminator:
Paul Robinson9d4eb692017-05-01 23:27:55 +0000526 State.Row.Discriminator = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000527 if (OS)
528 *OS << " (" << State.Row.Discriminator << ")";
Diego Novillo5b5cf502014-02-14 19:27:53 +0000529 break;
530
Benjamin Kramer5acab502011-09-15 02:12:05 +0000531 default:
Paul Robinsone0833342017-11-22 15:14:49 +0000532 if (OS)
533 *OS << format("Unrecognized extended op 0x%02.02" PRIx8, SubOpcode)
534 << format(" length %" PRIx64, Len);
535 // Len doesn't include the zero opcode byte or the length itself, but
536 // it does include the sub_opcode, so we have to adjust for that.
537 (*OffsetPtr) += Len - 1;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000538 break;
539 }
Paul Robinsone0833342017-11-22 15:14:49 +0000540 // Make sure the stated and parsed lengths are the same.
541 // Otherwise we have an unparseable line-number program.
542 if (*OffsetPtr - ExtOffset != Len) {
543 fprintf(stderr, "Unexpected line op length at offset 0x%8.8" PRIx32
544 " expected 0x%2.2" PRIx64 " found 0x%2.2" PRIx32 "\n",
545 ExtOffset, Len, *OffsetPtr - ExtOffset);
546 // Skip the rest of the line-number program.
547 *OffsetPtr = EndOffset;
548 return false;
549 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000550 } else if (Opcode < Prologue.OpcodeBase) {
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000551 if (OS)
552 *OS << LNStandardString(Opcode);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000553 switch (Opcode) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000554 // Standard Opcodes
555 case DW_LNS_copy:
556 // Takes no arguments. Append a row to the matrix using the
557 // current values of the state-machine registers. Then set
558 // the basic_block register to false.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000559 State.appendRowToMatrix(*OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000560 if (OS) {
561 *OS << "\n";
562 OS->indent(12);
563 State.Row.dump(*OS);
564 *OS << "\n";
565 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000566 break;
567
568 case DW_LNS_advance_pc:
569 // Takes a single unsigned LEB128 operand, multiplies it by the
570 // min_inst_length field of the prologue, and adds the
571 // result to the address register of the state machine.
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000572 {
573 uint64_t AddrOffset =
574 DebugLineData.getULEB128(OffsetPtr) * Prologue.MinInstLength;
575 State.Row.Address += AddrOffset;
576 if (OS)
577 *OS << " (" << AddrOffset << ")";
578 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000579 break;
580
581 case DW_LNS_advance_line:
582 // Takes a single signed LEB128 operand and adds that value to
583 // the line register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000584 State.Row.Line += DebugLineData.getSLEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000585 if (OS)
586 *OS << " (" << State.Row.Line << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000587 break;
588
589 case DW_LNS_set_file:
590 // Takes a single unsigned LEB128 operand and stores it in the file
591 // register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000592 State.Row.File = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000593 if (OS)
594 *OS << " (" << State.Row.File << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000595 break;
596
597 case DW_LNS_set_column:
598 // Takes a single unsigned LEB128 operand and stores it in the
599 // column register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000600 State.Row.Column = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000601 if (OS)
602 *OS << " (" << State.Row.Column << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000603 break;
604
605 case DW_LNS_negate_stmt:
606 // Takes no arguments. Set the is_stmt register of the state
607 // machine to the logical negation of its current value.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000608 State.Row.IsStmt = !State.Row.IsStmt;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000609 break;
610
611 case DW_LNS_set_basic_block:
612 // Takes no arguments. Set the basic_block register of the
613 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000614 State.Row.BasicBlock = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000615 break;
616
617 case DW_LNS_const_add_pc:
618 // Takes no arguments. Add to the address register of the state
619 // machine the address increment value corresponding to special
620 // opcode 255. The motivation for DW_LNS_const_add_pc is this:
621 // when the statement program needs to advance the address by a
622 // small amount, it can use a single special opcode, which occupies
623 // a single byte. When it needs to advance the address by up to
624 // twice the range of the last special opcode, it can use
625 // DW_LNS_const_add_pc followed by a special opcode, for a total
626 // of two bytes. Only if it needs to advance the address by more
627 // than twice that range will it need to use both DW_LNS_advance_pc
628 // and a special opcode, requiring three or more bytes.
629 {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000630 uint8_t AdjustOpcode = 255 - Prologue.OpcodeBase;
631 uint64_t AddrOffset =
632 (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength;
633 State.Row.Address += AddrOffset;
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000634 if (OS)
635 *OS
636 << format(" (0x%16.16" PRIx64 ")", AddrOffset);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000637 }
638 break;
639
640 case DW_LNS_fixed_advance_pc:
641 // Takes a single uhalf operand. Add to the address register of
642 // the state machine the value of the (unencoded) operand. This
643 // is the only extended opcode that takes an argument that is not
644 // a variable length number. The motivation for DW_LNS_fixed_advance_pc
645 // is this: existing assemblers cannot emit DW_LNS_advance_pc or
646 // special opcodes because they cannot encode LEB128 numbers or
647 // judge when the computation of a special opcode overflows and
648 // requires the use of DW_LNS_advance_pc. Such assemblers, however,
649 // can use DW_LNS_fixed_advance_pc instead, sacrificing compression.
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000650 {
651 uint16_t PCOffset = DebugLineData.getU16(OffsetPtr);
652 State.Row.Address += PCOffset;
653 if (OS)
654 *OS
655 << format(" (0x%16.16" PRIx64 ")", PCOffset);
656 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000657 break;
658
659 case DW_LNS_set_prologue_end:
660 // Takes no arguments. Set the prologue_end register of the
661 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000662 State.Row.PrologueEnd = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000663 break;
664
665 case DW_LNS_set_epilogue_begin:
666 // Takes no arguments. Set the basic_block register of the
667 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000668 State.Row.EpilogueBegin = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000669 break;
670
671 case DW_LNS_set_isa:
672 // Takes a single unsigned LEB128 operand and stores it in the
673 // column register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000674 State.Row.Isa = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000675 if (OS)
676 *OS << " (" << State.Row.Isa << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000677 break;
678
679 default:
680 // Handle any unknown standard opcodes here. We know the lengths
681 // of such opcodes because they are specified in the prologue
682 // as a multiple of LEB128 operands for each opcode.
683 {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000684 assert(Opcode - 1U < Prologue.StandardOpcodeLengths.size());
685 uint8_t OpcodeLength = Prologue.StandardOpcodeLengths[Opcode - 1];
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000686 for (uint8_t I = 0; I < OpcodeLength; ++I) {
687 uint64_t Value = DebugLineData.getULEB128(OffsetPtr);
688 if (OS)
689 *OS << format("Skipping ULEB128 value: 0x%16.16" PRIx64 ")\n",
690 Value);
691 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000692 }
693 break;
694 }
695 } else {
696 // Special Opcodes
697
698 // A special opcode value is chosen based on the amount that needs
699 // to be added to the line and address registers. The maximum line
700 // increment for a special opcode is the value of the line_base
701 // field in the header, plus the value of the line_range field,
702 // minus 1 (line base + line range - 1). If the desired line
703 // increment is greater than the maximum line increment, a standard
NAKAMURA Takumif9959852011-10-08 11:22:47 +0000704 // opcode must be used instead of a special opcode. The "address
705 // advance" is calculated by dividing the desired address increment
Benjamin Kramer5acab502011-09-15 02:12:05 +0000706 // by the minimum_instruction_length field from the header. The
707 // special opcode is then calculated using the following formula:
708 //
709 // opcode = (desired line increment - line_base) +
710 // (line_range * address advance) + opcode_base
711 //
712 // If the resulting opcode is greater than 255, a standard opcode
713 // must be used instead.
714 //
715 // To decode a special opcode, subtract the opcode_base from the
716 // opcode itself to give the adjusted opcode. The amount to
717 // increment the address register is the result of the adjusted
718 // opcode divided by the line_range multiplied by the
719 // minimum_instruction_length field from the header. That is:
720 //
721 // address increment = (adjusted opcode / line_range) *
722 // minimum_instruction_length
723 //
724 // The amount to increment the line register is the line_base plus
725 // the result of the adjusted opcode modulo the line_range. That is:
726 //
727 // line increment = line_base + (adjusted opcode % line_range)
728
Paul Robinson9d4eb692017-05-01 23:27:55 +0000729 uint8_t AdjustOpcode = Opcode - Prologue.OpcodeBase;
730 uint64_t AddrOffset =
731 (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength;
732 int32_t LineOffset =
733 Prologue.LineBase + (AdjustOpcode % Prologue.LineRange);
734 State.Row.Line += LineOffset;
735 State.Row.Address += AddrOffset;
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000736
737 if (OS) {
738 *OS << "address += " << ((uint32_t)AdjustOpcode)
739 << ", line += " << LineOffset << "\n";
740 OS->indent(12);
741 State.Row.dump(*OS);
742 }
743
Paul Robinson9d4eb692017-05-01 23:27:55 +0000744 State.appendRowToMatrix(*OffsetPtr);
Dehao Chen1b54fce2016-04-28 22:09:37 +0000745 // Reset discriminator to 0.
746 State.Row.Discriminator = 0;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000747 }
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000748 if(OS)
749 *OS << "\n";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000750 }
751
Alexey Samsonov110d5952014-04-30 00:09:19 +0000752 if (!State.Sequence.Empty) {
753 fprintf(stderr, "warning: last sequence in debug line table is not"
754 "terminated!\n");
755 }
756
757 // Sort all sequences so that address lookup will work faster.
758 if (!Sequences.empty()) {
759 std::sort(Sequences.begin(), Sequences.end(), Sequence::orderByLowPC);
760 // Note: actually, instruction address ranges of sequences should not
761 // overlap (in shared objects and executables). If they do, the address
762 // lookup would still work, though, but result would be ambiguous.
763 // We don't report warning in this case. For example,
764 // sometimes .so compiled from multiple object files contains a few
765 // rudimentary sequences for address ranges [0x0, 0xsomething).
766 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000767
Paul Robinson9d4eb692017-05-01 23:27:55 +0000768 return EndOffset;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000769}
770
Keno Fischerc2c60182015-05-31 23:37:04 +0000771uint32_t
Paul Robinson9d4eb692017-05-01 23:27:55 +0000772DWARFDebugLine::LineTable::findRowInSeq(const DWARFDebugLine::Sequence &Seq,
773 uint64_t Address) const {
774 if (!Seq.containsPC(Address))
Keno Fischerc2c60182015-05-31 23:37:04 +0000775 return UnknownRowIndex;
776 // Search for instruction address in the rows describing the sequence.
777 // Rows are stored in a vector, so we may use arithmetical operations with
778 // iterators.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000779 DWARFDebugLine::Row Row;
780 Row.Address = Address;
781 RowIter FirstRow = Rows.begin() + Seq.FirstRowIndex;
782 RowIter LastRow = Rows.begin() + Seq.LastRowIndex;
783 LineTable::RowIter RowPos = std::lower_bound(
784 FirstRow, LastRow, Row, DWARFDebugLine::Row::orderByAddress);
785 if (RowPos == LastRow) {
786 return Seq.LastRowIndex - 1;
Keno Fischerc2c60182015-05-31 23:37:04 +0000787 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000788 uint32_t Index = Seq.FirstRowIndex + (RowPos - FirstRow);
789 if (RowPos->Address > Address) {
790 if (RowPos == FirstRow)
Keno Fischerc2c60182015-05-31 23:37:04 +0000791 return UnknownRowIndex;
792 else
Paul Robinson9d4eb692017-05-01 23:27:55 +0000793 Index--;
Keno Fischerc2c60182015-05-31 23:37:04 +0000794 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000795 return Index;
Keno Fischerc2c60182015-05-31 23:37:04 +0000796}
797
Paul Robinson9d4eb692017-05-01 23:27:55 +0000798uint32_t DWARFDebugLine::LineTable::lookupAddress(uint64_t Address) const {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000799 if (Sequences.empty())
Keno Fischerc2c60182015-05-31 23:37:04 +0000800 return UnknownRowIndex;
Alexey Samsonov947228c2012-08-07 11:46:57 +0000801 // First, find an instruction sequence containing the given address.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000802 DWARFDebugLine::Sequence Sequence;
803 Sequence.LowPC = Address;
804 SequenceIter FirstSeq = Sequences.begin();
805 SequenceIter LastSeq = Sequences.end();
806 SequenceIter SeqPos = std::lower_bound(
807 FirstSeq, LastSeq, Sequence, DWARFDebugLine::Sequence::orderByLowPC);
808 DWARFDebugLine::Sequence FoundSeq;
809 if (SeqPos == LastSeq) {
810 FoundSeq = Sequences.back();
811 } else if (SeqPos->LowPC == Address) {
812 FoundSeq = *SeqPos;
Alexey Samsonov947228c2012-08-07 11:46:57 +0000813 } else {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000814 if (SeqPos == FirstSeq)
Keno Fischerc2c60182015-05-31 23:37:04 +0000815 return UnknownRowIndex;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000816 FoundSeq = *(SeqPos - 1);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000817 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000818 return findRowInSeq(FoundSeq, Address);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000819}
Alexey Samsonov45be7932012-08-30 07:49:50 +0000820
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000821bool DWARFDebugLine::LineTable::lookupAddressRange(
Paul Robinson9d4eb692017-05-01 23:27:55 +0000822 uint64_t Address, uint64_t Size, std::vector<uint32_t> &Result) const {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000823 if (Sequences.empty())
824 return false;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000825 uint64_t EndAddr = Address + Size;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000826 // First, find an instruction sequence containing the given address.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000827 DWARFDebugLine::Sequence Sequence;
828 Sequence.LowPC = Address;
829 SequenceIter FirstSeq = Sequences.begin();
830 SequenceIter LastSeq = Sequences.end();
831 SequenceIter SeqPos = std::lower_bound(
832 FirstSeq, LastSeq, Sequence, DWARFDebugLine::Sequence::orderByLowPC);
833 if (SeqPos == LastSeq || SeqPos->LowPC != Address) {
834 if (SeqPos == FirstSeq)
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000835 return false;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000836 SeqPos--;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000837 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000838 if (!SeqPos->containsPC(Address))
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000839 return false;
840
Paul Robinson9d4eb692017-05-01 23:27:55 +0000841 SequenceIter StartPos = SeqPos;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000842
843 // Add the rows from the first sequence to the vector, starting with the
844 // index we just calculated
845
Paul Robinson9d4eb692017-05-01 23:27:55 +0000846 while (SeqPos != LastSeq && SeqPos->LowPC < EndAddr) {
847 const DWARFDebugLine::Sequence &CurSeq = *SeqPos;
Keno Fischerc2c60182015-05-31 23:37:04 +0000848 // For the first sequence, we need to find which row in the sequence is the
849 // first in our range.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000850 uint32_t FirstRowIndex = CurSeq.FirstRowIndex;
851 if (SeqPos == StartPos)
852 FirstRowIndex = findRowInSeq(CurSeq, Address);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000853
Keno Fischerc2c60182015-05-31 23:37:04 +0000854 // Figure out the last row in the range.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000855 uint32_t LastRowIndex = findRowInSeq(CurSeq, EndAddr - 1);
856 if (LastRowIndex == UnknownRowIndex)
857 LastRowIndex = CurSeq.LastRowIndex - 1;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000858
Paul Robinson9d4eb692017-05-01 23:27:55 +0000859 assert(FirstRowIndex != UnknownRowIndex);
860 assert(LastRowIndex != UnknownRowIndex);
Keno Fischerc2c60182015-05-31 23:37:04 +0000861
Paul Robinson9d4eb692017-05-01 23:27:55 +0000862 for (uint32_t I = FirstRowIndex; I <= LastRowIndex; ++I) {
863 Result.push_back(I);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000864 }
865
Paul Robinson9d4eb692017-05-01 23:27:55 +0000866 ++SeqPos;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000867 }
NAKAMURA Takumi4b86cdb2013-01-26 01:45:06 +0000868
869 return true;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000870}
871
Paul Robinson9d4eb692017-05-01 23:27:55 +0000872bool DWARFDebugLine::LineTable::hasFileAtIndex(uint64_t FileIndex) const {
Pete Cooperb2ba7762016-07-22 01:41:32 +0000873 return FileIndex != 0 && FileIndex <= Prologue.FileNames.size();
874}
875
Paul Robinson9d4eb692017-05-01 23:27:55 +0000876bool DWARFDebugLine::LineTable::getFileNameByIndex(uint64_t FileIndex,
877 const char *CompDir,
878 FileLineInfoKind Kind,
879 std::string &Result) const {
Pete Cooperb2ba7762016-07-22 01:41:32 +0000880 if (Kind == FileLineInfoKind::None || !hasFileAtIndex(FileIndex))
Alexey Samsonov45be7932012-08-30 07:49:50 +0000881 return false;
882 const FileNameEntry &Entry = Prologue.FileNames[FileIndex - 1];
Paul Robinsonba1c9152017-05-02 17:37:32 +0000883 StringRef FileName = Entry.Name;
Alexey Samsonovdce67342014-05-15 21:24:32 +0000884 if (Kind != FileLineInfoKind::AbsoluteFilePath ||
Alexey Samsonov45be7932012-08-30 07:49:50 +0000885 sys::path::is_absolute(FileName)) {
886 Result = FileName;
887 return true;
888 }
Frederic Riss101b5e22014-09-19 15:11:51 +0000889
Alexey Samsonov45be7932012-08-30 07:49:50 +0000890 SmallString<16> FilePath;
891 uint64_t IncludeDirIndex = Entry.DirIdx;
Paul Robinsonba1c9152017-05-02 17:37:32 +0000892 StringRef IncludeDir;
Alexey Samsonov45be7932012-08-30 07:49:50 +0000893 // Be defensive about the contents of Entry.
894 if (IncludeDirIndex > 0 &&
Frederic Riss101b5e22014-09-19 15:11:51 +0000895 IncludeDirIndex <= Prologue.IncludeDirectories.size())
896 IncludeDir = Prologue.IncludeDirectories[IncludeDirIndex - 1];
897
898 // We may still need to append compilation directory of compile unit.
899 // We know that FileName is not absolute, the only way to have an
900 // absolute path at this point would be if IncludeDir is absolute.
901 if (CompDir && Kind == FileLineInfoKind::AbsoluteFilePath &&
902 sys::path::is_relative(IncludeDir))
903 sys::path::append(FilePath, CompDir);
904
905 // sys::path::append skips empty strings.
906 sys::path::append(FilePath, IncludeDir, FileName);
Alexey Samsonov45be7932012-08-30 07:49:50 +0000907 Result = FilePath.str();
908 return true;
909}
Frederic Riss101b5e22014-09-19 15:11:51 +0000910
Dehao Chen1b54fce2016-04-28 22:09:37 +0000911bool DWARFDebugLine::LineTable::getFileLineInfoForAddress(
912 uint64_t Address, const char *CompDir, FileLineInfoKind Kind,
913 DILineInfo &Result) const {
Frederic Riss101b5e22014-09-19 15:11:51 +0000914 // Get the index of row we're looking for in the line table.
915 uint32_t RowIndex = lookupAddress(Address);
916 if (RowIndex == -1U)
917 return false;
918 // Take file number and line/column from the row.
919 const auto &Row = Rows[RowIndex];
920 if (!getFileNameByIndex(Row.File, CompDir, Kind, Result.FileName))
921 return false;
922 Result.Line = Row.Line;
923 Result.Column = Row.Column;
Eric Christopherba1024c2016-12-14 18:29:39 +0000924 Result.Discriminator = Row.Discriminator;
Frederic Riss101b5e22014-09-19 15:11:51 +0000925 return true;
926}