1 //===-- SymbolFileDWARFDebugMap.h ------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef SymbolFileDWARF_SymbolFileDWARFDebugMap_h_
11 #define SymbolFileDWARF_SymbolFileDWARFDebugMap_h_
12 
13 #include <bitset>
14 #include <map>
15 #include <vector>
16 
17 #include "lldb/Core/RangeMap.h"
18 #include "lldb/Symbol/SymbolFile.h"
19 #include "llvm/Support/Chrono.h"
20 
21 #include "UniqueDWARFASTType.h"
22 
23 class SymbolFileDWARF;
24 class DWARFDebugAranges;
25 class DWARFDeclContext;
26 
27 class SymbolFileDWARFDebugMap : public lldb_private::SymbolFile {
28 public:
29   //------------------------------------------------------------------
30   // Static Functions
31   //------------------------------------------------------------------
32   static void Initialize();
33 
34   static void Terminate();
35 
36   static lldb_private::ConstString GetPluginNameStatic();
37 
38   static const char *GetPluginDescriptionStatic();
39 
40   static lldb_private::SymbolFile *
41   CreateInstance(lldb_private::ObjectFile *obj_file);
42 
43   //------------------------------------------------------------------
44   // Constructors and Destructors
45   //------------------------------------------------------------------
46   SymbolFileDWARFDebugMap(lldb_private::ObjectFile *ofile);
47   ~SymbolFileDWARFDebugMap() override;
48 
49   uint32_t CalculateAbilities() override;
50   void InitializeObject() override;
51 
52   //------------------------------------------------------------------
53   // Compile Unit function calls
54   //------------------------------------------------------------------
55   uint32_t GetNumCompileUnits() override;
56   lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t index) override;
57 
58   lldb::LanguageType
59   ParseCompileUnitLanguage(const lldb_private::SymbolContext &sc) override;
60   size_t
61   ParseCompileUnitFunctions(const lldb_private::SymbolContext &sc) override;
62   bool
63   ParseCompileUnitLineTable(const lldb_private::SymbolContext &sc) override;
64   bool
65   ParseCompileUnitDebugMacros(const lldb_private::SymbolContext &sc) override;
66   bool ParseCompileUnitSupportFiles(
67       const lldb_private::SymbolContext &sc,
68       lldb_private::FileSpecList &support_files) override;
69   bool
70   ParseCompileUnitIsOptimized(const lldb_private::SymbolContext &sc) override;
71   bool ParseImportedModules(
72       const lldb_private::SymbolContext &sc,
73       std::vector<lldb_private::ConstString> &imported_modules) override;
74   size_t ParseFunctionBlocks(const lldb_private::SymbolContext &sc) override;
75   size_t ParseTypes(const lldb_private::SymbolContext &sc) override;
76   size_t
77   ParseVariablesForContext(const lldb_private::SymbolContext &sc) override;
78 
79   lldb_private::Type *ResolveTypeUID(lldb::user_id_t type_uid) override;
80   llvm::Optional<ArrayInfo> GetDynamicArrayInfoForUID(
81       lldb::user_id_t type_uid,
82       const lldb_private::ExecutionContext *exe_ctx) override;
83 
84   lldb_private::CompilerDeclContext
85   GetDeclContextForUID(lldb::user_id_t uid) override;
86   lldb_private::CompilerDeclContext
87   GetDeclContextContainingUID(lldb::user_id_t uid) override;
88   void
89   ParseDeclsForContext(lldb_private::CompilerDeclContext decl_ctx) override;
90 
91   bool CompleteType(lldb_private::CompilerType &compiler_type) override;
92   uint32_t ResolveSymbolContext(const lldb_private::Address &so_addr,
93                                 lldb::SymbolContextItem resolve_scope,
94                                 lldb_private::SymbolContext &sc) override;
95   uint32_t
96   ResolveSymbolContext(const lldb_private::FileSpec &file_spec, uint32_t line,
97                        bool check_inlines,
98                        lldb::SymbolContextItem resolve_scope,
99                        lldb_private::SymbolContextList &sc_list) override;
100   uint32_t
101   FindGlobalVariables(const lldb_private::ConstString &name,
102                       const lldb_private::CompilerDeclContext *parent_decl_ctx,
103                       uint32_t max_matches,
104                       lldb_private::VariableList &variables) override;
105   uint32_t FindGlobalVariables(const lldb_private::RegularExpression &regex,
106                                uint32_t max_matches,
107                                lldb_private::VariableList &variables) override;
108   uint32_t
109   FindFunctions(const lldb_private::ConstString &name,
110                 const lldb_private::CompilerDeclContext *parent_decl_ctx,
111                 lldb::FunctionNameType name_type_mask, bool include_inlines,
112                 bool append, lldb_private::SymbolContextList &sc_list) override;
113   uint32_t FindFunctions(const lldb_private::RegularExpression &regex,
114                          bool include_inlines, bool append,
115                          lldb_private::SymbolContextList &sc_list) override;
116   uint32_t
117   FindTypes(const lldb_private::SymbolContext &sc,
118             const lldb_private::ConstString &name,
119             const lldb_private::CompilerDeclContext *parent_decl_ctx,
120             bool append, uint32_t max_matches,
121             llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
122             lldb_private::TypeMap &types) override;
123   lldb_private::CompilerDeclContext FindNamespace(
124       const lldb_private::SymbolContext &sc,
125       const lldb_private::ConstString &name,
126       const lldb_private::CompilerDeclContext *parent_decl_ctx) override;
127   size_t GetTypes(lldb_private::SymbolContextScope *sc_scope,
128                   lldb::TypeClass type_mask,
129                   lldb_private::TypeList &type_list) override;
130   std::vector<lldb_private::CallEdge>
131   ParseCallEdgesInFunction(lldb_private::UserID func_id) override;
132 
133   void DumpClangAST(lldb_private::Stream &s) override;
134 
135   //------------------------------------------------------------------
136   // PluginInterface protocol
137   //------------------------------------------------------------------
138   lldb_private::ConstString GetPluginName() override;
139 
140   uint32_t GetPluginVersion() override;
141 
142 protected:
143   enum { kHaveInitializedOSOs = (1 << 0), kNumFlags };
144 
145   friend class DebugMapModule;
146   friend struct DIERef;
147   friend class DWARFASTParserClang;
148   friend class DWARFUnit;
149   friend class SymbolFileDWARF;
150   struct OSOInfo {
151     lldb::ModuleSP module_sp;
152 
153     OSOInfo() : module_sp() {}
154   };
155 
156   typedef std::shared_ptr<OSOInfo> OSOInfoSP;
157 
158   typedef lldb_private::RangeDataVector<lldb::addr_t, lldb::addr_t,
159                                         lldb::addr_t>
160       FileRangeMap;
161 
162   //------------------------------------------------------------------
163   // Class specific types
164   //------------------------------------------------------------------
165   struct CompileUnitInfo {
166     lldb_private::FileSpec so_file;
167     lldb_private::ConstString oso_path;
168     llvm::sys::TimePoint<> oso_mod_time;
169     OSOInfoSP oso_sp;
170     lldb::CompUnitSP compile_unit_sp;
171     uint32_t first_symbol_index;
172     uint32_t last_symbol_index;
173     uint32_t first_symbol_id;
174     uint32_t last_symbol_id;
175     FileRangeMap file_range_map;
176     bool file_range_map_valid;
177 
178     CompileUnitInfo()
179         : so_file(), oso_path(), oso_mod_time(), oso_sp(), compile_unit_sp(),
180           first_symbol_index(UINT32_MAX), last_symbol_index(UINT32_MAX),
181           first_symbol_id(UINT32_MAX), last_symbol_id(UINT32_MAX),
182           file_range_map(), file_range_map_valid(false) {}
183 
184     const FileRangeMap &GetFileRangeMap(SymbolFileDWARFDebugMap *exe_symfile);
185   };
186 
187   //------------------------------------------------------------------
188   // Protected Member Functions
189   //------------------------------------------------------------------
190   void InitOSO();
191 
192   static uint32_t GetOSOIndexFromUserID(lldb::user_id_t uid) {
193     return (uint32_t)((uid >> 32ull) - 1ull);
194   }
195 
196   static SymbolFileDWARF *GetSymbolFileAsSymbolFileDWARF(SymbolFile *sym_file);
197 
198   bool GetFileSpecForSO(uint32_t oso_idx, lldb_private::FileSpec &file_spec);
199 
200   CompileUnitInfo *GetCompUnitInfo(const lldb_private::SymbolContext &sc);
201 
202   size_t GetCompUnitInfosForModule(const lldb_private::Module *oso_module,
203                                    std::vector<CompileUnitInfo *> &cu_infos);
204 
205   lldb_private::Module *
206   GetModuleByCompUnitInfo(CompileUnitInfo *comp_unit_info);
207 
208   lldb_private::Module *GetModuleByOSOIndex(uint32_t oso_idx);
209 
210   lldb_private::ObjectFile *
211   GetObjectFileByCompUnitInfo(CompileUnitInfo *comp_unit_info);
212 
213   lldb_private::ObjectFile *GetObjectFileByOSOIndex(uint32_t oso_idx);
214 
215   uint32_t GetCompUnitInfoIndex(const CompileUnitInfo *comp_unit_info);
216 
217   SymbolFileDWARF *GetSymbolFile(const lldb_private::SymbolContext &sc);
218 
219   SymbolFileDWARF *GetSymbolFileByCompUnitInfo(CompileUnitInfo *comp_unit_info);
220 
221   SymbolFileDWARF *GetSymbolFileByOSOIndex(uint32_t oso_idx);
222 
223   // If closure returns "false", iteration continues.  If it returns
224   // "true", iteration terminates.
225   void ForEachSymbolFile(std::function<bool(SymbolFileDWARF *)> closure) {
226     for (uint32_t oso_idx = 0, num_oso_idxs = m_compile_unit_infos.size();
227          oso_idx < num_oso_idxs; ++oso_idx) {
228       if (SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx)) {
229         if (closure(oso_dwarf))
230           return;
231       }
232     }
233   }
234 
235   CompileUnitInfo *GetCompileUnitInfoForSymbolWithIndex(uint32_t symbol_idx,
236                                                         uint32_t *oso_idx_ptr);
237 
238   CompileUnitInfo *GetCompileUnitInfoForSymbolWithID(lldb::user_id_t symbol_id,
239                                                      uint32_t *oso_idx_ptr);
240 
241   static int
242   SymbolContainsSymbolWithIndex(uint32_t *symbol_idx_ptr,
243                                 const CompileUnitInfo *comp_unit_info);
244 
245   static int SymbolContainsSymbolWithID(lldb::user_id_t *symbol_idx_ptr,
246                                         const CompileUnitInfo *comp_unit_info);
247 
248   uint32_t PrivateFindGlobalVariables(
249       const lldb_private::ConstString &name,
250       const lldb_private::CompilerDeclContext *parent_decl_ctx,
251       const std::vector<uint32_t> &name_symbol_indexes, uint32_t max_matches,
252       lldb_private::VariableList &variables);
253 
254   void SetCompileUnit(SymbolFileDWARF *oso_dwarf,
255                       const lldb::CompUnitSP &cu_sp);
256 
257   lldb::CompUnitSP GetCompileUnit(SymbolFileDWARF *oso_dwarf);
258 
259   CompileUnitInfo *GetCompileUnitInfo(SymbolFileDWARF *oso_dwarf);
260 
261   lldb::TypeSP
262   FindDefinitionTypeForDWARFDeclContext(const DWARFDeclContext &die_decl_ctx);
263 
264   bool Supports_DW_AT_APPLE_objc_complete_type(SymbolFileDWARF *skip_dwarf_oso);
265 
266   lldb::TypeSP FindCompleteObjCDefinitionTypeForDIE(
267       const DWARFDIE &die, const lldb_private::ConstString &type_name,
268       bool must_be_implementation);
269 
270   UniqueDWARFASTTypeMap &GetUniqueDWARFASTTypeMap() {
271     return m_unique_ast_type_map;
272   }
273 
274   //------------------------------------------------------------------
275   // OSOEntry
276   //------------------------------------------------------------------
277   class OSOEntry {
278   public:
279     OSOEntry()
280         : m_exe_sym_idx(UINT32_MAX), m_oso_file_addr(LLDB_INVALID_ADDRESS) {}
281 
282     OSOEntry(uint32_t exe_sym_idx, lldb::addr_t oso_file_addr)
283         : m_exe_sym_idx(exe_sym_idx), m_oso_file_addr(oso_file_addr) {}
284 
285     uint32_t GetExeSymbolIndex() const { return m_exe_sym_idx; }
286 
287     bool operator<(const OSOEntry &rhs) const {
288       return m_exe_sym_idx < rhs.m_exe_sym_idx;
289     }
290 
291     lldb::addr_t GetOSOFileAddress() const { return m_oso_file_addr; }
292 
293     void SetOSOFileAddress(lldb::addr_t oso_file_addr) {
294       m_oso_file_addr = oso_file_addr;
295     }
296 
297   protected:
298     uint32_t m_exe_sym_idx;
299     lldb::addr_t m_oso_file_addr;
300   };
301 
302   typedef lldb_private::RangeDataVector<lldb::addr_t, lldb::addr_t, OSOEntry>
303       DebugMap;
304 
305   //------------------------------------------------------------------
306   // Member Variables
307   //------------------------------------------------------------------
308   std::bitset<kNumFlags> m_flags;
309   std::vector<CompileUnitInfo> m_compile_unit_infos;
310   std::vector<uint32_t> m_func_indexes; // Sorted by address
311   std::vector<uint32_t> m_glob_indexes;
312   std::map<std::pair<lldb_private::ConstString, llvm::sys::TimePoint<>>,
313            OSOInfoSP>
314       m_oso_map;
315   UniqueDWARFASTTypeMap m_unique_ast_type_map;
316   lldb_private::LazyBool m_supports_DW_AT_APPLE_objc_complete_type;
317   DebugMap m_debug_map;
318 
319   //------------------------------------------------------------------
320   // When an object file from the debug map gets parsed in
321   // SymbolFileDWARF, it needs to tell the debug map about the object
322   // files addresses by calling this function once for each N_FUN,
323   // N_GSYM and N_STSYM and after all entries in the debug map have
324   // been matched up, FinalizeOSOFileRanges() should be called.
325   //------------------------------------------------------------------
326   bool AddOSOFileRange(CompileUnitInfo *cu_info, lldb::addr_t exe_file_addr,
327                        lldb::addr_t exe_byte_size, lldb::addr_t oso_file_addr,
328                        lldb::addr_t oso_byte_size);
329 
330   //------------------------------------------------------------------
331   // Called after calling AddOSOFileRange() for each object file debug
332   // map entry to finalize the info for the unlinked compile unit.
333   //------------------------------------------------------------------
334   void FinalizeOSOFileRanges(CompileUnitInfo *cu_info);
335 
336   //------------------------------------------------------------------
337   /// Convert \a addr from a .o file address, to an executable address.
338   ///
339   /// @param[in] addr
340   ///     A section offset address from a .o file
341   ///
342   /// @return
343   ///     Returns true if \a addr was converted to be an executable
344   ///     section/offset address, false otherwise.
345   //------------------------------------------------------------------
346   bool LinkOSOAddress(lldb_private::Address &addr);
347 
348   //------------------------------------------------------------------
349   /// Convert a .o file "file address" to an executable "file address".
350   ///
351   /// @param[in] oso_symfile
352   ///     The DWARF symbol file that contains \a oso_file_addr
353   ///
354   /// @param[in] oso_file_addr
355   ///     A .o file "file address" to convert.
356   ///
357   /// @return
358   ///     LLDB_INVALID_ADDRESS if \a oso_file_addr is not in the
359   ///     linked executable, otherwise a valid "file address" from the
360   ///     linked executable that contains the debug map.
361   //------------------------------------------------------------------
362   lldb::addr_t LinkOSOFileAddress(SymbolFileDWARF *oso_symfile,
363                                   lldb::addr_t oso_file_addr);
364 
365   //------------------------------------------------------------------
366   /// Given a line table full of lines with "file addresses" that are
367   /// for a .o file represented by \a oso_symfile, link a new line table
368   /// and return it.
369   ///
370   /// @param[in] oso_symfile
371   ///     The DWARF symbol file that produced the \a line_table
372   ///
373   /// @param[in] addr
374   ///     A section offset address from a .o file
375   ///
376   /// @return
377   ///     Returns a valid line table full of linked addresses, or NULL
378   ///     if none of the line table addresses exist in the main
379   ///     executable.
380   //------------------------------------------------------------------
381   lldb_private::LineTable *
382   LinkOSOLineTable(SymbolFileDWARF *oso_symfile,
383                    lldb_private::LineTable *line_table);
384 
385   size_t AddOSOARanges(SymbolFileDWARF *dwarf2Data,
386                        DWARFDebugAranges *debug_aranges);
387 };
388 
389 #endif // #ifndef SymbolFileDWARF_SymbolFileDWARFDebugMap_h_
390