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