LLVM 23.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
10#include "llvm/Support/Format.h"
13#include <cinttypes>
14#include <cstdint>
15
16using namespace llvm;
17
20 dwarf::Form &Form,
21 std::optional<int64_t> &ImplicitConst) {
22 Name = static_cast<dwarf::Attribute>(AbbrevData.getULEB128(Offset));
23 Form = static_cast<dwarf::Form>(AbbrevData.getULEB128(Offset));
24 ImplicitConst = std::nullopt;
25 if (Form == dwarf::DW_FORM_implicit_const)
26 ImplicitConst = AbbrevData.getSLEB128(Offset);
27 return Name != 0 || Form != 0;
28}
29
33
34void DWARFAbbreviationDeclarationSet::clear() {
35 Offset = 0;
36 FirstAbbrCode = 0;
37 Decls.clear();
38}
39
41 uint64_t *OffsetPtr) {
42 clear();
43 const uint64_t BeginOffset = *OffsetPtr;
44 Offset = BeginOffset;
46 uint32_t PrevAbbrCode = 0;
47 while (true) {
49 AbbrDecl.extract(Data, OffsetPtr);
50 if (!ES)
51 return ES.takeError();
52
54 break;
55
56 if (FirstAbbrCode == 0) {
57 FirstAbbrCode = AbbrDecl.getCode();
58 } else if (PrevAbbrCode + 1 != AbbrDecl.getCode()) {
59 // Codes are not consecutive, can't do O(1) lookups.
60 FirstAbbrCode = UINT32_MAX;
61 }
62 PrevAbbrCode = AbbrDecl.getCode();
63 Decls.push_back(std::move(AbbrDecl));
64 }
65 Decls.shrink_to_fit();
66 return Error::success();
67}
68
70 for (const auto &Decl : Decls)
71 Decl.dump(OS);
72}
73
76 uint32_t AbbrCode) const {
77 if (FirstAbbrCode == UINT32_MAX) {
78 for (const auto &Decl : Decls) {
79 if (Decl.getCode() == AbbrCode)
80 return &Decl;
81 }
82 return nullptr;
83 }
84 if (AbbrCode < FirstAbbrCode || AbbrCode >= FirstAbbrCode + Decls.size())
85 return nullptr;
86 return &Decls[AbbrCode - FirstAbbrCode];
87}
88
90 // Create a sorted list of all abbrev codes.
91 std::vector<uint32_t> Codes;
92 Codes.reserve(Decls.size());
93 for (const auto &Decl : Decls)
94 Codes.push_back(Decl.getCode());
95
96 std::string Buffer;
97 raw_string_ostream Stream(Buffer);
98 // Each iteration through this loop represents a single contiguous range in
99 // the set of codes.
100 for (auto Current = Codes.begin(), End = Codes.end(); Current != End;) {
101 uint32_t RangeStart = *Current;
102 // Add the current range start.
103 Stream << *Current;
104 uint32_t RangeEnd = RangeStart;
105 // Find the end of the current range.
106 while (++Current != End && *Current == RangeEnd + 1)
107 ++RangeEnd;
108 // If there is more than one value in the range, add the range end too.
109 if (RangeStart != RangeEnd)
110 Stream << "-" << RangeEnd;
111 // If there is at least one more range, add a separator.
112 if (Current != End)
113 Stream << ", ";
114 }
115 return Buffer;
116}
117
119 : AbbrDeclSets(), PrevAbbrOffsetPos(AbbrDeclSets.end()), Data(Data) {}
120
122 if (!Data)
123 return Error::success();
124 uint64_t Offset = 0;
125 auto I = AbbrDeclSets.begin();
126 while (Data->isValidOffset(Offset)) {
127 while (I != AbbrDeclSets.end() && I->first < Offset)
128 ++I;
129 uint64_t CUAbbrOffset = Offset;
131 if (Error Err = AbbrDecls.extract(*Data, &Offset)) {
132 Data = std::nullopt;
133 return Err;
134 }
135 AbbrDeclSets.insert(I, std::make_pair(CUAbbrOffset, std::move(AbbrDecls)));
136 }
137 Data = std::nullopt;
138 return Error::success();
139}
140
142 if (Error Err = parse())
143 // FIXME: We should propagate this error or otherwise display it.
144 llvm::consumeError(std::move(Err));
145
146 if (AbbrDeclSets.empty()) {
147 OS << "< EMPTY >\n";
148 return;
149 }
150
151 for (const auto &I : AbbrDeclSets) {
152 OS << formatv("Abbrev table for offset: {0:x+8}\n", I.first);
153 I.second.dump(OS);
154 }
155}
156
159 const auto End = AbbrDeclSets.end();
160 if (PrevAbbrOffsetPos != End && PrevAbbrOffsetPos->first == CUAbbrOffset) {
161 return &PrevAbbrOffsetPos->second;
162 }
163
164 const auto Pos = AbbrDeclSets.find(CUAbbrOffset);
165 if (Pos != End) {
166 PrevAbbrOffsetPos = Pos;
167 return &Pos->second;
168 }
169
170 if (!Data || CUAbbrOffset >= Data->getData().size())
172 "the abbreviation offset into the .debug_abbrev section is not valid");
173
174 uint64_t Offset = CUAbbrOffset;
176 if (Error Err = AbbrDecls.extract(*Data, &Offset))
177 return std::move(Err);
178
179 PrevAbbrOffsetPos =
180 AbbrDeclSets.insert(std::make_pair(CUAbbrOffset, std::move(AbbrDecls)))
181 .first;
182 return &PrevAbbrOffsetPos->second;
183}
#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:573
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