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