]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Core/Module.h
Merge ^/head r317281 through r317502.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / include / lldb / Core / Module.h
1 //===-- Module.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 liblldb_Module_h_
11 #define liblldb_Module_h_
12
13 #include "lldb/Core/Address.h" // for Address
14 #include "lldb/Core/ArchSpec.h"
15 #include "lldb/Core/ModuleSpec.h" // for ModuleSpec
16 #include "lldb/Symbol/SymbolContextScope.h"
17 #include "lldb/Symbol/TypeSystem.h"
18 #include "lldb/Target/PathMappingList.h"
19 #include "lldb/Utility/ConstString.h" // for ConstString
20 #include "lldb/Utility/Error.h"       // for Error
21 #include "lldb/Utility/FileSpec.h"
22 #include "lldb/Utility/UUID.h"
23 #include "lldb/lldb-defines.h"      // for DISALLOW_COPY_AND_ASSIGN
24 #include "lldb/lldb-enumerations.h" // for LanguageType, SymbolType
25 #include "lldb/lldb-forward.h"
26 #include "lldb/lldb-types.h" // for addr_t, offset_t
27
28 #include "llvm/ADT/DenseSet.h"
29 #include "llvm/ADT/StringRef.h"
30 #include "llvm/Support/Chrono.h"
31
32 #include <atomic>
33 #include <memory> // for enable_shared_from_this
34 #include <mutex>
35 #include <stddef.h> // for size_t
36 #include <stdint.h> // for uint32_t, uint64_t
37 #include <string>
38 #include <vector>
39
40 namespace lldb_private {
41 class CompilerDeclContext;
42 }
43 namespace lldb_private {
44 class Function;
45 }
46 namespace lldb_private {
47 class Log;
48 }
49 namespace lldb_private {
50 class ObjectFile;
51 }
52 namespace lldb_private {
53 class RegularExpression;
54 }
55 namespace lldb_private {
56 class SectionList;
57 }
58 namespace lldb_private {
59 class Stream;
60 }
61 namespace lldb_private {
62 class Symbol;
63 }
64 namespace lldb_private {
65 class SymbolContext;
66 }
67 namespace lldb_private {
68 class SymbolContextList;
69 }
70 namespace lldb_private {
71 class SymbolFile;
72 }
73 namespace lldb_private {
74 class SymbolVendor;
75 }
76 namespace lldb_private {
77 class Symtab;
78 }
79 namespace lldb_private {
80 class Target;
81 }
82 namespace lldb_private {
83 class TypeList;
84 }
85 namespace lldb_private {
86 class TypeMap;
87 }
88 namespace lldb_private {
89 class VariableList;
90 }
91
92 namespace lldb_private {
93
94 //----------------------------------------------------------------------
95 /// @class Module Module.h "lldb/Core/Module.h"
96 /// @brief A class that describes an executable image and its associated
97 ///        object and symbol files.
98 ///
99 /// The module is designed to be able to select a single slice of an
100 /// executable image as it would appear on disk and during program
101 /// execution.
102 ///
103 /// Modules control when and if information is parsed according to which
104 /// accessors are called. For example the object file (ObjectFile)
105 /// representation will only be parsed if the object file is requested
106 /// using the Module::GetObjectFile() is called. The debug symbols
107 /// will only be parsed if the symbol vendor (SymbolVendor) is
108 /// requested using the Module::GetSymbolVendor() is called.
109 ///
110 /// The module will parse more detailed information as more queries are
111 /// made.
112 //----------------------------------------------------------------------
113 class Module : public std::enable_shared_from_this<Module>,
114                public SymbolContextScope {
115 public:
116   // Static functions that can track the lifetime of module objects.
117   // This is handy because we might have Module objects that are in
118   // shared pointers that aren't in the global module list (from
119   // ModuleList). If this is the case we need to know about it.
120   // The modules in the global list maintained by these functions
121   // can be viewed using the "target modules list" command using the
122   // "--global" (-g for short).
123   static size_t GetNumberAllocatedModules();
124
125   static Module *GetAllocatedModuleAtIndex(size_t idx);
126
127   static std::recursive_mutex &GetAllocationModuleCollectionMutex();
128
129   //------------------------------------------------------------------
130   /// Construct with file specification and architecture.
131   ///
132   /// Clients that wish to share modules with other targets should
133   /// use ModuleList::GetSharedModule().
134   ///
135   /// @param[in] file_spec
136   ///     The file specification for the on disk representation of
137   ///     this executable image.
138   ///
139   /// @param[in] arch
140   ///     The architecture to set as the current architecture in
141   ///     this module.
142   ///
143   /// @param[in] object_name
144   ///     The name of an object in a module used to extract a module
145   ///     within a module (.a files and modules that contain multiple
146   ///     architectures).
147   ///
148   /// @param[in] object_offset
149   ///     The offset within an existing module used to extract a
150   ///     module within a module (.a files and modules that contain
151   ///     multiple architectures).
152   //------------------------------------------------------------------
153   Module(
154       const FileSpec &file_spec, const ArchSpec &arch,
155       const ConstString *object_name = nullptr,
156       lldb::offset_t object_offset = 0,
157       const llvm::sys::TimePoint<> &object_mod_time = llvm::sys::TimePoint<>());
158
159   Module(const ModuleSpec &module_spec);
160
161   static lldb::ModuleSP
162   CreateJITModule(const lldb::ObjectFileJITDelegateSP &delegate_sp);
163
164   //------------------------------------------------------------------
165   /// Destructor.
166   //------------------------------------------------------------------
167   ~Module() override;
168
169   bool MatchesModuleSpec(const ModuleSpec &module_ref);
170
171   //------------------------------------------------------------------
172   /// Set the load address for all sections in a module to be the
173   /// file address plus \a slide.
174   ///
175   /// Many times a module will be loaded in a target with a constant
176   /// offset applied to all top level sections. This function can
177   /// set the load address for all top level sections to be the
178   /// section file address + offset.
179   ///
180   /// @param[in] target
181   ///     The target in which to apply the section load addresses.
182   ///
183   /// @param[in] value
184   ///     if \a value_is_offset is true, then value is the offset to
185   ///     apply to all file addresses for all top level sections in
186   ///     the object file as each section load address is being set.
187   ///     If \a value_is_offset is false, then "value" is the new
188   ///     absolute base address for the image.
189   ///
190   /// @param[in] value_is_offset
191   ///     If \b true, then \a value is an offset to apply to each
192   ///     file address of each top level section.
193   ///     If \b false, then \a value is the image base address that
194   ///     will be used to rigidly slide all loadable sections.
195   ///
196   /// @param[out] changed
197   ///     If any section load addresses were changed in \a target,
198   ///     then \a changed will be set to \b true. Else \a changed
199   ///     will be set to false. This allows this function to be
200   ///     called multiple times on the same module for the same
201   ///     target. If the module hasn't moved, then \a changed will
202   ///     be false and no module updated notification will need to
203   ///     be sent out.
204   ///
205   /// @return
206   ///     /b True if any sections were successfully loaded in \a target,
207   ///     /b false otherwise.
208   //------------------------------------------------------------------
209   bool SetLoadAddress(Target &target, lldb::addr_t value, bool value_is_offset,
210                       bool &changed);
211
212   //------------------------------------------------------------------
213   /// @copydoc SymbolContextScope::CalculateSymbolContext(SymbolContext*)
214   ///
215   /// @see SymbolContextScope
216   //------------------------------------------------------------------
217   void CalculateSymbolContext(SymbolContext *sc) override;
218
219   lldb::ModuleSP CalculateSymbolContextModule() override;
220
221   void
222   GetDescription(Stream *s,
223                  lldb::DescriptionLevel level = lldb::eDescriptionLevelFull);
224
225   //------------------------------------------------------------------
226   /// Get the module path and object name.
227   ///
228   /// Modules can refer to object files. In this case the specification
229   /// is simple and would return the path to the file:
230   ///
231   ///     "/usr/lib/foo.dylib"
232   ///
233   /// Modules can be .o files inside of a BSD archive (.a file). In
234   /// this case, the object specification will look like:
235   ///
236   ///     "/usr/lib/foo.a(bar.o)"
237   ///
238   /// There are many places where logging wants to log this fully
239   /// qualified specification, so we centralize this functionality
240   /// here.
241   ///
242   /// @return
243   ///     The object path + object name if there is one.
244   //------------------------------------------------------------------
245   std::string GetSpecificationDescription() const;
246
247   //------------------------------------------------------------------
248   /// Dump a description of this object to a Stream.
249   ///
250   /// Dump a description of the contents of this object to the
251   /// supplied stream \a s. The dumped content will be only what has
252   /// been loaded or parsed up to this point at which this function
253   /// is called, so this is a good way to see what has been parsed
254   /// in a module.
255   ///
256   /// @param[in] s
257   ///     The stream to which to dump the object description.
258   //------------------------------------------------------------------
259   void Dump(Stream *s);
260
261   //------------------------------------------------------------------
262   /// @copydoc SymbolContextScope::DumpSymbolContext(Stream*)
263   ///
264   /// @see SymbolContextScope
265   //------------------------------------------------------------------
266   void DumpSymbolContext(Stream *s) override;
267
268   //------------------------------------------------------------------
269   /// Find a symbol in the object file's symbol table.
270   ///
271   /// @param[in] name
272   ///     The name of the symbol that we are looking for.
273   ///
274   /// @param[in] symbol_type
275   ///     If set to eSymbolTypeAny, find a symbol of any type that
276   ///     has a name that matches \a name. If set to any other valid
277   ///     SymbolType enumeration value, then search only for
278   ///     symbols that match \a symbol_type.
279   ///
280   /// @return
281   ///     Returns a valid symbol pointer if a symbol was found,
282   ///     nullptr otherwise.
283   //------------------------------------------------------------------
284   const Symbol *FindFirstSymbolWithNameAndType(
285       const ConstString &name,
286       lldb::SymbolType symbol_type = lldb::eSymbolTypeAny);
287
288   size_t FindSymbolsWithNameAndType(const ConstString &name,
289                                     lldb::SymbolType symbol_type,
290                                     SymbolContextList &sc_list);
291
292   size_t FindSymbolsMatchingRegExAndType(const RegularExpression &regex,
293                                          lldb::SymbolType symbol_type,
294                                          SymbolContextList &sc_list);
295
296   //------------------------------------------------------------------
297   /// Find a function symbols in the object file's symbol table.
298   ///
299   /// @param[in] name
300   ///     The name of the symbol that we are looking for.
301   ///
302   /// @param[in] name_type_mask
303   ///     A mask that has one or more bitwise OR'ed values from the
304   ///     lldb::FunctionNameType enumeration type that indicate what
305   ///     kind of names we are looking for.
306   ///
307   /// @param[out] sc_list
308   ///     A list to append any matching symbol contexts to.
309   ///
310   /// @return
311   ///     The number of symbol contexts that were added to \a sc_list
312   //------------------------------------------------------------------
313   size_t FindFunctionSymbols(const ConstString &name, uint32_t name_type_mask,
314                              SymbolContextList &sc_list);
315
316   //------------------------------------------------------------------
317   /// Find compile units by partial or full path.
318   ///
319   /// Finds all compile units that match \a path in all of the modules
320   /// and returns the results in \a sc_list.
321   ///
322   /// @param[in] path
323   ///     The name of the function we are looking for.
324   ///
325   /// @param[in] append
326   ///     If \b true, then append any compile units that were found
327   ///     to \a sc_list. If \b false, then the \a sc_list is cleared
328   ///     and the contents of \a sc_list are replaced.
329   ///
330   /// @param[out] sc_list
331   ///     A symbol context list that gets filled in with all of the
332   ///     matches.
333   ///
334   /// @return
335   ///     The number of matches added to \a sc_list.
336   //------------------------------------------------------------------
337   size_t FindCompileUnits(const FileSpec &path, bool append,
338                           SymbolContextList &sc_list);
339
340   //------------------------------------------------------------------
341   /// Find functions by name.
342   ///
343   /// If the function is an inlined function, it will have a block,
344   /// representing the inlined function, and the function will be the
345   /// containing function.  If it is not inlined, then the block will
346   /// be NULL.
347   ///
348   /// @param[in] name
349   ///     The name of the compile unit we are looking for.
350   ///
351   /// @param[in] namespace_decl
352   ///     If valid, a namespace to search in.
353   ///
354   /// @param[in] name_type_mask
355   ///     A bit mask of bits that indicate what kind of names should
356   ///     be used when doing the lookup. Bits include fully qualified
357   ///     names, base names, C++ methods, or ObjC selectors.
358   ///     See FunctionNameType for more details.
359   ///
360   /// @param[in] append
361   ///     If \b true, any matches will be appended to \a sc_list, else
362   ///     matches replace the contents of \a sc_list.
363   ///
364   /// @param[out] sc_list
365   ///     A symbol context list that gets filled in with all of the
366   ///     matches.
367   ///
368   /// @return
369   ///     The number of matches added to \a sc_list.
370   //------------------------------------------------------------------
371   size_t FindFunctions(const ConstString &name,
372                        const CompilerDeclContext *parent_decl_ctx,
373                        uint32_t name_type_mask, bool symbols_ok,
374                        bool inlines_ok, bool append,
375                        SymbolContextList &sc_list);
376
377   //------------------------------------------------------------------
378   /// Find functions by name.
379   ///
380   /// If the function is an inlined function, it will have a block,
381   /// representing the inlined function, and the function will be the
382   /// containing function.  If it is not inlined, then the block will
383   /// be NULL.
384   ///
385   /// @param[in] regex
386   ///     A regular expression to use when matching the name.
387   ///
388   /// @param[in] append
389   ///     If \b true, any matches will be appended to \a sc_list, else
390   ///     matches replace the contents of \a sc_list.
391   ///
392   /// @param[out] sc_list
393   ///     A symbol context list that gets filled in with all of the
394   ///     matches.
395   ///
396   /// @return
397   ///     The number of matches added to \a sc_list.
398   //------------------------------------------------------------------
399   size_t FindFunctions(const RegularExpression &regex, bool symbols_ok,
400                        bool inlines_ok, bool append,
401                        SymbolContextList &sc_list);
402
403   //------------------------------------------------------------------
404   /// Find addresses by file/line
405   ///
406   /// @param[in] target_sp
407   ///     The target the addresses are desired for.
408   ///
409   /// @param[in] file
410   ///     Source file to locate.
411   ///
412   /// @param[in] line
413   ///     Source line to locate.
414   ///
415   /// @param[in] function
416   ///       Optional filter function. Addresses within this function will be
417   ///     added to the 'local' list. All others will be added to the 'extern'
418   ///     list.
419   ///
420   /// @param[out] output_local
421   ///     All matching addresses within 'function'
422   ///
423   /// @param[out] output_extern
424   ///     All matching addresses not within 'function'
425   void FindAddressesForLine(const lldb::TargetSP target_sp,
426                             const FileSpec &file, uint32_t line,
427                             Function *function,
428                             std::vector<Address> &output_local,
429                             std::vector<Address> &output_extern);
430
431   //------------------------------------------------------------------
432   /// Find global and static variables by name.
433   ///
434   /// @param[in] name
435   ///     The name of the global or static variable we are looking
436   ///     for.
437   ///
438   /// @param[in] parent_decl_ctx
439   ///     If valid, a decl context that results must exist within
440   ///
441   /// @param[in] append
442   ///     If \b true, any matches will be appended to \a
443   ///     variable_list, else matches replace the contents of
444   ///     \a variable_list.
445   ///
446   /// @param[in] max_matches
447   ///     Allow the number of matches to be limited to \a
448   ///     max_matches. Specify UINT32_MAX to get all possible matches.
449   ///
450   /// @param[in] variable_list
451   ///     A list of variables that gets the matches appended to (if
452   ///     \a append it \b true), or replace (if \a append is \b false).
453   ///
454   /// @return
455   ///     The number of matches added to \a variable_list.
456   //------------------------------------------------------------------
457   size_t FindGlobalVariables(const ConstString &name,
458                              const CompilerDeclContext *parent_decl_ctx,
459                              bool append, size_t max_matches,
460                              VariableList &variable_list);
461
462   //------------------------------------------------------------------
463   /// Find global and static variables by regular expression.
464   ///
465   /// @param[in] regex
466   ///     A regular expression to use when matching the name.
467   ///
468   /// @param[in] append
469   ///     If \b true, any matches will be appended to \a
470   ///     variable_list, else matches replace the contents of
471   ///     \a variable_list.
472   ///
473   /// @param[in] max_matches
474   ///     Allow the number of matches to be limited to \a
475   ///     max_matches. Specify UINT32_MAX to get all possible matches.
476   ///
477   /// @param[in] variable_list
478   ///     A list of variables that gets the matches appended to (if
479   ///     \a append it \b true), or replace (if \a append is \b false).
480   ///
481   /// @return
482   ///     The number of matches added to \a variable_list.
483   //------------------------------------------------------------------
484   size_t FindGlobalVariables(const RegularExpression &regex, bool append,
485                              size_t max_matches, VariableList &variable_list);
486
487   //------------------------------------------------------------------
488   /// Find types by name.
489   ///
490   /// Type lookups in modules go through the SymbolVendor (which will
491   /// use one or more SymbolFile subclasses). The SymbolFile needs to
492   /// be able to lookup types by basename and not the fully qualified
493   /// typename. This allows the type accelerator tables to stay small,
494   /// even with heavily templatized C++. The type search will then
495   /// narrow down the search results. If "exact_match" is true, then
496   /// the type search will only match exact type name matches. If
497   /// "exact_match" is false, the type will match as long as the base
498   /// typename matches and as long as any immediate containing
499   /// namespaces/class scopes that are specified match. So to search
500   /// for a type "d" in "b::c", the name "b::c::d" can be specified
501   /// and it will match any class/namespace "b" which contains a
502   /// class/namespace "c" which contains type "d". We do this to
503   /// allow users to not always have to specify complete scoping on
504   /// all expressions, but it also allows for exact matching when
505   /// required.
506   ///
507   /// @param[in] sc
508   ///     A symbol context that scopes where to extract a type list
509   ///     from.
510   ///
511   /// @param[in] type_name
512   ///     The name of the type we are looking for that is a fully
513   ///     or partially qualified type name.
514   ///
515   /// @param[in] exact_match
516   ///     If \b true, \a type_name is fully qualified and must match
517   ///     exactly. If \b false, \a type_name is a partially qualified
518   ///     name where the leading namespaces or classes can be
519   ///     omitted to make finding types that a user may type
520   ///     easier.
521   ///
522   /// @param[out] type_list
523   ///     A type list gets populated with any matches.
524   ///
525   /// @return
526   ///     The number of matches added to \a type_list.
527   //------------------------------------------------------------------
528   size_t
529   FindTypes(const SymbolContext &sc, const ConstString &type_name,
530             bool exact_match, size_t max_matches,
531             llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
532             TypeList &types);
533
534   lldb::TypeSP FindFirstType(const SymbolContext &sc,
535                              const ConstString &type_name, bool exact_match);
536
537   //------------------------------------------------------------------
538   /// Find types by name that are in a namespace. This function is
539   /// used by the expression parser when searches need to happen in
540   /// an exact namespace scope.
541   ///
542   /// @param[in] sc
543   ///     A symbol context that scopes where to extract a type list
544   ///     from.
545   ///
546   /// @param[in] type_name
547   ///     The name of a type within a namespace that should not include
548   ///     any qualifying namespaces (just a type basename).
549   ///
550   /// @param[in] namespace_decl
551   ///     The namespace declaration that this type must exist in.
552   ///
553   /// @param[out] type_list
554   ///     A type list gets populated with any matches.
555   ///
556   /// @return
557   ///     The number of matches added to \a type_list.
558   //------------------------------------------------------------------
559   size_t FindTypesInNamespace(const SymbolContext &sc,
560                               const ConstString &type_name,
561                               const CompilerDeclContext *parent_decl_ctx,
562                               size_t max_matches, TypeList &type_list);
563
564   //------------------------------------------------------------------
565   /// Get const accessor for the module architecture.
566   ///
567   /// @return
568   ///     A const reference to the architecture object.
569   //------------------------------------------------------------------
570   const ArchSpec &GetArchitecture() const;
571
572   //------------------------------------------------------------------
573   /// Get const accessor for the module file specification.
574   ///
575   /// This function returns the file for the module on the host system
576   /// that is running LLDB. This can differ from the path on the
577   /// platform since we might be doing remote debugging.
578   ///
579   /// @return
580   ///     A const reference to the file specification object.
581   //------------------------------------------------------------------
582   const FileSpec &GetFileSpec() const { return m_file; }
583
584   //------------------------------------------------------------------
585   /// Get accessor for the module platform file specification.
586   ///
587   /// Platform file refers to the path of the module as it is known on
588   /// the remote system on which it is being debugged. For local
589   /// debugging this is always the same as Module::GetFileSpec(). But
590   /// remote debugging might mention a file "/usr/lib/liba.dylib"
591   /// which might be locally downloaded and cached. In this case the
592   /// platform file could be something like:
593   /// "/tmp/lldb/platform-cache/remote.host.computer/usr/lib/liba.dylib"
594   /// The file could also be cached in a local developer kit directory.
595   ///
596   /// @return
597   ///     A const reference to the file specification object.
598   //------------------------------------------------------------------
599   const FileSpec &GetPlatformFileSpec() const {
600     if (m_platform_file)
601       return m_platform_file;
602     return m_file;
603   }
604
605   void SetPlatformFileSpec(const FileSpec &file) { m_platform_file = file; }
606
607   const FileSpec &GetRemoteInstallFileSpec() const {
608     return m_remote_install_file;
609   }
610
611   void SetRemoteInstallFileSpec(const FileSpec &file) {
612     m_remote_install_file = file;
613   }
614
615   const FileSpec &GetSymbolFileFileSpec() const { return m_symfile_spec; }
616
617   void SetSymbolFileFileSpec(const FileSpec &file);
618
619   const llvm::sys::TimePoint<> &GetModificationTime() const {
620     return m_mod_time;
621   }
622
623   const llvm::sys::TimePoint<> &GetObjectModificationTime() const {
624     return m_object_mod_time;
625   }
626
627   void SetObjectModificationTime(const llvm::sys::TimePoint<> &mod_time) {
628     m_mod_time = mod_time;
629   }
630
631   //------------------------------------------------------------------
632   /// Tells whether this module is capable of being the main executable
633   /// for a process.
634   ///
635   /// @return
636   ///     \b true if it is, \b false otherwise.
637   //------------------------------------------------------------------
638   bool IsExecutable();
639
640   //------------------------------------------------------------------
641   /// Tells whether this module has been loaded in the target passed in.
642   /// This call doesn't distinguish between whether the module is loaded
643   /// by the dynamic loader, or by a "target module add" type call.
644   ///
645   /// @param[in] target
646   ///    The target to check whether this is loaded in.
647   ///
648   /// @return
649   ///     \b true if it is, \b false otherwise.
650   //------------------------------------------------------------------
651   bool IsLoadedInTarget(Target *target);
652
653   bool LoadScriptingResourceInTarget(Target *target, Error &error,
654                                      Stream *feedback_stream = nullptr);
655
656   //------------------------------------------------------------------
657   /// Get the number of compile units for this module.
658   ///
659   /// @return
660   ///     The number of compile units that the symbol vendor plug-in
661   ///     finds.
662   //------------------------------------------------------------------
663   size_t GetNumCompileUnits();
664
665   lldb::CompUnitSP GetCompileUnitAtIndex(size_t idx);
666
667   const ConstString &GetObjectName() const;
668
669   uint64_t GetObjectOffset() const { return m_object_offset; }
670
671   //------------------------------------------------------------------
672   /// Get the object file representation for the current architecture.
673   ///
674   /// If the object file has not been located or parsed yet, this
675   /// function will find the best ObjectFile plug-in that can parse
676   /// Module::m_file.
677   ///
678   /// @return
679   ///     If Module::m_file does not exist, or no plug-in was found
680   ///     that can parse the file, or the object file doesn't contain
681   ///     the current architecture in Module::m_arch, nullptr will be
682   ///     returned, else a valid object file interface will be
683   ///     returned. The returned pointer is owned by this object and
684   ///     remains valid as long as the object is around.
685   //------------------------------------------------------------------
686   virtual ObjectFile *GetObjectFile();
687
688   //------------------------------------------------------------------
689   /// Get the unified section list for the module. This is the section
690   /// list created by the module's object file and any debug info and
691   /// symbol files created by the symbol vendor.
692   ///
693   /// If the symbol vendor has not been loaded yet, this function
694   /// will return the section list for the object file.
695   ///
696   /// @return
697   ///     Unified module section list.
698   //------------------------------------------------------------------
699   virtual SectionList *GetSectionList();
700
701   //------------------------------------------------------------------
702   /// Notify the module that the file addresses for the Sections have
703   /// been updated.
704   ///
705   /// If the Section file addresses for a module are updated, this
706   /// method should be called.  Any parts of the module, object file,
707   /// or symbol file that has cached those file addresses must invalidate
708   /// or update its cache.
709   //------------------------------------------------------------------
710   virtual void SectionFileAddressesChanged();
711
712   uint32_t GetVersion(uint32_t *versions, uint32_t num_versions);
713
714   //------------------------------------------------------------------
715   /// Load an object file from memory.
716   ///
717   /// If available, the size of the object file in memory may be
718   /// passed to avoid additional round trips to process memory.
719   /// If the size is not provided, a default value is used. This
720   /// value should be large enough to enable the ObjectFile plugins
721   /// to read the header of the object file without going back to the
722   /// process.
723   ///
724   /// @return
725   ///     The object file loaded from memory or nullptr, if the operation
726   ///     failed (see the `error` for more information in that case).
727   //------------------------------------------------------------------
728   ObjectFile *GetMemoryObjectFile(const lldb::ProcessSP &process_sp,
729                                   lldb::addr_t header_addr, Error &error,
730                                   size_t size_to_read = 512);
731   //------------------------------------------------------------------
732   /// Get the symbol vendor interface for the current architecture.
733   ///
734   /// If the symbol vendor file has not been located yet, this
735   /// function will find the best SymbolVendor plug-in that can
736   /// use the current object file.
737   ///
738   /// @return
739   ///     If this module does not have a valid object file, or no
740   ///     plug-in can be found that can use the object file, nullptr will
741   ///     be returned, else a valid symbol vendor plug-in interface
742   ///     will be returned. The returned pointer is owned by this
743   ///     object and remains valid as long as the object is around.
744   //------------------------------------------------------------------
745   virtual SymbolVendor *
746   GetSymbolVendor(bool can_create = true,
747                   lldb_private::Stream *feedback_strm = nullptr);
748
749   //------------------------------------------------------------------
750   /// Get accessor the type list for this module.
751   ///
752   /// @return
753   ///     A valid type list pointer, or nullptr if there is no valid
754   ///     symbol vendor for this module.
755   //------------------------------------------------------------------
756   TypeList *GetTypeList();
757
758   //------------------------------------------------------------------
759   /// Get a pointer to the UUID value contained in this object.
760   ///
761   /// If the executable image file doesn't not have a UUID value built
762   /// into the file format, an MD5 checksum of the entire file, or
763   /// slice of the file for the current architecture should be used.
764   ///
765   /// @return
766   ///     A const pointer to the internal copy of the UUID value in
767   ///     this module if this module has a valid UUID value, NULL
768   ///     otherwise.
769   //------------------------------------------------------------------
770   const lldb_private::UUID &GetUUID();
771
772   //------------------------------------------------------------------
773   /// A debugging function that will cause everything in a module to
774   /// be parsed.
775   ///
776   /// All compile units will be parsed, along with all globals and
777   /// static variables and all functions for those compile units.
778   /// All types, scopes, local variables, static variables, global
779   /// variables, and line tables will be parsed. This can be used
780   /// prior to dumping a module to see a complete list of the
781   /// resulting debug information that gets parsed, or as a debug
782   /// function to ensure that the module can consume all of the
783   /// debug data the symbol vendor provides.
784   //------------------------------------------------------------------
785   void ParseAllDebugSymbols();
786
787   bool ResolveFileAddress(lldb::addr_t vm_addr, Address &so_addr);
788
789   //------------------------------------------------------------------
790   /// Resolve the symbol context for the given address.
791   ///
792   /// Tries to resolve the matching symbol context based on a lookup
793   /// from the current symbol vendor.  If the lazy lookup fails,
794   /// an attempt is made to parse the eh_frame section to handle
795   /// stripped symbols.  If this fails, an attempt is made to resolve
796   /// the symbol to the previous address to handle the case of a
797   /// function with a tail call.
798   ///
799   /// Use properties of the modified SymbolContext to inspect any
800   /// resolved target, module, compilation unit, symbol, function,
801   /// function block or line entry.  Use the return value to determine
802   /// which of these properties have been modified.
803   ///
804   /// @param[in] so_addr
805   ///     A load address to resolve.
806   ///
807   /// @param[in] resolve_scope
808   ///     The scope that should be resolved (see SymbolContext::Scope).
809   ///     A combination of flags from the enumeration SymbolContextItem
810   ///     requesting a resolution depth.  Note that the flags that are
811   ///     actually resolved may be a superset of the requested flags.
812   ///     For instance, eSymbolContextSymbol requires resolution of
813   ///     eSymbolContextModule, and eSymbolContextFunction requires
814   ///     eSymbolContextSymbol.
815   ///
816   /// @param[out] sc
817   ///     The SymbolContext that is modified based on symbol resolution.
818   ///
819   /// @param[in] resolve_tail_call_address
820   ///     Determines if so_addr should resolve to a symbol in the case
821   ///     of a function whose last instruction is a call.  In this case,
822   ///     the PC can be one past the address range of the function.
823   ///
824   /// @return
825   ///     The scope that has been resolved (see SymbolContext::Scope).
826   ///
827   /// @see SymbolContext::Scope
828   //------------------------------------------------------------------
829   uint32_t
830   ResolveSymbolContextForAddress(const Address &so_addr, uint32_t resolve_scope,
831                                  SymbolContext &sc,
832                                  bool resolve_tail_call_address = false);
833
834   //------------------------------------------------------------------
835   /// Resolve items in the symbol context for a given file and line.
836   ///
837   /// Tries to resolve \a file_path and \a line to a list of matching
838   /// symbol contexts.
839   ///
840   /// The line table entries contains addresses that can be used to
841   /// further resolve the values in each match: the function, block,
842   /// symbol. Care should be taken to minimize the amount of
843   /// information that is requested to only what is needed --
844   /// typically the module, compile unit, line table and line table
845   /// entry are sufficient.
846   ///
847   /// @param[in] file_path
848   ///     A path to a source file to match. If \a file_path does not
849   ///     specify a directory, then this query will match all files
850   ///     whose base filename matches. If \a file_path does specify
851   ///     a directory, the fullpath to the file must match.
852   ///
853   /// @param[in] line
854   ///     The source line to match, or zero if just the compile unit
855   ///     should be resolved.
856   ///
857   /// @param[in] check_inlines
858   ///     Check for inline file and line number matches. This option
859   ///     should be used sparingly as it will cause all line tables
860   ///     for every compile unit to be parsed and searched for
861   ///     matching inline file entries.
862   ///
863   /// @param[in] resolve_scope
864   ///     The scope that should be resolved (see
865   ///     SymbolContext::Scope).
866   ///
867   /// @param[out] sc_list
868   ///     A symbol context list that gets matching symbols contexts
869   ///     appended to.
870   ///
871   /// @return
872   ///     The number of matches that were added to \a sc_list.
873   ///
874   /// @see SymbolContext::Scope
875   //------------------------------------------------------------------
876   uint32_t ResolveSymbolContextForFilePath(const char *file_path, uint32_t line,
877                                            bool check_inlines,
878                                            uint32_t resolve_scope,
879                                            SymbolContextList &sc_list);
880
881   //------------------------------------------------------------------
882   /// Resolve items in the symbol context for a given file and line.
883   ///
884   /// Tries to resolve \a file_spec and \a line to a list of matching
885   /// symbol contexts.
886   ///
887   /// The line table entries contains addresses that can be used to
888   /// further resolve the values in each match: the function, block,
889   /// symbol. Care should be taken to minimize the amount of
890   /// information that is requested to only what is needed --
891   /// typically the module, compile unit, line table and line table
892   /// entry are sufficient.
893   ///
894   /// @param[in] file_spec
895   ///     A file spec to a source file to match. If \a file_path does
896   ///     not specify a directory, then this query will match all
897   ///     files whose base filename matches. If \a file_path does
898   ///     specify a directory, the fullpath to the file must match.
899   ///
900   /// @param[in] line
901   ///     The source line to match, or zero if just the compile unit
902   ///     should be resolved.
903   ///
904   /// @param[in] check_inlines
905   ///     Check for inline file and line number matches. This option
906   ///     should be used sparingly as it will cause all line tables
907   ///     for every compile unit to be parsed and searched for
908   ///     matching inline file entries.
909   ///
910   /// @param[in] resolve_scope
911   ///     The scope that should be resolved (see
912   ///     SymbolContext::Scope).
913   ///
914   /// @param[out] sc_list
915   ///     A symbol context list that gets filled in with all of the
916   ///     matches.
917   ///
918   /// @return
919   ///     A integer that contains SymbolContext::Scope bits set for
920   ///     each item that was successfully resolved.
921   ///
922   /// @see SymbolContext::Scope
923   //------------------------------------------------------------------
924   uint32_t ResolveSymbolContextsForFileSpec(const FileSpec &file_spec,
925                                             uint32_t line, bool check_inlines,
926                                             uint32_t resolve_scope,
927                                             SymbolContextList &sc_list);
928
929   void SetFileSpecAndObjectName(const FileSpec &file,
930                                 const ConstString &object_name);
931
932   bool GetIsDynamicLinkEditor();
933
934   TypeSystem *GetTypeSystemForLanguage(lldb::LanguageType language);
935
936   // Special error functions that can do printf style formatting that will
937   // prepend the message with
938   // something appropriate for this module (like the architecture, path and
939   // object name (if any)).
940   // This centralizes code so that everyone doesn't need to format their error
941   // and log messages on
942   // their own and keeps the output a bit more consistent.
943   void LogMessage(Log *log, const char *format, ...)
944       __attribute__((format(printf, 3, 4)));
945
946   void LogMessageVerboseBacktrace(Log *log, const char *format, ...)
947       __attribute__((format(printf, 3, 4)));
948
949   void ReportWarning(const char *format, ...)
950       __attribute__((format(printf, 2, 3)));
951
952   void ReportError(const char *format, ...)
953       __attribute__((format(printf, 2, 3)));
954
955   // Only report an error once when the module is first detected to be modified
956   // so we don't spam the console with many messages.
957   void ReportErrorIfModifyDetected(const char *format, ...)
958       __attribute__((format(printf, 2, 3)));
959
960   //------------------------------------------------------------------
961   // Return true if the file backing this module has changed since the
962   // module was originally created  since we saved the initial file
963   // modification time when the module first gets created.
964   //------------------------------------------------------------------
965   bool FileHasChanged() const;
966
967   //------------------------------------------------------------------
968   // SymbolVendor, SymbolFile and ObjectFile member objects should
969   // lock the module mutex to avoid deadlocks.
970   //------------------------------------------------------------------
971   std::recursive_mutex &GetMutex() const { return m_mutex; }
972
973   PathMappingList &GetSourceMappingList() { return m_source_mappings; }
974
975   const PathMappingList &GetSourceMappingList() const {
976     return m_source_mappings;
977   }
978
979   //------------------------------------------------------------------
980   /// Finds a source file given a file spec using the module source
981   /// path remappings (if any).
982   ///
983   /// Tries to resolve \a orig_spec by checking the module source path
984   /// remappings. It makes sure the file exists, so this call can be
985   /// expensive if the remappings are on a network file system, so
986   /// use this function sparingly (not in a tight debug info parsing
987   /// loop).
988   ///
989   /// @param[in] orig_spec
990   ///     The original source file path to try and remap.
991   ///
992   /// @param[out] new_spec
993   ///     The newly remapped filespec that is guaranteed to exist.
994   ///
995   /// @return
996   ///     /b true if \a orig_spec was successfully located and
997   ///     \a new_spec is filled in with an existing file spec,
998   ///     \b false otherwise.
999   //------------------------------------------------------------------
1000   bool FindSourceFile(const FileSpec &orig_spec, FileSpec &new_spec) const;
1001
1002   //------------------------------------------------------------------
1003   /// Remaps a source file given \a path into \a new_path.
1004   ///
1005   /// Remaps \a path if any source remappings match. This function
1006   /// does NOT stat the file system so it can be used in tight loops
1007   /// where debug info is being parsed.
1008   ///
1009   /// @param[in] path
1010   ///     The original source file path to try and remap.
1011   ///
1012   /// @param[out] new_path
1013   ///     The newly remapped filespec that is may or may not exist.
1014   ///
1015   /// @return
1016   ///     /b true if \a path was successfully located and \a new_path
1017   ///     is filled in with a new source path, \b false otherwise.
1018   //------------------------------------------------------------------
1019   bool RemapSourceFile(llvm::StringRef path, std::string &new_path) const;
1020   bool RemapSourceFile(const char *, std::string &) const = delete;
1021
1022   //------------------------------------------------------------------
1023   /// Loads this module to memory.
1024   ///
1025   /// Loads the bits needed to create an executable image to the memory.
1026   /// It is useful with bare-metal targets where target does not have the
1027   /// ability to start a process itself.
1028   ///
1029   /// @param[in] target
1030   ///     Target where to load the module.
1031   ///
1032   /// @return
1033   //------------------------------------------------------------------
1034   Error LoadInMemory(Target &target, bool set_pc);
1035
1036   //----------------------------------------------------------------------
1037   /// @class LookupInfo Module.h "lldb/Core/Module.h"
1038   /// @brief A class that encapsulates name lookup information.
1039   ///
1040   /// Users can type a wide variety of partial names when setting
1041   /// breakpoints by name or when looking for functions by name.
1042   /// SymbolVendor and SymbolFile objects are only required to implement
1043   /// name lookup for function basenames and for fully mangled names.
1044   /// This means if the user types in a partial name, we must reduce this
1045   /// to a name lookup that will work with all SymbolFile objects. So we
1046   /// might reduce a name lookup to look for a basename, and then prune
1047   /// out any results that don't match.
1048   ///
1049   /// The "m_name" member variable represents the name as it was typed
1050   /// by the user. "m_lookup_name" will be the name we actually search
1051   /// for through the symbol or objects files. Lanaguage is included in
1052   /// case we need to filter results by language at a later date. The
1053   /// "m_name_type_mask" member variable tells us what kinds of names we
1054   /// are looking for and can help us prune out unwanted results.
1055   ///
1056   /// Function lookups are done in Module.cpp, ModuleList.cpp and in
1057   /// BreakpointResolverName.cpp and they all now use this class to do
1058   /// lookups correctly.
1059   //----------------------------------------------------------------------
1060   class LookupInfo {
1061   public:
1062     LookupInfo()
1063         : m_name(), m_lookup_name(), m_language(lldb::eLanguageTypeUnknown),
1064           m_name_type_mask(0), m_match_name_after_lookup(false) {}
1065
1066     LookupInfo(const ConstString &name, uint32_t name_type_mask,
1067                lldb::LanguageType language);
1068
1069     const ConstString &GetName() const { return m_name; }
1070
1071     void SetName(const ConstString &name) { m_name = name; }
1072
1073     const ConstString &GetLookupName() const { return m_lookup_name; }
1074
1075     void SetLookupName(const ConstString &name) { m_lookup_name = name; }
1076
1077     uint32_t GetNameTypeMask() const { return m_name_type_mask; }
1078
1079     void SetNameTypeMask(uint32_t mask) { m_name_type_mask = mask; }
1080
1081     void Prune(SymbolContextList &sc_list, size_t start_idx) const;
1082
1083   protected:
1084     ConstString m_name;        ///< What the user originally typed
1085     ConstString m_lookup_name; ///< The actual name will lookup when calling in
1086                                ///the object or symbol file
1087     lldb::LanguageType
1088         m_language;            ///< Limit matches to only be for this language
1089     uint32_t m_name_type_mask; ///< One or more bits from lldb::FunctionNameType
1090                                ///that indicate what kind of names we are
1091                                ///looking for
1092     bool m_match_name_after_lookup; ///< If \b true, then demangled names that
1093                                     ///match will need to contain "m_name" in
1094                                     ///order to be considered a match
1095   };
1096
1097 protected:
1098   //------------------------------------------------------------------
1099   // Member Variables
1100   //------------------------------------------------------------------
1101   mutable std::recursive_mutex m_mutex; ///< A mutex to keep this object happy
1102                                         ///in multi-threaded environments.
1103
1104   /// The modification time for this module when it was created.
1105   llvm::sys::TimePoint<> m_mod_time;
1106
1107   ArchSpec m_arch;      ///< The architecture for this module.
1108   UUID m_uuid; ///< Each module is assumed to have a unique identifier to help
1109                ///match it up to debug symbols.
1110   FileSpec m_file; ///< The file representation on disk for this module (if
1111                    ///there is one).
1112   FileSpec m_platform_file; ///< The path to the module on the platform on which
1113                             ///it is being debugged
1114   FileSpec m_remote_install_file; ///< If set when debugging on remote
1115                                   ///platforms, this module will be installed at
1116                                   ///this location
1117   FileSpec m_symfile_spec;   ///< If this path is valid, then this is the file
1118                              ///that _will_ be used as the symbol file for this
1119                              ///module
1120   ConstString m_object_name; ///< The name an object within this module that is
1121                              ///selected, or empty of the module is represented
1122                              ///by \a m_file.
1123   uint64_t m_object_offset;
1124   llvm::sys::TimePoint<> m_object_mod_time;
1125   lldb::ObjectFileSP m_objfile_sp; ///< A shared pointer to the object file
1126                                    ///parser for this module as it may or may
1127                                    ///not be shared with the SymbolFile
1128   lldb::SymbolVendorUP
1129       m_symfile_ap; ///< A pointer to the symbol vendor for this module.
1130   std::vector<lldb::SymbolVendorUP>
1131       m_old_symfiles; ///< If anyone calls Module::SetSymbolFileFileSpec() and
1132                       ///changes the symbol file,
1133   ///< we need to keep all old symbol files around in case anyone has type
1134   ///references to them
1135   TypeSystemMap m_type_system_map;   ///< A map of any type systems associated
1136                                      ///with this module
1137   PathMappingList m_source_mappings; ///< Module specific source remappings for
1138                                      ///when you have debug info for a module
1139                                      ///that doesn't match where the sources
1140                                      ///currently are
1141   lldb::SectionListUP m_sections_ap; ///< Unified section list for module that
1142                                      ///is used by the ObjectFile and and
1143                                      ///ObjectFile instances for the debug info
1144
1145   std::atomic<bool> m_did_load_objfile{false};
1146   std::atomic<bool> m_did_load_symbol_vendor{false};
1147   std::atomic<bool> m_did_parse_uuid{false};
1148   mutable bool m_file_has_changed : 1,
1149       m_first_file_changed_log : 1; /// See if the module was modified after it
1150                                     /// was initially opened.
1151
1152   //------------------------------------------------------------------
1153   /// Resolve a file or load virtual address.
1154   ///
1155   /// Tries to resolve \a vm_addr as a file address (if \a
1156   /// vm_addr_is_file_addr is true) or as a load address if \a
1157   /// vm_addr_is_file_addr is false) in the symbol vendor.
1158   /// \a resolve_scope indicates what clients wish to resolve
1159   /// and can be used to limit the scope of what is parsed.
1160   ///
1161   /// @param[in] vm_addr
1162   ///     The load virtual address to resolve.
1163   ///
1164   /// @param[in] vm_addr_is_file_addr
1165   ///     If \b true, \a vm_addr is a file address, else \a vm_addr
1166   ///     if a load address.
1167   ///
1168   /// @param[in] resolve_scope
1169   ///     The scope that should be resolved (see
1170   ///     SymbolContext::Scope).
1171   ///
1172   /// @param[out] so_addr
1173   ///     The section offset based address that got resolved if
1174   ///     any bits are returned.
1175   ///
1176   /// @param[out] sc
1177   //      The symbol context that has objects filled in. Each bit
1178   ///     in the \a resolve_scope pertains to a member in the \a sc.
1179   ///
1180   /// @return
1181   ///     A integer that contains SymbolContext::Scope bits set for
1182   ///     each item that was successfully resolved.
1183   ///
1184   /// @see SymbolContext::Scope
1185   //------------------------------------------------------------------
1186   uint32_t ResolveSymbolContextForAddress(lldb::addr_t vm_addr,
1187                                           bool vm_addr_is_file_addr,
1188                                           uint32_t resolve_scope,
1189                                           Address &so_addr, SymbolContext &sc);
1190
1191   void SymbolIndicesToSymbolContextList(Symtab *symtab,
1192                                         std::vector<uint32_t> &symbol_indexes,
1193                                         SymbolContextList &sc_list);
1194
1195   bool SetArchitecture(const ArchSpec &new_arch);
1196
1197   SectionList *GetUnifiedSectionList();
1198
1199   friend class ModuleList;
1200   friend class ObjectFile;
1201   friend class SymbolFile;
1202
1203 private:
1204   Module(); // Only used internally by CreateJITModule ()
1205
1206   size_t FindTypes_Impl(
1207       const SymbolContext &sc, const ConstString &name,
1208       const CompilerDeclContext *parent_decl_ctx, bool append,
1209       size_t max_matches,
1210       llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
1211       TypeMap &types);
1212
1213   DISALLOW_COPY_AND_ASSIGN(Module);
1214 };
1215
1216 } // namespace lldb_private
1217
1218 #endif // liblldb_Module_h_