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 return Error::success();
66}
67
69 for (const auto &Decl : Decls)
70 Decl.dump(OS);
71}
72
75 uint32_t AbbrCode) const {
76 if (FirstAbbrCode == UINT32_MAX) {
77 for (const auto &Decl : Decls) {
78 if (Decl.getCode() == AbbrCode)
79 return &Decl;
80 }
81 return nullptr;
82 }
83 if (AbbrCode < FirstAbbrCode || AbbrCode >= FirstAbbrCode + Decls.size())
84 return nullptr;
85 return &Decls[AbbrCode - FirstAbbrCode];
86}
87
89 // Create a sorted list of all abbrev codes.
90 std::vector<uint32_t> Codes;
91 Codes.reserve(Decls.size());
92 for (const auto &Decl : Decls)
93 Codes.push_back(Decl.getCode());
94
95 std::string Buffer;
96 raw_string_ostream Stream(Buffer);
97 // Each iteration through this loop represents a single contiguous range in
98 // the set of codes.
99 for (auto Current = Codes.begin(), End = Codes.end(); Current != End;) {
100 uint32_t RangeStart = *Current;
101 // Add the current range start.
102 Stream << *Current;
103 uint32_t RangeEnd = RangeStart;
104 // Find the end of the current range.
105 while (++Current != End && *Current == RangeEnd + 1)
106 ++RangeEnd;
107 // If there is more than one value in the range, add the range end too.
108 if (RangeStart != RangeEnd)
109 Stream << "-" << RangeEnd;
110 // If there is at least one more range, add a separator.
111 if (Current != End)
112 Stream << ", ";
113 }
114 return Buffer;
115}
116
118 : AbbrDeclSets(), PrevAbbrOffsetPos(AbbrDeclSets.end()), Data(Data) {}
119
121 if (!Data)
122 return Error::success();
123 uint64_t Offset = 0;
124 auto I = AbbrDeclSets.begin();
125 while (Data->isValidOffset(Offset)) {
126 while (I != AbbrDeclSets.end() && I->first < Offset)
127 ++I;
128 uint64_t CUAbbrOffset = Offset;
130 if (Error Err = AbbrDecls.extract(*Data, &Offset)) {
131 Data = std::nullopt;
132 return Err;
133 }
134 AbbrDeclSets.insert(I, std::make_pair(CUAbbrOffset, std::move(AbbrDecls)));
135 }
136 Data = std::nullopt;
137 return Error::success();
138}
139
141 if (Error Err = parse())
142 // FIXME: We should propagate this error or otherwise display it.
143 llvm::consumeError(std::move(Err));
144
145 if (AbbrDeclSets.empty()) {
146 OS << "< EMPTY >\n";
147 return;
148 }
149
150 for (const auto &I : AbbrDeclSets) {
151 OS << formatv("Abbrev table for offset: {0:x+8}\n", I.first);
152 I.second.dump(OS);
153 }
154}
155
158 const auto End = AbbrDeclSets.end();
159 if (PrevAbbrOffsetPos != End && PrevAbbrOffsetPos->first == CUAbbrOffset) {
160 return &PrevAbbrOffsetPos->second;
161 }
162
163 const auto Pos = AbbrDeclSets.find(CUAbbrOffset);
164 if (Pos != End) {
165 PrevAbbrOffsetPos = Pos;
166 return &Pos->second;
167 }
168
169 if (!Data || CUAbbrOffset >= Data->getData().size())
171 "the abbreviation offset into the .debug_abbrev section is not valid");
172
173 uint64_t Offset = CUAbbrOffset;
175 if (Error Err = AbbrDecls.extract(*Data, &Offset))
176 return std::move(Err);
177
178 PrevAbbrOffsetPos =
179 AbbrDeclSets.insert(std::make_pair(CUAbbrOffset, std::move(AbbrDecls)))
180 .first;
181 return &PrevAbbrOffsetPos->second;
182}
#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