1 //===- NativeSession.h - Native implementation of IPDBSession ---*- C++ -*-===//
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 
9 #ifndef LLVM_DEBUGINFO_PDB_NATIVE_NATIVESESSION_H
10 #define LLVM_DEBUGINFO_PDB_NATIVE_NATIVESESSION_H
11 
12 #include "llvm/ADT/DenseMap.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/DebugInfo/CodeView/TypeIndex.h"
15 #include "llvm/DebugInfo/PDB/IPDBRawSymbol.h"
16 #include "llvm/DebugInfo/PDB/IPDBSession.h"
17 #include "llvm/DebugInfo/PDB/Native/NativeRawSymbol.h"
18 #include "llvm/DebugInfo/PDB/Native/SymbolCache.h"
19 #include "llvm/Support/Allocator.h"
20 #include "llvm/Support/Error.h"
21 
22 namespace llvm {
23 class MemoryBuffer;
24 namespace pdb {
25 class PDBFile;
26 class NativeExeSymbol;
27 
28 class NativeSession : public IPDBSession {
29   struct PdbSearchOptions {
30     StringRef ExePath;
31     // FIXME: Add other PDB search options (_NT_SYMBOL_PATH, symsrv)
32   };
33 
34 public:
35   NativeSession(std::unique_ptr<PDBFile> PdbFile,
36                 std::unique_ptr<BumpPtrAllocator> Allocator);
37   ~NativeSession() override;
38 
39   static Error createFromPdb(std::unique_ptr<MemoryBuffer> MB,
40                              std::unique_ptr<IPDBSession> &Session);
41   static Error createFromPdbPath(StringRef PdbPath,
42                                  std::unique_ptr<IPDBSession> &Session);
43   static Error createFromExe(StringRef Path,
44                              std::unique_ptr<IPDBSession> &Session);
45   static Expected<std::string> searchForPdb(const PdbSearchOptions &Opts);
46 
47   uint64_t getLoadAddress() const override;
48   bool setLoadAddress(uint64_t Address) override;
49   std::unique_ptr<PDBSymbolExe> getGlobalScope() override;
50   std::unique_ptr<PDBSymbol> getSymbolById(SymIndexId SymbolId) const override;
51 
52   bool addressForVA(uint64_t VA, uint32_t &Section,
53                     uint32_t &Offset) const override;
54   bool addressForRVA(uint32_t RVA, uint32_t &Section,
55                      uint32_t &Offset) const override;
56 
57   std::unique_ptr<PDBSymbol> findSymbolByAddress(uint64_t Address,
58                                                  PDB_SymType Type) override;
59   std::unique_ptr<PDBSymbol> findSymbolByRVA(uint32_t RVA,
60                                              PDB_SymType Type) override;
61   std::unique_ptr<PDBSymbol> findSymbolBySectOffset(uint32_t Sect,
62                                                     uint32_t Offset,
63                                                     PDB_SymType Type) override;
64 
65   std::unique_ptr<IPDBEnumLineNumbers>
66   findLineNumbers(const PDBSymbolCompiland &Compiland,
67                   const IPDBSourceFile &File) const override;
68   std::unique_ptr<IPDBEnumLineNumbers>
69   findLineNumbersByAddress(uint64_t Address, uint32_t Length) const override;
70   std::unique_ptr<IPDBEnumLineNumbers>
71   findLineNumbersByRVA(uint32_t RVA, uint32_t Length) const override;
72   std::unique_ptr<IPDBEnumLineNumbers>
73   findLineNumbersBySectOffset(uint32_t Section, uint32_t Offset,
74                               uint32_t Length) const override;
75 
76   std::unique_ptr<IPDBEnumSourceFiles>
77   findSourceFiles(const PDBSymbolCompiland *Compiland, llvm::StringRef Pattern,
78                   PDB_NameSearchFlags Flags) const override;
79   std::unique_ptr<IPDBSourceFile>
80   findOneSourceFile(const PDBSymbolCompiland *Compiland,
81                     llvm::StringRef Pattern,
82                     PDB_NameSearchFlags Flags) const override;
83   std::unique_ptr<IPDBEnumChildren<PDBSymbolCompiland>>
84   findCompilandsForSourceFile(llvm::StringRef Pattern,
85                               PDB_NameSearchFlags Flags) const override;
86   std::unique_ptr<PDBSymbolCompiland>
87   findOneCompilandForSourceFile(llvm::StringRef Pattern,
88                                 PDB_NameSearchFlags Flags) const override;
89   std::unique_ptr<IPDBEnumSourceFiles> getAllSourceFiles() const override;
90   std::unique_ptr<IPDBEnumSourceFiles> getSourceFilesForCompiland(
91       const PDBSymbolCompiland &Compiland) const override;
92   std::unique_ptr<IPDBSourceFile>
93   getSourceFileById(uint32_t FileId) const override;
94 
95   std::unique_ptr<IPDBEnumDataStreams> getDebugStreams() const override;
96 
97   std::unique_ptr<IPDBEnumTables> getEnumTables() const override;
98 
99   std::unique_ptr<IPDBEnumInjectedSources> getInjectedSources() const override;
100 
101   std::unique_ptr<IPDBEnumSectionContribs> getSectionContribs() const override;
102 
103   std::unique_ptr<IPDBEnumFrameData> getFrameData() const override;
104 
105   PDBFile &getPDBFile() { return *Pdb; }
106   const PDBFile &getPDBFile() const { return *Pdb; }
107 
108   NativeExeSymbol &getNativeGlobalScope() const;
109   SymbolCache &getSymbolCache() { return Cache; }
110   const SymbolCache &getSymbolCache() const { return Cache; }
111   uint32_t getRVAFromSectOffset(uint32_t Section, uint32_t Offset) const;
112   uint64_t getVAFromSectOffset(uint32_t Section, uint32_t Offset) const;
113 
114 private:
115   void initializeExeSymbol();
116 
117   std::unique_ptr<PDBFile> Pdb;
118   std::unique_ptr<BumpPtrAllocator> Allocator;
119 
120   SymbolCache Cache;
121   SymIndexId ExeSymbol = 0;
122   uint64_t LoadAddress = 0;
123 };
124 } // namespace pdb
125 } // namespace llvm
126 
127 #endif
128