LLVM  7.0.0svn
DWARFDebugLine.cpp
Go to the documentation of this file.
1 //===- 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 
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/ADT/SmallVector.h"
13 #include "llvm/ADT/StringRef.h"
17 #include "llvm/Support/Format.h"
18 #include "llvm/Support/Path.h"
20 #include <algorithm>
21 #include <cassert>
22 #include <cinttypes>
23 #include <cstdint>
24 #include <cstdio>
25 #include <utility>
26 
27 using namespace llvm;
28 using namespace dwarf;
29 
31 
32 namespace {
33 
34 struct ContentDescriptor {
37 };
38 
39 using ContentDescriptors = SmallVector<ContentDescriptor, 4>;
40 
41 } // end anonmyous namespace
42 
44 
46  TotalLength = PrologueLength = 0;
47  SegSelectorSize = 0;
48  MinInstLength = MaxOpsPerInst = DefaultIsStmt = LineBase = LineRange = 0;
49  OpcodeBase = 0;
50  FormParams = DWARFFormParams({0, 0, DWARF32});
51  HasMD5 = false;
52  StandardOpcodeLengths.clear();
53  IncludeDirectories.clear();
54  FileNames.clear();
55 }
56 
58  DIDumpOptions DumpOptions) const {
59  OS << "Line table prologue:\n"
60  << format(" total_length: 0x%8.8" PRIx64 "\n", TotalLength)
61  << format(" version: %u\n", getVersion());
62  if (getVersion() >= 5)
63  OS << format(" address_size: %u\n", getAddressSize())
64  << format(" seg_select_size: %u\n", SegSelectorSize);
65  OS << format(" prologue_length: 0x%8.8" PRIx64 "\n", PrologueLength)
66  << format(" min_inst_length: %u\n", MinInstLength)
67  << format(getVersion() >= 4 ? "max_ops_per_inst: %u\n" : "", MaxOpsPerInst)
68  << format(" default_is_stmt: %u\n", DefaultIsStmt)
69  << format(" line_base: %i\n", LineBase)
70  << format(" line_range: %u\n", LineRange)
71  << format(" opcode_base: %u\n", OpcodeBase);
72 
73  for (uint32_t I = 0; I != StandardOpcodeLengths.size(); ++I)
74  OS << format("standard_opcode_lengths[%s] = %u\n",
75  LNStandardString(I + 1).data(), StandardOpcodeLengths[I]);
76 
77  if (!IncludeDirectories.empty()) {
78  // DWARF v5 starts directory indexes at 0.
79  uint32_t DirBase = getVersion() >= 5 ? 0 : 1;
80  for (uint32_t I = 0; I != IncludeDirectories.size(); ++I) {
81  OS << format("include_directories[%3u] = ", I + DirBase);
82  IncludeDirectories[I].dump(OS, DumpOptions);
83  OS << '\n';
84  }
85  }
86 
87  if (!FileNames.empty()) {
88  if (HasMD5)
89  OS << " Dir MD5 Checksum File Name\n"
90  << " ---- -------------------------------- -----------"
91  "---------------\n";
92  else
93  OS << " Dir Mod Time File Len File Name\n"
94  << " ---- ---------- ---------- -----------"
95  "----------------\n";
96  // DWARF v5 starts file indexes at 0.
97  uint32_t FileBase = getVersion() >= 5 ? 0 : 1;
98  for (uint32_t I = 0; I != FileNames.size(); ++I) {
99  const FileNameEntry &FileEntry = FileNames[I];
100  OS << format("file_names[%3u] %4" PRIu64 " ", I + FileBase,
101  FileEntry.DirIdx);
102  if (HasMD5)
103  OS << FileEntry.Checksum.digest();
104  else
105  OS << format("0x%8.8" PRIx64 " 0x%8.8" PRIx64, FileEntry.ModTime,
106  FileEntry.Length);
107  OS << ' ';
108  FileEntry.Name.dump(OS, DumpOptions);
109  OS << '\n';
110  }
111  }
112 }
113 
114 // Parse v2-v4 directory and file tables.
115 static void
117  uint32_t *OffsetPtr, uint64_t EndPrologueOffset,
118  std::vector<DWARFFormValue> &IncludeDirectories,
119  std::vector<DWARFDebugLine::FileNameEntry> &FileNames) {
120  while (*OffsetPtr < EndPrologueOffset) {
121  StringRef S = DebugLineData.getCStrRef(OffsetPtr);
122  if (S.empty())
123  break;
124  DWARFFormValue Dir(dwarf::DW_FORM_string);
125  Dir.setPValue(S.data());
126  IncludeDirectories.push_back(Dir);
127  }
128 
129  while (*OffsetPtr < EndPrologueOffset) {
130  StringRef Name = DebugLineData.getCStrRef(OffsetPtr);
131  if (Name.empty())
132  break;
134  FileEntry.Name.setForm(dwarf::DW_FORM_string);
135  FileEntry.Name.setPValue(Name.data());
136  FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr);
137  FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr);
138  FileEntry.Length = DebugLineData.getULEB128(OffsetPtr);
139  FileNames.push_back(FileEntry);
140  }
141 }
142 
143 // Parse v5 directory/file entry content descriptions.
144 // Returns the descriptors, or an empty vector if we did not find a path or
145 // ran off the end of the prologue.
146 static ContentDescriptors
147 parseV5EntryFormat(const DWARFDataExtractor &DebugLineData, uint32_t *OffsetPtr,
148  uint64_t EndPrologueOffset, bool *HasMD5) {
149  ContentDescriptors Descriptors;
150  int FormatCount = DebugLineData.getU8(OffsetPtr);
151  bool HasPath = false;
152  for (int I = 0; I != FormatCount; ++I) {
153  if (*OffsetPtr >= EndPrologueOffset)
154  return ContentDescriptors();
155  ContentDescriptor Descriptor;
156  Descriptor.Type =
157  dwarf::LineNumberEntryFormat(DebugLineData.getULEB128(OffsetPtr));
158  Descriptor.Form = dwarf::Form(DebugLineData.getULEB128(OffsetPtr));
159  if (Descriptor.Type == dwarf::DW_LNCT_path)
160  HasPath = true;
161  else if (Descriptor.Type == dwarf::DW_LNCT_MD5 && HasMD5)
162  *HasMD5 = true;
163  Descriptors.push_back(Descriptor);
164  }
165  return HasPath ? Descriptors : ContentDescriptors();
166 }
167 
168 static bool
170  uint32_t *OffsetPtr, uint64_t EndPrologueOffset,
171  const DWARFFormParams &FormParams, const DWARFContext &Ctx,
172  const DWARFUnit *U, bool &HasMD5,
173  std::vector<DWARFFormValue> &IncludeDirectories,
174  std::vector<DWARFDebugLine::FileNameEntry> &FileNames) {
175  // Get the directory entry description.
176  ContentDescriptors DirDescriptors =
177  parseV5EntryFormat(DebugLineData, OffsetPtr, EndPrologueOffset, nullptr);
178  if (DirDescriptors.empty())
179  return false;
180 
181  // Get the directory entries, according to the format described above.
182  int DirEntryCount = DebugLineData.getU8(OffsetPtr);
183  for (int I = 0; I != DirEntryCount; ++I) {
184  if (*OffsetPtr >= EndPrologueOffset)
185  return false;
186  for (auto Descriptor : DirDescriptors) {
187  DWARFFormValue Value(Descriptor.Form);
188  switch (Descriptor.Type) {
189  case DW_LNCT_path:
190  if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, &Ctx, U))
191  return false;
192  IncludeDirectories.push_back(Value);
193  break;
194  default:
195  if (!Value.skipValue(DebugLineData, OffsetPtr, FormParams))
196  return false;
197  }
198  }
199  }
200 
201  // Get the file entry description.
202  ContentDescriptors FileDescriptors =
203  parseV5EntryFormat(DebugLineData, OffsetPtr, EndPrologueOffset, &HasMD5);
204  if (FileDescriptors.empty())
205  return false;
206 
207  // Get the file entries, according to the format described above.
208  int FileEntryCount = DebugLineData.getU8(OffsetPtr);
209  for (int I = 0; I != FileEntryCount; ++I) {
210  if (*OffsetPtr >= EndPrologueOffset)
211  return false;
213  for (auto Descriptor : FileDescriptors) {
214  DWARFFormValue Value(Descriptor.Form);
215  if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, &Ctx, U))
216  return false;
217  switch (Descriptor.Type) {
218  case DW_LNCT_path:
219  FileEntry.Name = Value;
220  break;
221  case DW_LNCT_directory_index:
222  FileEntry.DirIdx = Value.getAsUnsignedConstant().getValue();
223  break;
224  case DW_LNCT_timestamp:
225  FileEntry.ModTime = Value.getAsUnsignedConstant().getValue();
226  break;
227  case DW_LNCT_size:
228  FileEntry.Length = Value.getAsUnsignedConstant().getValue();
229  break;
230  case DW_LNCT_MD5:
231  assert(Value.getAsBlock().getValue().size() == 16);
232  std::uninitialized_copy_n(Value.getAsBlock().getValue().begin(), 16,
233  FileEntry.Checksum.Bytes.begin());
234  break;
235  default:
236  break;
237  }
238  }
239  FileNames.push_back(FileEntry);
240  }
241  return true;
242 }
243 
245  uint32_t *OffsetPtr,
246  const DWARFContext &Ctx,
247  const DWARFUnit *U) {
248  const uint64_t PrologueOffset = *OffsetPtr;
249 
250  clear();
251  TotalLength = DebugLineData.getU32(OffsetPtr);
252  if (TotalLength == UINT32_MAX) {
253  FormParams.Format = dwarf::DWARF64;
254  TotalLength = DebugLineData.getU64(OffsetPtr);
255  } else if (TotalLength >= 0xffffff00) {
256  return false;
257  }
258  FormParams.Version = DebugLineData.getU16(OffsetPtr);
259  if (getVersion() < 2)
260  return false;
261 
262  if (getVersion() >= 5) {
263  FormParams.AddrSize = DebugLineData.getU8(OffsetPtr);
264  assert((DebugLineData.getAddressSize() == 0 ||
265  DebugLineData.getAddressSize() == getAddressSize()) &&
266  "Line table header and data extractor disagree");
267  SegSelectorSize = DebugLineData.getU8(OffsetPtr);
268  }
269 
270  PrologueLength = DebugLineData.getUnsigned(OffsetPtr, sizeofPrologueLength());
271  const uint64_t EndPrologueOffset = PrologueLength + *OffsetPtr;
272  MinInstLength = DebugLineData.getU8(OffsetPtr);
273  if (getVersion() >= 4)
274  MaxOpsPerInst = DebugLineData.getU8(OffsetPtr);
275  DefaultIsStmt = DebugLineData.getU8(OffsetPtr);
276  LineBase = DebugLineData.getU8(OffsetPtr);
277  LineRange = DebugLineData.getU8(OffsetPtr);
278  OpcodeBase = DebugLineData.getU8(OffsetPtr);
279 
280  StandardOpcodeLengths.reserve(OpcodeBase - 1);
281  for (uint32_t I = 1; I < OpcodeBase; ++I) {
282  uint8_t OpLen = DebugLineData.getU8(OffsetPtr);
283  StandardOpcodeLengths.push_back(OpLen);
284  }
285 
286  if (getVersion() >= 5) {
287  if (!parseV5DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset,
288  FormParams, Ctx, U, HasMD5, IncludeDirectories,
289  FileNames)) {
290  fprintf(stderr,
291  "warning: parsing line table prologue at 0x%8.8" PRIx64
292  " found an invalid directory or file table description at"
293  " 0x%8.8" PRIx64 "\n", PrologueOffset, (uint64_t)*OffsetPtr);
294  return false;
295  }
296  } else
297  parseV2DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset,
298  IncludeDirectories, FileNames);
299 
300  if (*OffsetPtr != EndPrologueOffset) {
301  fprintf(stderr,
302  "warning: parsing line table prologue at 0x%8.8" PRIx64
303  " should have ended at 0x%8.8" PRIx64
304  " but it ended at 0x%8.8" PRIx64 "\n",
305  PrologueOffset, EndPrologueOffset, (uint64_t)*OffsetPtr);
306  return false;
307  }
308  return true;
309 }
310 
311 DWARFDebugLine::Row::Row(bool DefaultIsStmt) { reset(DefaultIsStmt); }
312 
314  BasicBlock = false;
315  PrologueEnd = false;
316  EpilogueBegin = false;
317 }
318 
319 void DWARFDebugLine::Row::reset(bool DefaultIsStmt) {
320  Address = 0;
321  Line = 1;
322  Column = 0;
323  File = 1;
324  Isa = 0;
325  Discriminator = 0;
326  IsStmt = DefaultIsStmt;
327  BasicBlock = false;
328  EndSequence = false;
329  PrologueEnd = false;
330  EpilogueBegin = false;
331 }
332 
334  OS << "Address Line Column File ISA Discriminator Flags\n"
335  << "------------------ ------ ------ ------ --- ------------- "
336  "-------------\n";
337 }
338 
340  OS << format("0x%16.16" PRIx64 " %6u %6u", Address, Line, Column)
341  << format(" %6u %3u %13u ", File, Isa, Discriminator)
342  << (IsStmt ? " is_stmt" : "") << (BasicBlock ? " basic_block" : "")
343  << (PrologueEnd ? " prologue_end" : "")
344  << (EpilogueBegin ? " epilogue_begin" : "")
345  << (EndSequence ? " end_sequence" : "") << '\n';
346 }
347 
349 
351  LowPC = 0;
352  HighPC = 0;
353  FirstRowIndex = 0;
354  LastRowIndex = 0;
355  Empty = true;
356 }
357 
359 
361  DIDumpOptions DumpOptions) const {
362  Prologue.dump(OS, DumpOptions);
363  OS << '\n';
364 
365  if (!Rows.empty()) {
366  Row::dumpTableHeader(OS);
367  for (const Row &R : Rows) {
368  R.dump(OS);
369  }
370  }
371 }
372 
374  Prologue.clear();
375  Rows.clear();
376  Sequences.clear();
377 }
378 
379 DWARFDebugLine::ParsingState::ParsingState(struct LineTable *LT)
380  : LineTable(LT) {
381  resetRowAndSequence();
382 }
383 
384 void DWARFDebugLine::ParsingState::resetRowAndSequence() {
386  Sequence.reset();
387 }
388 
389 void DWARFDebugLine::ParsingState::appendRowToMatrix(uint32_t Offset) {
390  if (Sequence.Empty) {
391  // Record the beginning of instruction sequence.
392  Sequence.Empty = false;
394  Sequence.FirstRowIndex = RowNumber;
395  }
396  ++RowNumber;
398  if (Row.EndSequence) {
399  // Record the end of instruction sequence.
401  Sequence.LastRowIndex = RowNumber;
402  if (Sequence.isValid())
404  Sequence.reset();
405  }
406  Row.postAppend();
407 }
408 
411  LineTableConstIter Pos = LineTableMap.find(Offset);
412  if (Pos != LineTableMap.end())
413  return &Pos->second;
414  return nullptr;
415 }
416 
419  uint32_t Offset, const DWARFContext &Ctx,
420  const DWARFUnit *U) {
421  std::pair<LineTableIter, bool> Pos =
422  LineTableMap.insert(LineTableMapTy::value_type(Offset, LineTable()));
423  LineTable *LT = &Pos.first->second;
424  if (Pos.second) {
425  if (!LT->parse(DebugLineData, &Offset, Ctx, U))
426  return nullptr;
427  }
428  return LT;
429 }
430 
432  uint32_t *OffsetPtr,
433  const DWARFContext &Ctx,
434  const DWARFUnit *U, raw_ostream *OS) {
435  const uint32_t DebugLineOffset = *OffsetPtr;
436 
437  clear();
438 
439  if (!Prologue.parse(DebugLineData, OffsetPtr, Ctx, U)) {
440  // Restore our offset and return false to indicate failure!
441  *OffsetPtr = DebugLineOffset;
442  return false;
443  }
444 
445  if (OS) {
446  // The presence of OS signals verbose dumping.
447  DIDumpOptions DumpOptions;
448  DumpOptions.Verbose = true;
449  Prologue.dump(*OS, DumpOptions);
450  }
451 
452  const uint32_t EndOffset =
453  DebugLineOffset + Prologue.TotalLength + Prologue.sizeofTotalLength();
454 
455  // See if we should tell the data extractor the address size.
456  if (DebugLineData.getAddressSize() == 0)
457  DebugLineData.setAddressSize(Prologue.getAddressSize());
458  else
459  assert(Prologue.getAddressSize() == 0 ||
460  Prologue.getAddressSize() == DebugLineData.getAddressSize());
461 
462  ParsingState State(this);
463 
464  while (*OffsetPtr < EndOffset) {
465  if (OS)
466  *OS << format("0x%08.08" PRIx32 ": ", *OffsetPtr);
467 
468  uint8_t Opcode = DebugLineData.getU8(OffsetPtr);
469 
470  if (OS)
471  *OS << format("%02.02" PRIx8 " ", Opcode);
472 
473  if (Opcode == 0) {
474  // Extended Opcodes always start with a zero opcode followed by
475  // a uleb128 length so you can skip ones you don't know about
476  uint64_t Len = DebugLineData.getULEB128(OffsetPtr);
477  uint32_t ExtOffset = *OffsetPtr;
478 
479  // Tolerate zero-length; assume length is correct and soldier on.
480  if (Len == 0) {
481  if (OS)
482  *OS << "Badly formed extended line op (length 0)\n";
483  continue;
484  }
485 
486  uint8_t SubOpcode = DebugLineData.getU8(OffsetPtr);
487  if (OS)
488  *OS << LNExtendedString(SubOpcode);
489  switch (SubOpcode) {
490  case DW_LNE_end_sequence:
491  // Set the end_sequence register of the state machine to true and
492  // append a row to the matrix using the current values of the
493  // state-machine registers. Then reset the registers to the initial
494  // values specified above. Every statement program sequence must end
495  // with a DW_LNE_end_sequence instruction which creates a row whose
496  // address is that of the byte after the last target machine instruction
497  // of the sequence.
498  State.Row.EndSequence = true;
499  State.appendRowToMatrix(*OffsetPtr);
500  if (OS) {
501  *OS << "\n";
502  OS->indent(12);
503  State.Row.dump(*OS);
504  }
505  State.resetRowAndSequence();
506  break;
507 
508  case DW_LNE_set_address:
509  // Takes a single relocatable address as an operand. The size of the
510  // operand is the size appropriate to hold an address on the target
511  // machine. Set the address register to the value given by the
512  // relocatable address. All of the other statement program opcodes
513  // that affect the address register add a delta to it. This instruction
514  // stores a relocatable value into it instead.
515  //
516  // Make sure the extractor knows the address size. If not, infer it
517  // from the size of the operand.
518  if (DebugLineData.getAddressSize() == 0)
519  DebugLineData.setAddressSize(Len - 1);
520  else
521  assert(DebugLineData.getAddressSize() == Len - 1);
522  State.Row.Address = DebugLineData.getRelocatedAddress(OffsetPtr);
523  if (OS)
524  *OS << format(" (0x%16.16" PRIx64 ")", State.Row.Address);
525  break;
526 
527  case DW_LNE_define_file:
528  // Takes 4 arguments. The first is a null terminated string containing
529  // a source file name. The second is an unsigned LEB128 number
530  // representing the directory index of the directory in which the file
531  // was found. The third is an unsigned LEB128 number representing the
532  // time of last modification of the file. The fourth is an unsigned
533  // LEB128 number representing the length in bytes of the file. The time
534  // and length fields may contain LEB128(0) if the information is not
535  // available.
536  //
537  // The directory index represents an entry in the include_directories
538  // section of the statement program prologue. The index is LEB128(0)
539  // if the file was found in the current directory of the compilation,
540  // LEB128(1) if it was found in the first directory in the
541  // include_directories section, and so on. The directory index is
542  // ignored for file names that represent full path names.
543  //
544  // The files are numbered, starting at 1, in the order in which they
545  // appear; the names in the prologue come before names defined by
546  // the DW_LNE_define_file instruction. These numbers are used in the
547  // the file register of the state machine.
548  {
549  FileNameEntry FileEntry;
550  const char *Name = DebugLineData.getCStr(OffsetPtr);
551  FileEntry.Name.setForm(dwarf::DW_FORM_string);
552  FileEntry.Name.setPValue(Name);
553  FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr);
554  FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr);
555  FileEntry.Length = DebugLineData.getULEB128(OffsetPtr);
556  Prologue.FileNames.push_back(FileEntry);
557  if (OS)
558  *OS << " (" << Name << ", dir=" << FileEntry.DirIdx << ", mod_time="
559  << format("(0x%16.16" PRIx64 ")", FileEntry.ModTime)
560  << ", length=" << FileEntry.Length << ")";
561  }
562  break;
563 
564  case DW_LNE_set_discriminator:
565  State.Row.Discriminator = DebugLineData.getULEB128(OffsetPtr);
566  if (OS)
567  *OS << " (" << State.Row.Discriminator << ")";
568  break;
569 
570  default:
571  if (OS)
572  *OS << format("Unrecognized extended op 0x%02.02" PRIx8, SubOpcode)
573  << format(" length %" PRIx64, Len);
574  // Len doesn't include the zero opcode byte or the length itself, but
575  // it does include the sub_opcode, so we have to adjust for that.
576  (*OffsetPtr) += Len - 1;
577  break;
578  }
579  // Make sure the stated and parsed lengths are the same.
580  // Otherwise we have an unparseable line-number program.
581  if (*OffsetPtr - ExtOffset != Len) {
582  fprintf(stderr, "Unexpected line op length at offset 0x%8.8" PRIx32
583  " expected 0x%2.2" PRIx64 " found 0x%2.2" PRIx32 "\n",
584  ExtOffset, Len, *OffsetPtr - ExtOffset);
585  // Skip the rest of the line-number program.
586  *OffsetPtr = EndOffset;
587  return false;
588  }
589  } else if (Opcode < Prologue.OpcodeBase) {
590  if (OS)
591  *OS << LNStandardString(Opcode);
592  switch (Opcode) {
593  // Standard Opcodes
594  case DW_LNS_copy:
595  // Takes no arguments. Append a row to the matrix using the
596  // current values of the state-machine registers. Then set
597  // the basic_block register to false.
598  State.appendRowToMatrix(*OffsetPtr);
599  if (OS) {
600  *OS << "\n";
601  OS->indent(12);
602  State.Row.dump(*OS);
603  *OS << "\n";
604  }
605  break;
606 
607  case DW_LNS_advance_pc:
608  // Takes a single unsigned LEB128 operand, multiplies it by the
609  // min_inst_length field of the prologue, and adds the
610  // result to the address register of the state machine.
611  {
612  uint64_t AddrOffset =
613  DebugLineData.getULEB128(OffsetPtr) * Prologue.MinInstLength;
614  State.Row.Address += AddrOffset;
615  if (OS)
616  *OS << " (" << AddrOffset << ")";
617  }
618  break;
619 
620  case DW_LNS_advance_line:
621  // Takes a single signed LEB128 operand and adds that value to
622  // the line register of the state machine.
623  State.Row.Line += DebugLineData.getSLEB128(OffsetPtr);
624  if (OS)
625  *OS << " (" << State.Row.Line << ")";
626  break;
627 
628  case DW_LNS_set_file:
629  // Takes a single unsigned LEB128 operand and stores it in the file
630  // register of the state machine.
631  State.Row.File = DebugLineData.getULEB128(OffsetPtr);
632  if (OS)
633  *OS << " (" << State.Row.File << ")";
634  break;
635 
636  case DW_LNS_set_column:
637  // Takes a single unsigned LEB128 operand and stores it in the
638  // column register of the state machine.
639  State.Row.Column = DebugLineData.getULEB128(OffsetPtr);
640  if (OS)
641  *OS << " (" << State.Row.Column << ")";
642  break;
643 
644  case DW_LNS_negate_stmt:
645  // Takes no arguments. Set the is_stmt register of the state
646  // machine to the logical negation of its current value.
647  State.Row.IsStmt = !State.Row.IsStmt;
648  break;
649 
650  case DW_LNS_set_basic_block:
651  // Takes no arguments. Set the basic_block register of the
652  // state machine to true
653  State.Row.BasicBlock = true;
654  break;
655 
656  case DW_LNS_const_add_pc:
657  // Takes no arguments. Add to the address register of the state
658  // machine the address increment value corresponding to special
659  // opcode 255. The motivation for DW_LNS_const_add_pc is this:
660  // when the statement program needs to advance the address by a
661  // small amount, it can use a single special opcode, which occupies
662  // a single byte. When it needs to advance the address by up to
663  // twice the range of the last special opcode, it can use
664  // DW_LNS_const_add_pc followed by a special opcode, for a total
665  // of two bytes. Only if it needs to advance the address by more
666  // than twice that range will it need to use both DW_LNS_advance_pc
667  // and a special opcode, requiring three or more bytes.
668  {
669  uint8_t AdjustOpcode = 255 - Prologue.OpcodeBase;
670  uint64_t AddrOffset =
671  (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength;
672  State.Row.Address += AddrOffset;
673  if (OS)
674  *OS
675  << format(" (0x%16.16" PRIx64 ")", AddrOffset);
676  }
677  break;
678 
679  case DW_LNS_fixed_advance_pc:
680  // Takes a single uhalf operand. Add to the address register of
681  // the state machine the value of the (unencoded) operand. This
682  // is the only extended opcode that takes an argument that is not
683  // a variable length number. The motivation for DW_LNS_fixed_advance_pc
684  // is this: existing assemblers cannot emit DW_LNS_advance_pc or
685  // special opcodes because they cannot encode LEB128 numbers or
686  // judge when the computation of a special opcode overflows and
687  // requires the use of DW_LNS_advance_pc. Such assemblers, however,
688  // can use DW_LNS_fixed_advance_pc instead, sacrificing compression.
689  {
690  uint16_t PCOffset = DebugLineData.getU16(OffsetPtr);
691  State.Row.Address += PCOffset;
692  if (OS)
693  *OS
694  << format(" (0x%16.16" PRIx64 ")", PCOffset);
695  }
696  break;
697 
698  case DW_LNS_set_prologue_end:
699  // Takes no arguments. Set the prologue_end register of the
700  // state machine to true
701  State.Row.PrologueEnd = true;
702  break;
703 
704  case DW_LNS_set_epilogue_begin:
705  // Takes no arguments. Set the basic_block register of the
706  // state machine to true
707  State.Row.EpilogueBegin = true;
708  break;
709 
710  case DW_LNS_set_isa:
711  // Takes a single unsigned LEB128 operand and stores it in the
712  // column register of the state machine.
713  State.Row.Isa = DebugLineData.getULEB128(OffsetPtr);
714  if (OS)
715  *OS << " (" << State.Row.Isa << ")";
716  break;
717 
718  default:
719  // Handle any unknown standard opcodes here. We know the lengths
720  // of such opcodes because they are specified in the prologue
721  // as a multiple of LEB128 operands for each opcode.
722  {
723  assert(Opcode - 1U < Prologue.StandardOpcodeLengths.size());
724  uint8_t OpcodeLength = Prologue.StandardOpcodeLengths[Opcode - 1];
725  for (uint8_t I = 0; I < OpcodeLength; ++I) {
726  uint64_t Value = DebugLineData.getULEB128(OffsetPtr);
727  if (OS)
728  *OS << format("Skipping ULEB128 value: 0x%16.16" PRIx64 ")\n",
729  Value);
730  }
731  }
732  break;
733  }
734  } else {
735  // Special Opcodes
736 
737  // A special opcode value is chosen based on the amount that needs
738  // to be added to the line and address registers. The maximum line
739  // increment for a special opcode is the value of the line_base
740  // field in the header, plus the value of the line_range field,
741  // minus 1 (line base + line range - 1). If the desired line
742  // increment is greater than the maximum line increment, a standard
743  // opcode must be used instead of a special opcode. The "address
744  // advance" is calculated by dividing the desired address increment
745  // by the minimum_instruction_length field from the header. The
746  // special opcode is then calculated using the following formula:
747  //
748  // opcode = (desired line increment - line_base) +
749  // (line_range * address advance) + opcode_base
750  //
751  // If the resulting opcode is greater than 255, a standard opcode
752  // must be used instead.
753  //
754  // To decode a special opcode, subtract the opcode_base from the
755  // opcode itself to give the adjusted opcode. The amount to
756  // increment the address register is the result of the adjusted
757  // opcode divided by the line_range multiplied by the
758  // minimum_instruction_length field from the header. That is:
759  //
760  // address increment = (adjusted opcode / line_range) *
761  // minimum_instruction_length
762  //
763  // The amount to increment the line register is the line_base plus
764  // the result of the adjusted opcode modulo the line_range. That is:
765  //
766  // line increment = line_base + (adjusted opcode % line_range)
767 
768  uint8_t AdjustOpcode = Opcode - Prologue.OpcodeBase;
769  uint64_t AddrOffset =
770  (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength;
771  int32_t LineOffset =
772  Prologue.LineBase + (AdjustOpcode % Prologue.LineRange);
773  State.Row.Line += LineOffset;
774  State.Row.Address += AddrOffset;
775 
776  if (OS) {
777  *OS << "address += " << ((uint32_t)AdjustOpcode)
778  << ", line += " << LineOffset << "\n";
779  OS->indent(12);
780  State.Row.dump(*OS);
781  }
782 
783  State.appendRowToMatrix(*OffsetPtr);
784  // Reset discriminator to 0.
785  State.Row.Discriminator = 0;
786  }
787  if(OS)
788  *OS << "\n";
789  }
790 
791  if (!State.Sequence.Empty) {
792  fprintf(stderr, "warning: last sequence in debug line table is not"
793  "terminated!\n");
794  }
795 
796  // Sort all sequences so that address lookup will work faster.
797  if (!Sequences.empty()) {
798  std::sort(Sequences.begin(), Sequences.end(), Sequence::orderByLowPC);
799  // Note: actually, instruction address ranges of sequences should not
800  // overlap (in shared objects and executables). If they do, the address
801  // lookup would still work, though, but result would be ambiguous.
802  // We don't report warning in this case. For example,
803  // sometimes .so compiled from multiple object files contains a few
804  // rudimentary sequences for address ranges [0x0, 0xsomething).
805  }
806 
807  return EndOffset;
808 }
809 
810 uint32_t
811 DWARFDebugLine::LineTable::findRowInSeq(const DWARFDebugLine::Sequence &Seq,
812  uint64_t Address) const {
813  if (!Seq.containsPC(Address))
814  return UnknownRowIndex;
815  // Search for instruction address in the rows describing the sequence.
816  // Rows are stored in a vector, so we may use arithmetical operations with
817  // iterators.
819  Row.Address = Address;
820  RowIter FirstRow = Rows.begin() + Seq.FirstRowIndex;
821  RowIter LastRow = Rows.begin() + Seq.LastRowIndex;
823  FirstRow, LastRow, Row, DWARFDebugLine::Row::orderByAddress);
824  if (RowPos == LastRow) {
825  return Seq.LastRowIndex - 1;
826  }
827  uint32_t Index = Seq.FirstRowIndex + (RowPos - FirstRow);
828  if (RowPos->Address > Address) {
829  if (RowPos == FirstRow)
830  return UnknownRowIndex;
831  else
832  Index--;
833  }
834  return Index;
835 }
836 
838  if (Sequences.empty())
839  return UnknownRowIndex;
840  // First, find an instruction sequence containing the given address.
842  Sequence.LowPC = Address;
843  SequenceIter FirstSeq = Sequences.begin();
844  SequenceIter LastSeq = Sequences.end();
846  FirstSeq, LastSeq, Sequence, DWARFDebugLine::Sequence::orderByLowPC);
847  DWARFDebugLine::Sequence FoundSeq;
848  if (SeqPos == LastSeq) {
849  FoundSeq = Sequences.back();
850  } else if (SeqPos->LowPC == Address) {
851  FoundSeq = *SeqPos;
852  } else {
853  if (SeqPos == FirstSeq)
854  return UnknownRowIndex;
855  FoundSeq = *(SeqPos - 1);
856  }
857  return findRowInSeq(FoundSeq, Address);
858 }
859 
861  uint64_t Address, uint64_t Size, std::vector<uint32_t> &Result) const {
862  if (Sequences.empty())
863  return false;
864  uint64_t EndAddr = Address + Size;
865  // First, find an instruction sequence containing the given address.
867  Sequence.LowPC = Address;
868  SequenceIter FirstSeq = Sequences.begin();
869  SequenceIter LastSeq = Sequences.end();
871  FirstSeq, LastSeq, Sequence, DWARFDebugLine::Sequence::orderByLowPC);
872  if (SeqPos == LastSeq || SeqPos->LowPC != Address) {
873  if (SeqPos == FirstSeq)
874  return false;
875  SeqPos--;
876  }
877  if (!SeqPos->containsPC(Address))
878  return false;
879 
880  SequenceIter StartPos = SeqPos;
881 
882  // Add the rows from the first sequence to the vector, starting with the
883  // index we just calculated
884 
885  while (SeqPos != LastSeq && SeqPos->LowPC < EndAddr) {
886  const DWARFDebugLine::Sequence &CurSeq = *SeqPos;
887  // For the first sequence, we need to find which row in the sequence is the
888  // first in our range.
889  uint32_t FirstRowIndex = CurSeq.FirstRowIndex;
890  if (SeqPos == StartPos)
891  FirstRowIndex = findRowInSeq(CurSeq, Address);
892 
893  // Figure out the last row in the range.
894  uint32_t LastRowIndex = findRowInSeq(CurSeq, EndAddr - 1);
895  if (LastRowIndex == UnknownRowIndex)
896  LastRowIndex = CurSeq.LastRowIndex - 1;
897 
898  assert(FirstRowIndex != UnknownRowIndex);
899  assert(LastRowIndex != UnknownRowIndex);
900 
901  for (uint32_t I = FirstRowIndex; I <= LastRowIndex; ++I) {
902  Result.push_back(I);
903  }
904 
905  ++SeqPos;
906  }
907 
908  return true;
909 }
910 
911 bool DWARFDebugLine::LineTable::hasFileAtIndex(uint64_t FileIndex) const {
912  return FileIndex != 0 && FileIndex <= Prologue.FileNames.size();
913 }
914 
916  const char *CompDir,
918  std::string &Result) const {
919  if (Kind == FileLineInfoKind::None || !hasFileAtIndex(FileIndex))
920  return false;
921  const FileNameEntry &Entry = Prologue.FileNames[FileIndex - 1];
922  StringRef FileName = Entry.Name.getAsCString().getValue();
923  if (Kind != FileLineInfoKind::AbsoluteFilePath ||
924  sys::path::is_absolute(FileName)) {
925  Result = FileName;
926  return true;
927  }
928 
929  SmallString<16> FilePath;
930  uint64_t IncludeDirIndex = Entry.DirIdx;
931  StringRef IncludeDir;
932  // Be defensive about the contents of Entry.
933  if (IncludeDirIndex > 0 &&
934  IncludeDirIndex <= Prologue.IncludeDirectories.size())
935  IncludeDir = Prologue.IncludeDirectories[IncludeDirIndex - 1]
936  .getAsCString()
937  .getValue();
938 
939  // We may still need to append compilation directory of compile unit.
940  // We know that FileName is not absolute, the only way to have an
941  // absolute path at this point would be if IncludeDir is absolute.
942  if (CompDir && Kind == FileLineInfoKind::AbsoluteFilePath &&
943  sys::path::is_relative(IncludeDir))
944  sys::path::append(FilePath, CompDir);
945 
946  // sys::path::append skips empty strings.
947  sys::path::append(FilePath, IncludeDir, FileName);
948  Result = FilePath.str();
949  return true;
950 }
951 
953  uint64_t Address, const char *CompDir, FileLineInfoKind Kind,
954  DILineInfo &Result) const {
955  // Get the index of row we're looking for in the line table.
956  uint32_t RowIndex = lookupAddress(Address);
957  if (RowIndex == -1U)
958  return false;
959  // Take file number and line/column from the row.
960  const auto &Row = Rows[RowIndex];
961  if (!getFileNameByIndex(Row.File, CompDir, Kind, Result.FileName))
962  return false;
963  Result.Line = Row.Line;
964  Result.Column = Row.Column;
966  return true;
967 }
SequenceVector::const_iterator SequenceIter
void dump(raw_ostream &OS, DIDumpOptions DumpOpts=DIDumpOptions()) const
uint32_t Discriminator
Definition: DIContext.h:39
uint64_t LowPC
Sequence describes instructions at address range [LowPC, HighPC) and is described by line table rows ...
std::string FileName
Definition: DIContext.h:32
uint64_t getULEB128(uint32_t *offset_ptr) const
Extract a unsigned LEB128 value from *offset_ptr.
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
Row(bool DefaultIsStmt=false)
static void parseV2DirFileTables(const DWARFDataExtractor &DebugLineData, uint32_t *OffsetPtr, uint64_t EndPrologueOffset, std::vector< DWARFFormValue > &IncludeDirectories, std::vector< DWARFDebugLine::FileNameEntry > &FileNames)
uint32_t sizeofTotalLength() const
SmallString< 32 > digest() const
Definition: MD5.cpp:264
StringRef LNExtendedString(unsigned Encoding)
Definition: Dwarf.cpp:407
static bool orderByAddress(const Row &LHS, const Row &RHS)
StringRef LNStandardString(unsigned Standard)
Definition: Dwarf.cpp:396
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
raw_ostream & indent(unsigned NumSpaces)
indent - Insert &#39;NumSpaces&#39; spaces.
uint8_t MinInstLength
The size in bytes of the smallest target machine instruction.
uint32_t Line
An unsigned integer indicating a source line number.
uint64_t getRelocatedAddress(uint32_t *Off, uint64_t *SecIx=nullptr) const
Extracts an address-sized value and applies a relocation to the result if one exists for the given of...
void dump(raw_ostream &OS, DIDumpOptions DumpOptions) const
bool parse(DWARFDataExtractor &DebugLineData, uint32_t *OffsetPtr, const DWARFContext &Ctx, const DWARFUnit *U, raw_ostream *OS=nullptr)
Parse prologue and all rows.
uint16_t getU16(uint32_t *offset_ptr) const
Extract a uint16_t value from *offset_ptr.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:128
std::vector< DWARFFormValue > IncludeDirectories
A format-neutral container for source line information.
Definition: DIContext.h:31
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition: Path.cpp:466
uint32_t getU32(uint32_t *offset_ptr) const
Extract a uint32_t value from *offset_ptr.
static ContentDescriptors parseV5EntryFormat(const DWARFDataExtractor &DebugLineData, uint32_t *OffsetPtr, uint64_t EndPrologueOffset, bool *HasMD5)
Represents a series of contiguous machine instructions.
Optional< ArrayRef< uint8_t > > getAsBlock() const
uint64_t Address
The program-counter value corresponding to a machine instruction generated by the compiler...
bool hasFileAtIndex(uint64_t FileIndex) const
void postAppend()
Called after a row is appended to the matrix.
bool is_absolute(const Twine &path, Style style=Style::native)
Is path absolute?
Definition: Path.cpp:674
uint32_t lookupAddress(uint64_t Address) const
Returns the index of the row with file/line info for a given address, or UnknownRowIndex if there is ...
void setForm(dwarf::Form F)
uint8_t LineRange
This parameter affects the meaning of the special opcodes. See below.
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:267
uint8_t OpcodeBase
The number assigned to the first special opcode.
void reset(bool DefaultIsStmt)
const T & getValue() const LLVM_LVALUE_FUNCTION
Definition: Optional.h:179
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
bool skipValue(DataExtractor DebugInfoData, uint32_t *OffsetPtr, const DWARFFormParams Params) const
Skip a form&#39;s value in DebugInfoData at the offset specified by OffsetPtr.
uint64_t TotalLength
The size in bytes of the statement information for this compilation unit (not including the total_len...
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
auto lower_bound(R &&Range, ForwardIt I) -> decltype(adl_begin(Range))
Provide wrappers to std::lower_bound which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:910
int64_t getSLEB128(uint32_t *offset_ptr) const
Extract a signed LEB128 value from *offset_ptr.
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:153
void setPValue(const char *V)
uint8_t EndSequence
A boolean indicating that the current address is that of the first byte after the end of a sequence o...
bool parse(const DWARFDataExtractor &DebugLineData, uint32_t *OffsetPtr, const DWARFContext &Ctx, const DWARFUnit *U=nullptr)
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
uint32_t Column
Definition: DIContext.h:35
void dump(raw_ostream &OS) const
bool extractValue(const DWARFDataExtractor &Data, uint32_t *OffsetPtr, DWARFFormParams FormParams, const DWARFContext *Context=nullptr, const DWARFUnit *Unit=nullptr)
Extracts a value in Data at offset *OffsetPtr.
LLVM Basic Block Representation.
Definition: BasicBlock.h:59
uint16_t File
An unsigned integer indicating the identity of the source file corresponding to a machine instruction...
uint8_t getAddressSize() const
Get the address size for this extractor.
Definition: DataExtractor.h:59
uint8_t getU8(uint32_t *offset_ptr) const
Extract a uint8_t value from *offset_ptr.
Optional< uint64_t > getAsUnsignedConstant() const
uint64_t getU64(uint32_t *offset_ptr) const
Extract a uint64_t value from *offset_ptr.
void appendSequence(const DWARFDebugLine::Sequence &S)
void dump(raw_ostream &OS, DIDumpOptions DumpOptions) const
Standard .debug_line state machine structure.
A DataExtractor (typically for an in-memory copy of an object-file section) plus a relocation map for...
bool lookupAddressRange(uint64_t Address, uint64_t Size, std::vector< uint32_t > &Result) const
bool containsPC(uint64_t PC) const
uint16_t Column
An unsigned integer indicating a column number within a source line.
bool getFileLineInfoForAddress(uint64_t Address, const char *CompDir, DILineInfoSpecifier::FileLineInfoKind Kind, DILineInfo &Result) const
Fills the Result argument with the file and line information corresponding to Address.
uint32_t Line
Definition: DIContext.h:34
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:862
void appendRow(const DWARFDebugLine::Row &R)
DWARFContext This data structure is the top level entity that deals with dwarf debug information pars...
Definition: DWARFContext.h:59
A helper struct for DWARFFormValue methods, providing information that allows it to know the byte siz...
static void dumpTableHeader(raw_ostream &OS)
void setAddressSize(uint8_t Size)
Set the address size for this extractor.
Definition: DataExtractor.h:61
std::vector< uint8_t > StandardOpcodeLengths
This file contains constants used for implementing Dwarf debug support.
static void clear(coro::Shape &Shape)
Definition: Coroutines.cpp:210
static bool parseV5DirFileTables(const DWARFDataExtractor &DebugLineData, uint32_t *OffsetPtr, uint64_t EndPrologueOffset, const DWARFFormParams &FormParams, const DWARFContext &Ctx, const DWARFUnit *U, bool &HasMD5, std::vector< DWARFFormValue > &IncludeDirectories, std::vector< DWARFDebugLine::FileNameEntry > &FileNames)
std::vector< FileNameEntry > FileNames
uint32_t Discriminator
An unsigned integer representing the DWARF path discriminator value for this location.
RowVector::const_iterator RowIter
uint64_t getUnsigned(uint32_t *offset_ptr, uint32_t byte_size) const
Extract an unsigned integer of size byte_size from *offset_ptr.
#define I(x, y, z)
Definition: MD5.cpp:58
static bool orderByLowPC(const Sequence &LHS, const Sequence &RHS)
Optional< const char * > getAsCString() const
bool is_relative(const Twine &path, Style style=Style::native)
Is path relative?
Definition: Path.cpp:685
Sequence
A sequence of states that a pointer may go through in which an objc_retain and objc_release are actua...
Definition: PtrState.h:41
const char * getCStr(uint32_t *offset_ptr) const
Extract a C string from *offset_ptr.
const LineTable * getOrParseLineTable(DWARFDataExtractor &DebugLineData, uint32_t Offset, const DWARFContext &C, const DWARFUnit *U)
uint8_t DefaultIsStmt
The initial value of theis_stmtregister.
const unsigned Kind
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
std::array< uint8_t, 16 > Bytes
Definition: MD5.h:56
int8_t LineBase
This parameter affects the meaning of the special opcodes. See below.
bool getFileNameByIndex(uint64_t FileIndex, const char *CompDir, DILineInfoSpecifier::FileLineInfoKind Kind, std::string &Result) const
Extracts filename by its index in filename table in prologue.
const LineTable * getLineTable(uint32_t Offset) const
LLVM Value Representation.
Definition: Value.h:73
StringRef getCStrRef(uint32_t *OffsetPtr) const
Extract a C string from *OffsetPtr.
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
void sort(Policy policy, RandomAccessIterator Start, RandomAccessIterator End, const Comparator &Comp=Comparator())
Definition: Parallel.h:199
LineNumberEntryFormat
Definition: Dwarf.h:236