xref: /freebsd-src/contrib/llvm-project/llvm/include/llvm/DebugInfo/LogicalView/LVReaderHandler.h (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
1*bdd1243dSDimitry Andric //===-- LVReaderHandler.h ---------------------------------------*- C++ -*-===//
2*bdd1243dSDimitry Andric //
3*bdd1243dSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*bdd1243dSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*bdd1243dSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*bdd1243dSDimitry Andric //
7*bdd1243dSDimitry Andric //===----------------------------------------------------------------------===//
8*bdd1243dSDimitry Andric //
9*bdd1243dSDimitry Andric // This class implements the Reader handler.
10*bdd1243dSDimitry Andric //
11*bdd1243dSDimitry Andric //===----------------------------------------------------------------------===//
12*bdd1243dSDimitry Andric 
13*bdd1243dSDimitry Andric #ifndef LLVM_DEBUGINFO_LOGICALVIEW_READERS_LVREADERHANDLER_H
14*bdd1243dSDimitry Andric #define LLVM_DEBUGINFO_LOGICALVIEW_READERS_LVREADERHANDLER_H
15*bdd1243dSDimitry Andric 
16*bdd1243dSDimitry Andric #include "llvm/ADT/PointerUnion.h"
17*bdd1243dSDimitry Andric #include "llvm/DebugInfo/LogicalView/Core/LVReader.h"
18*bdd1243dSDimitry Andric #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
19*bdd1243dSDimitry Andric #include "llvm/Object/Archive.h"
20*bdd1243dSDimitry Andric #include "llvm/Object/MachOUniversal.h"
21*bdd1243dSDimitry Andric #include "llvm/Object/ObjectFile.h"
22*bdd1243dSDimitry Andric #include "llvm/Support/MemoryBuffer.h"
23*bdd1243dSDimitry Andric #include "llvm/Support/ScopedPrinter.h"
24*bdd1243dSDimitry Andric #include <string>
25*bdd1243dSDimitry Andric #include <vector>
26*bdd1243dSDimitry Andric 
27*bdd1243dSDimitry Andric namespace llvm {
28*bdd1243dSDimitry Andric namespace logicalview {
29*bdd1243dSDimitry Andric 
30*bdd1243dSDimitry Andric using LVReaders = std::vector<LVReader *>;
31*bdd1243dSDimitry Andric using ArgVector = std::vector<std::string>;
32*bdd1243dSDimitry Andric using PdbOrObj = PointerUnion<object::ObjectFile *, pdb::PDBFile *>;
33*bdd1243dSDimitry Andric 
34*bdd1243dSDimitry Andric // This class performs the following tasks:
35*bdd1243dSDimitry Andric // - Creates a logical reader for every binary file in the command line,
36*bdd1243dSDimitry Andric //   that parses the debug information and creates a high level logical
37*bdd1243dSDimitry Andric //   view representation containing scopes, symbols, types and lines.
38*bdd1243dSDimitry Andric // - Prints and compares the logical views.
39*bdd1243dSDimitry Andric //
40*bdd1243dSDimitry Andric // The supported binary formats are: ELF, Mach-O and CodeView.
41*bdd1243dSDimitry Andric class LVReaderHandler {
42*bdd1243dSDimitry Andric   ArgVector &Objects;
43*bdd1243dSDimitry Andric   ScopedPrinter &W;
44*bdd1243dSDimitry Andric   raw_ostream &OS;
45*bdd1243dSDimitry Andric   LVReaders TheReaders;
46*bdd1243dSDimitry Andric 
47*bdd1243dSDimitry Andric   Error createReaders();
48*bdd1243dSDimitry Andric   void destroyReaders();
49*bdd1243dSDimitry Andric   Error printReaders();
50*bdd1243dSDimitry Andric   Error compareReaders();
51*bdd1243dSDimitry Andric 
52*bdd1243dSDimitry Andric   Error handleArchive(LVReaders &Readers, StringRef Filename,
53*bdd1243dSDimitry Andric                       object::Archive &Arch);
54*bdd1243dSDimitry Andric   Error handleBuffer(LVReaders &Readers, StringRef Filename,
55*bdd1243dSDimitry Andric                      MemoryBufferRef Buffer, StringRef ExePath = {});
56*bdd1243dSDimitry Andric   Error handleFile(LVReaders &Readers, StringRef Filename,
57*bdd1243dSDimitry Andric                    StringRef ExePath = {});
58*bdd1243dSDimitry Andric   Error handleMach(LVReaders &Readers, StringRef Filename,
59*bdd1243dSDimitry Andric                    object::MachOUniversalBinary &Mach);
60*bdd1243dSDimitry Andric   Error handleObject(LVReaders &Readers, StringRef Filename,
61*bdd1243dSDimitry Andric                      object::Binary &Binary);
62*bdd1243dSDimitry Andric 
63*bdd1243dSDimitry Andric   Error createReader(StringRef Filename, LVReaders &Readers, PdbOrObj &Input,
64*bdd1243dSDimitry Andric                      StringRef FileFormatName, StringRef ExePath = {});
65*bdd1243dSDimitry Andric 
66*bdd1243dSDimitry Andric public:
67*bdd1243dSDimitry Andric   LVReaderHandler() = delete;
68*bdd1243dSDimitry Andric   LVReaderHandler(ArgVector &Objects, ScopedPrinter &W,
69*bdd1243dSDimitry Andric                   LVOptions &ReaderOptions)
70*bdd1243dSDimitry Andric       : Objects(Objects), W(W), OS(W.getOStream()) {
71*bdd1243dSDimitry Andric     setOptions(&ReaderOptions);
72*bdd1243dSDimitry Andric   }
73*bdd1243dSDimitry Andric   LVReaderHandler(const LVReaderHandler &) = delete;
74*bdd1243dSDimitry Andric   LVReaderHandler &operator=(const LVReaderHandler &) = delete;
75*bdd1243dSDimitry Andric   ~LVReaderHandler() { destroyReaders(); }
76*bdd1243dSDimitry Andric 
77*bdd1243dSDimitry Andric   Error createReader(StringRef Filename, LVReaders &Readers) {
78*bdd1243dSDimitry Andric     return handleFile(Readers, Filename);
79*bdd1243dSDimitry Andric   }
80*bdd1243dSDimitry Andric   Error process();
81*bdd1243dSDimitry Andric 
82*bdd1243dSDimitry Andric   Expected<LVReader *> createReader(StringRef Pathname) {
83*bdd1243dSDimitry Andric     LVReaders Readers;
84*bdd1243dSDimitry Andric     if (Error Err = createReader(Pathname, Readers))
85*bdd1243dSDimitry Andric       return std::move(Err);
86*bdd1243dSDimitry Andric     return Readers[0];
87*bdd1243dSDimitry Andric   }
88*bdd1243dSDimitry Andric   void deleteReader(LVReader *Reader) { delete Reader; }
89*bdd1243dSDimitry Andric 
90*bdd1243dSDimitry Andric   void print(raw_ostream &OS) const;
91*bdd1243dSDimitry Andric 
92*bdd1243dSDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
93*bdd1243dSDimitry Andric   void dump() const { print(dbgs()); }
94*bdd1243dSDimitry Andric #endif
95*bdd1243dSDimitry Andric };
96*bdd1243dSDimitry Andric 
97*bdd1243dSDimitry Andric } // end namespace logicalview
98*bdd1243dSDimitry Andric } // namespace llvm
99*bdd1243dSDimitry Andric 
100*bdd1243dSDimitry Andric #endif // LLVM_DEBUGINFO_LOGICALVIEW_READERS_LVREADERHANDLER_H
101