blob: a0bee0da17656422c1b921c59b110f13f9a83ff0 [file] [log] [blame]
Benjamin Kramer5acab502011-09-15 02:12:05 +00001//===-- DWARFDebugLine.cpp ------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Zachary Turner82af9432015-01-30 18:07:45 +000010#include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
Benjamin Kramer5acab502011-09-15 02:12:05 +000011#include "llvm/Support/Dwarf.h"
12#include "llvm/Support/Format.h"
Alexey Samsonov45be7932012-08-30 07:49:50 +000013#include "llvm/Support/Path.h"
Benjamin Kramer5acab502011-09-15 02:12:05 +000014#include "llvm/Support/raw_ostream.h"
Benjamin Kramera57c46a2011-09-15 02:19:33 +000015#include <algorithm>
Benjamin Kramer5acab502011-09-15 02:12:05 +000016using namespace llvm;
17using namespace dwarf;
Alexey Samsonovdce67342014-05-15 21:24:32 +000018typedef DILineInfoSpecifier::FileLineInfoKind FileLineInfoKind;
Benjamin Kramer5acab502011-09-15 02:12:05 +000019
Alexey Samsonov836b1ae2014-04-29 21:28:13 +000020DWARFDebugLine::Prologue::Prologue() {
21 clear();
22}
23
24void DWARFDebugLine::Prologue::clear() {
25 TotalLength = Version = PrologueLength = 0;
26 MinInstLength = MaxOpsPerInst = DefaultIsStmt = LineBase = LineRange = 0;
27 OpcodeBase = 0;
Ed Maste6d0bee52015-05-28 15:38:17 +000028 IsDWARF64 = false;
Alexey Samsonov836b1ae2014-04-29 21:28:13 +000029 StandardOpcodeLengths.clear();
30 IncludeDirectories.clear();
31 FileNames.clear();
32}
33
Benjamin Kramer5acab502011-09-15 02:12:05 +000034void DWARFDebugLine::Prologue::dump(raw_ostream &OS) const {
35 OS << "Line table prologue:\n"
Ed Maste6d0bee52015-05-28 15:38:17 +000036 << format(" total_length: 0x%8.8" PRIx64 "\n", TotalLength)
David Blaikie1d4736e2014-02-24 23:58:54 +000037 << format(" version: %u\n", Version)
Ed Maste6d0bee52015-05-28 15:38:17 +000038 << format(" prologue_length: 0x%8.8" PRIx64 "\n", PrologueLength)
David Blaikie1d4736e2014-02-24 23:58:54 +000039 << format(" min_inst_length: %u\n", MinInstLength)
40 << format(Version >= 4 ? "max_ops_per_inst: %u\n" : "", MaxOpsPerInst)
41 << format(" default_is_stmt: %u\n", DefaultIsStmt)
42 << format(" line_base: %i\n", LineBase)
43 << format(" line_range: %u\n", LineRange)
44 << format(" opcode_base: %u\n", OpcodeBase);
Benjamin Kramer5acab502011-09-15 02:12:05 +000045
46 for (uint32_t i = 0; i < StandardOpcodeLengths.size(); ++i)
47 OS << format("standard_opcode_lengths[%s] = %u\n", LNStandardString(i+1),
48 StandardOpcodeLengths[i]);
49
50 if (!IncludeDirectories.empty())
51 for (uint32_t i = 0; i < IncludeDirectories.size(); ++i)
52 OS << format("include_directories[%3u] = '", i+1)
53 << IncludeDirectories[i] << "'\n";
54
55 if (!FileNames.empty()) {
56 OS << " Dir Mod Time File Len File Name\n"
57 << " ---- ---------- ---------- -----------"
58 "----------------\n";
59 for (uint32_t i = 0; i < FileNames.size(); ++i) {
60 const FileNameEntry& fileEntry = FileNames[i];
Benjamin Kramer79730ad2011-11-05 16:01:13 +000061 OS << format("file_names[%3u] %4" PRIu64 " ", i+1, fileEntry.DirIdx)
62 << format("0x%8.8" PRIx64 " 0x%8.8" PRIx64 " ",
63 fileEntry.ModTime, fileEntry.Length)
Benjamin Kramer5acab502011-09-15 02:12:05 +000064 << fileEntry.Name << '\n';
65 }
66 }
67}
68
Alexey Samsonov836b1ae2014-04-29 21:28:13 +000069bool DWARFDebugLine::Prologue::parse(DataExtractor debug_line_data,
70 uint32_t *offset_ptr) {
Ed Maste6d0bee52015-05-28 15:38:17 +000071 const uint64_t prologue_offset = *offset_ptr;
Alexey Samsonov836b1ae2014-04-29 21:28:13 +000072
73 clear();
74 TotalLength = debug_line_data.getU32(offset_ptr);
Ed Maste6d0bee52015-05-28 15:38:17 +000075 if (TotalLength == UINT32_MAX) {
76 IsDWARF64 = true;
77 TotalLength = debug_line_data.getU64(offset_ptr);
78 } else if (TotalLength > 0xffffff00) {
79 return false;
80 }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +000081 Version = debug_line_data.getU16(offset_ptr);
82 if (Version < 2)
83 return false;
84
Ed Maste6d0bee52015-05-28 15:38:17 +000085 PrologueLength = debug_line_data.getUnsigned(offset_ptr,
86 sizeofPrologueLength());
87 const uint64_t end_prologue_offset = PrologueLength + *offset_ptr;
Alexey Samsonov836b1ae2014-04-29 21:28:13 +000088 MinInstLength = debug_line_data.getU8(offset_ptr);
89 if (Version >= 4)
90 MaxOpsPerInst = debug_line_data.getU8(offset_ptr);
91 DefaultIsStmt = debug_line_data.getU8(offset_ptr);
92 LineBase = debug_line_data.getU8(offset_ptr);
93 LineRange = debug_line_data.getU8(offset_ptr);
94 OpcodeBase = debug_line_data.getU8(offset_ptr);
95
96 StandardOpcodeLengths.reserve(OpcodeBase - 1);
97 for (uint32_t i = 1; i < OpcodeBase; ++i) {
98 uint8_t op_len = debug_line_data.getU8(offset_ptr);
99 StandardOpcodeLengths.push_back(op_len);
100 }
101
102 while (*offset_ptr < end_prologue_offset) {
103 const char *s = debug_line_data.getCStr(offset_ptr);
104 if (s && s[0])
105 IncludeDirectories.push_back(s);
106 else
107 break;
108 }
109
110 while (*offset_ptr < end_prologue_offset) {
111 const char *name = debug_line_data.getCStr(offset_ptr);
112 if (name && name[0]) {
113 FileNameEntry fileEntry;
114 fileEntry.Name = name;
115 fileEntry.DirIdx = debug_line_data.getULEB128(offset_ptr);
116 fileEntry.ModTime = debug_line_data.getULEB128(offset_ptr);
117 fileEntry.Length = debug_line_data.getULEB128(offset_ptr);
118 FileNames.push_back(fileEntry);
119 } else {
120 break;
121 }
122 }
123
124 if (*offset_ptr != end_prologue_offset) {
Ed Maste6d0bee52015-05-28 15:38:17 +0000125 fprintf(stderr, "warning: parsing line table prologue at 0x%8.8" PRIx64
126 " should have ended at 0x%8.8" PRIx64
127 " but it ended at 0x%8.8" PRIx64 "\n",
128 prologue_offset, end_prologue_offset, (uint64_t)*offset_ptr);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000129 return false;
130 }
131 return true;
132}
133
134DWARFDebugLine::Row::Row(bool default_is_stmt) {
135 reset(default_is_stmt);
136}
137
Benjamin Kramer5acab502011-09-15 02:12:05 +0000138void DWARFDebugLine::Row::postAppend() {
139 BasicBlock = false;
140 PrologueEnd = false;
141 EpilogueBegin = false;
142}
143
144void DWARFDebugLine::Row::reset(bool default_is_stmt) {
145 Address = 0;
146 Line = 1;
147 Column = 0;
148 File = 1;
149 Isa = 0;
Diego Novillo5b5cf502014-02-14 19:27:53 +0000150 Discriminator = 0;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000151 IsStmt = default_is_stmt;
152 BasicBlock = false;
153 EndSequence = false;
154 PrologueEnd = false;
155 EpilogueBegin = false;
156}
157
158void DWARFDebugLine::Row::dump(raw_ostream &OS) const {
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000159 OS << format("0x%16.16" PRIx64 " %6u %6u", Address, Line, Column)
Diego Novillo5b5cf502014-02-14 19:27:53 +0000160 << format(" %6u %3u %13u ", File, Isa, Discriminator)
Benjamin Kramer5acab502011-09-15 02:12:05 +0000161 << (IsStmt ? " is_stmt" : "")
162 << (BasicBlock ? " basic_block" : "")
163 << (PrologueEnd ? " prologue_end" : "")
164 << (EpilogueBegin ? " epilogue_begin" : "")
165 << (EndSequence ? " end_sequence" : "")
166 << '\n';
167}
168
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000169DWARFDebugLine::Sequence::Sequence() {
170 reset();
171}
172
173void DWARFDebugLine::Sequence::reset() {
174 LowPC = 0;
175 HighPC = 0;
176 FirstRowIndex = 0;
177 LastRowIndex = 0;
178 Empty = true;
179}
180
181DWARFDebugLine::LineTable::LineTable() {
182 clear();
183}
184
Benjamin Kramer5acab502011-09-15 02:12:05 +0000185void DWARFDebugLine::LineTable::dump(raw_ostream &OS) const {
186 Prologue.dump(OS);
187 OS << '\n';
188
189 if (!Rows.empty()) {
Diego Novillo5b5cf502014-02-14 19:27:53 +0000190 OS << "Address Line Column File ISA Discriminator Flags\n"
191 << "------------------ ------ ------ ------ --- ------------- "
192 "-------------\n";
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000193 for (const Row &R : Rows) {
194 R.dump(OS);
195 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000196 }
197}
198
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000199void DWARFDebugLine::LineTable::clear() {
200 Prologue.clear();
201 Rows.clear();
202 Sequences.clear();
203}
204
Alexey Samsonov110d5952014-04-30 00:09:19 +0000205DWARFDebugLine::ParsingState::ParsingState(struct LineTable *LT)
206 : LineTable(LT), RowNumber(0) {
207 resetRowAndSequence();
208}
Nick Lewycky4d044922011-09-15 03:41:51 +0000209
Alexey Samsonov110d5952014-04-30 00:09:19 +0000210void DWARFDebugLine::ParsingState::resetRowAndSequence() {
211 Row.reset(LineTable->Prologue.DefaultIsStmt);
212 Sequence.reset();
213}
214
215void DWARFDebugLine::ParsingState::appendRowToMatrix(uint32_t offset) {
216 if (Sequence.Empty) {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000217 // Record the beginning of instruction sequence.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000218 Sequence.Empty = false;
219 Sequence.LowPC = Row.Address;
220 Sequence.FirstRowIndex = RowNumber;
Alexey Samsonov947228c2012-08-07 11:46:57 +0000221 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000222 ++RowNumber;
223 LineTable->appendRow(Row);
224 if (Row.EndSequence) {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000225 // Record the end of instruction sequence.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000226 Sequence.HighPC = Row.Address;
227 Sequence.LastRowIndex = RowNumber;
228 if (Sequence.isValid())
229 LineTable->appendSequence(Sequence);
230 Sequence.reset();
Alexey Samsonov947228c2012-08-07 11:46:57 +0000231 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000232 Row.postAppend();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000233}
234
Benjamin Kramer5acab502011-09-15 02:12:05 +0000235const DWARFDebugLine::LineTable *
236DWARFDebugLine::getLineTable(uint32_t offset) const {
237 LineTableConstIter pos = LineTableMap.find(offset);
238 if (pos != LineTableMap.end())
239 return &pos->second;
Craig Topper2617dcc2014-04-15 06:32:26 +0000240 return nullptr;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000241}
242
Benjamin Kramer679e1752011-09-15 20:43:18 +0000243const DWARFDebugLine::LineTable *
244DWARFDebugLine::getOrParseLineTable(DataExtractor debug_line_data,
245 uint32_t offset) {
Benjamin Kramer2eeb4e52011-09-21 17:31:42 +0000246 std::pair<LineTableIter, bool> pos =
247 LineTableMap.insert(LineTableMapTy::value_type(offset, LineTable()));
Alexey Samsonov110d5952014-04-30 00:09:19 +0000248 LineTable *LT = &pos.first->second;
Benjamin Kramer2eeb4e52011-09-21 17:31:42 +0000249 if (pos.second) {
Alexey Samsonov110d5952014-04-30 00:09:19 +0000250 if (!LT->parse(debug_line_data, RelocMap, &offset))
Craig Topper2617dcc2014-04-15 06:32:26 +0000251 return nullptr;
Benjamin Kramer679e1752011-09-15 20:43:18 +0000252 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000253 return LT;
Benjamin Kramer679e1752011-09-15 20:43:18 +0000254}
255
Alexey Samsonov110d5952014-04-30 00:09:19 +0000256bool DWARFDebugLine::LineTable::parse(DataExtractor debug_line_data,
257 const RelocAddrMap *RMap,
258 uint32_t *offset_ptr) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000259 const uint32_t debug_line_offset = *offset_ptr;
260
Alexey Samsonov110d5952014-04-30 00:09:19 +0000261 clear();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000262
Alexey Samsonov110d5952014-04-30 00:09:19 +0000263 if (!Prologue.parse(debug_line_data, offset_ptr)) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000264 // Restore our offset and return false to indicate failure!
265 *offset_ptr = debug_line_offset;
266 return false;
267 }
268
Alexey Samsonov110d5952014-04-30 00:09:19 +0000269 const uint32_t end_offset = debug_line_offset + Prologue.TotalLength +
Ed Maste6d0bee52015-05-28 15:38:17 +0000270 Prologue.sizeofTotalLength();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000271
Alexey Samsonov110d5952014-04-30 00:09:19 +0000272 ParsingState State(this);
Benjamin Kramer112ec172011-09-15 21:59:13 +0000273
Benjamin Kramer5acab502011-09-15 02:12:05 +0000274 while (*offset_ptr < end_offset) {
275 uint8_t opcode = debug_line_data.getU8(offset_ptr);
276
277 if (opcode == 0) {
278 // Extended Opcodes always start with a zero opcode followed by
279 // a uleb128 length so you can skip ones you don't know about
280 uint32_t ext_offset = *offset_ptr;
281 uint64_t len = debug_line_data.getULEB128(offset_ptr);
282 uint32_t arg_size = len - (*offset_ptr - ext_offset);
283
284 uint8_t sub_opcode = debug_line_data.getU8(offset_ptr);
285 switch (sub_opcode) {
286 case DW_LNE_end_sequence:
287 // Set the end_sequence register of the state machine to true and
288 // append a row to the matrix using the current values of the
289 // state-machine registers. Then reset the registers to the initial
290 // values specified above. Every statement program sequence must end
291 // with a DW_LNE_end_sequence instruction which creates a row whose
292 // address is that of the byte after the last target machine instruction
293 // of the sequence.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000294 State.Row.EndSequence = true;
295 State.appendRowToMatrix(*offset_ptr);
296 State.resetRowAndSequence();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000297 break;
298
299 case DW_LNE_set_address:
300 // Takes a single relocatable address as an operand. The size of the
301 // operand is the size appropriate to hold an address on the target
302 // machine. Set the address register to the value given by the
303 // relocatable address. All of the other statement program opcodes
304 // that affect the address register add a delta to it. This instruction
305 // stores a relocatable value into it instead.
Andrew Kaylord55d7012013-01-25 22:50:58 +0000306 {
307 // If this address is in our relocation map, apply the relocation.
308 RelocAddrMap::const_iterator AI = RMap->find(*offset_ptr);
309 if (AI != RMap->end()) {
310 const std::pair<uint8_t, int64_t> &R = AI->second;
Alexey Samsonov110d5952014-04-30 00:09:19 +0000311 State.Row.Address =
312 debug_line_data.getAddress(offset_ptr) + R.second;
Andrew Kaylord55d7012013-01-25 22:50:58 +0000313 } else
Alexey Samsonov110d5952014-04-30 00:09:19 +0000314 State.Row.Address = debug_line_data.getAddress(offset_ptr);
Andrew Kaylord55d7012013-01-25 22:50:58 +0000315 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000316 break;
317
318 case DW_LNE_define_file:
319 // Takes 4 arguments. The first is a null terminated string containing
320 // a source file name. The second is an unsigned LEB128 number
321 // representing the directory index of the directory in which the file
322 // was found. The third is an unsigned LEB128 number representing the
323 // time of last modification of the file. The fourth is an unsigned
324 // LEB128 number representing the length in bytes of the file. The time
325 // and length fields may contain LEB128(0) if the information is not
326 // available.
327 //
328 // The directory index represents an entry in the include_directories
329 // section of the statement program prologue. The index is LEB128(0)
330 // if the file was found in the current directory of the compilation,
331 // LEB128(1) if it was found in the first directory in the
332 // include_directories section, and so on. The directory index is
333 // ignored for file names that represent full path names.
334 //
335 // The files are numbered, starting at 1, in the order in which they
336 // appear; the names in the prologue come before names defined by
337 // the DW_LNE_define_file instruction. These numbers are used in the
338 // the file register of the state machine.
339 {
340 FileNameEntry fileEntry;
341 fileEntry.Name = debug_line_data.getCStr(offset_ptr);
342 fileEntry.DirIdx = debug_line_data.getULEB128(offset_ptr);
343 fileEntry.ModTime = debug_line_data.getULEB128(offset_ptr);
344 fileEntry.Length = debug_line_data.getULEB128(offset_ptr);
Alexey Samsonov110d5952014-04-30 00:09:19 +0000345 Prologue.FileNames.push_back(fileEntry);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000346 }
347 break;
348
Diego Novillo5b5cf502014-02-14 19:27:53 +0000349 case DW_LNE_set_discriminator:
Alexey Samsonov110d5952014-04-30 00:09:19 +0000350 State.Row.Discriminator = debug_line_data.getULEB128(offset_ptr);
Diego Novillo5b5cf502014-02-14 19:27:53 +0000351 break;
352
Benjamin Kramer5acab502011-09-15 02:12:05 +0000353 default:
354 // Length doesn't include the zero opcode byte or the length itself, but
355 // it does include the sub_opcode, so we have to adjust for that below
356 (*offset_ptr) += arg_size;
357 break;
358 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000359 } else if (opcode < Prologue.OpcodeBase) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000360 switch (opcode) {
361 // Standard Opcodes
362 case DW_LNS_copy:
363 // Takes no arguments. Append a row to the matrix using the
364 // current values of the state-machine registers. Then set
365 // the basic_block register to false.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000366 State.appendRowToMatrix(*offset_ptr);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000367 break;
368
369 case DW_LNS_advance_pc:
370 // Takes a single unsigned LEB128 operand, multiplies it by the
371 // min_inst_length field of the prologue, and adds the
372 // result to the address register of the state machine.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000373 State.Row.Address +=
374 debug_line_data.getULEB128(offset_ptr) * Prologue.MinInstLength;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000375 break;
376
377 case DW_LNS_advance_line:
378 // Takes a single signed LEB128 operand and adds that value to
379 // the line register of the state machine.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000380 State.Row.Line += debug_line_data.getSLEB128(offset_ptr);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000381 break;
382
383 case DW_LNS_set_file:
384 // Takes a single unsigned LEB128 operand and stores it in the file
385 // register of the state machine.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000386 State.Row.File = debug_line_data.getULEB128(offset_ptr);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000387 break;
388
389 case DW_LNS_set_column:
390 // Takes a single unsigned LEB128 operand and stores it in the
391 // column register of the state machine.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000392 State.Row.Column = debug_line_data.getULEB128(offset_ptr);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000393 break;
394
395 case DW_LNS_negate_stmt:
396 // Takes no arguments. Set the is_stmt register of the state
397 // machine to the logical negation of its current value.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000398 State.Row.IsStmt = !State.Row.IsStmt;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000399 break;
400
401 case DW_LNS_set_basic_block:
402 // Takes no arguments. Set the basic_block register of the
403 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000404 State.Row.BasicBlock = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000405 break;
406
407 case DW_LNS_const_add_pc:
408 // Takes no arguments. Add to the address register of the state
409 // machine the address increment value corresponding to special
410 // opcode 255. The motivation for DW_LNS_const_add_pc is this:
411 // when the statement program needs to advance the address by a
412 // small amount, it can use a single special opcode, which occupies
413 // a single byte. When it needs to advance the address by up to
414 // twice the range of the last special opcode, it can use
415 // DW_LNS_const_add_pc followed by a special opcode, for a total
416 // of two bytes. Only if it needs to advance the address by more
417 // than twice that range will it need to use both DW_LNS_advance_pc
418 // and a special opcode, requiring three or more bytes.
419 {
Alexey Samsonov110d5952014-04-30 00:09:19 +0000420 uint8_t adjust_opcode = 255 - Prologue.OpcodeBase;
421 uint64_t addr_offset =
422 (adjust_opcode / Prologue.LineRange) * Prologue.MinInstLength;
423 State.Row.Address += addr_offset;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000424 }
425 break;
426
427 case DW_LNS_fixed_advance_pc:
428 // Takes a single uhalf operand. Add to the address register of
429 // the state machine the value of the (unencoded) operand. This
430 // is the only extended opcode that takes an argument that is not
431 // a variable length number. The motivation for DW_LNS_fixed_advance_pc
432 // is this: existing assemblers cannot emit DW_LNS_advance_pc or
433 // special opcodes because they cannot encode LEB128 numbers or
434 // judge when the computation of a special opcode overflows and
435 // requires the use of DW_LNS_advance_pc. Such assemblers, however,
436 // can use DW_LNS_fixed_advance_pc instead, sacrificing compression.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000437 State.Row.Address += debug_line_data.getU16(offset_ptr);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000438 break;
439
440 case DW_LNS_set_prologue_end:
441 // Takes no arguments. Set the prologue_end register of the
442 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000443 State.Row.PrologueEnd = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000444 break;
445
446 case DW_LNS_set_epilogue_begin:
447 // Takes no arguments. Set the basic_block register of the
448 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000449 State.Row.EpilogueBegin = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000450 break;
451
452 case DW_LNS_set_isa:
453 // Takes a single unsigned LEB128 operand and stores it in the
454 // column register of the state machine.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000455 State.Row.Isa = debug_line_data.getULEB128(offset_ptr);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000456 break;
457
458 default:
459 // Handle any unknown standard opcodes here. We know the lengths
460 // of such opcodes because they are specified in the prologue
461 // as a multiple of LEB128 operands for each opcode.
462 {
Alexey Samsonov110d5952014-04-30 00:09:19 +0000463 assert(opcode - 1U < Prologue.StandardOpcodeLengths.size());
464 uint8_t opcode_length = Prologue.StandardOpcodeLengths[opcode - 1];
465 for (uint8_t i = 0; i < opcode_length; ++i)
Benjamin Kramer5acab502011-09-15 02:12:05 +0000466 debug_line_data.getULEB128(offset_ptr);
467 }
468 break;
469 }
470 } else {
471 // Special Opcodes
472
473 // A special opcode value is chosen based on the amount that needs
474 // to be added to the line and address registers. The maximum line
475 // increment for a special opcode is the value of the line_base
476 // field in the header, plus the value of the line_range field,
477 // minus 1 (line base + line range - 1). If the desired line
478 // increment is greater than the maximum line increment, a standard
NAKAMURA Takumif9959852011-10-08 11:22:47 +0000479 // opcode must be used instead of a special opcode. The "address
480 // advance" is calculated by dividing the desired address increment
Benjamin Kramer5acab502011-09-15 02:12:05 +0000481 // by the minimum_instruction_length field from the header. The
482 // special opcode is then calculated using the following formula:
483 //
484 // opcode = (desired line increment - line_base) +
485 // (line_range * address advance) + opcode_base
486 //
487 // If the resulting opcode is greater than 255, a standard opcode
488 // must be used instead.
489 //
490 // To decode a special opcode, subtract the opcode_base from the
491 // opcode itself to give the adjusted opcode. The amount to
492 // increment the address register is the result of the adjusted
493 // opcode divided by the line_range multiplied by the
494 // minimum_instruction_length field from the header. That is:
495 //
496 // address increment = (adjusted opcode / line_range) *
497 // minimum_instruction_length
498 //
499 // The amount to increment the line register is the line_base plus
500 // the result of the adjusted opcode modulo the line_range. That is:
501 //
502 // line increment = line_base + (adjusted opcode % line_range)
503
Alexey Samsonov110d5952014-04-30 00:09:19 +0000504 uint8_t adjust_opcode = opcode - Prologue.OpcodeBase;
505 uint64_t addr_offset =
506 (adjust_opcode / Prologue.LineRange) * Prologue.MinInstLength;
507 int32_t line_offset =
508 Prologue.LineBase + (adjust_opcode % Prologue.LineRange);
509 State.Row.Line += line_offset;
510 State.Row.Address += addr_offset;
511 State.appendRowToMatrix(*offset_ptr);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000512 }
513 }
514
Alexey Samsonov110d5952014-04-30 00:09:19 +0000515 if (!State.Sequence.Empty) {
516 fprintf(stderr, "warning: last sequence in debug line table is not"
517 "terminated!\n");
518 }
519
520 // Sort all sequences so that address lookup will work faster.
521 if (!Sequences.empty()) {
522 std::sort(Sequences.begin(), Sequences.end(), Sequence::orderByLowPC);
523 // Note: actually, instruction address ranges of sequences should not
524 // overlap (in shared objects and executables). If they do, the address
525 // lookup would still work, though, but result would be ambiguous.
526 // We don't report warning in this case. For example,
527 // sometimes .so compiled from multiple object files contains a few
528 // rudimentary sequences for address ranges [0x0, 0xsomething).
529 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000530
531 return end_offset;
532}
533
Keno Fischerc2c60182015-05-31 23:37:04 +0000534uint32_t
535DWARFDebugLine::LineTable::findRowInSeq(const DWARFDebugLine::Sequence &seq,
536 uint64_t address) const {
537 if (!seq.containsPC(address))
538 return UnknownRowIndex;
539 // Search for instruction address in the rows describing the sequence.
540 // Rows are stored in a vector, so we may use arithmetical operations with
541 // iterators.
542 DWARFDebugLine::Row row;
543 row.Address = address;
544 RowIter first_row = Rows.begin() + seq.FirstRowIndex;
545 RowIter last_row = Rows.begin() + seq.LastRowIndex;
546 LineTable::RowIter row_pos = std::lower_bound(
547 first_row, last_row, row, DWARFDebugLine::Row::orderByAddress);
548 if (row_pos == last_row) {
549 return seq.LastRowIndex - 1;
550 }
551 uint32_t index = seq.FirstRowIndex + (row_pos - first_row);
552 if (row_pos->Address > address) {
553 if (row_pos == first_row)
554 return UnknownRowIndex;
555 else
556 index--;
557 }
558 return index;
559}
560
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000561uint32_t DWARFDebugLine::LineTable::lookupAddress(uint64_t address) const {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000562 if (Sequences.empty())
Keno Fischerc2c60182015-05-31 23:37:04 +0000563 return UnknownRowIndex;
Alexey Samsonov947228c2012-08-07 11:46:57 +0000564 // First, find an instruction sequence containing the given address.
565 DWARFDebugLine::Sequence sequence;
566 sequence.LowPC = address;
567 SequenceIter first_seq = Sequences.begin();
568 SequenceIter last_seq = Sequences.end();
569 SequenceIter seq_pos = std::lower_bound(first_seq, last_seq, sequence,
570 DWARFDebugLine::Sequence::orderByLowPC);
571 DWARFDebugLine::Sequence found_seq;
572 if (seq_pos == last_seq) {
573 found_seq = Sequences.back();
574 } else if (seq_pos->LowPC == address) {
575 found_seq = *seq_pos;
576 } else {
577 if (seq_pos == first_seq)
Keno Fischerc2c60182015-05-31 23:37:04 +0000578 return UnknownRowIndex;
Alexey Samsonov947228c2012-08-07 11:46:57 +0000579 found_seq = *(seq_pos - 1);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000580 }
Keno Fischerc2c60182015-05-31 23:37:04 +0000581 return findRowInSeq(found_seq, address);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000582}
Alexey Samsonov45be7932012-08-30 07:49:50 +0000583
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000584bool DWARFDebugLine::LineTable::lookupAddressRange(
585 uint64_t address, uint64_t size, std::vector<uint32_t> &result) const {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000586 if (Sequences.empty())
587 return false;
588 uint64_t end_addr = address + size;
589 // First, find an instruction sequence containing the given address.
590 DWARFDebugLine::Sequence sequence;
591 sequence.LowPC = address;
592 SequenceIter first_seq = Sequences.begin();
593 SequenceIter last_seq = Sequences.end();
594 SequenceIter seq_pos = std::lower_bound(first_seq, last_seq, sequence,
595 DWARFDebugLine::Sequence::orderByLowPC);
596 if (seq_pos == last_seq || seq_pos->LowPC != address) {
597 if (seq_pos == first_seq)
598 return false;
599 seq_pos--;
600 }
601 if (!seq_pos->containsPC(address))
602 return false;
603
604 SequenceIter start_pos = seq_pos;
605
606 // Add the rows from the first sequence to the vector, starting with the
607 // index we just calculated
608
609 while (seq_pos != last_seq && seq_pos->LowPC < end_addr) {
Keno Fischerc2c60182015-05-31 23:37:04 +0000610 const DWARFDebugLine::Sequence &cur_seq = *seq_pos;
611 // For the first sequence, we need to find which row in the sequence is the
612 // first in our range.
613 uint32_t first_row_index = cur_seq.FirstRowIndex;
614 if (seq_pos == start_pos)
615 first_row_index = findRowInSeq(cur_seq, address);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000616
Keno Fischerc2c60182015-05-31 23:37:04 +0000617 // Figure out the last row in the range.
618 uint32_t last_row_index = findRowInSeq(cur_seq, end_addr - 1);
619 if (last_row_index == UnknownRowIndex)
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000620 last_row_index = cur_seq.LastRowIndex - 1;
621
Keno Fischerc2c60182015-05-31 23:37:04 +0000622 assert(first_row_index != UnknownRowIndex);
623 assert(last_row_index != UnknownRowIndex);
624
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000625 for (uint32_t i = first_row_index; i <= last_row_index; ++i) {
626 result.push_back(i);
627 }
628
629 ++seq_pos;
630 }
NAKAMURA Takumi4b86cdb2013-01-26 01:45:06 +0000631
632 return true;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000633}
634
635bool
Alexey Samsonov45be7932012-08-30 07:49:50 +0000636DWARFDebugLine::LineTable::getFileNameByIndex(uint64_t FileIndex,
Frederic Riss101b5e22014-09-19 15:11:51 +0000637 const char *CompDir,
Alexey Samsonovdce67342014-05-15 21:24:32 +0000638 FileLineInfoKind Kind,
Alexey Samsonov45be7932012-08-30 07:49:50 +0000639 std::string &Result) const {
Alexey Samsonovdce67342014-05-15 21:24:32 +0000640 if (FileIndex == 0 || FileIndex > Prologue.FileNames.size() ||
641 Kind == FileLineInfoKind::None)
Alexey Samsonov45be7932012-08-30 07:49:50 +0000642 return false;
643 const FileNameEntry &Entry = Prologue.FileNames[FileIndex - 1];
644 const char *FileName = Entry.Name;
Alexey Samsonovdce67342014-05-15 21:24:32 +0000645 if (Kind != FileLineInfoKind::AbsoluteFilePath ||
Alexey Samsonov45be7932012-08-30 07:49:50 +0000646 sys::path::is_absolute(FileName)) {
647 Result = FileName;
648 return true;
649 }
Frederic Riss101b5e22014-09-19 15:11:51 +0000650
Alexey Samsonov45be7932012-08-30 07:49:50 +0000651 SmallString<16> FilePath;
652 uint64_t IncludeDirIndex = Entry.DirIdx;
Frederic Riss101b5e22014-09-19 15:11:51 +0000653 const char *IncludeDir = "";
Alexey Samsonov45be7932012-08-30 07:49:50 +0000654 // Be defensive about the contents of Entry.
655 if (IncludeDirIndex > 0 &&
Frederic Riss101b5e22014-09-19 15:11:51 +0000656 IncludeDirIndex <= Prologue.IncludeDirectories.size())
657 IncludeDir = Prologue.IncludeDirectories[IncludeDirIndex - 1];
658
659 // We may still need to append compilation directory of compile unit.
660 // We know that FileName is not absolute, the only way to have an
661 // absolute path at this point would be if IncludeDir is absolute.
662 if (CompDir && Kind == FileLineInfoKind::AbsoluteFilePath &&
663 sys::path::is_relative(IncludeDir))
664 sys::path::append(FilePath, CompDir);
665
666 // sys::path::append skips empty strings.
667 sys::path::append(FilePath, IncludeDir, FileName);
Alexey Samsonov45be7932012-08-30 07:49:50 +0000668 Result = FilePath.str();
669 return true;
670}
Frederic Riss101b5e22014-09-19 15:11:51 +0000671
672bool
673DWARFDebugLine::LineTable::getFileLineInfoForAddress(uint64_t Address,
674 const char *CompDir,
675 FileLineInfoKind Kind,
676 DILineInfo &Result) const {
677 // Get the index of row we're looking for in the line table.
678 uint32_t RowIndex = lookupAddress(Address);
679 if (RowIndex == -1U)
680 return false;
681 // Take file number and line/column from the row.
682 const auto &Row = Rows[RowIndex];
683 if (!getFileNameByIndex(Row.File, CompDir, Kind, Result.FileName))
684 return false;
685 Result.Line = Row.Line;
686 Result.Column = Row.Column;
687 return true;
688}