27#define DEBUG_TYPE "ReaderHandler"
30 if (
Error Err = createReaders())
32 if (
Error Err = printReaders())
34 if (
Error Err = compareReaders())
43 auto CreateOneReader = [&]() -> std::unique_ptr<LVReader> {
44 if (isa<ObjectFile *>(Input)) {
48 return std::make_unique<LVCodeViewReader>(
Filename, FileFormatName,
52 return std::make_unique<LVDWARFReader>(
Filename, FileFormatName, Obj,
55 if (isa<PDBFile *>(Input)) {
57 return std::make_unique<LVCodeViewReader>(
Filename, FileFormatName,
Pdb,
63 std::unique_ptr<LVReader> ReaderObj = CreateOneReader();
66 "unable to create reader for: '%s'",
70 Readers.emplace_back(std::move(ReaderObj));
87 if (
Error Err = handleBuffer(Readers,
Name, BuffOrErr.
get()))
101 std::unique_ptr<IPDBSession> Session;
115 if (ConvertedPath ==
Path)
116 return std::string(ExePath);
131 return std::string(ObjPath);
143 if (!ExePath.
empty())
150 std::vector<StringRef> ExecutableExtensions = {
"exe",
"dll"};
153 if (ExecutableImage.empty())
163 std::vector<StringRef> ObjectExtensions = {
"o",
"obj",
"lib"};
166 if (ObjectImage.empty())
168 if (
Error Err = handleFile(Readers, ObjectImage)) {
184 "Binary object format in '%s' does not have debug info.",
188 return handleFile(Readers, PdbPath.
get(),
Filename);
194 "Binary object format in '%s' is not supported.",
197 return handleObject(Readers,
Filename, *BinOrErr.
get());
203 std::string ConvertedPath =
209 "File '%s' does not exist.",
210 ConvertedPath.c_str());
212 std::unique_ptr<MemoryBuffer> Buffer = std::move(BuffOrErr.
get());
213 return handleBuffer(Readers, ConvertedPath, *Buffer, ExePath);
220 Twine(ObjForArch.getArchFlagName()) +
Twine(
")"))
222 if (
Expected<std::unique_ptr<MachOObjectFile>> MachOOrErr =
223 ObjForArch.getAsObjectFile()) {
232 if (
Expected<std::unique_ptr<Archive>> ArchiveOrErr =
233 ObjForArch.getAsArchive()) {
234 if (
Error Err = handleArchive(Readers, ObjName, *ArchiveOrErr.get()))
246 return createReader(
Filename, Readers, Input,
247 cast<ObjectFile *>(Input)->getFileFormatName());
250 return handleMach(Readers,
Filename, *Fat);
253 return handleArchive(Readers,
Filename, *Arch);
256 "Binary object format in '%s' is not supported.",
262 std::unique_ptr<IPDBSession> Session;
267 std::unique_ptr<NativeSession> PdbSession;
268 PdbSession.reset(
static_cast<NativeSession *
>(Session.release()));
269 PdbOrObj Input = &PdbSession->getPDBFile();
273 FileFormatName = Buffer.
substr(0, Pos - 1);
274 return createReader(
Filename, Readers, Input, FileFormatName, ExePath);
277Error LVReaderHandler::createReaders() {
279 for (std::string &Object : Objects) {
281 if (
Error Err = createReader(Object, Readers))
283 TheReaders.insert(TheReaders.end(),
284 std::make_move_iterator(Readers.begin()),
285 std::make_move_iterator(Readers.end()));
291Error LVReaderHandler::printReaders() {
293 if (
options().getPrintExecute())
294 for (
const std::unique_ptr<LVReader> &Reader : TheReaders)
301Error LVReaderHandler::compareReaders() {
303 size_t ReadersCount = TheReaders.size();
304 if (
options().getCompareExecute() && ReadersCount >= 2) {
306 size_t ViewPairs = ReadersCount / 2;
308 for (
size_t Pair = 0,
Index = 0; Pair < ViewPairs; ++Pair) {
310 TheReaders[
Index + 1].get()))
static std::string searchForObj(const StringRef Path, const StringRef Extension)
static std::string searchForExe(const StringRef Path, const StringRef Extension)
Represents either an error or a value T.
std::error_code getError() const
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
reference get()
Returns a reference to the stored T value.
StringRef getBuffer() const
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
A discriminated union of two or more pointer types, with the discriminator in the low bit of the poin...
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
StringRef - Represent a constant reference to a string, i.e.
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
constexpr bool empty() const
empty - Check if the string is empty.
size_t find_first_of(char C, size_t From=0) const
Find the first character in the string that is C, or npos if not found.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
void print(raw_ostream &OS) const
The logical reader owns of all the logical elements created during the debug information parsing.
iterator_range< child_iterator > children(Error &Err, bool SkipInternal=true) const
StringRef getFileFormatName() const override
iterator_range< object_iterator > objects() const
This class is the base class for all object file types.
static Expected< std::string > searchForPdb(const PdbSearchOptions &Opts)
This class implements an extremely fast bulk output stream that can only output to a stream.
std::vector< std::unique_ptr< LVReader > > LVReaders
Expected< std::unique_ptr< Binary > > createBinary(MemoryBufferRef Source, LLVMContext *Context=nullptr, bool InitContent=true)
Create a Binary from Source, autodetecting the file type.
Error loadDataForEXE(PDB_ReaderType Type, StringRef Path, std::unique_ptr< IPDBSession > &Session)
Error loadDataForPDB(PDB_ReaderType Type, StringRef Path, std::unique_ptr< IPDBSession > &Session)
bool exists(const basic_file_status &status)
Does file exist?
void replace_extension(SmallVectorImpl< char > &path, const Twine &extension, Style style=Style::native)
Replace the file extension of path with extension.
std::string convert_to_slash(StringRef path, Style style=Style::native)
Replaces backslashes with slashes if Windows.
This is an optimization pass for GlobalISel generic memory operations.
file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
void consumeError(Error Err)
Consume a Error without doing anything.
file_magic - An "enum class" enumeration of file types based on magic (the first N bytes of the file)...
@ pdb
Windows PDB debug info file.
@ pecoff_executable
PECOFF executable file.