ModuleList.h revision 3e8c25f62f92145b6fb699b379cbfe72b1245d4a
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    /// Find functions by name.
170    ///
171    /// Finds all functions that match \a name in all of the modules and
172    /// returns the results in \a sc_list.
173    ///
174    /// @param[in] name
175    ///     The name of the function we are looking for.
176    ///
177    /// @param[in] name_type_mask
178    ///     A bit mask of bits that indicate what kind of names should
179    ///     be used when doing the lookup. Bits include fully qualified
180    ///     names, base names, C++ methods, or ObjC selectors.
181    ///     See FunctionNameType for more details.
182    ///
183    /// @param[out] sc_list
184    ///     A symbol context list that gets filled in with all of the
185    ///     matches.
186    ///
187    /// @return
188    ///     The number of matches added to \a sc_list.
189    //------------------------------------------------------------------
190    uint32_t
191    FindFunctions (const ConstString &name,
192                   uint32_t name_type_mask,
193                   bool include_symbols,
194                   bool append,
195                   SymbolContextList &sc_list);
196
197    //------------------------------------------------------------------
198    /// Find global and static variables by name.
199    ///
200    /// @param[in] name
201    ///     The name of the global or static variable we are looking
202    ///     for.
203    ///
204    /// @param[in] append
205    ///     If \b true, any matches will be appended to \a
206    ///     variable_list, else matches replace the contents of
207    ///     \a variable_list.
208    ///
209    /// @param[in] max_matches
210    ///     Allow the number of matches to be limited to \a
211    ///     max_matches. Specify UINT32_MAX to get all possible matches.
212    ///
213    /// @param[in] variable_list
214    ///     A list of variables that gets the matches appended to (if
215    ///     \a append it \b true), or replace (if \a append is \b false).
216    ///
217    /// @return
218    ///     The number of matches added to \a variable_list.
219    //------------------------------------------------------------------
220    uint32_t
221    FindGlobalVariables (const ConstString &name,
222                         bool append,
223                         uint32_t max_matches,
224                         VariableList& variable_list);
225
226    //------------------------------------------------------------------
227    /// Find global and static variables by regular exression.
228    ///
229    /// @param[in] regex
230    ///     A regular expression to use when matching the name.
231    ///
232    /// @param[in] append
233    ///     If \b true, any matches will be appended to \a
234    ///     variable_list, else matches replace the contents of
235    ///     \a variable_list.
236    ///
237    /// @param[in] max_matches
238    ///     Allow the number of matches to be limited to \a
239    ///     max_matches. Specify UINT32_MAX to get all possible matches.
240    ///
241    /// @param[in] variable_list
242    ///     A list of variables that gets the matches appended to (if
243    ///     \a append it \b true), or replace (if \a append is \b false).
244    ///
245    /// @return
246    ///     The number of matches added to \a variable_list.
247    //------------------------------------------------------------------
248    uint32_t
249    FindGlobalVariables (const RegularExpression& regex,
250                         bool append,
251                         uint32_t max_matches,
252                         VariableList& variable_list);
253
254    //------------------------------------------------------------------
255    /// Finds the first module whose file specification matches \a
256    /// file_spec.
257    ///
258    /// @param[in] file_spec_ptr
259    ///     A file specification object to match against the Module's
260    ///     file specifications. If \a file_spec does not have
261    ///     directory information, matches will occur by matching only
262    ///     the basename of any modules in this list. If this value is
263    ///     NULL, then file specifications won't be compared when
264    ///     searching for matching modules.
265    ///
266    /// @param[in] arch_ptr
267    ///     The architecture to search for if non-NULL. If this value
268    ///     is NULL no architecture matching will be performed.
269    ///
270    /// @param[in] uuid_ptr
271    ///     The uuid to search for if non-NULL. If this value is NULL
272    ///     no uuid matching will be performed.
273    ///
274    /// @param[in] object_name
275    ///     An optional object name that must match as well. This value
276    ///     can be NULL.
277    ///
278    /// @param[out] matching_module_list
279    ///     A module list that gets filled in with any modules that
280    ///     match the search criteria.
281    ///
282    /// @return
283    ///     The number of matching modules found by the search.
284    //------------------------------------------------------------------
285    size_t
286    FindModules (const FileSpec *file_spec_ptr,
287                 const ArchSpec *arch_ptr,
288                 const lldb_private::UUID *uuid_ptr,
289                 const ConstString *object_name,
290                 ModuleList& matching_module_list) const;
291
292    lldb::ModuleSP
293    FindModule (const Module *module_ptr);
294
295    //------------------------------------------------------------------
296    // Find a module by UUID
297    //
298    // The UUID value for a module is extracted from the ObjectFile and
299    // is the MD5 checksum, or a smarter object file equivalent, so
300    // finding modules by UUID values is very efficient and accurate.
301    //------------------------------------------------------------------
302    lldb::ModuleSP
303    FindModule (const UUID &uuid);
304
305    lldb::ModuleSP
306    FindFirstModuleForFileSpec (const FileSpec &file_spec,
307                                const ArchSpec *arch_ptr,
308                                const ConstString *object_name);
309
310    lldb::ModuleSP
311    FindFirstModuleForPlatormFileSpec (const FileSpec &platform_file_spec,
312                                       const ArchSpec *arch_ptr,
313                                       const ConstString *object_name);
314
315    size_t
316    FindSymbolsWithNameAndType (const ConstString &name,
317                                lldb::SymbolType symbol_type,
318                                SymbolContextList &sc_list);
319
320    //------------------------------------------------------------------
321    /// Find types by name.
322    ///
323    /// @param[in] sc
324    ///     A symbol context that scopes where to extract a type list
325    ///     from.
326    ///
327    /// @param[in] name
328    ///     The name of the type we are looking for.
329    ///
330    /// @param[in] append
331    ///     If \b true, any matches will be appended to \a
332    ///     variable_list, else matches replace the contents of
333    ///     \a variable_list.
334    ///
335    /// @param[in] max_matches
336    ///     Allow the number of matches to be limited to \a
337    ///     max_matches. Specify UINT32_MAX to get all possible matches.
338    ///
339    /// @param[in] encoding
340    ///     Limit the search to specific types, or get all types if
341    ///     set to Type::invalid.
342    ///
343    /// @param[in] udt_name
344    ///     If the encoding is a user defined type, specify the name
345    ///     of the user defined type ("struct", "union", "class", etc).
346    ///
347    /// @param[out] type_list
348    ///     A type list gets populated with any matches.
349    ///
350    /// @return
351    ///     The number of matches added to \a type_list.
352    //------------------------------------------------------------------
353    uint32_t
354    FindTypes (const SymbolContext& sc,
355               const ConstString &name,
356               bool append,
357               uint32_t max_matches,
358               TypeList& types);
359
360    bool
361    Remove (const lldb::ModuleSP &module_sp);
362
363    size_t
364    Remove (ModuleList &module_list);
365
366    size_t
367    RemoveOrphans ();
368
369    bool
370    ResolveFileAddress (lldb::addr_t vm_addr,
371                        Address& so_addr);
372
373    //------------------------------------------------------------------
374    /// @copydoc Module::ResolveSymbolContextForAddress (const Address &,uint32_t,SymbolContext&)
375    //------------------------------------------------------------------
376    uint32_t
377    ResolveSymbolContextForAddress (const Address& so_addr,
378                                    uint32_t resolve_scope,
379                                    SymbolContext& sc);
380
381    //------------------------------------------------------------------
382    /// @copydoc Module::ResolveSymbolContextForFilePath (const char *,uint32_t,bool,uint32_t,SymbolContextList&)
383    //------------------------------------------------------------------
384    uint32_t
385    ResolveSymbolContextForFilePath (const char *file_path,
386                                     uint32_t line,
387                                     bool check_inlines,
388                                     uint32_t resolve_scope,
389                                     SymbolContextList& sc_list);
390
391    //------------------------------------------------------------------
392    /// @copydoc Module::ResolveSymbolContextsForFileSpec (const FileSpec &,uint32_t,bool,uint32_t,SymbolContextList&)
393    //------------------------------------------------------------------
394    uint32_t
395    ResolveSymbolContextsForFileSpec (const FileSpec &file_spec,
396                                     uint32_t line,
397                                     bool check_inlines,
398                                     uint32_t resolve_scope,
399                                     SymbolContextList& sc_list);
400
401    //------------------------------------------------------------------
402    /// Gets the size of the module list.
403    ///
404    /// @return
405    ///     The number of modules in the module list.
406    //------------------------------------------------------------------
407    size_t
408    GetSize () const;
409
410    static const lldb::ModuleSP
411    GetModuleSP (const Module *module_ptr);
412
413    static Error
414    GetSharedModule (const FileSpec& file_spec,
415                     const ArchSpec& arch,
416                     const lldb_private::UUID *uuid_ptr,
417                     const ConstString *object_name,
418                     off_t object_offset,
419                     lldb::ModuleSP &module_sp,
420                     lldb::ModuleSP *old_module_sp_ptr,
421                     bool *did_create_ptr,
422                     bool always_create = false);
423
424    static bool
425    RemoveSharedModule (lldb::ModuleSP &module_sp);
426
427    static size_t
428    FindSharedModules (const FileSpec& in_file_spec,
429                       const ArchSpec& arch,
430                       const lldb_private::UUID *uuid_ptr,
431                       const ConstString *object_name_ptr,
432                       ModuleList &matching_module_list);
433
434    static uint32_t
435    RemoveOrphanSharedModules ();
436
437protected:
438    //------------------------------------------------------------------
439    // Class typedefs.
440    //------------------------------------------------------------------
441    typedef std::vector<lldb::ModuleSP> collection; ///< The module collection type.
442
443    //------------------------------------------------------------------
444    // Member variables.
445    //------------------------------------------------------------------
446    collection m_modules; ///< The collection of modules.
447    mutable Mutex m_modules_mutex;
448
449private:
450    uint32_t
451    FindTypes_Impl (const SymbolContext& sc,
452                    const ConstString &name,
453                    bool append,
454                    uint32_t max_matches,
455                    TypeList& types);
456};
457
458} // namespace lldb_private
459
460#endif  // liblldb_ModuleList_h_
461