DWARFExpression.h revision a1b9a90b7aef446302de9b845dc4f3b0e1473aa7
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    /// @return
146    ///     True if IsLocationList() is false and the \a file_addr was
147    ///     is contained in a DW_OP_addr location opcode or if \a file_addr
148    ///     was invalid and there are any DW_OP_addr opcodes, false
149    ///     otherwise.
150    //------------------------------------------------------------------
151    bool
152    LocationContains_DW_OP_addr (lldb::addr_t file_addr = LLDB_INVALID_ADDRESS) const;
153
154    bool
155    Update_DW_OP_addr (lldb::addr_t file_addr);
156
157    //------------------------------------------------------------------
158    /// Make the expression parser read its location information from a
159    /// given data source.  Does not change the offset and length
160    ///
161    /// @param[in] data
162    ///     A data extractor configured to read the DWARF location expression's
163    ///     bytecode.
164    //------------------------------------------------------------------
165    void
166    SetOpcodeData(const DataExtractor& data);
167
168    //------------------------------------------------------------------
169    /// Make the expression parser read its location information from a
170    /// given data source
171    ///
172    /// @param[in] data
173    ///     A data extractor configured to read the DWARF location expression's
174    ///     bytecode.
175    ///
176    /// @param[in] data_offset
177    ///     The offset of the location expression in the extractor.
178    ///
179    /// @param[in] data_length
180    ///     The byte length of the location expression.
181    //------------------------------------------------------------------
182    void
183    SetOpcodeData(const DataExtractor& data, uint32_t data_offset, uint32_t data_length);
184
185    //------------------------------------------------------------------
186    /// Tells the expression that it refers to a location list.
187    ///
188    /// @param[in] slide
189    ///     This value should be a slide that is applied to any values
190    ///     in the location list data so the values become zero based
191    ///     offsets into the object that owns the location list. We need
192    ///     to make location lists relative to the objects that own them
193    ///     so we can relink addresses on the fly.
194    //------------------------------------------------------------------
195    void
196    SetLocationListSlide (lldb::addr_t slide);
197
198    //------------------------------------------------------------------
199    /// Return the call-frame-info style register kind
200    //------------------------------------------------------------------
201    int
202    GetRegisterKind ();
203
204    //------------------------------------------------------------------
205    /// Set the call-frame-info style register kind
206    ///
207    /// @param[in] reg_kind
208    ///     The register kind.
209    //------------------------------------------------------------------
210    void
211    SetRegisterKind (lldb::RegisterKind reg_kind);
212
213    //------------------------------------------------------------------
214    /// Wrapper for the static evaluate function that accepts an
215    /// ExecutionContextScope instead of an ExecutionContext and uses
216    /// member variables to populate many operands
217    //------------------------------------------------------------------
218    bool
219    Evaluate (ExecutionContextScope *exe_scope,
220              clang::ASTContext *ast_context,
221              ClangExpressionVariableList *expr_locals,
222              ClangExpressionDeclMap *decl_map,
223              lldb::addr_t loclist_base_load_addr,
224              const Value* initial_value_ptr,
225              Value& result,
226              Error *error_ptr) const;
227
228    //------------------------------------------------------------------
229    /// Wrapper for the static evaluate function that uses member
230    /// variables to populate many operands
231    //------------------------------------------------------------------
232    bool
233    Evaluate (ExecutionContext *exe_ctx,
234              clang::ASTContext *ast_context,
235              ClangExpressionVariableList *expr_locals,
236              ClangExpressionDeclMap *decl_map,
237              RegisterContext *reg_ctx,
238              lldb::addr_t loclist_base_load_addr,
239              const Value* initial_value_ptr,
240              Value& result,
241              Error *error_ptr) const;
242
243    //------------------------------------------------------------------
244    /// Evaluate a DWARF location expression in a particular context
245    ///
246    /// @param[in] exe_ctx
247    ///     The execution context in which to evaluate the location
248    ///     expression.  The location expression may access the target's
249    ///     memory, especially if it comes from the expression parser.
250    ///
251    /// @param[in] ast_context
252    ///     The context in which to interpret types.
253    ///
254    /// @param[in] opcodes
255    ///     This is a static method so the opcodes need to be provided
256    ///     explicitly.
257    ///
258    /// @param[in] expr_locals
259    ///     If the location expression was produced by the expression parser,
260    ///     the list of local variables referenced by the DWARF expression.
261    ///     This list should already have been populated during parsing;
262    ///     the DWARF expression refers to variables by index.  Can be NULL if
263    ///     the location expression uses no locals.
264    ///
265    /// @param[in] decl_map
266    ///     If the location expression was produced by the expression parser,
267    ///     the list of external variables referenced by the location
268    ///     expression.  Can be NULL if the location expression uses no
269    ///     external variables.
270    ///
271    ///  @param[in] reg_ctx
272    ///     An optional parameter which provides a RegisterContext for use
273    ///     when evaluating the expression (i.e. for fetching register values).
274    ///     Normally this will come from the ExecutionContext's StackFrame but
275    ///     in the case where an expression needs to be evaluated while building
276    ///     the stack frame list, this short-cut is available.
277    ///
278    /// @param[in] offset
279    ///     The offset of the location expression in the data extractor.
280    ///
281    /// @param[in] length
282    ///     The length in bytes of the location expression.
283    ///
284    /// @param[in] reg_set
285    ///     The call-frame-info style register kind.
286    ///
287    /// @param[in] initial_value_ptr
288    ///     A value to put on top of the interpreter stack before evaluating
289    ///     the expression, if the expression is parametrized.  Can be NULL.
290    ///
291    /// @param[in] result
292    ///     A value into which the result of evaluating the expression is
293    ///     to be placed.
294    ///
295    /// @param[in] error_ptr
296    ///     If non-NULL, used to report errors in expression evaluation.
297    ///
298    /// @return
299    ///     True on success; false otherwise.  If error_ptr is non-NULL,
300    ///     details of the failure are provided through it.
301    //------------------------------------------------------------------
302    static bool
303    Evaluate (ExecutionContext *exe_ctx,
304              clang::ASTContext *ast_context,
305              ClangExpressionVariableList *expr_locals,
306              ClangExpressionDeclMap *decl_map,
307              RegisterContext *reg_ctx,
308              const DataExtractor& opcodes,
309              const uint32_t offset,
310              const uint32_t length,
311              const uint32_t reg_set,
312              const Value* initial_value_ptr,
313              Value& result,
314              Error *error_ptr);
315
316    //------------------------------------------------------------------
317    /// Loads a ClangExpressionVariableList into the object
318    ///
319    /// @param[in] locals
320    ///     If non-NULL, the list of locals used by this expression.
321    ///     See Evaluate().
322    //------------------------------------------------------------------
323    void
324    SetExpressionLocalVariableList (ClangExpressionVariableList *locals);
325
326    //------------------------------------------------------------------
327    /// Loads a ClangExpressionDeclMap into the object
328    ///
329    /// @param[in] locals
330    ///     If non-NULL, the list of external variables used by this
331    ///     expression.  See Evaluate().
332    //------------------------------------------------------------------
333    void
334    SetExpressionDeclMap (ClangExpressionDeclMap *decl_map);
335
336    bool
337    GetExpressionData (DataExtractor &data) const
338    {
339        data = m_data;
340        return data.GetByteSize() > 0;
341    }
342
343    bool
344    DumpLocationForAddress (Stream *s,
345                            lldb::DescriptionLevel level,
346                            lldb::addr_t loclist_base_load_addr,
347                            lldb::addr_t address,
348                            ABI *abi);
349
350protected:
351    //------------------------------------------------------------------
352    /// Pretty-prints the location expression to a stream
353    ///
354    /// @param[in] stream
355    ///     The stream to use for pretty-printing.
356    ///
357    /// @param[in] offset
358    ///     The offset into the data buffer of the opcodes to be printed.
359    ///
360    /// @param[in] length
361    ///     The length in bytes of the opcodes to be printed.
362    ///
363    /// @param[in] level
364    ///     The level of detail to use in pretty-printing.
365    ///
366    /// @param[in] abi
367    ///     An optional ABI plug-in that can be used to resolve register
368    ///     names.
369    //------------------------------------------------------------------
370    void
371    DumpLocation(Stream *s,
372                 uint32_t offset,
373                 uint32_t length,
374                 lldb::DescriptionLevel level,
375                 ABI *abi) const;
376
377    bool
378    GetLocation (lldb::addr_t base_addr,
379                 lldb::addr_t pc,
380                 uint32_t &offset,
381                 uint32_t &len);
382
383    //------------------------------------------------------------------
384    /// Classes that inherit from DWARFExpression can see and modify these
385    //------------------------------------------------------------------
386
387    DataExtractor m_data;                       ///< A data extractor capable of reading opcode bytes
388    lldb::RegisterKind m_reg_kind;              ///< One of the defines that starts with LLDB_REGKIND_
389    lldb::addr_t m_loclist_slide;               ///< A value used to slide the location list offsets so that
390                                                ///< they are relative to the object that owns the location list
391                                                ///< (the function for frame base and variable location lists)
392
393};
394
395} // namespace lldb_private
396
397#endif  // liblldb_DWARFExpression_h_
398