SymbolContext.h revision 52c8b6e3205e11e90adc83521c01915fc13626de
1//===-- SymbolContext.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
11#ifndef liblldb_SymbolContext_h_
12#define liblldb_SymbolContext_h_
13
14#include <vector>
15
16#include "lldb/lldb-private.h"
17#include "lldb/Core/Address.h"
18#include "lldb/Symbol/ClangASTType.h"
19#include "lldb/Symbol/LineEntry.h"
20
21namespace lldb_private {
22
23class SymbolContextScope;
24//----------------------------------------------------------------------
25/// @class SymbolContext SymbolContext.h "lldb/Symbol/SymbolContext.h"
26/// @brief Defines a symbol context baton that can be handed other debug
27/// core functions.
28///
29/// Many debugger functions require a context when doing lookups. This
30/// class provides a common structure that can be used as the result
31/// of a query that can contain a single result. Examples of such
32/// queries include
33///     @li Looking up a load address.
34//----------------------------------------------------------------------
35class SymbolContext
36{
37public:
38
39    //------------------------------------------------------------------
40    /// Default constructor.
41    ///
42    /// Initialize all pointer members to NULL and all struct members
43    /// to their default state.
44    //------------------------------------------------------------------
45    SymbolContext ();
46
47    //------------------------------------------------------------------
48    /// Construct with an object that knows how to reconstruct its
49    /// symbol context.
50    ///
51    /// @param[in] sc_scope
52    ///     A symbol context scope object that knows how to reconstruct
53    ///     it's context.
54    //------------------------------------------------------------------
55    explicit
56    SymbolContext (SymbolContextScope *sc_scope);
57
58    //------------------------------------------------------------------
59    /// Construct with module, and optional compile unit, function,
60    /// block, line table, line entry and symbol.
61    ///
62    /// Initialize all pointer to the specified values.
63    ///
64    /// @param[in] module
65    ///     A Module pointer to the module for this context.
66    ///
67    /// @param[in] comp_unit
68    ///     A CompileUnit pointer to the compile unit for this context.
69    ///
70    /// @param[in] function
71    ///     A Function pointer to the function for this context.
72    ///
73    /// @param[in] block
74    ///     A Block pointer to the deepest block for this context.
75    ///
76    /// @param[in] line_entry
77    ///     A LineEntry pointer to the line entry for this context.
78    ///
79    /// @param[in] symbol
80    ///     A Symbol pointer to the symbol for this context.
81    //------------------------------------------------------------------
82    explicit
83    SymbolContext (const lldb::TargetSP &target_sp,
84                   const lldb::ModuleSP &module_sp,
85                   CompileUnit *comp_unit = NULL,
86                   Function *function = NULL,
87                   Block *block = NULL,
88                   LineEntry *line_entry = NULL,
89                   Symbol *symbol = NULL);
90
91    // This version sets the target to a NULL TargetSP if you don't know it.
92    explicit
93    SymbolContext (const lldb::ModuleSP &module_sp,
94                   CompileUnit *comp_unit = NULL,
95                   Function *function = NULL,
96                   Block *block = NULL,
97                   LineEntry *line_entry = NULL,
98                   Symbol *symbol = NULL);
99
100    //------------------------------------------------------------------
101    /// Copy constructor
102    ///
103    /// Makes a copy of the another SymbolContext object \a rhs.
104    ///
105    /// @param[in] rhs
106    ///     A const SymbolContext object reference to copy.
107    //------------------------------------------------------------------
108    SymbolContext (const SymbolContext& rhs);
109
110    //------------------------------------------------------------------
111    /// Assignment operator.
112    ///
113    /// Copies the address value from another SymbolContext object \a
114    /// rhs into \a this object.
115    ///
116    /// @param[in] rhs
117    ///     A const SymbolContext object reference to copy.
118    ///
119    /// @return
120    ///     A const SymbolContext object reference to \a this.
121    //------------------------------------------------------------------
122    const SymbolContext&
123    operator= (const SymbolContext& rhs);
124
125    //------------------------------------------------------------------
126    /// Clear the object's state.
127    ///
128    /// Resets all pointer members to NULL, and clears any class objects
129    /// to their default state.
130    //------------------------------------------------------------------
131    void
132    Clear ();
133
134    //------------------------------------------------------------------
135    /// Dump a description of this object to a Stream.
136    ///
137    /// Dump a description of the contents of this object to the
138    /// supplied stream \a s.
139    ///
140    /// @param[in] s
141    ///     The stream to which to dump the object descripton.
142    //------------------------------------------------------------------
143    void
144    Dump (Stream *s, Target *target) const;
145
146    //------------------------------------------------------------------
147    /// Dump the stop context in this object to a Stream.
148    ///
149    /// Dump the best description of this object to the stream. The
150    /// information displayed depends on the amount and quality of the
151    /// information in this context. If a module, function, file and
152    /// line number are available, they will be dumped. If only a
153    /// module and function or symbol name with offset is available,
154    /// that will be ouput. Else just the address at which the target
155    /// was stopped will be displayed.
156    ///
157    /// @param[in] s
158    ///     The stream to which to dump the object descripton.
159    ///
160    /// @param[in] so_addr
161    ///     The resolved section offset address.
162    //------------------------------------------------------------------
163    void
164    DumpStopContext (Stream *s,
165                     ExecutionContextScope *exe_scope,
166                     const Address &so_addr,
167                     bool show_fullpaths,
168                     bool show_module,
169                     bool show_inlined_frames) const;
170
171    //------------------------------------------------------------------
172    /// Get the address range contained within a symbol context.
173    ///
174    /// Address range priority is as follows:
175    ///     - line_entry address range if line_entry is valid
176    ///     - function address range if function is not NULL
177    ///     - symbol address range if symbol is not NULL
178    ///
179    /// @param[out] range
180    ///     An address range object that will be filled in if \b true
181    ///     is returned.
182    ///
183    /// @return
184    ///     \b True if this symbol context contains items that describe
185    ///     an address range, \b false otherwise.
186    //------------------------------------------------------------------
187    bool
188    GetAddressRange (uint32_t scope, AddressRange &range) const;
189
190
191    void
192    GetDescription(Stream *s,
193                   lldb::DescriptionLevel level,
194                   Target *target) const;
195
196    uint32_t
197    GetResolvedMask () const;
198
199
200    //------------------------------------------------------------------
201    /// Find a function matching the given name, working out from this
202    /// symbol context.
203    ///
204    /// @return
205    ///     The number of symbol contexts found.
206    //------------------------------------------------------------------
207    size_t
208    FindFunctionsByName (const ConstString &name,
209                         bool include_symbols,
210                         bool append,
211                         SymbolContextList &sc_list) const;
212
213
214    ClangNamespaceDecl
215    FindNamespace (const ConstString &name) const;
216
217    //------------------------------------------------------------------
218    /// Find a variable matching the given name, working out from this
219    /// symbol context.
220    ///
221    /// @return
222    ///     A shared pointer to the variable found.
223    //------------------------------------------------------------------
224    //lldb::VariableSP
225    //FindVariableByName (const char *name) const;
226
227    //------------------------------------------------------------------
228    /// Find a type matching the given name, working out from this
229    /// symbol context.
230    ///
231    /// @return
232    ///     A shared pointer to the variable found.
233    //------------------------------------------------------------------
234    lldb::TypeSP
235    FindTypeByName (const ConstString &name) const;
236
237//    static SymbolContext
238//    CreateSymbolContextFromDescription (lldb::TargetSP &target,
239//                                        const char *module,
240//                                        const char *comp_unit,
241//                                        const char *function,
242//                                        const char *block_spec
243//                                        const char *line_number,
244//                                        const char *symbol);
245
246    //------------------------------------------------------------------
247    // Member variables
248    //------------------------------------------------------------------
249    lldb::TargetSP  target_sp;  ///< The Target for a given query
250    lldb::ModuleSP  module_sp;  ///< The Module for a given query
251    CompileUnit *   comp_unit;  ///< The CompileUnit for a given query
252    Function *      function;   ///< The Function for a given query
253    Block *         block;      ///< The Block for a given query
254    LineEntry       line_entry; ///< The LineEntry for a given query
255    Symbol *        symbol;     ///< The Symbol for a given query
256};
257
258
259class SymbolContextSpecifier
260{
261public:
262    typedef enum SpecificationType
263    {
264        eNothingSpecified          = 0,
265        eModuleSpecified           = 1 << 0,
266        eFileSpecified             = 1 << 1,
267        eLineStartSpecified        = 1 << 2,
268        eLineEndSpecified          = 1 << 3,
269        eFunctionSpecified         = 1 << 4,
270        eClassOrNamespaceSpecified = 1 << 5,
271        eAddressRangeSpecified     = 1 << 6
272    } SpecificationType;
273
274    // This one produces a specifier that matches everything...
275    SymbolContextSpecifier (lldb::TargetSP target_sp) :
276        m_start_line(0),
277        m_end_line(0)
278    {
279        m_target_sp = target_sp;
280        m_type = eNothingSpecified;
281    }
282
283    bool
284    AddSpecification (const char *spec_string, SpecificationType type);
285
286    bool
287    AddLineSpecification (uint32_t line_no, SpecificationType type);
288
289    void
290    Clear();
291
292    bool
293    SymbolContextMatches(SymbolContext &sc);
294
295    bool
296    AddressMatches(lldb::addr_t addr);
297
298    void
299    GetDescription (Stream *s, lldb::DescriptionLevel level) const;
300
301private:
302    lldb::TargetSP                 m_target_sp;
303    std::string                    m_module_spec;
304    lldb::ModuleSP                 m_module_sp;
305    std::auto_ptr<FileSpec>        m_file_spec_ap;
306    size_t                         m_start_line;
307    size_t                         m_end_line;
308    std::string                    m_function_spec;
309    std::string                    m_class_name;
310    std::auto_ptr<AddressRange>    m_address_range_ap;
311    uint32_t                       m_type; // Or'ed bits from SpecificationType
312
313};
314
315//----------------------------------------------------------------------
316/// @class SymbolContextList SymbolContext.h "lldb/Symbol/SymbolContext.h"
317/// @brief Defines a list of symbol context objects.
318///
319/// This class provides a common structure that can be used to contain
320/// the result of a query that can contain a multiple results. Examples
321/// of such queries include:
322///     @li Looking up a function by name.
323///     @li Finding all addressses for a specified file and line number.
324//----------------------------------------------------------------------
325class SymbolContextList
326{
327public:
328    //------------------------------------------------------------------
329    /// Default constructor.
330    ///
331    /// Initialize with an empty list.
332    //------------------------------------------------------------------
333    SymbolContextList ();
334
335    //------------------------------------------------------------------
336    /// Destructor.
337    //------------------------------------------------------------------
338    ~SymbolContextList ();
339
340    //------------------------------------------------------------------
341    /// Append a new symbol context to the list.
342    ///
343    /// @param[in] sc
344    ///     A symbol context to append to the list.
345    //------------------------------------------------------------------
346    void
347    Append (const SymbolContext& sc);
348
349    bool
350    AppendIfUnique (const SymbolContext& sc, bool merge_symbol_into_function);
351
352    //------------------------------------------------------------------
353    /// Clear the object's state.
354    ///
355    /// Clears the symbol context list.
356    //------------------------------------------------------------------
357    void
358    Clear();
359
360    //------------------------------------------------------------------
361    /// Dump a description of this object to a Stream.
362    ///
363    /// Dump a description of the contents of each symbol context in
364    /// the list to the supplied stream \a s.
365    ///
366    /// @param[in] s
367    ///     The stream to which to dump the object descripton.
368    //------------------------------------------------------------------
369    void
370    Dump(Stream *s, Target *target) const;
371
372    //------------------------------------------------------------------
373    /// Get accessor for a symbol context at index \a idx.
374    ///
375    /// Dump a description of the contents of each symbol context in
376    /// the list to the supplied stream \a s.
377    ///
378    /// @param[in] idx
379    ///     The zero based index into the symbol context list.
380    ///
381    /// @param[out] sc
382    ///     A reference to the symbol context to fill in.
383    ///
384    /// @return
385    ///     Returns \b true if \a idx was a valid index into this
386    ///     symbol context list and \a sc was filled in, \b false
387    ///     otherwise.
388    //------------------------------------------------------------------
389    bool
390    GetContextAtIndex(uint32_t idx, SymbolContext& sc) const;
391
392    bool
393    RemoveContextAtIndex (uint32_t idx);
394    //------------------------------------------------------------------
395    /// Get accessor for a symbol context list size.
396    ///
397    /// @return
398    ///     Returns the number of symbol context objects in the list.
399    //------------------------------------------------------------------
400    uint32_t
401    GetSize() const;
402
403    uint32_t
404    NumLineEntriesWithLine (uint32_t line) const;
405
406protected:
407    typedef std::vector<SymbolContext> collection; ///< The collection type for the list.
408
409    //------------------------------------------------------------------
410    // Member variables.
411    //------------------------------------------------------------------
412    collection m_symbol_contexts; ///< The list of symbol contexts.
413};
414
415bool operator== (const SymbolContext& lhs, const SymbolContext& rhs);
416bool operator!= (const SymbolContext& lhs, const SymbolContext& rhs);
417
418} // namespace lldb_private
419
420#endif  // liblldb_SymbolContext_h_
421