DWARFExpression.h revision 2f28ece553d2ef0d7b3e8d1419020591ec3818f9
1//===-- DWARFExpression.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_DWARFExpression_h_
11#define liblldb_DWARFExpression_h_
12
13#include "lldb/lldb-private.h"
14#include "lldb/Core/ClangForward.h"
15#include "lldb/Core/Address.h"
16#include "lldb/Core/DataExtractor.h"
17#include "lldb/Core/Error.h"
18#include "lldb/Core/Scalar.h"
19
20class ClangExpressionVariable;
21class ClangExpressionVariableList;
22
23namespace lldb_private {
24
25class ClangExpressionDeclMap;
26
27//----------------------------------------------------------------------
28/// @class DWARFExpression DWARFExpression.h "lldb/Expression/DWARFExpression.h"
29/// @brief Encapsulates a DWARF location expression and interprets it.
30///
31/// DWARF location expressions are used in two ways by LLDB.  The first
32/// use is to find entities specified in the debug information, since
33/// their locations are specified in precisely this language.  The second
34/// is to interpret expressions without having to run the target in cases
35/// where the overhead from copying JIT-compiled code into the target is
36/// too high or where the target cannot be run.  This class encapsulates
37/// a single DWARF location expression or a location list and interprets
38/// it.
39//----------------------------------------------------------------------
40class DWARFExpression
41{
42public:
43    //------------------------------------------------------------------
44    /// Constructor
45    //------------------------------------------------------------------
46    DWARFExpression();
47
48    //------------------------------------------------------------------
49    /// Constructor
50    ///
51    /// @param[in] data
52    ///     A data extractor configured to read the DWARF location expression's
53    ///     bytecode.
54    ///
55    /// @param[in] data_offset
56    ///     The offset of the location expression in the extractor.
57    ///
58    /// @param[in] data_length
59    ///     The byte length of the location expression.
60    //------------------------------------------------------------------
61    DWARFExpression(const DataExtractor& data,
62                    uint32_t data_offset,
63                    uint32_t data_length);
64
65    //------------------------------------------------------------------
66    /// Copy constructor
67    //------------------------------------------------------------------
68    DWARFExpression(const DWARFExpression& rhs);
69
70    //------------------------------------------------------------------
71    /// Destructor
72    //------------------------------------------------------------------
73    virtual
74    ~DWARFExpression();
75
76    //------------------------------------------------------------------
77    /// Print the description of the expression to a stream
78    ///
79    /// @param[in] s
80    ///     The stream to print to.
81    ///
82    /// @param[in] level
83    ///     The level of verbosity to use.
84    ///
85    /// @param[in] location_list_base_addr
86    ///     If this is a location list based expression, this is the
87    ///     address of the object that owns it. NOTE: this value is
88    ///     different from the DWARF version of the location list base
89    ///     address which is compile unit relative. This base address
90    ///     is the address of the object that owns the location list.
91    ///
92    /// @param[in] abi
93    ///     An optional ABI plug-in that can be used to resolve register
94    ///     names.
95    //------------------------------------------------------------------
96    void
97    GetDescription (Stream *s,
98                    lldb::DescriptionLevel level,
99                    lldb::addr_t location_list_base_addr,
100                    ABI *abi) const;
101
102    //------------------------------------------------------------------
103    /// Return true if the location expression contains data
104    //------------------------------------------------------------------
105    bool
106    IsValid() const;
107
108    //------------------------------------------------------------------
109    /// Return true if a location list was provided
110    //------------------------------------------------------------------
111    bool
112    IsLocationList() const;
113
114    //------------------------------------------------------------------
115    /// Search for a load address in the location list
116    ///
117    /// @param[in] process
118    ///     The process to use when resolving the load address
119    ///
120    /// @param[in] addr
121    ///     The address to resolve
122    ///
123    /// @return
124    ///     True if IsLocationList() is true and the address was found;
125    ///     false otherwise.
126    //------------------------------------------------------------------
127//    bool
128//    LocationListContainsLoadAddress (Process* process, const Address &addr) const;
129//
130    bool
131    LocationListContainsAddress (lldb::addr_t loclist_base_addr, lldb::addr_t addr) const;
132
133    //------------------------------------------------------------------
134    /// If a location is not a location list, return true if the location
135    /// contains a DW_OP_addr () opcode in the stream that matches \a
136    /// file_addr. If file_addr is LLDB_INVALID_ADDRESS, the this
137    /// function will return true if the variable there is any DW_OP_addr
138    /// in a location that (yet still is NOT a location list). This helps
139    /// us detect if a variable is a global or static variable since
140    /// there is no other indication from DWARF debug info.
141    ///
142    /// @param[in] file_addr
143    ///     The file address to search for in the location.
144    ///
145    /// @param[out] error
146    ///     If the location stream contains unknown DW_OP opcodes or the
147    ///     data is missing, \a error will be set to \b true.
148    ///
149    /// @return
150    ///     True if IsLocationList() is false and the \a file_addr was
151    ///     is contained in a DW_OP_addr location opcode or if \a file_addr
152    ///     was invalid and there are any DW_OP_addr opcodes, false
153    ///     otherwise.
154    //------------------------------------------------------------------
155    bool
156    LocationContains_DW_OP_addr (lldb::addr_t file_addr, bool &error) const;
157
158    bool
159    Update_DW_OP_addr (lldb::addr_t file_addr);
160
161    //------------------------------------------------------------------
162    /// Make the expression parser read its location information from a
163    /// given data source.  Does not change the offset and length
164    ///
165    /// @param[in] data
166    ///     A data extractor configured to read the DWARF location expression's
167    ///     bytecode.
168    //------------------------------------------------------------------
169    void
170    SetOpcodeData(const DataExtractor& data);
171
172    //------------------------------------------------------------------
173    /// Make the expression parser read its location information from a
174    /// given data source
175    ///
176    /// @param[in] data
177    ///     A data extractor configured to read the DWARF location expression's
178    ///     bytecode.
179    ///
180    /// @param[in] data_offset
181    ///     The offset of the location expression in the extractor.
182    ///
183    /// @param[in] data_length
184    ///     The byte length of the location expression.
185    //------------------------------------------------------------------
186    void
187    SetOpcodeData(const DataExtractor& data, uint32_t data_offset, uint32_t data_length);
188
189    //------------------------------------------------------------------
190    /// Tells the expression that it refers to a location list.
191    ///
192    /// @param[in] slide
193    ///     This value should be a slide that is applied to any values
194    ///     in the location list data so the values become zero based
195    ///     offsets into the object that owns the location list. We need
196    ///     to make location lists relative to the objects that own them
197    ///     so we can relink addresses on the fly.
198    //------------------------------------------------------------------
199    void
200    SetLocationListSlide (lldb::addr_t slide);
201
202    //------------------------------------------------------------------
203    /// Return the call-frame-info style register kind
204    //------------------------------------------------------------------
205    int
206    GetRegisterKind ();
207
208    //------------------------------------------------------------------
209    /// Set the call-frame-info style register kind
210    ///
211    /// @param[in] reg_kind
212    ///     The register kind.
213    //------------------------------------------------------------------
214    void
215    SetRegisterKind (lldb::RegisterKind reg_kind);
216
217    //------------------------------------------------------------------
218    /// Wrapper for the static evaluate function that accepts an
219    /// ExecutionContextScope instead of an ExecutionContext and uses
220    /// member variables to populate many operands
221    //------------------------------------------------------------------
222    bool
223    Evaluate (ExecutionContextScope *exe_scope,
224              clang::ASTContext *ast_context,
225              ClangExpressionVariableList *expr_locals,
226              ClangExpressionDeclMap *decl_map,
227              lldb::addr_t loclist_base_load_addr,
228              const Value* initial_value_ptr,
229              Value& result,
230              Error *error_ptr) const;
231
232    //------------------------------------------------------------------
233    /// Wrapper for the static evaluate function that uses member
234    /// variables to populate many operands
235    //------------------------------------------------------------------
236    bool
237    Evaluate (ExecutionContext *exe_ctx,
238              clang::ASTContext *ast_context,
239              ClangExpressionVariableList *expr_locals,
240              ClangExpressionDeclMap *decl_map,
241              RegisterContext *reg_ctx,
242              lldb::addr_t loclist_base_load_addr,
243              const Value* initial_value_ptr,
244              Value& result,
245              Error *error_ptr) const;
246
247    //------------------------------------------------------------------
248    /// Evaluate a DWARF location expression in a particular context
249    ///
250    /// @param[in] exe_ctx
251    ///     The execution context in which to evaluate the location
252    ///     expression.  The location expression may access the target's
253    ///     memory, especially if it comes from the expression parser.
254    ///
255    /// @param[in] ast_context
256    ///     The context in which to interpret types.
257    ///
258    /// @param[in] opcodes
259    ///     This is a static method so the opcodes need to be provided
260    ///     explicitly.
261    ///
262    /// @param[in] expr_locals
263    ///     If the location expression was produced by the expression parser,
264    ///     the list of local variables referenced by the DWARF expression.
265    ///     This list should already have been populated during parsing;
266    ///     the DWARF expression refers to variables by index.  Can be NULL if
267    ///     the location expression uses no locals.
268    ///
269    /// @param[in] decl_map
270    ///     If the location expression was produced by the expression parser,
271    ///     the list of external variables referenced by the location
272    ///     expression.  Can be NULL if the location expression uses no
273    ///     external variables.
274    ///
275    ///  @param[in] reg_ctx
276    ///     An optional parameter which provides a RegisterContext for use
277    ///     when evaluating the expression (i.e. for fetching register values).
278    ///     Normally this will come from the ExecutionContext's StackFrame but
279    ///     in the case where an expression needs to be evaluated while building
280    ///     the stack frame list, this short-cut is available.
281    ///
282    /// @param[in] offset
283    ///     The offset of the location expression in the data extractor.
284    ///
285    /// @param[in] length
286    ///     The length in bytes of the location expression.
287    ///
288    /// @param[in] reg_set
289    ///     The call-frame-info style register kind.
290    ///
291    /// @param[in] initial_value_ptr
292    ///     A value to put on top of the interpreter stack before evaluating
293    ///     the expression, if the expression is parametrized.  Can be NULL.
294    ///
295    /// @param[in] result
296    ///     A value into which the result of evaluating the expression is
297    ///     to be placed.
298    ///
299    /// @param[in] error_ptr
300    ///     If non-NULL, used to report errors in expression evaluation.
301    ///
302    /// @return
303    ///     True on success; false otherwise.  If error_ptr is non-NULL,
304    ///     details of the failure are provided through it.
305    //------------------------------------------------------------------
306    static bool
307    Evaluate (ExecutionContext *exe_ctx,
308              clang::ASTContext *ast_context,
309              ClangExpressionVariableList *expr_locals,
310              ClangExpressionDeclMap *decl_map,
311              RegisterContext *reg_ctx,
312              const DataExtractor& opcodes,
313              const uint32_t offset,
314              const uint32_t length,
315              const uint32_t reg_set,
316              const Value* initial_value_ptr,
317              Value& result,
318              Error *error_ptr);
319
320    //------------------------------------------------------------------
321    /// Loads a ClangExpressionVariableList into the object
322    ///
323    /// @param[in] locals
324    ///     If non-NULL, the list of locals used by this expression.
325    ///     See Evaluate().
326    //------------------------------------------------------------------
327    void
328    SetExpressionLocalVariableList (ClangExpressionVariableList *locals);
329
330    //------------------------------------------------------------------
331    /// Loads a ClangExpressionDeclMap into the object
332    ///
333    /// @param[in] locals
334    ///     If non-NULL, the list of external variables used by this
335    ///     expression.  See Evaluate().
336    //------------------------------------------------------------------
337    void
338    SetExpressionDeclMap (ClangExpressionDeclMap *decl_map);
339
340    bool
341    GetExpressionData (DataExtractor &data) const
342    {
343        data = m_data;
344        return data.GetByteSize() > 0;
345    }
346
347    bool
348    DumpLocationForAddress (Stream *s,
349                            lldb::DescriptionLevel level,
350                            lldb::addr_t loclist_base_load_addr,
351                            lldb::addr_t address,
352                            ABI *abi);
353
354protected:
355    //------------------------------------------------------------------
356    /// Pretty-prints the location expression to a stream
357    ///
358    /// @param[in] stream
359    ///     The stream to use for pretty-printing.
360    ///
361    /// @param[in] offset
362    ///     The offset into the data buffer of the opcodes to be printed.
363    ///
364    /// @param[in] length
365    ///     The length in bytes of the opcodes to be printed.
366    ///
367    /// @param[in] level
368    ///     The level of detail to use in pretty-printing.
369    ///
370    /// @param[in] abi
371    ///     An optional ABI plug-in that can be used to resolve register
372    ///     names.
373    //------------------------------------------------------------------
374    void
375    DumpLocation(Stream *s,
376                 uint32_t offset,
377                 uint32_t length,
378                 lldb::DescriptionLevel level,
379                 ABI *abi) const;
380
381    bool
382    GetLocation (lldb::addr_t base_addr,
383                 lldb::addr_t pc,
384                 uint32_t &offset,
385                 uint32_t &len);
386
387    //------------------------------------------------------------------
388    /// Classes that inherit from DWARFExpression can see and modify these
389    //------------------------------------------------------------------
390
391    DataExtractor m_data;                       ///< A data extractor capable of reading opcode bytes
392    lldb::RegisterKind m_reg_kind;              ///< One of the defines that starts with LLDB_REGKIND_
393    lldb::addr_t m_loclist_slide;               ///< A value used to slide the location list offsets so that
394                                                ///< they are relative to the object that owns the location list
395                                                ///< (the function for frame base and variable location lists)
396
397};
398
399} // namespace lldb_private
400
401#endif  // liblldb_DWARFExpression_h_
402