Skip to content

Commit cd7c30a

Browse files
author
Alex B
committed
Address feedback nr.2
1 parent fe23779 commit cd7c30a

File tree

6 files changed

+76
-122
lines changed

6 files changed

+76
-122
lines changed

llvm/include/llvm/DebugInfo/GSYM/CallSiteInfo.h

Lines changed: 13 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ struct FunctionsYAML;
3232

3333
namespace gsym {
3434
class FileWriter;
35+
class GsymCreator;
3536
struct FunctionInfo;
3637
struct CallSiteInfo {
3738
enum Flags : uint8_t {
@@ -45,7 +46,7 @@ struct CallSiteInfo {
4546
};
4647

4748
/// The return address of the call site.
48-
uint64_t ReturnAddress;
49+
uint64_t ReturnAddress = 0;
4950

5051
/// Offsets into the string table for function names regex patterns.
5152
std::vector<uint32_t> MatchRegex;
@@ -57,12 +58,9 @@ struct CallSiteInfo {
5758
///
5859
/// \param Data The binary stream to read the data from.
5960
/// \param Offset The current offset within the data stream.
60-
/// \param BaseAddr The base address for decoding (unused here but included
61-
/// for consistency).
62-
///
6361
/// \returns A CallSiteInfo or an error describing the issue.
64-
static llvm::Expected<CallSiteInfo>
65-
decode(DataExtractor &Data, uint64_t &Offset, uint64_t BaseAddr);
62+
static llvm::Expected<CallSiteInfo> decode(DataExtractor &Data,
63+
uint64_t &Offset);
6664

6765
/// Encode this CallSiteInfo object into a FileWriter stream.
6866
///
@@ -77,12 +75,8 @@ struct CallSiteInfoCollection {
7775
/// Decode a CallSiteInfoCollection object from a binary data stream.
7876
///
7977
/// \param Data The binary stream to read the data from.
80-
/// \param BaseAddr The base address for decoding (unused here but included
81-
/// for consistency).
82-
///
8378
/// \returns A CallSiteInfoCollection or an error describing the issue.
84-
static llvm::Expected<CallSiteInfoCollection> decode(DataExtractor &Data,
85-
uint64_t BaseAddr);
79+
static llvm::Expected<CallSiteInfoCollection> decode(DataExtractor &Data);
8680

8781
/// Encode this CallSiteInfoCollection object into a FileWriter stream.
8882
///
@@ -91,67 +85,34 @@ struct CallSiteInfoCollection {
9185
llvm::Error encode(FileWriter &O) const;
9286
};
9387

94-
bool operator==(const CallSiteInfoCollection &LHS,
95-
const CallSiteInfoCollection &RHS);
96-
97-
bool operator==(const CallSiteInfo &LHS, const CallSiteInfo &RHS);
98-
9988
class CallSiteInfoLoader {
10089
public:
10190
/// Constructor that initializes the CallSiteInfoLoader with necessary data
10291
/// structures.
10392
///
10493
/// \param StringOffsetMap A reference to a DenseMap that maps existing string
105-
/// offsets to CachedHashStringRef. \param StrTab A reference to a
106-
/// StringTableBuilder used for managing looking up and creating new strings.
107-
/// \param StringStorage A reference to a StringSet for storing the data for
108-
/// generated strings.
109-
CallSiteInfoLoader(DenseMap<uint64_t, CachedHashStringRef> &StringOffsetMap,
110-
StringTableBuilder &StrTab, StringSet<> &StringStorage)
111-
: StringOffsetMap(StringOffsetMap), StrTab(StrTab),
112-
StringStorage(StringStorage) {}
113-
114-
/// Loads call site information from a YAML file and populates the provided
115-
/// FunctionInfo vector.
116-
///
94+
/// offsets to CachedHashStringRef.
95+
/// \param StrTab A reference to a StringTableBuilder used for managing
96+
/// looking up and creating new strings. \param StringStorage A reference to a
97+
/// StringSet for storing the data for generated strings.
98+
CallSiteInfoLoader(GsymCreator &GCreator) : GCreator(GCreator) {}
99+
117100
/// This method reads the specified YAML file, parses its content, and updates
118101
/// the `Funcs` vector with call site information based on the YAML data.
119102
///
120103
/// \param Funcs A reference to a vector of FunctionInfo objects to be
121104
/// populated.
122105
/// \param YAMLFile A StringRef representing the path to the YAML
123106
/// file to be loaded.
124-
///
125107
/// \returns An `llvm::Error` indicating success or describing any issues
126108
/// encountered during the loading process.
127109
llvm::Error loadYAML(std::vector<FunctionInfo> &Funcs, StringRef YAMLFile);
128110

129111
private:
130-
/// Retrieves an existing string from the StringOffsetMap using the provided
131-
/// offset.
132-
///
133-
/// \param offset A 32-bit unsigned integer representing the offset of the
134-
/// string.
135-
///
136-
/// \returns A StringRef corresponding to the string for the given offset.
137-
///
138-
/// \note This method asserts that the offset exists in the StringOffsetMap.
139-
StringRef stringFromOffset(uint32_t offset) const;
140-
141-
/// Obtains the offset corresponding to a given string in the StrTab. If the
142-
/// string does not already exist, it is created.
143-
///
144-
/// \param str A StringRef representing the string for which the offset is
145-
/// requested.
146-
///
147-
/// \returns A 32-bit unsigned integer representing the offset of the string.
148-
uint32_t offsetFromString(StringRef str);
149-
150112
/// Builds a map from function names to FunctionInfo pointers based on the
151113
/// provided `Funcs` vector.
152114
///
153115
/// \param Funcs A reference to a vector of FunctionInfo objects.
154-
///
155116
/// \returns A StringMap mapping function names (StringRef) to their
156117
/// corresponding FunctionInfo pointers.
157118
StringMap<FunctionInfo *> buildFunctionMap(std::vector<FunctionInfo> &Funcs);
@@ -162,21 +123,14 @@ class CallSiteInfoLoader {
162123
/// object containing parsed YAML data.
163124
/// \param FuncMap A reference to a StringMap mapping function names to
164125
/// FunctionInfo pointers.
165-
///
166126
/// \returns An `llvm::Error` indicating success or describing any issues
167127
/// encountered during processing.
168128
llvm::Error
169129
processYAMLFunctions(const llvm::yaml::FunctionsYAML &functionsYAML,
170130
StringMap<FunctionInfo *> &FuncMap);
171131

172-
/// Map of existing string offsets to CachedHashStringRef.
173-
DenseMap<uint64_t, CachedHashStringRef> &StringOffsetMap;
174-
175-
/// The gSYM string table builder.
176-
StringTableBuilder &StrTab;
177-
178-
/// The gSYM string storage - we store generated strings here.
179-
StringSet<> &StringStorage;
132+
/// Reference to the parent Gsym Creator object.
133+
GsymCreator &GCreator;
180134
};
181135

182136
raw_ostream &operator<<(raw_ostream &OS, const CallSiteInfo &CSI);

llvm/include/llvm/DebugInfo/GSYM/GsymCreator.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,15 @@ class GsymCreator {
329329
/// \returns The unique 32 bit offset into the string table.
330330
uint32_t insertString(StringRef S, bool Copy = true);
331331

332+
/// Retrieve a string fromt he GSYM string table given its offset.
333+
///
334+
/// The offset is assumed to be a valid offset into the string table.
335+
/// otherwise an assert will be triggered.
336+
///
337+
/// \param offset The offset of the string to retrieve, previously returned by
338+
/// insertString. \returns The string at the given offset in the string table.
339+
StringRef getString(uint32_t offset);
340+
332341
/// Insert a file into this GSYM creator.
333342
///
334343
/// Inserts a file by adding a FileEntry into the "Files" member variable if

llvm/lib/DebugInfo/GSYM/CallSiteInfo.cpp

Lines changed: 40 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "llvm/ADT/CachedHashString.h"
1111
#include "llvm/DebugInfo/GSYM/FileWriter.h"
1212
#include "llvm/DebugInfo/GSYM/FunctionInfo.h"
13+
#include "llvm/DebugInfo/GSYM/GsymCreator.h"
1314
#include "llvm/MC/StringTableBuilder.h"
1415
#include "llvm/Support/DataExtractor.h"
1516
#include "llvm/Support/YAMLParser.h"
@@ -23,17 +24,17 @@
2324
using namespace llvm;
2425
using namespace gsym;
2526

26-
llvm::Error CallSiteInfo::encode(FileWriter &O) const {
27+
Error CallSiteInfo::encode(FileWriter &O) const {
2728
O.writeU64(ReturnAddress);
2829
O.writeU8(Flags);
2930
O.writeU32(MatchRegex.size());
3031
for (uint32_t Entry : MatchRegex)
3132
O.writeU32(Entry);
32-
return llvm::Error::success();
33+
return Error::success();
3334
}
3435

35-
llvm::Expected<CallSiteInfo>
36-
CallSiteInfo::decode(DataExtractor &Data, uint64_t &Offset, uint64_t BaseAddr) {
36+
Expected<CallSiteInfo> CallSiteInfo::decode(DataExtractor &Data,
37+
uint64_t &Offset) {
3738
CallSiteInfo CSI;
3839

3940
// Read ReturnAddress
@@ -68,17 +69,17 @@ CallSiteInfo::decode(DataExtractor &Data, uint64_t &Offset, uint64_t BaseAddr) {
6869
return CSI;
6970
}
7071

71-
llvm::Error CallSiteInfoCollection::encode(FileWriter &O) const {
72+
Error CallSiteInfoCollection::encode(FileWriter &O) const {
7273
O.writeU32(CallSites.size());
7374
for (const CallSiteInfo &CSI : CallSites) {
74-
if (llvm::Error Err = CSI.encode(O))
75+
if (Error Err = CSI.encode(O))
7576
return Err;
7677
}
77-
return llvm::Error::success();
78+
return Error::success();
7879
}
7980

80-
llvm::Expected<CallSiteInfoCollection>
81-
CallSiteInfoCollection::decode(DataExtractor &Data, uint64_t BaseAddr) {
81+
Expected<CallSiteInfoCollection>
82+
CallSiteInfoCollection::decode(DataExtractor &Data) {
8283
CallSiteInfoCollection CSC;
8384
uint64_t Offset = 0;
8485

@@ -91,8 +92,7 @@ CallSiteInfoCollection::decode(DataExtractor &Data, uint64_t BaseAddr) {
9192

9293
CSC.CallSites.reserve(NumCallSites);
9394
for (uint32_t i = 0; i < NumCallSites; ++i) {
94-
llvm::Expected<CallSiteInfo> ECSI =
95-
CallSiteInfo::decode(Data, Offset, BaseAddr);
95+
Expected<CallSiteInfo> ECSI = CallSiteInfo::decode(Data, Offset);
9696
if (!ECSI)
9797
return ECSI.takeError();
9898
CSC.CallSites.emplace_back(*ECSI);
@@ -108,7 +108,7 @@ namespace yaml {
108108
struct CallSiteYAML {
109109
// The offset of the return address of the call site - relative to the start
110110
// of the function.
111-
llvm::yaml::Hex64 return_offset;
111+
Hex64 return_offset;
112112
std::vector<std::string> match_regex;
113113
std::vector<std::string> flags;
114114
};
@@ -149,34 +149,22 @@ template <> struct MappingTraits<FunctionsYAML> {
149149
LLVM_YAML_IS_SEQUENCE_VECTOR(CallSiteYAML)
150150
LLVM_YAML_IS_SEQUENCE_VECTOR(FunctionYAML)
151151

152-
// Implementation of CallSiteInfoLoader
153-
StringRef CallSiteInfoLoader::stringFromOffset(uint32_t offset) const {
154-
assert(StringOffsetMap.count(offset) &&
155-
"expected function name offset to already be in StringOffsetMap");
156-
return StringOffsetMap.find(offset)->second.val();
157-
}
158-
159-
uint32_t CallSiteInfoLoader::offsetFromString(StringRef str) {
160-
return StrTab.add(StringStorage.insert(str).first->getKey());
161-
}
162-
163-
llvm::Error CallSiteInfoLoader::loadYAML(std::vector<FunctionInfo> &Funcs,
164-
StringRef YAMLFile) {
152+
Error CallSiteInfoLoader::loadYAML(std::vector<FunctionInfo> &Funcs,
153+
StringRef YAMLFile) {
165154
// Step 1: Read YAML file
166-
auto BufferOrError = llvm::MemoryBuffer::getFile(YAMLFile);
155+
auto BufferOrError = MemoryBuffer::getFile(YAMLFile);
167156
if (!BufferOrError)
168157
return errorCodeToError(BufferOrError.getError());
169158

170-
std::unique_ptr<llvm::MemoryBuffer> Buffer = std::move(*BufferOrError);
159+
std::unique_ptr<MemoryBuffer> Buffer = std::move(*BufferOrError);
171160

172161
// Step 2: Parse YAML content
173-
llvm::yaml::FunctionsYAML functionsYAML;
174-
llvm::yaml::Input yin(Buffer->getMemBufferRef());
162+
yaml::FunctionsYAML functionsYAML;
163+
yaml::Input yin(Buffer->getMemBufferRef());
175164
yin >> functionsYAML;
176-
if (yin.error()) {
177-
return llvm::createStringError(yin.error(), "Error parsing YAML file: %s\n",
178-
Buffer->getBufferIdentifier().str().c_str());
179-
}
165+
if (yin.error())
166+
return createStringError(yin.error(), "Error parsing YAML file: %s\n",
167+
Buffer->getBufferIdentifier().str().c_str());
180168

181169
// Step 3: Build function map from Funcs
182170
auto FuncMap = buildFunctionMap(Funcs);
@@ -189,7 +177,7 @@ StringMap<FunctionInfo *>
189177
CallSiteInfoLoader::buildFunctionMap(std::vector<FunctionInfo> &Funcs) {
190178
StringMap<FunctionInfo *> FuncMap;
191179
auto insertFunc = [&](auto &Function) {
192-
std::string FuncName = stringFromOffset(Function.Name).str();
180+
StringRef FuncName = GCreator.getString(Function.Name);
193181
// If the function name is already in the map, don't update it. This way we
194182
// preferentially use the first encountered function. Since symbols are
195183
// loaded from dSYM first, we end up preferring keeping track of symbols
@@ -208,19 +196,19 @@ CallSiteInfoLoader::buildFunctionMap(std::vector<FunctionInfo> &Funcs) {
208196
return FuncMap;
209197
}
210198

211-
llvm::Error CallSiteInfoLoader::processYAMLFunctions(
212-
const llvm::yaml::FunctionsYAML &functionsYAML,
199+
Error CallSiteInfoLoader::processYAMLFunctions(
200+
const yaml::FunctionsYAML &functionsYAML,
213201
StringMap<FunctionInfo *> &FuncMap) {
214202
// For each function in the YAML file
215203
for (const auto &FuncYAML : functionsYAML.functions) {
216-
auto it = FuncMap.find(FuncYAML.name);
217-
if (it == FuncMap.end()) {
218-
return llvm::createStringError(
204+
auto It = FuncMap.find(FuncYAML.name);
205+
if (It == FuncMap.end())
206+
return createStringError(
219207
std::errc::invalid_argument,
220208
"Can't find function '%s' specified in callsite YAML\n",
221209
FuncYAML.name.c_str());
222-
}
223-
FunctionInfo *FuncInfo = it->second;
210+
211+
FunctionInfo *FuncInfo = It->second;
224212
// Create a CallSiteInfoCollection if not already present
225213
if (!FuncInfo->CallSites)
226214
FuncInfo->CallSites = CallSiteInfoCollection();
@@ -229,30 +217,30 @@ llvm::Error CallSiteInfoLoader::processYAMLFunctions(
229217
// Since YAML has specifies relative return offsets, add the function
230218
// start address to make the offset absolute.
231219
CSI.ReturnAddress = FuncInfo->Range.start() + CallSiteYAML.return_offset;
232-
for (const auto &Regex : CallSiteYAML.match_regex)
233-
CSI.MatchRegex.push_back(offsetFromString(Regex));
220+
for (const auto &Regex : CallSiteYAML.match_regex) {
221+
uint32_t StrOffset = GCreator.insertString(Regex);
222+
CSI.MatchRegex.push_back(StrOffset);
223+
}
234224

235-
// Initialize flags to None
236-
CSI.Flags = CallSiteInfo::None;
237225
// Parse flags and combine them
238226
for (const auto &FlagStr : CallSiteYAML.flags) {
239227
if (FlagStr == "InternalCall") {
240228
CSI.Flags |= static_cast<uint8_t>(CallSiteInfo::InternalCall);
241229
} else if (FlagStr == "ExternalCall") {
242230
CSI.Flags |= static_cast<uint8_t>(CallSiteInfo::ExternalCall);
243231
} else {
244-
return llvm::createStringError(std::errc::invalid_argument,
245-
"Unknown flag in callsite YAML: %s\n",
246-
FlagStr.c_str());
232+
return createStringError(std::errc::invalid_argument,
233+
"Unknown flag in callsite YAML: %s\n",
234+
FlagStr.c_str());
247235
}
248236
}
249237
FuncInfo->CallSites->CallSites.push_back(CSI);
250238
}
251239
}
252-
return llvm::Error::success();
240+
return Error::success();
253241
}
254242

255-
raw_ostream &llvm::gsym::operator<<(raw_ostream &OS, const CallSiteInfo &CSI) {
243+
raw_ostream &gsym::operator<<(raw_ostream &OS, const CallSiteInfo &CSI) {
256244
OS << " Return=" << HEX64(CSI.ReturnAddress);
257245
OS << " Flags=" << HEX8(CSI.Flags);
258246

@@ -265,8 +253,8 @@ raw_ostream &llvm::gsym::operator<<(raw_ostream &OS, const CallSiteInfo &CSI) {
265253
return OS;
266254
}
267255

268-
raw_ostream &llvm::gsym::operator<<(raw_ostream &OS,
269-
const CallSiteInfoCollection &CSIC) {
256+
raw_ostream &gsym::operator<<(raw_ostream &OS,
257+
const CallSiteInfoCollection &CSIC) {
270258
for (const auto &CS : CSIC.CallSites) {
271259
OS << CS;
272260
OS << "\n";

llvm/lib/DebugInfo/GSYM/FunctionInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ llvm::Expected<FunctionInfo> FunctionInfo::decode(DataExtractor &Data,
100100

101101
case InfoType::CallSiteInfo:
102102
if (Expected<llvm::gsym::CallSiteInfoCollection> CI =
103-
llvm::gsym::CallSiteInfoCollection::decode(InfoData, BaseAddr))
103+
llvm::gsym::CallSiteInfoCollection::decode(InfoData))
104104
FI.CallSites = std::move(CI.get());
105105
else
106106
return CI.takeError();

llvm/lib/DebugInfo/GSYM/GsymCreator.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ llvm::Error GsymCreator::encode(FileWriter &O) const {
191191

192192
llvm::Error GsymCreator::loadCallSitesFromYAML(StringRef YAMLFile) {
193193
// Use the loader to load call site information from the YAML file.
194-
CallSiteInfoLoader Loader(StringOffsetMap, StrTab, StringStorage);
194+
CallSiteInfoLoader Loader(*this);
195195
return Loader.loadYAML(Funcs, YAMLFile);
196196
}
197197

@@ -385,6 +385,12 @@ uint32_t GsymCreator::insertString(StringRef S, bool Copy) {
385385
return StrOff;
386386
}
387387

388+
StringRef GsymCreator::getString(uint32_t offset) {
389+
assert(StringOffsetMap.count(offset) &&
390+
"GsymCreator::getString expects a valid offset as parameter.");
391+
return StringOffsetMap.find(offset)->second.val();
392+
}
393+
388394
void GsymCreator::addFunctionInfo(FunctionInfo &&FI) {
389395
std::lock_guard<std::mutex> Guard(Mutex);
390396
Funcs.emplace_back(std::move(FI));

0 commit comments

Comments
 (0)