LLVM 24.0.0git
DWARFDebugAbbrev.cpp
Go to the documentation of this file.
1//===- DWARFDebugAbbrev.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
12#include <cstdint>
13
14using namespace llvm;
15
17 uint64_t *Offset, dwarf::Attribute &Name,
18 dwarf::Form &Form,
19 std::optional<int64_t> &ImplicitConst) {
20 Name = static_cast<dwarf::Attribute>(AbbrevData.getULEB128(Offset));
21 Form = static_cast<dwarf::Form>(AbbrevData.getULEB128(Offset));
22 ImplicitConst = std::nullopt;
23 if (Form == dwarf::DW_FORM_implicit_const)
24 ImplicitConst = AbbrevData.getSLEB128(Offset);
25 return Name != 0 || Form != 0;
26}
27
31
32void DWARFAbbreviationDeclarationSet::clear() {
33 Offset = 0;
34 FirstAbbrCode = 0;
35 Decls.clear();
36}
37
39 uint64_t *OffsetPtr) {
40 clear();
41 const uint64_t BeginOffset = *OffsetPtr;
42 Offset = BeginOffset;
44 uint32_t PrevAbbrCode = 0;
45 while (true) {
47 AbbrDecl.extract(Data, OffsetPtr);
48 if (!ES)
49 return ES.takeError();
50
52 break;
53
54 if (FirstAbbrCode == 0) {
55 FirstAbbrCode = AbbrDecl.getCode();
56 } else if (PrevAbbrCode + 1 != AbbrDecl.getCode()) {
57 // Codes are not consecutive, can't do O(1) lookups.
58 FirstAbbrCode = UINT32_MAX;
59 }
60 PrevAbbrCode = AbbrDecl.getCode();
61 Decls.push_back(std::move(AbbrDecl));
62 }
63 Decls.shrink_to_fit();
64 return Error::success();
65}
66
68 for (const auto &Decl : Decls)
69 Decl.dump(OS);
70}
71
74 uint32_t AbbrCode) const {
75 if (FirstAbbrCode == UINT32_MAX) {
76 for (const auto &Decl : Decls) {
77 if (Decl.getCode() == AbbrCode)
78 return &Decl;
79 }
80 return nullptr;
81 }
82 if (AbbrCode < FirstAbbrCode || AbbrCode >= FirstAbbrCode + Decls.size())
83 return nullptr;
84 return &Decls[AbbrCode - FirstAbbrCode];
85}
86
88 // Create a sorted list of all abbrev codes.
89 std::vector<uint32_t> Codes;
90 Codes.reserve(Decls.size());
91 for (const auto &Decl : Decls)
92 Codes.push_back(Decl.getCode());
93
94 std::string Buffer;
95 raw_string_ostream Stream(Buffer);
96 // Each iteration through this loop represents a single contiguous range in
97 // the set of codes.
98 for (auto Current = Codes.begin(), End = Codes.end(); Current != End;) {
99 uint32_t RangeStart = *Current;
100 // Add the current range start.
101 Stream << *Current;
102 uint32_t RangeEnd = RangeStart;
103 // Find the end of the current range.
104 while (++Current != End && *Current == RangeEnd + 1)
105 ++RangeEnd;
106 // If there is more than one value in the range, add the range end too.
107 if (RangeStart != RangeEnd)
108 Stream << "-" << RangeEnd;
109 // If there is at least one more range, add a separator.
110 if (Current != End)
111 Stream << ", ";
112 }
113 return Buffer;
114}
115
117 : AbbrDeclSets(), PrevAbbrOffsetPos(AbbrDeclSets.end()), Data(Data) {}
118
120 if (!Data)
121 return Error::success();
122 uint64_t Offset = 0;
123 auto I = AbbrDeclSets.begin();
124 while (Data->isValidOffset(Offset)) {
125 while (I != AbbrDeclSets.end() && I->first < Offset)
126 ++I;
127 uint64_t CUAbbrOffset = Offset;
129 if (Error Err = AbbrDecls.extract(*Data, &Offset)) {
130 Data = std::nullopt;
131 return Err;
132 }
133 AbbrDeclSets.insert(I, std::make_pair(CUAbbrOffset, std::move(AbbrDecls)));
134 }
135 Data = std::nullopt;
136 return Error::success();
137}
138
140 if (Error Err = parse())
141 // FIXME: We should propagate this error or otherwise display it.
142 llvm::consumeError(std::move(Err));
143
144 if (AbbrDeclSets.empty()) {
145 OS << "< EMPTY >\n";
146 return;
147 }
148
149 for (const auto &I : AbbrDeclSets) {
150 OS << formatv("Abbrev table for offset: {0:x+8}\n", I.first);
151 I.second.dump(OS);
152 }
153}
154
157 const auto End = AbbrDeclSets.end();
158 if (PrevAbbrOffsetPos != End && PrevAbbrOffsetPos->first == CUAbbrOffset) {
159 return &PrevAbbrOffsetPos->second;
160 }
161
162 const auto Pos = AbbrDeclSets.find(CUAbbrOffset);
163 if (Pos != End) {
164 PrevAbbrOffsetPos = Pos;
165 return &Pos->second;
166 }
167
168 if (!Data || CUAbbrOffset >= Data->getData().size())
170 "the abbreviation offset into the .debug_abbrev section is not valid");
171
172 uint64_t Offset = CUAbbrOffset;
174 if (Error Err = AbbrDecls.extract(*Data, &Offset))
175 return std::move(Err);
176
177 PrevAbbrOffsetPos =
178 AbbrDeclSets.insert(std::make_pair(CUAbbrOffset, std::move(AbbrDecls)))
179 .first;
180 return &PrevAbbrOffsetPos->second;
181}
#define I(x, y, z)
Definition MD5.cpp:57
LLVM_ABI void dump(raw_ostream &OS) const
LLVM_ABI const DWARFAbbreviationDeclaration * getAbbreviationDeclaration(uint32_t AbbrCode) const
LLVM_ABI Error extract(DataExtractor Data, uint64_t *OffsetPtr)
LLVM_ABI std::string getCodeRange() const
LLVM_ABI llvm::Expected< ExtractState > extract(DataExtractor Data, uint64_t *OffsetPtr)
LLVM_ABI Expected< const DWARFAbbreviationDeclarationSet * > getAbbreviationDeclarationSet(uint64_t CUAbbrOffset) const
LLVM_ABI Error parse() const
DWARFAbbreviationDeclarationSetMap::const_iterator end() const
LLVM_ABI DWARFDebugAbbrev(DataExtractor Data)
LLVM_ABI void dump(raw_ostream &OS) const
LLVM_ABI uint64_t getULEB128(uint64_t *offset_ptr, llvm::Error *Err=nullptr) const
Extract a unsigned LEB128 value from *offset_ptr.
LLVM_ABI int64_t getSLEB128(uint64_t *OffsetPtr, Error *Err=nullptr) const
Extract a signed LEB128 value from *offset_ptr.
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
A raw_ostream that writes to an std::string.
Attribute
Attributes.
Definition Dwarf.h:125
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
LLVM_ABI bool readAbbrevAttribute(const DataExtractor &AbbrevData, uint64_t *Offset, dwarf::Attribute &Name, dwarf::Form &Form, std::optional< int64_t > &ImplicitConst)
Read the next (attribute, form) specification from an abbreviation declaration at Offset,...
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
void consumeError(Error Err)
Consume a Error without doing anything.
Definition Error.h:1106