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