ModuleList.h revision 9f95fb63a492b53206d578f46e73899d60d70321
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    uint32_t
120    GetIndexForModule (const Module *module) const;
121
122    //------------------------------------------------------------------
123    /// Get the module shared pointer for the module at index \a idx.
124    ///
125    /// @param[in] idx
126    ///     An index into this module collection.
127    ///
128    /// @return
129    ///     A shared pointer to a Module which can contain NULL if
130    ///     \a idx is out of range.
131    ///
132    /// @see ModuleList::GetSize()
133    //------------------------------------------------------------------
134    lldb::ModuleSP
135    GetModuleAtIndex (uint32_t idx);
136
137    //------------------------------------------------------------------
138    /// Get the module pointer for the module at index \a idx.
139    ///
140    /// @param[in] idx
141    ///     An index into this module collection.
142    ///
143    /// @return
144    ///     A pointer to a Module which can by NULL if \a idx is out
145    ///     of range.
146    ///
147    /// @see ModuleList::GetSize()
148    //------------------------------------------------------------------
149    Module*
150    GetModulePointerAtIndex (uint32_t idx) const;
151
152    //------------------------------------------------------------------
153    /// Find compile units by partial or full path.
154    ///
155    /// Finds all compile units that match \a path in all of the modules
156    /// and returns the results in \a sc_list.
157    ///
158    /// @param[in] path
159    ///     The name of the compile unit we are looking for.
160    ///
161    /// @param[in] append
162    ///     If \b true, then append any compile units that were found
163    ///     to \a sc_list. If \b false, then the \a sc_list is cleared
164    ///     and the contents of \a sc_list are replaced.
165    ///
166    /// @param[out] sc_list
167    ///     A symbol context list that gets filled in with all of the
168    ///     matches.
169    ///
170    /// @return
171    ///     The number of matches added to \a sc_list.
172    //------------------------------------------------------------------
173    uint32_t
174    FindCompileUnits (const FileSpec &path,
175                      bool append,
176                      SymbolContextList &sc_list);
177
178    //------------------------------------------------------------------
179    /// @see Module::FindFunctions ()
180    //------------------------------------------------------------------
181    uint32_t
182    FindFunctions (const ConstString &name,
183                   uint32_t name_type_mask,
184                   bool include_symbols,
185                   bool include_inlines,
186                   bool append,
187                   SymbolContextList &sc_list);
188
189    //------------------------------------------------------------------
190    /// Find global and static variables by name.
191    ///
192    /// @param[in] name
193    ///     The name of the global or static variable we are looking
194    ///     for.
195    ///
196    /// @param[in] append
197    ///     If \b true, any matches will be appended to \a
198    ///     variable_list, else matches replace the contents of
199    ///     \a variable_list.
200    ///
201    /// @param[in] max_matches
202    ///     Allow the number of matches to be limited to \a
203    ///     max_matches. Specify UINT32_MAX to get all possible matches.
204    ///
205    /// @param[in] variable_list
206    ///     A list of variables that gets the matches appended to (if
207    ///     \a append it \b true), or replace (if \a append is \b false).
208    ///
209    /// @return
210    ///     The number of matches added to \a variable_list.
211    //------------------------------------------------------------------
212    uint32_t
213    FindGlobalVariables (const ConstString &name,
214                         bool append,
215                         uint32_t max_matches,
216                         VariableList& variable_list);
217
218    //------------------------------------------------------------------
219    /// Find global and static variables by regular exression.
220    ///
221    /// @param[in] regex
222    ///     A regular expression to use when matching the name.
223    ///
224    /// @param[in] append
225    ///     If \b true, any matches will be appended to \a
226    ///     variable_list, else matches replace the contents of
227    ///     \a variable_list.
228    ///
229    /// @param[in] max_matches
230    ///     Allow the number of matches to be limited to \a
231    ///     max_matches. Specify UINT32_MAX to get all possible matches.
232    ///
233    /// @param[in] variable_list
234    ///     A list of variables that gets the matches appended to (if
235    ///     \a append it \b true), or replace (if \a append is \b false).
236    ///
237    /// @return
238    ///     The number of matches added to \a variable_list.
239    //------------------------------------------------------------------
240    uint32_t
241    FindGlobalVariables (const RegularExpression& regex,
242                         bool append,
243                         uint32_t max_matches,
244                         VariableList& variable_list);
245
246    //------------------------------------------------------------------
247    /// Finds the first module whose file specification matches \a
248    /// file_spec.
249    ///
250    /// @param[in] file_spec_ptr
251    ///     A file specification object to match against the Module's
252    ///     file specifications. If \a file_spec does not have
253    ///     directory information, matches will occur by matching only
254    ///     the basename of any modules in this list. If this value is
255    ///     NULL, then file specifications won't be compared when
256    ///     searching for matching modules.
257    ///
258    /// @param[in] arch_ptr
259    ///     The architecture to search for if non-NULL. If this value
260    ///     is NULL no architecture matching will be performed.
261    ///
262    /// @param[in] uuid_ptr
263    ///     The uuid to search for if non-NULL. If this value is NULL
264    ///     no uuid matching will be performed.
265    ///
266    /// @param[in] object_name
267    ///     An optional object name that must match as well. This value
268    ///     can be NULL.
269    ///
270    /// @param[out] matching_module_list
271    ///     A module list that gets filled in with any modules that
272    ///     match the search criteria.
273    ///
274    /// @return
275    ///     The number of matching modules found by the search.
276    //------------------------------------------------------------------
277    size_t
278    FindModules (const ModuleSpec &module_spec,
279                 ModuleList& matching_module_list) const;
280
281    lldb::ModuleSP
282    FindModule (const Module *module_ptr);
283
284    //------------------------------------------------------------------
285    // Find a module by UUID
286    //
287    // The UUID value for a module is extracted from the ObjectFile and
288    // is the MD5 checksum, or a smarter object file equivalent, so
289    // finding modules by UUID values is very efficient and accurate.
290    //------------------------------------------------------------------
291    lldb::ModuleSP
292    FindModule (const UUID &uuid);
293
294    lldb::ModuleSP
295    FindFirstModule (const ModuleSpec &module_spec);
296
297    size_t
298    FindSymbolsWithNameAndType (const ConstString &name,
299                                lldb::SymbolType symbol_type,
300                                SymbolContextList &sc_list,
301                                bool append = false);
302
303    size_t
304    FindSymbolsMatchingRegExAndType (const RegularExpression &regex,
305                                     lldb::SymbolType symbol_type,
306                                     SymbolContextList &sc_list,
307                                     bool append = false);
308
309    //------------------------------------------------------------------
310    /// Find types by name.
311    ///
312    /// @param[in] sc
313    ///     A symbol context that scopes where to extract a type list
314    ///     from.
315    ///
316    /// @param[in] name
317    ///     The name of the type we are looking for.
318    ///
319    /// @param[in] append
320    ///     If \b true, any matches will be appended to \a
321    ///     variable_list, else matches replace the contents of
322    ///     \a variable_list.
323    ///
324    /// @param[in] max_matches
325    ///     Allow the number of matches to be limited to \a
326    ///     max_matches. Specify UINT32_MAX to get all possible matches.
327    ///
328    /// @param[in] encoding
329    ///     Limit the search to specific types, or get all types if
330    ///     set to Type::invalid.
331    ///
332    /// @param[in] udt_name
333    ///     If the encoding is a user defined type, specify the name
334    ///     of the user defined type ("struct", "union", "class", etc).
335    ///
336    /// @param[out] type_list
337    ///     A type list gets populated with any matches.
338    ///
339    /// @return
340    ///     The number of matches added to \a type_list.
341    //------------------------------------------------------------------
342    uint32_t
343    FindTypes (const SymbolContext& sc,
344               const ConstString &name,
345               bool name_is_fully_qualified,
346               uint32_t max_matches,
347               TypeList& types);
348
349    bool
350    FindSourceFile (const FileSpec &orig_spec, FileSpec &new_spec) const;
351
352    bool
353    Remove (const lldb::ModuleSP &module_sp);
354
355    size_t
356    Remove (ModuleList &module_list);
357
358    size_t
359    RemoveOrphans ();
360
361    bool
362    ResolveFileAddress (lldb::addr_t vm_addr,
363                        Address& so_addr);
364
365    //------------------------------------------------------------------
366    /// @copydoc Module::ResolveSymbolContextForAddress (const Address &,uint32_t,SymbolContext&)
367    //------------------------------------------------------------------
368    uint32_t
369    ResolveSymbolContextForAddress (const Address& so_addr,
370                                    uint32_t resolve_scope,
371                                    SymbolContext& sc);
372
373    //------------------------------------------------------------------
374    /// @copydoc Module::ResolveSymbolContextForFilePath (const char *,uint32_t,bool,uint32_t,SymbolContextList&)
375    //------------------------------------------------------------------
376    uint32_t
377    ResolveSymbolContextForFilePath (const char *file_path,
378                                     uint32_t line,
379                                     bool check_inlines,
380                                     uint32_t resolve_scope,
381                                     SymbolContextList& sc_list);
382
383    //------------------------------------------------------------------
384    /// @copydoc Module::ResolveSymbolContextsForFileSpec (const FileSpec &,uint32_t,bool,uint32_t,SymbolContextList&)
385    //------------------------------------------------------------------
386    uint32_t
387    ResolveSymbolContextsForFileSpec (const FileSpec &file_spec,
388                                     uint32_t line,
389                                     bool check_inlines,
390                                     uint32_t resolve_scope,
391                                     SymbolContextList& sc_list);
392
393    //------------------------------------------------------------------
394    /// Gets the size of the module list.
395    ///
396    /// @return
397    ///     The number of modules in the module list.
398    //------------------------------------------------------------------
399    size_t
400    GetSize () const;
401
402    static bool
403    ModuleIsInCache (const Module *module_ptr);
404
405    static Error
406    GetSharedModule (const ModuleSpec &module_spec,
407                     lldb::ModuleSP &module_sp,
408                     const FileSpecList *module_search_paths_ptr,
409                     lldb::ModuleSP *old_module_sp_ptr,
410                     bool *did_create_ptr,
411                     bool always_create = false);
412
413    static bool
414    RemoveSharedModule (lldb::ModuleSP &module_sp);
415
416    static size_t
417    FindSharedModules (const ModuleSpec &module_spec,
418                       ModuleList &matching_module_list);
419
420    static uint32_t
421    RemoveOrphanSharedModules ();
422
423protected:
424    //------------------------------------------------------------------
425    // Class typedefs.
426    //------------------------------------------------------------------
427    typedef std::vector<lldb::ModuleSP> collection; ///< The module collection type.
428
429    //------------------------------------------------------------------
430    // Member variables.
431    //------------------------------------------------------------------
432    collection m_modules; ///< The collection of modules.
433    mutable Mutex m_modules_mutex;
434
435};
436
437} // namespace lldb_private
438
439#endif  // liblldb_ModuleList_h_
440