1 //===-- SymbolFileBreakpad.h ------------------------------------*- 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 LLDB_SOURCE_PLUGINS_SYMBOLFILE_BREAKPAD_SYMBOLFILEBREAKPAD_H
10 #define LLDB_SOURCE_PLUGINS_SYMBOLFILE_BREAKPAD_SYMBOLFILEBREAKPAD_H
11 
12 #include "Plugins/ObjectFile/Breakpad/BreakpadRecords.h"
13 #include "lldb/Core/FileSpecList.h"
14 #include "lldb/Symbol/LineTable.h"
15 #include "lldb/Symbol/PostfixExpression.h"
16 #include "lldb/Symbol/SymbolFile.h"
17 #include "lldb/Symbol/UnwindPlan.h"
18 
19 namespace lldb_private {
20 
21 namespace breakpad {
22 
23 class SymbolFileBreakpad : public SymbolFileCommon {
24   /// LLVM RTTI support.
25   static char ID;
26 
27 public:
28   /// LLVM RTTI support.
29   /// \{
30   bool isA(const void *ClassID) const override {
31     return ClassID == &ID || SymbolFileCommon::isA(ClassID);
32   }
33   static bool classof(const SymbolFile *obj) { return obj->isA(&ID); }
34   /// \}
35 
36   // Static Functions
37   static void Initialize();
38   static void Terminate();
39   static void DebuggerInitialize(Debugger &debugger) {}
40   static llvm::StringRef GetPluginNameStatic() { return "breakpad"; }
41 
42   static llvm::StringRef GetPluginDescriptionStatic() {
43     return "Breakpad debug symbol file reader.";
44   }
45 
46   static SymbolFile *CreateInstance(lldb::ObjectFileSP objfile_sp) {
47     return new SymbolFileBreakpad(std::move(objfile_sp));
48   }
49 
50   // Constructors and Destructors
51   SymbolFileBreakpad(lldb::ObjectFileSP objfile_sp)
52       : SymbolFileCommon(std::move(objfile_sp)) {}
53 
54   ~SymbolFileBreakpad() override = default;
55 
56   uint32_t CalculateAbilities() override;
57 
58   void InitializeObject() override {}
59 
60   // Compile Unit function calls
61 
62   lldb::LanguageType ParseLanguage(CompileUnit &comp_unit) override {
63     return lldb::eLanguageTypeUnknown;
64   }
65 
66   lldb::FunctionSP GetOrCreateFunction(CompileUnit &comp_unit);
67 
68   size_t ParseFunctions(CompileUnit &comp_unit) override;
69 
70   bool ParseLineTable(CompileUnit &comp_unit) override;
71 
72   bool ParseDebugMacros(CompileUnit &comp_unit) override { return false; }
73 
74   bool ParseSupportFiles(CompileUnit &comp_unit,
75                          FileSpecList &support_files) override;
76   size_t ParseTypes(CompileUnit &cu) override { return 0; }
77 
78   bool ParseImportedModules(
79       const SymbolContext &sc,
80       std::vector<lldb_private::SourceModule> &imported_modules) override {
81     return false;
82   }
83 
84   size_t ParseBlocksRecursive(Function &func) override;
85 
86   void FindGlobalVariables(ConstString name,
87                            const CompilerDeclContext &parent_decl_ctx,
88                            uint32_t max_matches,
89                            VariableList &variables) override {}
90 
91   size_t ParseVariablesForContext(const SymbolContext &sc) override {
92     return 0;
93   }
94   Type *ResolveTypeUID(lldb::user_id_t type_uid) override { return nullptr; }
95   llvm::Optional<ArrayInfo> GetDynamicArrayInfoForUID(
96       lldb::user_id_t type_uid,
97       const lldb_private::ExecutionContext *exe_ctx) override {
98     return llvm::None;
99   }
100 
101   bool CompleteType(CompilerType &compiler_type) override { return false; }
102   uint32_t ResolveSymbolContext(const Address &so_addr,
103                                 lldb::SymbolContextItem resolve_scope,
104                                 SymbolContext &sc) override;
105 
106   uint32_t ResolveSymbolContext(const SourceLocationSpec &src_location_spec,
107                                 lldb::SymbolContextItem resolve_scope,
108                                 SymbolContextList &sc_list) override;
109 
110   void GetTypes(SymbolContextScope *sc_scope, lldb::TypeClass type_mask,
111                 TypeList &type_list) override {}
112 
113   void FindFunctions(ConstString name,
114                      const CompilerDeclContext &parent_decl_ctx,
115                      lldb::FunctionNameType name_type_mask,
116                      bool include_inlines, SymbolContextList &sc_list) override;
117 
118   void FindFunctions(const RegularExpression &regex, bool include_inlines,
119                      SymbolContextList &sc_list) override;
120 
121   void FindTypes(ConstString name, const CompilerDeclContext &parent_decl_ctx,
122                  uint32_t max_matches,
123                  llvm::DenseSet<SymbolFile *> &searched_symbol_files,
124                  TypeMap &types) override;
125 
126   void FindTypes(llvm::ArrayRef<CompilerContext> pattern, LanguageSet languages,
127                  llvm::DenseSet<SymbolFile *> &searched_symbol_files,
128                  TypeMap &types) override;
129 
130   llvm::Expected<TypeSystem &>
131   GetTypeSystemForLanguage(lldb::LanguageType language) override {
132     return llvm::make_error<llvm::StringError>(
133         "SymbolFileBreakpad does not support GetTypeSystemForLanguage",
134         llvm::inconvertibleErrorCode());
135   }
136 
137   CompilerDeclContext
138   FindNamespace(ConstString name,
139                 const CompilerDeclContext &parent_decl_ctx) override {
140     return CompilerDeclContext();
141   }
142 
143   void AddSymbols(Symtab &symtab) override;
144 
145   llvm::Expected<lldb::addr_t> GetParameterStackSize(Symbol &symbol) override;
146 
147   lldb::UnwindPlanSP
148   GetUnwindPlan(const Address &address,
149                 const RegisterInfoResolver &resolver) override;
150 
151   llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
152 
153   uint64_t GetDebugInfoSize() override;
154 
155 private:
156   // A class representing a position in the breakpad file. Useful for
157   // remembering the position so we can go back to it later and parse more data.
158   // Can be converted to/from a LineIterator, but it has a much smaller memory
159   // footprint.
160   struct Bookmark {
161     uint32_t section;
162     size_t offset;
163 
164     friend bool operator<(const Bookmark &lhs, const Bookmark &rhs) {
165       return std::tie(lhs.section, lhs.offset) <
166              std::tie(rhs.section, rhs.offset);
167     }
168   };
169 
170   // At iterator class for simplifying algorithms reading data from the breakpad
171   // file. It iterates over all records (lines) in the sections of a given type.
172   // It also supports saving a specific position (via the GetBookmark() method)
173   // and then resuming from it afterwards.
174   class LineIterator;
175 
176   // Return an iterator range for all records in the given object file of the
177   // given type.
178   llvm::iterator_range<LineIterator> lines(Record::Kind section_type);
179 
180   // Breakpad files do not contain sufficient information to correctly
181   // reconstruct compile units. The approach chosen here is to treat each
182   // function as a compile unit. The compile unit name is the name if the first
183   // line entry belonging to this function.
184   // This class is our internal representation of a compile unit. It stores the
185   // CompileUnit object and a bookmark pointing to the FUNC record of the
186   // compile unit function. It also lazily construct the list of support files
187   // and line table entries for the compile unit, when these are needed.
188   class CompUnitData {
189   public:
190     CompUnitData(Bookmark bookmark) : bookmark(bookmark) {}
191 
192     CompUnitData() = default;
193     CompUnitData(const CompUnitData &rhs) : bookmark(rhs.bookmark) {}
194     CompUnitData &operator=(const CompUnitData &rhs) {
195       bookmark = rhs.bookmark;
196       support_files.reset();
197       line_table_up.reset();
198       return *this;
199     }
200     friend bool operator<(const CompUnitData &lhs, const CompUnitData &rhs) {
201       return lhs.bookmark < rhs.bookmark;
202     }
203 
204     Bookmark bookmark;
205     llvm::Optional<FileSpecList> support_files;
206     std::unique_ptr<LineTable> line_table_up;
207 
208   };
209 
210   uint32_t CalculateNumCompileUnits() override;
211   lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t index) override;
212 
213   lldb::addr_t GetBaseFileAddress();
214   void ParseFileRecords();
215   void ParseCUData();
216   void ParseLineTableAndSupportFiles(CompileUnit &cu, CompUnitData &data);
217   void ParseUnwindData();
218   llvm::ArrayRef<uint8_t> SaveAsDWARF(postfix::Node &node);
219   lldb::UnwindPlanSP ParseCFIUnwindPlan(const Bookmark &bookmark,
220                                         const RegisterInfoResolver &resolver);
221   bool ParseCFIUnwindRow(llvm::StringRef unwind_rules,
222                          const RegisterInfoResolver &resolver,
223                          UnwindPlan::Row &row);
224   lldb::UnwindPlanSP ParseWinUnwindPlan(const Bookmark &bookmark,
225                                         const RegisterInfoResolver &resolver);
226   void ParseInlineOriginRecords();
227 
228   using CompUnitMap = RangeDataVector<lldb::addr_t, lldb::addr_t, CompUnitData>;
229 
230   llvm::Optional<std::vector<FileSpec>> m_files;
231   llvm::Optional<CompUnitMap> m_cu_data;
232   llvm::Optional<std::vector<llvm::StringRef>> m_inline_origins;
233 
234   using UnwindMap = RangeDataVector<lldb::addr_t, lldb::addr_t, Bookmark>;
235   struct UnwindData {
236     UnwindMap cfi;
237     UnwindMap win;
238   };
239   llvm::Optional<UnwindData> m_unwind_data;
240   llvm::BumpPtrAllocator m_allocator;
241 };
242 
243 } // namespace breakpad
244 } // namespace lldb_private
245 
246 #endif
247