ModuleList.h revision 952e9dc874944fcdbbb224f3ec4fc2c859376f64
15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//===-- ModuleList.h --------------------------------------------*- C++ -*-===//
25c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//
35c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//                     The LLVM Compiler Infrastructure
45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//
55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// This file is distributed under the University of Illinois Open Source
65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// License. See LICENSE.TXT for details.
75c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//
85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//===----------------------------------------------------------------------===//
95c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#ifndef liblldb_ModuleList_h_
115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#define liblldb_ModuleList_h_
125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include <vector>
145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "lldb/lldb-private.h"
165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "lldb/Host/Mutex.h"
175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)namespace lldb_private {
195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//----------------------------------------------------------------------
215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/// @class ModuleList ModuleList.h "lldb/Core/ModuleList.h"
225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/// @brief A collection class for Module objects.
2302772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch///
245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/// Modules in the module collection class are stored as reference
255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/// counted shared pointers to Module objects.
265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//----------------------------------------------------------------------
275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)class ModuleList
285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
2906f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)public:
3006f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)
3153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    class Notifier
32e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    {
33e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    public:
34e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch        virtual void
355c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        ModuleAdded (const ModuleList& module_list, const lldb::ModuleSP& module_sp) = 0;
365c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        virtual void
375c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        ModuleRemoved (const ModuleList& module_list, const lldb::ModuleSP& module_sp) = 0;
385c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        virtual void
395c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        ModuleUpdated (const ModuleList& module_list, const lldb::ModuleSP& old_module_sp,
405c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)                       const lldb::ModuleSP& new_module_sp) = 0;
415c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        virtual void
425c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        WillClearList (const ModuleList& module_list) = 0;
435c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
445c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        virtual
455c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        ~Notifier ()
465c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        {}
475c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    };
485c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
495c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    //------------------------------------------------------------------
505c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    /// Default constructor.
515c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    ///
525c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    /// Creates an empty list of Module objects.
535c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    //------------------------------------------------------------------
545c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    ModuleList ();
555c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
565c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    //------------------------------------------------------------------
575c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    /// Copy Constructor.
585c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    ///
595c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    /// Creates a new module list object with a copy of the modules from
605c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    /// \a rhs.
615c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    ///
625c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    /// @param[in] rhs
635c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    ///     Another module list object.
645c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    //------------------------------------------------------------------
655c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    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 (Log *log, const char *prefix_cstr);
160
161    Mutex &
162    GetMutex () const
163    {
164        return m_modules_mutex;
165    }
166
167    size_t
168    GetIndexForModule (const Module *module) const;
169
170    //------------------------------------------------------------------
171    /// Get the module shared pointer for the module at index \a idx.
172    ///
173    /// @param[in] idx
174    ///     An index into this module collection.
175    ///
176    /// @return
177    ///     A shared pointer to a Module which can contain NULL if
178    ///     \a idx is out of range.
179    ///
180    /// @see ModuleList::GetSize()
181    //------------------------------------------------------------------
182    lldb::ModuleSP
183    GetModuleAtIndex (size_t idx) const;
184
185    //------------------------------------------------------------------
186    /// Get the module shared pointer for the module at index \a idx without
187    /// acquiring the ModuleList mutex.  This MUST already have been
188    /// acquired with ModuleList::GetMutex and locked for this call to be safe.
189    ///
190    /// @param[in] idx
191    ///     An index into this module collection.
192    ///
193    /// @return
194    ///     A shared pointer to a Module which can contain NULL if
195    ///     \a idx is out of range.
196    ///
197    /// @see ModuleList::GetSize()
198    //------------------------------------------------------------------
199    lldb::ModuleSP
200    GetModuleAtIndexUnlocked (size_t idx) const;
201
202    //------------------------------------------------------------------
203    /// Get the module pointer for the module at index \a idx.
204    ///
205    /// @param[in] idx
206    ///     An index into this module collection.
207    ///
208    /// @return
209    ///     A pointer to a Module which can by NULL if \a idx is out
210    ///     of range.
211    ///
212    /// @see ModuleList::GetSize()
213    //------------------------------------------------------------------
214    Module*
215    GetModulePointerAtIndex (size_t idx) const;
216
217    //------------------------------------------------------------------
218    /// Get the module pointer for the module at index \a idx without
219    /// acquiring the ModuleList mutex.  This MUST already have been
220    /// acquired with ModuleList::GetMutex and locked for this call to be safe.
221    ///
222    /// @param[in] idx
223    ///     An index into this module collection.
224    ///
225    /// @return
226    ///     A pointer to a Module which can by NULL if \a idx is out
227    ///     of range.
228    ///
229    /// @see ModuleList::GetSize()
230    //------------------------------------------------------------------
231    Module*
232    GetModulePointerAtIndexUnlocked (size_t idx) const;
233
234    //------------------------------------------------------------------
235    /// Find compile units by partial or full path.
236    ///
237    /// Finds all compile units that match \a path in all of the modules
238    /// and returns the results in \a sc_list.
239    ///
240    /// @param[in] path
241    ///     The name of the compile unit we are looking for.
242    ///
243    /// @param[in] append
244    ///     If \b true, then append any compile units that were found
245    ///     to \a sc_list. If \b false, then the \a sc_list is cleared
246    ///     and the contents of \a sc_list are replaced.
247    ///
248    /// @param[out] sc_list
249    ///     A symbol context list that gets filled in with all of the
250    ///     matches.
251    ///
252    /// @return
253    ///     The number of matches added to \a sc_list.
254    //------------------------------------------------------------------
255    size_t
256    FindCompileUnits (const FileSpec &path,
257                      bool append,
258                      SymbolContextList &sc_list) const;
259
260    //------------------------------------------------------------------
261    /// @see Module::FindFunctions ()
262    //------------------------------------------------------------------
263    size_t
264    FindFunctions (const ConstString &name,
265                   uint32_t name_type_mask,
266                   bool include_symbols,
267                   bool include_inlines,
268                   bool append,
269                   SymbolContextList &sc_list) const;
270
271    //------------------------------------------------------------------
272    /// Find global and static variables by name.
273    ///
274    /// @param[in] name
275    ///     The name of the global or static variable we are looking
276    ///     for.
277    ///
278    /// @param[in] append
279    ///     If \b true, any matches will be appended to \a
280    ///     variable_list, else matches replace the contents of
281    ///     \a variable_list.
282    ///
283    /// @param[in] max_matches
284    ///     Allow the number of matches to be limited to \a
285    ///     max_matches. Specify UINT32_MAX to get all possible matches.
286    ///
287    /// @param[in] variable_list
288    ///     A list of variables that gets the matches appended to (if
289    ///     \a append it \b true), or replace (if \a append is \b false).
290    ///
291    /// @return
292    ///     The number of matches added to \a variable_list.
293    //------------------------------------------------------------------
294    size_t
295    FindGlobalVariables (const ConstString &name,
296                         bool append,
297                         size_t max_matches,
298                         VariableList& variable_list) const;
299
300    //------------------------------------------------------------------
301    /// Find global and static variables by regular exression.
302    ///
303    /// @param[in] regex
304    ///     A regular expression to use when matching the name.
305    ///
306    /// @param[in] append
307    ///     If \b true, any matches will be appended to \a
308    ///     variable_list, else matches replace the contents of
309    ///     \a variable_list.
310    ///
311    /// @param[in] max_matches
312    ///     Allow the number of matches to be limited to \a
313    ///     max_matches. Specify UINT32_MAX to get all possible matches.
314    ///
315    /// @param[in] variable_list
316    ///     A list of variables that gets the matches appended to (if
317    ///     \a append it \b true), or replace (if \a append is \b false).
318    ///
319    /// @return
320    ///     The number of matches added to \a variable_list.
321    //------------------------------------------------------------------
322    size_t
323    FindGlobalVariables (const RegularExpression& regex,
324                         bool append,
325                         size_t max_matches,
326                         VariableList& variable_list) const;
327
328    //------------------------------------------------------------------
329    /// Finds the first module whose file specification matches \a
330    /// file_spec.
331    ///
332    /// @param[in] file_spec_ptr
333    ///     A file specification object to match against the Module's
334    ///     file specifications. If \a file_spec does not have
335    ///     directory information, matches will occur by matching only
336    ///     the basename of any modules in this list. If this value is
337    ///     NULL, then file specifications won't be compared when
338    ///     searching for matching modules.
339    ///
340    /// @param[in] arch_ptr
341    ///     The architecture to search for if non-NULL. If this value
342    ///     is NULL no architecture matching will be performed.
343    ///
344    /// @param[in] uuid_ptr
345    ///     The uuid to search for if non-NULL. If this value is NULL
346    ///     no uuid matching will be performed.
347    ///
348    /// @param[in] object_name
349    ///     An optional object name that must match as well. This value
350    ///     can be NULL.
351    ///
352    /// @param[out] matching_module_list
353    ///     A module list that gets filled in with any modules that
354    ///     match the search criteria.
355    ///
356    /// @return
357    ///     The number of matching modules found by the search.
358    //------------------------------------------------------------------
359    size_t
360    FindModules (const ModuleSpec &module_spec,
361                 ModuleList& matching_module_list) const;
362
363    lldb::ModuleSP
364    FindModule (const Module *module_ptr) const;
365
366    //------------------------------------------------------------------
367    // Find a module by UUID
368    //
369    // The UUID value for a module is extracted from the ObjectFile and
370    // is the MD5 checksum, or a smarter object file equivalent, so
371    // finding modules by UUID values is very efficient and accurate.
372    //------------------------------------------------------------------
373    lldb::ModuleSP
374    FindModule (const UUID &uuid) const;
375
376    lldb::ModuleSP
377    FindFirstModule (const ModuleSpec &module_spec) const;
378
379    size_t
380    FindSymbolsWithNameAndType (const ConstString &name,
381                                lldb::SymbolType symbol_type,
382                                SymbolContextList &sc_list,
383                                bool append = false) const;
384
385    size_t
386    FindSymbolsMatchingRegExAndType (const RegularExpression &regex,
387                                     lldb::SymbolType symbol_type,
388                                     SymbolContextList &sc_list,
389                                     bool append = false) const;
390
391    //------------------------------------------------------------------
392    /// Find types by name.
393    ///
394    /// @param[in] sc
395    ///     A symbol context that scopes where to extract a type list
396    ///     from.
397    ///
398    /// @param[in] name
399    ///     The name of the type we are looking for.
400    ///
401    /// @param[in] append
402    ///     If \b true, any matches will be appended to \a
403    ///     variable_list, else matches replace the contents of
404    ///     \a variable_list.
405    ///
406    /// @param[in] max_matches
407    ///     Allow the number of matches to be limited to \a
408    ///     max_matches. Specify UINT32_MAX to get all possible matches.
409    ///
410    /// @param[in] encoding
411    ///     Limit the search to specific types, or get all types if
412    ///     set to Type::invalid.
413    ///
414    /// @param[in] udt_name
415    ///     If the encoding is a user defined type, specify the name
416    ///     of the user defined type ("struct", "union", "class", etc).
417    ///
418    /// @param[out] type_list
419    ///     A type list gets populated with any matches.
420    ///
421    /// @return
422    ///     The number of matches added to \a type_list.
423    //------------------------------------------------------------------
424    size_t
425    FindTypes (const SymbolContext& sc,
426               const ConstString &name,
427               bool name_is_fully_qualified,
428               size_t max_matches,
429               TypeList& types) const;
430
431    bool
432    FindSourceFile (const FileSpec &orig_spec, FileSpec &new_spec) const;
433
434    bool
435    Remove (const lldb::ModuleSP &module_sp);
436
437    size_t
438    Remove (ModuleList &module_list);
439
440    bool
441    RemoveIfOrphaned (const Module *module_ptr);
442
443    size_t
444    RemoveOrphans (bool mandatory);
445
446    bool
447    ResolveFileAddress (lldb::addr_t vm_addr,
448                        Address& so_addr) const;
449
450    //------------------------------------------------------------------
451    /// @copydoc Module::ResolveSymbolContextForAddress (const Address &,uint32_t,SymbolContext&)
452    //------------------------------------------------------------------
453    uint32_t
454    ResolveSymbolContextForAddress (const Address& so_addr,
455                                    uint32_t resolve_scope,
456                                    SymbolContext& sc) const;
457
458    //------------------------------------------------------------------
459    /// @copydoc Module::ResolveSymbolContextForFilePath (const char *,uint32_t,bool,uint32_t,SymbolContextList&)
460    //------------------------------------------------------------------
461    uint32_t
462    ResolveSymbolContextForFilePath (const char *file_path,
463                                     uint32_t line,
464                                     bool check_inlines,
465                                     uint32_t resolve_scope,
466                                     SymbolContextList& sc_list) const;
467
468    //------------------------------------------------------------------
469    /// @copydoc Module::ResolveSymbolContextsForFileSpec (const FileSpec &,uint32_t,bool,uint32_t,SymbolContextList&)
470    //------------------------------------------------------------------
471    uint32_t
472    ResolveSymbolContextsForFileSpec (const FileSpec &file_spec,
473                                     uint32_t line,
474                                     bool check_inlines,
475                                     uint32_t resolve_scope,
476                                     SymbolContextList& sc_list) const;
477
478    //------------------------------------------------------------------
479    /// Gets the size of the module list.
480    ///
481    /// @return
482    ///     The number of modules in the module list.
483    //------------------------------------------------------------------
484    size_t
485    GetSize () const;
486
487    static bool
488    ModuleIsInCache (const Module *module_ptr);
489
490    static Error
491    GetSharedModule (const ModuleSpec &module_spec,
492                     lldb::ModuleSP &module_sp,
493                     const FileSpecList *module_search_paths_ptr,
494                     lldb::ModuleSP *old_module_sp_ptr,
495                     bool *did_create_ptr,
496                     bool always_create = false);
497
498    static bool
499    RemoveSharedModule (lldb::ModuleSP &module_sp);
500
501    static size_t
502    FindSharedModules (const ModuleSpec &module_spec,
503                       ModuleList &matching_module_list);
504
505    static size_t
506    RemoveOrphanSharedModules (bool mandatory);
507
508    static bool
509    RemoveSharedModuleIfOrphaned (const Module *module_ptr);
510
511protected:
512    //------------------------------------------------------------------
513    // Class typedefs.
514    //------------------------------------------------------------------
515    typedef std::vector<lldb::ModuleSP> collection; ///< The module collection type.
516
517    void
518    AppendImpl (const lldb::ModuleSP &module_sp, bool use_notifier = true);
519
520    bool
521    RemoveImpl (const lldb::ModuleSP &module_sp, bool use_notifier = true);
522
523    collection::iterator
524    RemoveImpl (collection::iterator pos, bool use_notifier = true);
525
526    void
527    ClearImpl (bool use_notifier = true);
528
529    //------------------------------------------------------------------
530    // Member variables.
531    //------------------------------------------------------------------
532    collection m_modules; ///< The collection of modules.
533    mutable Mutex m_modules_mutex;
534
535    Notifier* m_notifier;
536
537};
538
539} // namespace lldb_private
540
541#endif  // liblldb_ModuleList_h_
542