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