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