ModuleList.h revision 0b2f5cc32d01cf48d6216a232ec87afab1466315
1//===-- ModuleList.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_ModuleList_h_
11#define liblldb_ModuleList_h_
12
13#include <vector>
14
15#include "lldb/lldb-private.h"
16#include "lldb/Host/Mutex.h"
17
18namespace lldb_private {
19
20//----------------------------------------------------------------------
21/// @class ModuleList ModuleList.h "lldb/Core/ModuleList.h"
22/// @brief A collection class for Module objects.
23///
24/// Modules in the module collection class are stored as reference
25/// counted shared pointers to Module objects.
26//----------------------------------------------------------------------
27class ModuleList
28{
29public:
30
31    class Notifier
32    {
33    public:
34        virtual void
35        ModuleAdded (const ModuleList& module_list, const lldb::ModuleSP& module_sp) = 0;
36        virtual void
37        ModuleRemoved (const ModuleList& module_list, const lldb::ModuleSP& module_sp) = 0;
38        virtual void
39        ModuleUpdated (const ModuleList& module_list, const lldb::ModuleSP& old_module_sp,
40                       const lldb::ModuleSP& new_module_sp) = 0;
41        virtual void
42        WillClearList (const ModuleList& module_list) = 0;
43
44        virtual
45        ~Notifier ()
46        {}
47    };
48
49    //------------------------------------------------------------------
50    /// Default constructor.
51    ///
52    /// Creates an empty list of Module objects.
53    //------------------------------------------------------------------
54    ModuleList ();
55
56    //------------------------------------------------------------------
57    /// Copy Constructor.
58    ///
59    /// Creates a new module list object with a copy of the modules from
60    /// \a rhs.
61    ///
62    /// @param[in] rhs
63    ///     Another module list object.
64    //------------------------------------------------------------------
65    ModuleList (const ModuleList& rhs);
66
67    ModuleList (ModuleList::Notifier* notifier);
68
69    //------------------------------------------------------------------
70    /// Destructor.
71    //------------------------------------------------------------------
72    ~ModuleList ();
73
74    //------------------------------------------------------------------
75    /// Assignment operator.
76    ///
77    /// Copies the module list from \a rhs into this list.
78    ///
79    /// @param[in] rhs
80    ///     Another module list object.
81    ///
82    /// @return
83    ///     A const reference to this object.
84    //------------------------------------------------------------------
85    const ModuleList&
86    operator= (const ModuleList& rhs);
87
88    //------------------------------------------------------------------
89    /// Append a module to the module list.
90    ///
91    /// Appends the module to the collection.
92    ///
93    /// @param[in] module_sp
94    ///     A shared pointer to a module to add to this collection.
95    //------------------------------------------------------------------
96    void
97    Append (const lldb::ModuleSP &module_sp);
98
99    //------------------------------------------------------------------
100    /// Append a module to the module list and remove any equivalent
101    /// modules. Equivalent modules are ones whose file, platform file
102    /// and architecture matches.
103    ///
104    /// Replaces the module to the collection.
105    ///
106    /// @param[in] module_sp
107    ///     A shared pointer to a module to replace in this collection.
108    //------------------------------------------------------------------
109    void
110    ReplaceEquivalent (const lldb::ModuleSP &module_sp);
111
112    bool
113    AppendIfNeeded (const lldb::ModuleSP &module_sp);
114
115    void
116    Append (const ModuleList& module_list);
117
118    bool
119    AppendIfNeeded (const ModuleList& module_list);
120
121    bool
122    ReplaceModule (const lldb::ModuleSP &old_module_sp, const lldb::ModuleSP &new_module_sp);
123
124    //------------------------------------------------------------------
125    /// Clear the object's state.
126    ///
127    /// Clears the list of modules and releases a reference to each
128    /// module object and if the reference count goes to zero, the
129    /// module will be deleted.
130    //------------------------------------------------------------------
131    void
132    Clear ();
133
134    //------------------------------------------------------------------
135    /// Clear the object's state.
136    ///
137    /// Clears the list of modules and releases a reference to each
138    /// module object and if the reference count goes to zero, the
139    /// module will be deleted. Also relese all memory that might be
140    /// held by any collection classes (like std::vector)
141    //------------------------------------------------------------------
142    void
143    Destroy();
144    //------------------------------------------------------------------
145    /// Dump the description of each module contained in this list.
146    ///
147    /// Dump the description of each module contained in this list to
148    /// the supplied stream \a s.
149    ///
150    /// @param[in] s
151    ///     The stream to which to dump the object descripton.
152    ///
153    /// @see Module::Dump(Stream *) const
154    //------------------------------------------------------------------
155    void
156    Dump (Stream *s) const;
157
158    void
159    LogUUIDAndPaths (lldb::LogSP &log_sp,
160                     const char *prefix_cstr);
161
162    Mutex &
163    GetMutex () const
164    {
165        return m_modules_mutex;
166    }
167
168    uint32_t
169    GetIndexForModule (const Module *module) const;
170
171    //------------------------------------------------------------------
172    /// Get the module shared pointer for the module at index \a idx.
173    ///
174    /// @param[in] idx
175    ///     An index into this module collection.
176    ///
177    /// @return
178    ///     A shared pointer to a Module which can contain NULL if
179    ///     \a idx is out of range.
180    ///
181    /// @see ModuleList::GetSize()
182    //------------------------------------------------------------------
183    lldb::ModuleSP
184    GetModuleAtIndex (uint32_t idx) const;
185
186    //------------------------------------------------------------------
187    /// Get the module shared pointer for the module at index \a idx without
188    /// acquiring the ModuleList mutex.  This MUST already have been
189    /// acquired with ModuleList::GetMutex and locked for this call to be safe.
190    ///
191    /// @param[in] idx
192    ///     An index into this module collection.
193    ///
194    /// @return
195    ///     A shared pointer to a Module which can contain NULL if
196    ///     \a idx is out of range.
197    ///
198    /// @see ModuleList::GetSize()
199    //------------------------------------------------------------------
200    lldb::ModuleSP
201    GetModuleAtIndexUnlocked (uint32_t idx) const;
202
203    //------------------------------------------------------------------
204    /// Get the module pointer for the module at index \a idx.
205    ///
206    /// @param[in] idx
207    ///     An index into this module collection.
208    ///
209    /// @return
210    ///     A pointer to a Module which can by NULL if \a idx is out
211    ///     of range.
212    ///
213    /// @see ModuleList::GetSize()
214    //------------------------------------------------------------------
215    Module*
216    GetModulePointerAtIndex (uint32_t idx) const;
217
218    //------------------------------------------------------------------
219    /// Get the module pointer for the module at index \a idx without
220    /// acquiring the ModuleList mutex.  This MUST already have been
221    /// acquired with ModuleList::GetMutex and locked for this call to be safe.
222    ///
223    /// @param[in] idx
224    ///     An index into this module collection.
225    ///
226    /// @return
227    ///     A pointer to a Module which can by NULL if \a idx is out
228    ///     of range.
229    ///
230    /// @see ModuleList::GetSize()
231    //------------------------------------------------------------------
232    Module*
233    GetModulePointerAtIndexUnlocked (uint32_t idx) const;
234
235    //------------------------------------------------------------------
236    /// Find compile units by partial or full path.
237    ///
238    /// Finds all compile units that match \a path in all of the modules
239    /// and returns the results in \a sc_list.
240    ///
241    /// @param[in] path
242    ///     The name of the compile unit we are looking for.
243    ///
244    /// @param[in] append
245    ///     If \b true, then append any compile units that were found
246    ///     to \a sc_list. If \b false, then the \a sc_list is cleared
247    ///     and the contents of \a sc_list are replaced.
248    ///
249    /// @param[out] sc_list
250    ///     A symbol context list that gets filled in with all of the
251    ///     matches.
252    ///
253    /// @return
254    ///     The number of matches added to \a sc_list.
255    //------------------------------------------------------------------
256    uint32_t
257    FindCompileUnits (const FileSpec &path,
258                      bool append,
259                      SymbolContextList &sc_list) const;
260
261    //------------------------------------------------------------------
262    /// @see Module::FindFunctions ()
263    //------------------------------------------------------------------
264    uint32_t
265    FindFunctions (const ConstString &name,
266                   uint32_t name_type_mask,
267                   bool include_symbols,
268                   bool include_inlines,
269                   bool append,
270                   SymbolContextList &sc_list) const;
271
272    //------------------------------------------------------------------
273    /// Find global and static variables by name.
274    ///
275    /// @param[in] name
276    ///     The name of the global or static variable we are looking
277    ///     for.
278    ///
279    /// @param[in] append
280    ///     If \b true, any matches will be appended to \a
281    ///     variable_list, else matches replace the contents of
282    ///     \a variable_list.
283    ///
284    /// @param[in] max_matches
285    ///     Allow the number of matches to be limited to \a
286    ///     max_matches. Specify UINT32_MAX to get all possible matches.
287    ///
288    /// @param[in] variable_list
289    ///     A list of variables that gets the matches appended to (if
290    ///     \a append it \b true), or replace (if \a append is \b false).
291    ///
292    /// @return
293    ///     The number of matches added to \a variable_list.
294    //------------------------------------------------------------------
295    uint32_t
296    FindGlobalVariables (const ConstString &name,
297                         bool append,
298                         uint32_t max_matches,
299                         VariableList& variable_list) const;
300
301    //------------------------------------------------------------------
302    /// Find global and static variables by regular exression.
303    ///
304    /// @param[in] regex
305    ///     A regular expression to use when matching the name.
306    ///
307    /// @param[in] append
308    ///     If \b true, any matches will be appended to \a
309    ///     variable_list, else matches replace the contents of
310    ///     \a variable_list.
311    ///
312    /// @param[in] max_matches
313    ///     Allow the number of matches to be limited to \a
314    ///     max_matches. Specify UINT32_MAX to get all possible matches.
315    ///
316    /// @param[in] variable_list
317    ///     A list of variables that gets the matches appended to (if
318    ///     \a append it \b true), or replace (if \a append is \b false).
319    ///
320    /// @return
321    ///     The number of matches added to \a variable_list.
322    //------------------------------------------------------------------
323    uint32_t
324    FindGlobalVariables (const RegularExpression& regex,
325                         bool append,
326                         uint32_t max_matches,
327                         VariableList& variable_list) const;
328
329    //------------------------------------------------------------------
330    /// Finds the first module whose file specification matches \a
331    /// file_spec.
332    ///
333    /// @param[in] file_spec_ptr
334    ///     A file specification object to match against the Module's
335    ///     file specifications. If \a file_spec does not have
336    ///     directory information, matches will occur by matching only
337    ///     the basename of any modules in this list. If this value is
338    ///     NULL, then file specifications won't be compared when
339    ///     searching for matching modules.
340    ///
341    /// @param[in] arch_ptr
342    ///     The architecture to search for if non-NULL. If this value
343    ///     is NULL no architecture matching will be performed.
344    ///
345    /// @param[in] uuid_ptr
346    ///     The uuid to search for if non-NULL. If this value is NULL
347    ///     no uuid matching will be performed.
348    ///
349    /// @param[in] object_name
350    ///     An optional object name that must match as well. This value
351    ///     can be NULL.
352    ///
353    /// @param[out] matching_module_list
354    ///     A module list that gets filled in with any modules that
355    ///     match the search criteria.
356    ///
357    /// @return
358    ///     The number of matching modules found by the search.
359    //------------------------------------------------------------------
360    size_t
361    FindModules (const ModuleSpec &module_spec,
362                 ModuleList& matching_module_list) const;
363
364    lldb::ModuleSP
365    FindModule (const Module *module_ptr) const;
366
367    //------------------------------------------------------------------
368    // Find a module by UUID
369    //
370    // The UUID value for a module is extracted from the ObjectFile and
371    // is the MD5 checksum, or a smarter object file equivalent, so
372    // finding modules by UUID values is very efficient and accurate.
373    //------------------------------------------------------------------
374    lldb::ModuleSP
375    FindModule (const UUID &uuid) const;
376
377    lldb::ModuleSP
378    FindFirstModule (const ModuleSpec &module_spec) const;
379
380    size_t
381    FindSymbolsWithNameAndType (const ConstString &name,
382                                lldb::SymbolType symbol_type,
383                                SymbolContextList &sc_list,
384                                bool append = false) const;
385
386    size_t
387    FindSymbolsMatchingRegExAndType (const RegularExpression &regex,
388                                     lldb::SymbolType symbol_type,
389                                     SymbolContextList &sc_list,
390                                     bool append = false) const;
391
392    //------------------------------------------------------------------
393    /// Find types by name.
394    ///
395    /// @param[in] sc
396    ///     A symbol context that scopes where to extract a type list
397    ///     from.
398    ///
399    /// @param[in] name
400    ///     The name of the type we are looking for.
401    ///
402    /// @param[in] append
403    ///     If \b true, any matches will be appended to \a
404    ///     variable_list, else matches replace the contents of
405    ///     \a variable_list.
406    ///
407    /// @param[in] max_matches
408    ///     Allow the number of matches to be limited to \a
409    ///     max_matches. Specify UINT32_MAX to get all possible matches.
410    ///
411    /// @param[in] encoding
412    ///     Limit the search to specific types, or get all types if
413    ///     set to Type::invalid.
414    ///
415    /// @param[in] udt_name
416    ///     If the encoding is a user defined type, specify the name
417    ///     of the user defined type ("struct", "union", "class", etc).
418    ///
419    /// @param[out] type_list
420    ///     A type list gets populated with any matches.
421    ///
422    /// @return
423    ///     The number of matches added to \a type_list.
424    //------------------------------------------------------------------
425    uint32_t
426    FindTypes (const SymbolContext& sc,
427               const ConstString &name,
428               bool name_is_fully_qualified,
429               uint32_t max_matches,
430               TypeList& types) const;
431
432    bool
433    FindSourceFile (const FileSpec &orig_spec, FileSpec &new_spec) const;
434
435    bool
436    Remove (const lldb::ModuleSP &module_sp);
437
438    size_t
439    Remove (ModuleList &module_list);
440
441    bool
442    RemoveIfOrphaned (const Module *module_ptr);
443
444    size_t
445    RemoveOrphans (bool mandatory);
446
447    bool
448    ResolveFileAddress (lldb::addr_t vm_addr,
449                        Address& so_addr) const;
450
451    //------------------------------------------------------------------
452    /// @copydoc Module::ResolveSymbolContextForAddress (const Address &,uint32_t,SymbolContext&)
453    //------------------------------------------------------------------
454    uint32_t
455    ResolveSymbolContextForAddress (const Address& so_addr,
456                                    uint32_t resolve_scope,
457                                    SymbolContext& sc) const;
458
459    //------------------------------------------------------------------
460    /// @copydoc Module::ResolveSymbolContextForFilePath (const char *,uint32_t,bool,uint32_t,SymbolContextList&)
461    //------------------------------------------------------------------
462    uint32_t
463    ResolveSymbolContextForFilePath (const char *file_path,
464                                     uint32_t line,
465                                     bool check_inlines,
466                                     uint32_t resolve_scope,
467                                     SymbolContextList& sc_list) const;
468
469    //------------------------------------------------------------------
470    /// @copydoc Module::ResolveSymbolContextsForFileSpec (const FileSpec &,uint32_t,bool,uint32_t,SymbolContextList&)
471    //------------------------------------------------------------------
472    uint32_t
473    ResolveSymbolContextsForFileSpec (const FileSpec &file_spec,
474                                     uint32_t line,
475                                     bool check_inlines,
476                                     uint32_t resolve_scope,
477                                     SymbolContextList& sc_list) const;
478
479    //------------------------------------------------------------------
480    /// Gets the size of the module list.
481    ///
482    /// @return
483    ///     The number of modules in the module list.
484    //------------------------------------------------------------------
485    size_t
486    GetSize () const;
487
488    static bool
489    ModuleIsInCache (const Module *module_ptr);
490
491    static Error
492    GetSharedModule (const ModuleSpec &module_spec,
493                     lldb::ModuleSP &module_sp,
494                     const FileSpecList *module_search_paths_ptr,
495                     lldb::ModuleSP *old_module_sp_ptr,
496                     bool *did_create_ptr,
497                     bool always_create = false);
498
499    static bool
500    RemoveSharedModule (lldb::ModuleSP &module_sp);
501
502    static size_t
503    FindSharedModules (const ModuleSpec &module_spec,
504                       ModuleList &matching_module_list);
505
506    static uint32_t
507    RemoveOrphanSharedModules (bool mandatory);
508
509    static bool
510    RemoveSharedModuleIfOrphaned (const Module *module_ptr);
511
512protected:
513    //------------------------------------------------------------------
514    // Class typedefs.
515    //------------------------------------------------------------------
516    typedef std::vector<lldb::ModuleSP> collection; ///< The module collection type.
517
518    void
519    AppendImpl (const lldb::ModuleSP &module_sp, bool use_notifier = true);
520
521    bool
522    RemoveImpl (const lldb::ModuleSP &module_sp, bool use_notifier = true);
523
524    collection::iterator
525    RemoveImpl (collection::iterator pos, bool use_notifier = true);
526
527    void
528    ClearImpl (bool use_notifier = true);
529
530    //------------------------------------------------------------------
531    // Member variables.
532    //------------------------------------------------------------------
533    collection m_modules; ///< The collection of modules.
534    mutable Mutex m_modules_mutex;
535
536    Notifier* m_notifier;
537
538};
539
540} // namespace lldb_private
541
542#endif  // liblldb_ModuleList_h_
543