LLVM  5.0.0
DWARFDebugLoc.cpp
Go to the documentation of this file.
1 //===- DWARFDebugLoc.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/StringRef.h"
15 #include "llvm/Support/Format.h"
17 #include <algorithm>
18 #include <cinttypes>
19 #include <cstdint>
20 
21 using namespace llvm;
22 
24  for (const LocationList &L : Locations) {
25  OS << format("0x%8.8x: ", L.Offset);
26  const unsigned Indent = 12;
27  for (const Entry &E : L.Entries) {
28  if (&E != L.Entries.begin())
29  OS.indent(Indent);
30  OS << "Beginning address offset: " << format("0x%016" PRIx64, E.Begin)
31  << '\n';
32  OS.indent(Indent) << " Ending address offset: "
33  << format("0x%016" PRIx64, E.End) << '\n';
34  OS.indent(Indent) << " Location description: ";
35  for (unsigned char Loc : E.Loc) {
36  OS << format("%2.2x ", Loc);
37  }
38  OS << "\n\n";
39  }
40  }
41 }
42 
44  uint32_t Offset = 0;
45  while (data.isValidOffset(Offset+data.getAddressSize()-1)) {
46  Locations.resize(Locations.size() + 1);
47  LocationList &Loc = Locations.back();
48  Loc.Offset = Offset;
49  // 2.6.2 Location Lists
50  // A location list entry consists of:
51  while (true) {
52  // A beginning and ending address offsets.
53  Entry E;
54  E.Begin = data.getRelocatedAddress(&Offset);
55  E.End = data.getRelocatedAddress(&Offset);
56 
57  // The end of any given location list is marked by an end of list entry,
58  // which consists of a 0 for the beginning address offset and a 0 for the
59  // ending address offset.
60  if (E.Begin == 0 && E.End == 0)
61  break;
62 
63  unsigned Bytes = data.getU16(&Offset);
64  // A single location description describing the location of the object...
65  StringRef str = data.getData().substr(Offset, Bytes);
66  Offset += Bytes;
67  E.Loc.append(str.begin(), str.end());
68  Loc.Entries.push_back(std::move(E));
69  }
70  }
71  if (data.isValidOffset(Offset))
72  errs() << "error: failed to consume entire .debug_loc section\n";
73 }
74 
76  uint32_t Offset = 0;
77  while (data.isValidOffset(Offset)) {
78  Locations.resize(Locations.size() + 1);
79  LocationList &Loc = Locations.back();
80  Loc.Offset = Offset;
82  while ((Kind = static_cast<dwarf::LocationListEntry>(
83  data.getU8(&Offset))) != dwarf::DW_LLE_end_of_list) {
84 
85  if (Kind != dwarf::DW_LLE_startx_length) {
86  errs() << "error: dumping support for LLE of kind " << (int)Kind
87  << " not implemented\n";
88  return;
89  }
90 
91  Entry E;
92 
93  E.Start = data.getULEB128(&Offset);
94  E.Length = data.getU32(&Offset);
95 
96  unsigned Bytes = data.getU16(&Offset);
97  // A single location description describing the location of the object...
98  StringRef str = data.getData().substr(Offset, Bytes);
99  Offset += Bytes;
100  E.Loc.resize(str.size());
101  std::copy(str.begin(), str.end(), E.Loc.begin());
102 
103  Loc.Entries.push_back(std::move(E));
104  }
105  }
106 }
107 
109  for (const LocationList &L : Locations) {
110  OS << format("0x%8.8x: ", L.Offset);
111  const unsigned Indent = 12;
112  for (const Entry &E : L.Entries) {
113  if (&E != L.Entries.begin())
114  OS.indent(Indent);
115  OS << "Beginning address index: " << E.Start << '\n';
116  OS.indent(Indent) << " Length: " << E.Length << '\n';
117  OS.indent(Indent) << " Location description: ";
118  for (unsigned char Loc : E.Loc)
119  OS << format("%2.2x ", Loc);
120  OS << "\n\n";
121  }
122  }
123 }
MachineLoop * L
StringRef getData() const
Get the data pointed to by this extractor.
Definition: DataExtractor.h:55
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
void dump(raw_ostream &OS) const
uint8_t getAddressSize() const
Get the address size for this extractor.
Definition: DataExtractor.h:59
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
bool isValidOffset(uint32_t offset) const
Test the validity of offset.
uint32_t getU32(uint32_t *offset_ptr) const
Extract a uint32_t value from *offset_ptr.
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...
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
void parse(DataExtractor data)
iterator begin() const
Definition: StringRef.h:106
format_object< Ts...> format(const char *Fmt, const Ts &...Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
iterator begin() const
Definition: LoopInfo.h:133
uint8_t getU8(uint32_t *offset_ptr) const
Extract a uint8_t value from *offset_ptr.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:598
uint64_t getULEB128(uint32_t *offset_ptr) const
Extract a unsigned LEB128 value from *offset_ptr.
A DataExtractor (typically for an in-memory copy of an object-file section) plus a relocation map for...
#define E
Definition: LargeTest.cpp:27
LocationListEntry
Constants for location lists in DWARF v5.
Definition: Dwarf.h:299
uint16_t getU16(uint32_t *offset_ptr) const
Extract a uint16_t value from *offset_ptr.
void dump(raw_ostream &OS) const
Print the location lists found within the debug_loc section.
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:135
const unsigned Kind
iterator end() const
Definition: StringRef.h:108
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 parse(const DWARFDataExtractor &data)
Parse the debug_loc section accessible via the 'data' parameter using the address size also given in ...
void resize(size_type N)
Definition: SmallVector.h:352