LLVM  12.0.0git
DWARFDebugLoc.cpp
Go to the documentation of this file.
1 //===- DWARFDebugLoc.cpp --------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Support/Compiler.h"
17 #include "llvm/Support/Format.h"
18 #include "llvm/Support/WithColor.h"
20 #include <algorithm>
21 #include <cinttypes>
22 #include <cstdint>
23 
24 using namespace llvm;
26 
27 namespace {
28 class DWARFLocationInterpreter {
30  std::function<Optional<object::SectionedAddress>(uint32_t)> LookupAddr;
31 
32 public:
33  DWARFLocationInterpreter(
36  : Base(Base), LookupAddr(std::move(LookupAddr)) {}
37 
39  Interpret(const DWARFLocationEntry &E);
40 };
41 } // namespace
42 
45  "Unable to resolve indirect address %u for: %s",
46  Index, dwarf::LocListEncodingString(Kind).data());
47 }
48 
50 DWARFLocationInterpreter::Interpret(const DWARFLocationEntry &E) {
51  switch (E.Kind) {
52  case dwarf::DW_LLE_end_of_list:
53  return None;
54  case dwarf::DW_LLE_base_addressx: {
55  Base = LookupAddr(E.Value0);
56  if (!Base)
57  return createResolverError(E.Value0, E.Kind);
58  return None;
59  }
60  case dwarf::DW_LLE_startx_endx: {
61  Optional<SectionedAddress> LowPC = LookupAddr(E.Value0);
62  if (!LowPC)
63  return createResolverError(E.Value0, E.Kind);
64  Optional<SectionedAddress> HighPC = LookupAddr(E.Value1);
65  if (!HighPC)
66  return createResolverError(E.Value1, E.Kind);
68  DWARFAddressRange{LowPC->Address, HighPC->Address, LowPC->SectionIndex},
69  E.Loc};
70  }
71  case dwarf::DW_LLE_startx_length: {
72  Optional<SectionedAddress> LowPC = LookupAddr(E.Value0);
73  if (!LowPC)
74  return createResolverError(E.Value0, E.Kind);
76  LowPC->Address + E.Value1,
77  LowPC->SectionIndex},
78  E.Loc};
79  }
80  case dwarf::DW_LLE_offset_pair: {
81  if (!Base) {
83  "Unable to resolve location list offset pair: "
84  "Base address not defined");
85  }
86  DWARFAddressRange Range{Base->Address + E.Value0, Base->Address + E.Value1,
87  Base->SectionIndex};
88  if (Range.SectionIndex == SectionedAddress::UndefSection)
89  Range.SectionIndex = E.SectionIndex;
90  return DWARFLocationExpression{Range, E.Loc};
91  }
92  case dwarf::DW_LLE_default_location:
93  return DWARFLocationExpression{None, E.Loc};
94  case dwarf::DW_LLE_base_address:
96  return None;
97  case dwarf::DW_LLE_start_end:
100  case dwarf::DW_LLE_start_length:
103  E.Loc};
104  default:
105  llvm_unreachable("unreachable locations list kind");
106  }
107 }
108 
110  bool IsLittleEndian, unsigned AddressSize,
111  const MCRegisterInfo *MRI, DWARFUnit *U) {
112  DWARFDataExtractor Extractor(Data, IsLittleEndian, AddressSize);
113  // Note. We do not pass any format to DWARFExpression, even if the
114  // corresponding unit is known. For now, there is only one operation,
115  // DW_OP_call_ref, which depends on the format; it is rarely used, and
116  // is unexpected in location tables.
117  DWARFExpression(Extractor, AddressSize).print(OS, MRI, U);
118 }
119 
122  const MCRegisterInfo *MRI,
123  const DWARFObject &Obj, DWARFUnit *U,
124  DIDumpOptions DumpOpts,
125  unsigned Indent) const {
126  DWARFLocationInterpreter Interp(
127  BaseAddr, [U](uint32_t Index) -> Optional<SectionedAddress> {
128  if (U)
129  return U->getAddrOffsetSectionItem(Index);
130  return None;
131  });
132  OS << format("0x%8.8" PRIx64 ": ", *Offset);
133  Error E = visitLocationList(Offset, [&](const DWARFLocationEntry &E) {
134  Expected<Optional<DWARFLocationExpression>> Loc = Interp.Interpret(E);
135  if (!Loc || DumpOpts.DisplayRawContents)
136  dumpRawEntry(E, OS, Indent, DumpOpts, Obj);
137  if (Loc && *Loc) {
138  OS << "\n";
139  OS.indent(Indent);
140  if (DumpOpts.DisplayRawContents)
141  OS << " => ";
142 
143  DIDumpOptions RangeDumpOpts(DumpOpts);
144  RangeDumpOpts.DisplayRawContents = false;
145  if (Loc.get()->Range)
146  Loc.get()->Range->dump(OS, Data.getAddressSize(), RangeDumpOpts, &Obj);
147  else
148  OS << "<default>";
149  }
150  if (!Loc)
151  consumeError(Loc.takeError());
152 
153  if (E.Kind != dwarf::DW_LLE_base_address &&
154  E.Kind != dwarf::DW_LLE_base_addressx &&
155  E.Kind != dwarf::DW_LLE_end_of_list) {
156  OS << ": ";
157  dumpExpression(OS, E.Loc, Data.isLittleEndian(), Data.getAddressSize(),
158  MRI, U);
159  }
160  return true;
161  });
162  if (E) {
163  DumpOpts.RecoverableErrorHandler(std::move(E));
164  return false;
165  }
166  return true;
167 }
168 
170  uint64_t Offset, Optional<SectionedAddress> BaseAddr,
172  function_ref<bool(Expected<DWARFLocationExpression>)> Callback) const {
173  DWARFLocationInterpreter Interp(BaseAddr, std::move(LookupAddr));
174  return visitLocationList(&Offset, [&](const DWARFLocationEntry &E) {
175  Expected<Optional<DWARFLocationExpression>> Loc = Interp.Interpret(E);
176  if (!Loc)
177  return Callback(Loc.takeError());
178  if (*Loc)
179  return Callback(**Loc);
180  return true;
181  });
182 }
183 
185  const DWARFObject &Obj, DIDumpOptions DumpOpts,
186  Optional<uint64_t> DumpOffset) const {
187  auto BaseAddr = None;
188  unsigned Indent = 12;
189  if (DumpOffset) {
190  dumpLocationList(&*DumpOffset, OS, BaseAddr, MRI, Obj, nullptr, DumpOpts,
191  Indent);
192  } else {
193  uint64_t Offset = 0;
194  StringRef Separator;
195  bool CanContinue = true;
196  while (CanContinue && Data.isValidOffset(Offset)) {
197  OS << Separator;
198  Separator = "\n";
199 
200  CanContinue = dumpLocationList(&Offset, OS, BaseAddr, MRI, Obj, nullptr,
201  DumpOpts, Indent);
202  OS << '\n';
203  }
204  }
205 }
206 
208  uint64_t *Offset,
209  function_ref<bool(const DWARFLocationEntry &)> Callback) const {
210  DataExtractor::Cursor C(*Offset);
211  while (true) {
212  uint64_t SectionIndex;
213  uint64_t Value0 = Data.getRelocatedAddress(C);
214  uint64_t Value1 = Data.getRelocatedAddress(C, &SectionIndex);
215 
217 
218  // The end of any given location list is marked by an end of list entry,
219  // which consists of a 0 for the beginning address offset and a 0 for the
220  // ending address offset. A beginning offset of 0xff...f marks the base
221  // address selection entry.
222  if (Value0 == 0 && Value1 == 0) {
223  E.Kind = dwarf::DW_LLE_end_of_list;
224  } else if (Value0 == (Data.getAddressSize() == 4 ? -1U : -1ULL)) {
225  E.Kind = dwarf::DW_LLE_base_address;
226  E.Value0 = Value1;
227  E.SectionIndex = SectionIndex;
228  } else {
229  E.Kind = dwarf::DW_LLE_offset_pair;
230  E.Value0 = Value0;
231  E.Value1 = Value1;
232  E.SectionIndex = SectionIndex;
233  unsigned Bytes = Data.getU16(C);
234  // A single location description describing the location of the object...
235  Data.getU8(C, E.Loc, Bytes);
236  }
237 
238  if (!C)
239  return C.takeError();
240  if (!Callback(E) || E.Kind == dwarf::DW_LLE_end_of_list)
241  break;
242  }
243  *Offset = C.tell();
244  return Error::success();
245 }
246 
248  raw_ostream &OS, unsigned Indent,
249  DIDumpOptions DumpOpts,
250  const DWARFObject &Obj) const {
251  uint64_t Value0, Value1;
252  switch (Entry.Kind) {
253  case dwarf::DW_LLE_base_address:
254  Value0 = Data.getAddressSize() == 4 ? -1U : -1ULL;
255  Value1 = Entry.Value0;
256  break;
257  case dwarf::DW_LLE_offset_pair:
258  Value0 = Entry.Value0;
259  Value1 = Entry.Value1;
260  break;
261  case dwarf::DW_LLE_end_of_list:
262  Value0 = Value1 = 0;
263  return;
264  default:
265  llvm_unreachable("Not possible in DWARF4!");
266  }
267  OS << '\n';
268  OS.indent(Indent);
269  OS << '(' << format_hex(Value0, 2 + Data.getAddressSize() * 2) << ", "
270  << format_hex(Value1, 2 + Data.getAddressSize() * 2) << ')';
271  DWARFFormValue::dumpAddressSection(Obj, OS, DumpOpts, Entry.SectionIndex);
272 }
273 
275  uint64_t *Offset, function_ref<bool(const DWARFLocationEntry &)> F) const {
276 
277  DataExtractor::Cursor C(*Offset);
278  bool Continue = true;
279  while (Continue) {
281  E.Kind = Data.getU8(C);
282  switch (E.Kind) {
283  case dwarf::DW_LLE_end_of_list:
284  break;
285  case dwarf::DW_LLE_base_addressx:
286  E.Value0 = Data.getULEB128(C);
287  break;
288  case dwarf::DW_LLE_startx_endx:
289  E.Value0 = Data.getULEB128(C);
290  E.Value1 = Data.getULEB128(C);
291  break;
292  case dwarf::DW_LLE_startx_length:
293  E.Value0 = Data.getULEB128(C);
294  // Pre-DWARF 5 has different interpretation of the length field. We have
295  // to support both pre- and standartized styles for the compatibility.
296  if (Version < 5)
297  E.Value1 = Data.getU32(C);
298  else
299  E.Value1 = Data.getULEB128(C);
300  break;
301  case dwarf::DW_LLE_offset_pair:
302  E.Value0 = Data.getULEB128(C);
303  E.Value1 = Data.getULEB128(C);
304  E.SectionIndex = SectionedAddress::UndefSection;
305  break;
306  case dwarf::DW_LLE_default_location:
307  break;
308  case dwarf::DW_LLE_base_address:
309  E.Value0 = Data.getRelocatedAddress(C, &E.SectionIndex);
310  break;
311  case dwarf::DW_LLE_start_end:
312  E.Value0 = Data.getRelocatedAddress(C, &E.SectionIndex);
313  E.Value1 = Data.getRelocatedAddress(C);
314  break;
315  case dwarf::DW_LLE_start_length:
316  E.Value0 = Data.getRelocatedAddress(C, &E.SectionIndex);
317  E.Value1 = Data.getULEB128(C);
318  break;
319  default:
320  cantFail(C.takeError());
322  "LLE of kind %x not supported", (int)E.Kind);
323  }
324 
325  if (E.Kind != dwarf::DW_LLE_base_address &&
326  E.Kind != dwarf::DW_LLE_base_addressx &&
327  E.Kind != dwarf::DW_LLE_end_of_list) {
328  unsigned Bytes = Version >= 5 ? Data.getULEB128(C) : Data.getU16(C);
329  // A single location description describing the location of the object...
330  Data.getU8(C, E.Loc, Bytes);
331  }
332 
333  if (!C)
334  return C.takeError();
335  Continue = F(E) && E.Kind != dwarf::DW_LLE_end_of_list;
336  }
337  *Offset = C.tell();
338  return Error::success();
339 }
340 
342  raw_ostream &OS, unsigned Indent,
343  DIDumpOptions DumpOpts,
344  const DWARFObject &Obj) const {
345  size_t MaxEncodingStringLength = 0;
346 #define HANDLE_DW_LLE(ID, NAME) \
347  MaxEncodingStringLength = std::max(MaxEncodingStringLength, \
348  dwarf::LocListEncodingString(ID).size());
349 #include "llvm/BinaryFormat/Dwarf.def"
350 
351  OS << "\n";
352  OS.indent(Indent);
353  StringRef EncodingString = dwarf::LocListEncodingString(Entry.Kind);
354  // Unsupported encodings should have been reported during parsing.
355  assert(!EncodingString.empty() && "Unknown loclist entry encoding");
356  OS << format("%-*s(", MaxEncodingStringLength, EncodingString.data());
357  unsigned FieldSize = 2 + 2 * Data.getAddressSize();
358  switch (Entry.Kind) {
359  case dwarf::DW_LLE_end_of_list:
360  case dwarf::DW_LLE_default_location:
361  break;
362  case dwarf::DW_LLE_startx_endx:
363  case dwarf::DW_LLE_startx_length:
364  case dwarf::DW_LLE_offset_pair:
365  case dwarf::DW_LLE_start_end:
366  case dwarf::DW_LLE_start_length:
367  OS << format_hex(Entry.Value0, FieldSize) << ", "
368  << format_hex(Entry.Value1, FieldSize);
369  break;
370  case dwarf::DW_LLE_base_addressx:
371  case dwarf::DW_LLE_base_address:
372  OS << format_hex(Entry.Value0, FieldSize);
373  break;
374  }
375  OS << ')';
376  switch (Entry.Kind) {
377  case dwarf::DW_LLE_base_address:
378  case dwarf::DW_LLE_start_end:
379  case dwarf::DW_LLE_start_length:
380  DWARFFormValue::dumpAddressSection(Obj, OS, DumpOpts, Entry.SectionIndex);
381  break;
382  default:
383  break;
384  }
385 }
386 
387 void DWARFDebugLoclists::dumpRange(uint64_t StartOffset, uint64_t Size,
388  raw_ostream &OS, const MCRegisterInfo *MRI,
389  const DWARFObject &Obj,
390  DIDumpOptions DumpOpts) {
391  if (!Data.isValidOffsetForDataOfSize(StartOffset, Size)) {
392  OS << "Invalid dump range\n";
393  return;
394  }
395  uint64_t Offset = StartOffset;
396  StringRef Separator;
397  bool CanContinue = true;
398  while (CanContinue && Offset < StartOffset + Size) {
399  OS << Separator;
400  Separator = "\n";
401 
402  CanContinue = dumpLocationList(&Offset, OS, /*BaseAddr=*/None, MRI, Obj,
403  nullptr, DumpOpts, /*Indent=*/12);
404  OS << '\n';
405  }
406 }
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:708
uint64_t CallInst * C
Error visitAbsoluteLocationList(uint64_t Offset, Optional< object::SectionedAddress > BaseAddr, std::function< Optional< object::SectionedAddress >(uint32_t)> LookupAddr, function_ref< bool(Expected< DWARFLocationExpression >)> Callback) const
This class represents lattice values for constants.
Definition: AllocatorList.h:23
bool dumpLocationList(uint64_t *Offset, raw_ostream &OS, Optional< object::SectionedAddress > BaseAddr, const MCRegisterInfo *MRI, const DWARFObject &Obj, DWARFUnit *U, DIDumpOptions DumpOpts, unsigned Indent) const
Dump the location list at the given Offset.
uint64_t tell() const
Return the current position of this Cursor.
Definition: DataExtractor.h:71
FormattedNumber format_hex(uint64_t N, unsigned Width, bool Upper=false)
format_hex - Output N as a fixed width hexadecimal.
Definition: Format.h:186
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.
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:176
void dumpRange(uint64_t StartOffset, uint64_t Size, raw_ostream &OS, const MCRegisterInfo *MRI, const DWARFObject &Obj, DIDumpOptions DumpOpts)
Dump all location lists within the given range.
F(f)
A single location within a location list.
Definition: DWARFDebugLoc.h:27
Error takeError()
Take ownership of the stored error.
Definition: Error.h:557
static Error createResolverError(uint32_t Index, unsigned Kind)
Error visitLocationList(uint64_t *Offset, function_ref< bool(const DWARFLocationEntry &)> Callback) const override
Call the user-provided callback for each entry (including the end-of-list entry) in the location list...
uint8_t Kind
The entry kind (DW_LLE_***).
Definition: DWARFDebugLoc.h:29
Definition: BitVector.h:959
uint64_t Value1
The second value of the location entry (if applicable).
Definition: DWARFDebugLoc.h:35
Error takeError()
Return error contained inside this Cursor, if any.
Definition: DataExtractor.h:75
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:156
uint64_t Value0
The first value of the location entry (if applicable).
Definition: DWARFDebugLoc.h:32
SmallVector< uint8_t, 4 > Loc
The location expression itself (if applicable).
Definition: DWARFDebugLoc.h:41
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:181
unsigned const MachineRegisterInfo * MRI
std::function< void(Error)> RecoverableErrorHandler
Definition: DIContext.h:213
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
void dumpRawEntry(const DWARFLocationEntry &Entry, raw_ostream &OS, unsigned Indent, DIDumpOptions DumpOpts, const DWARFObject &Obj) const override
static void dumpAddressSection(const DWARFObject &Obj, raw_ostream &OS, DIDumpOptions DumpOpts, uint64_t SectionIndex)
A class representing a position in a DataExtractor, as well as any error encountered during extractio...
Definition: DataExtractor.h:54
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1006
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void dumpRawEntry(const DWARFLocationEntry &Entry, raw_ostream &OS, unsigned Indent, DIDumpOptions DumpOpts, const DWARFObject &Obj) const override
void dump(raw_ostream &OS, const MCRegisterInfo *RegInfo, const DWARFObject &Obj, DIDumpOptions DumpOpts, Optional< uint64_t > Offset) const
Print the location lists found within the debug_loc section.
A DataExtractor (typically for an in-memory copy of an object-file section) plus a relocation map for...
static ErrorSuccess success()
Create a success value.
Definition: Error.h:332
Represents a single DWARF expression, whose value is location-dependent.
static void dumpExpression(raw_ostream &OS, ArrayRef< uint8_t > Data, bool IsLittleEndian, unsigned AddressSize, const MCRegisterInfo *MRI, DWARFUnit *U)
void print(raw_ostream &OS, const MCRegisterInfo *RegInfo, DWARFUnit *U, bool IsEH=false) const
reference get()
Returns a reference to the stored T value.
Definition: Error.h:537
This file contains constants used for implementing Dwarf debug support.
uint32_t Size
Definition: Profile.cpp:46
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:152
StringRef LocListEncodingString(unsigned Encoding)
Definition: Dwarf.cpp:508
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Error visitLocationList(uint64_t *Offset, function_ref< bool(const DWARFLocationEntry &)> Callback) const override
Call the user-provided callback for each entry (including the end-of-list entry) in the location list...
uint64_t SectionIndex
The index of the section this entry is relative to (if applicable).
Definition: DWARFDebugLoc.h:38
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
print Print MemDeps of function
Optional< object::SectionedAddress > getAddrOffsetSectionItem(uint32_t Index) const
Definition: DWARFUnit.cpp:219
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
const uint64_t Version
Definition: InstrProf.h:996
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1202
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:77