SymbolContext.h revision 263363
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/Core/Mangled.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    ~SymbolContext ();
101    //------------------------------------------------------------------
102    /// Copy constructor
103    ///
104    /// Makes a copy of the another SymbolContext object \a rhs.
105    ///
106    /// @param[in] rhs
107    ///     A const SymbolContext object reference to copy.
108    //------------------------------------------------------------------
109    SymbolContext (const SymbolContext& rhs);
110
111    //------------------------------------------------------------------
112    /// Assignment operator.
113    ///
114    /// Copies the address value from another SymbolContext object \a
115    /// rhs into \a this object.
116    ///
117    /// @param[in] rhs
118    ///     A const SymbolContext object reference to copy.
119    ///
120    /// @return
121    ///     A const SymbolContext object reference to \a this.
122    //------------------------------------------------------------------
123    const SymbolContext&
124    operator= (const SymbolContext& rhs);
125
126    //------------------------------------------------------------------
127    /// Clear the object's state.
128    ///
129    /// Resets all pointer members to NULL, and clears any class objects
130    /// to their default state.
131    //------------------------------------------------------------------
132    void
133    Clear (bool clear_target);
134
135    //------------------------------------------------------------------
136    /// Dump a description of this object to a Stream.
137    ///
138    /// Dump a description of the contents of this object to the
139    /// supplied stream \a s.
140    ///
141    /// @param[in] s
142    ///     The stream to which to dump the object descripton.
143    //------------------------------------------------------------------
144    void
145    Dump (Stream *s, Target *target) const;
146
147    //------------------------------------------------------------------
148    /// Dump the stop context in this object to a Stream.
149    ///
150    /// Dump the best description of this object to the stream. The
151    /// information displayed depends on the amount and quality of the
152    /// information in this context. If a module, function, file and
153    /// line number are available, they will be dumped. If only a
154    /// module and function or symbol name with offset is available,
155    /// that will be output. Else just the address at which the target
156    /// was stopped will be displayed.
157    ///
158    /// @param[in] s
159    ///     The stream to which to dump the object descripton.
160    ///
161    /// @param[in] so_addr
162    ///     The resolved section offset address.
163    //------------------------------------------------------------------
164    bool
165    DumpStopContext (Stream *s,
166                     ExecutionContextScope *exe_scope,
167                     const Address &so_addr,
168                     bool show_fullpaths,
169                     bool show_module,
170                     bool show_inlined_frames) const;
171
172    //------------------------------------------------------------------
173    /// Get the address range contained within a symbol context.
174    ///
175    /// Address range priority is as follows:
176    ///     - line_entry address range if line_entry is valid and eSymbolContextLineEntry is set in \a scope
177    ///     - block address range if block is not NULL and eSymbolContextBlock is set in \a scope
178    ///     - function address range if function is not NULL and eSymbolContextFunction is set in \a scope
179    ///     - symbol address range if symbol is not NULL and eSymbolContextSymbol is set in \a scope
180    ///
181    /// @param[in] scope
182    ///     A mask of symbol context bits telling this function which
183    ///     address ranges it can use when trying to extract one from
184    ///     the valid (non-NULL) symbol context classes.
185    ///
186    /// @param[in] range_idx
187    ///     The address range index to grab. Since many functions and
188    ///     blocks are not always contiguous, they may have more than
189    ///     one address range.
190    ///
191    /// @param[in] use_inline_block_range
192    ///     If \a scope has the eSymbolContextBlock bit set, and there
193    ///     is a valid block in the symbol context, return the block
194    ///     address range for the containing inline function block, not
195    ///     the deepest most block. This allows us to extract information
196    ///     for the address range of the inlined function block, not
197    ///     the deepest lexical block.
198    ///
199    /// @param[out] range
200    ///     An address range object that will be filled in if \b true
201    ///     is returned.
202    ///
203    /// @return
204    ///     \b True if this symbol context contains items that describe
205    ///     an address range, \b false otherwise.
206    //------------------------------------------------------------------
207    bool
208    GetAddressRange (uint32_t scope,
209                     uint32_t range_idx,
210                     bool use_inline_block_range,
211                     AddressRange &range) const;
212
213
214    void
215    GetDescription(Stream *s,
216                   lldb::DescriptionLevel level,
217                   Target *target) const;
218
219    uint32_t
220    GetResolvedMask () const;
221
222
223    //------------------------------------------------------------------
224    /// Find a block that defines the function represented by this
225    /// symbol context.
226    ///
227    /// If this symbol context points to a block that is an inlined
228    /// function, or is contained within an inlined function, the block
229    /// that defines the inlined function is returned.
230    ///
231    /// If this symbol context has no block in it, or the block is not
232    /// itself an inlined function block or contained within one, we
233    /// return the top level function block.
234    ///
235    /// This is a handy function to call when you want to get the block
236    /// whose variable list will include the arguments for the function
237    /// that is represented by this symbol context (whether the function
238    /// is an inline function or not).
239    ///
240    /// @return
241    ///     The block object pointer that defines the function that is
242    ///     represented by this symbol context object, NULL otherwise.
243    //------------------------------------------------------------------
244    Block *
245    GetFunctionBlock ();
246
247
248    //------------------------------------------------------------------
249    /// If this symbol context represents a function that is a method,
250    /// return true and provide information about the method.
251    ///
252    /// @param[out] language
253    ///     If \b true is returned, the language for the method.
254    ///
255    /// @param[out] is_instance_method
256    ///     If \b true is returned, \b true if this is a instance method,
257    ///     \b false if this is a static/class function.
258    ///
259    /// @param[out] language_object_name
260    ///     If \b true is returned, the name of the artificial variable
261    ///     for the language ("this" for C++, "self" for ObjC).
262    ///
263    /// @return
264    ///     \b True if this symbol context represents a function that
265    ///     is a method of a class, \b false otherwise.
266    //------------------------------------------------------------------
267    bool
268    GetFunctionMethodInfo (lldb::LanguageType &language,
269                           bool &is_instance_method,
270                           ConstString &language_object_name);
271
272    //------------------------------------------------------------------
273    /// Find a name of the innermost function for the symbol context.
274    ///
275    /// For instance, if the symbol context contains an inlined block,
276    /// it will return the inlined function name.
277    ///
278    /// @param[in] prefer_mangled
279    ///    if \btrue, then the mangled name will be returned if there
280    ///    is one.  Otherwise the unmangled name will be returned if it
281    ///    is available.
282    ///
283    /// @return
284    ///     The name of the function represented by this symbol context.
285    //------------------------------------------------------------------
286    ConstString
287    GetFunctionName (Mangled::NamePreference preference = Mangled::ePreferDemangled) const;
288
289
290    //------------------------------------------------------------------
291    /// Get the line entry that corresponds to the function.
292    ///
293    /// If the symbol context contains an inlined block, the line entry
294    /// for the start address of the inlined function will be returned,
295    /// otherwise the line entry for the start address of the function
296    /// will be returned. This can be used after doing a
297    /// Module::FindFunctions(...) or ModuleList::FindFunctions(...)
298    /// call in order to get the correct line table information for
299    /// the symbol context.
300    /// it will return the inlined function name.
301    ///
302    /// @param[in] prefer_mangled
303    ///    if \btrue, then the mangled name will be returned if there
304    ///    is one.  Otherwise the unmangled name will be returned if it
305    ///    is available.
306    ///
307    /// @return
308    ///     The name of the function represented by this symbol context.
309    //------------------------------------------------------------------
310    LineEntry
311    GetFunctionStartLineEntry () const;
312
313    //------------------------------------------------------------------
314    /// Find the block containing the inlined block that contains this block.
315    ///
316    /// For instance, if the symbol context contains an inlined block,
317    /// it will return the inlined function name.
318    ///
319    /// @param[in] curr_frame_pc
320    ///    The address within the block of this object.
321    ///
322    /// @param[out] next_frame_sc
323    ///     A new symbol context that does what the title says it does.
324    ///
325    /// @param[out] next_frame_addr
326    ///     This is what you should report as the PC in \a next_frame_sc.
327    ///
328    /// @return
329    ///     \b true if this SymbolContext specifies a block contained in an
330    ///     inlined block.  If this returns \b true, \a next_frame_sc and
331    ///     \a next_frame_addr will be filled in correctly.
332    //------------------------------------------------------------------
333    bool
334    GetParentOfInlinedScope (const Address &curr_frame_pc,
335                             SymbolContext &next_frame_sc,
336                             Address &inlined_frame_addr) const;
337
338    //------------------------------------------------------------------
339    // Member variables
340    //------------------------------------------------------------------
341    lldb::TargetSP  target_sp;  ///< The Target for a given query
342    lldb::ModuleSP  module_sp;  ///< The Module for a given query
343    CompileUnit *   comp_unit;  ///< The CompileUnit for a given query
344    Function *      function;   ///< The Function for a given query
345    Block *         block;      ///< The Block for a given query
346    LineEntry       line_entry; ///< The LineEntry for a given query
347    Symbol *        symbol;     ///< The Symbol for a given query
348};
349
350
351class SymbolContextSpecifier
352{
353public:
354    typedef enum SpecificationType
355    {
356        eNothingSpecified          = 0,
357        eModuleSpecified           = 1 << 0,
358        eFileSpecified             = 1 << 1,
359        eLineStartSpecified        = 1 << 2,
360        eLineEndSpecified          = 1 << 3,
361        eFunctionSpecified         = 1 << 4,
362        eClassOrNamespaceSpecified = 1 << 5,
363        eAddressRangeSpecified     = 1 << 6
364    } SpecificationType;
365
366    // This one produces a specifier that matches everything...
367    SymbolContextSpecifier (const lldb::TargetSP& target_sp);
368
369    ~SymbolContextSpecifier();
370
371    bool
372    AddSpecification (const char *spec_string, SpecificationType type);
373
374    bool
375    AddLineSpecification (uint32_t line_no, SpecificationType type);
376
377    void
378    Clear();
379
380    bool
381    SymbolContextMatches(SymbolContext &sc);
382
383    bool
384    AddressMatches(lldb::addr_t addr);
385
386    void
387    GetDescription (Stream *s, lldb::DescriptionLevel level) const;
388
389private:
390    lldb::TargetSP                 m_target_sp;
391    std::string                    m_module_spec;
392    lldb::ModuleSP                 m_module_sp;
393    std::unique_ptr<FileSpec>       m_file_spec_ap;
394    size_t                         m_start_line;
395    size_t                         m_end_line;
396    std::string                    m_function_spec;
397    std::string                    m_class_name;
398    std::unique_ptr<AddressRange>   m_address_range_ap;
399    uint32_t                       m_type; // Or'ed bits from SpecificationType
400
401};
402
403//----------------------------------------------------------------------
404/// @class SymbolContextList SymbolContext.h "lldb/Symbol/SymbolContext.h"
405/// @brief Defines a list of symbol context objects.
406///
407/// This class provides a common structure that can be used to contain
408/// the result of a query that can contain a multiple results. Examples
409/// of such queries include:
410///     @li Looking up a function by name.
411///     @li Finding all addressses for a specified file and line number.
412//----------------------------------------------------------------------
413class SymbolContextList
414{
415public:
416    //------------------------------------------------------------------
417    /// Default constructor.
418    ///
419    /// Initialize with an empty list.
420    //------------------------------------------------------------------
421    SymbolContextList ();
422
423    //------------------------------------------------------------------
424    /// Destructor.
425    //------------------------------------------------------------------
426    ~SymbolContextList ();
427
428    //------------------------------------------------------------------
429    /// Append a new symbol context to the list.
430    ///
431    /// @param[in] sc
432    ///     A symbol context to append to the list.
433    //------------------------------------------------------------------
434    void
435    Append (const SymbolContext& sc);
436
437    void
438    Append (const SymbolContextList& sc_list);
439
440    bool
441    AppendIfUnique (const SymbolContext& sc,
442                    bool merge_symbol_into_function);
443
444    bool
445    MergeSymbolContextIntoFunctionContext (const SymbolContext& symbol_sc,
446                                           uint32_t start_idx = 0,
447                                           uint32_t stop_idx = UINT32_MAX);
448
449    uint32_t
450    AppendIfUnique (const SymbolContextList& sc_list,
451                    bool merge_symbol_into_function);
452    //------------------------------------------------------------------
453    /// Clear the object's state.
454    ///
455    /// Clears the symbol context list.
456    //------------------------------------------------------------------
457    void
458    Clear();
459
460    //------------------------------------------------------------------
461    /// Dump a description of this object to a Stream.
462    ///
463    /// Dump a description of the contents of each symbol context in
464    /// the list to the supplied stream \a s.
465    ///
466    /// @param[in] s
467    ///     The stream to which to dump the object descripton.
468    //------------------------------------------------------------------
469    void
470    Dump(Stream *s, Target *target) const;
471
472    //------------------------------------------------------------------
473    /// Get accessor for a symbol context at index \a idx.
474    ///
475    /// Dump a description of the contents of each symbol context in
476    /// the list to the supplied stream \a s.
477    ///
478    /// @param[in] idx
479    ///     The zero based index into the symbol context list.
480    ///
481    /// @param[out] sc
482    ///     A reference to the symbol context to fill in.
483    ///
484    /// @return
485    ///     Returns \b true if \a idx was a valid index into this
486    ///     symbol context list and \a sc was filled in, \b false
487    ///     otherwise.
488    //------------------------------------------------------------------
489    bool
490    GetContextAtIndex(size_t idx, SymbolContext& sc) const;
491
492    //------------------------------------------------------------------
493    /// Direct reference accessor for a symbol context at index \a idx.
494    ///
495    /// The index \a idx must be a valid index, no error checking will
496    /// be done to ensure that it is valid.
497    ///
498    /// @param[in] idx
499    ///     The zero based index into the symbol context list.
500    ///
501    /// @return
502    ///     A const reference to the symbol context to fill in.
503    //------------------------------------------------------------------
504    SymbolContext&
505    operator [] (size_t idx)
506    {
507        return m_symbol_contexts[idx];
508    }
509
510    const SymbolContext&
511    operator [] (size_t idx) const
512    {
513        return m_symbol_contexts[idx];
514    }
515
516    //------------------------------------------------------------------
517    /// Get accessor for the last symbol context in the list.
518    ///
519    /// @param[out] sc
520    ///     A reference to the symbol context to fill in.
521    ///
522    /// @return
523    ///     Returns \b true if \a sc was filled in, \b false if the
524    ///     list is empty.
525    //------------------------------------------------------------------
526    bool
527    GetLastContext(SymbolContext& sc) const;
528
529    bool
530    RemoveContextAtIndex (size_t idx);
531    //------------------------------------------------------------------
532    /// Get accessor for a symbol context list size.
533    ///
534    /// @return
535    ///     Returns the number of symbol context objects in the list.
536    //------------------------------------------------------------------
537    uint32_t
538    GetSize() const;
539
540    uint32_t
541    NumLineEntriesWithLine (uint32_t line) const;
542
543    void
544    GetDescription(Stream *s,
545                   lldb::DescriptionLevel level,
546                   Target *target) const;
547
548protected:
549    typedef std::vector<SymbolContext> collection; ///< The collection type for the list.
550
551    //------------------------------------------------------------------
552    // Member variables.
553    //------------------------------------------------------------------
554    collection m_symbol_contexts; ///< The list of symbol contexts.
555};
556
557bool operator== (const SymbolContext& lhs, const SymbolContext& rhs);
558bool operator!= (const SymbolContext& lhs, const SymbolContext& rhs);
559
560bool operator== (const SymbolContextList& lhs, const SymbolContextList& rhs);
561bool operator!= (const SymbolContextList& lhs, const SymbolContextList& rhs);
562
563} // namespace lldb_private
564
565#endif  // liblldb_SymbolContext_h_
566