1254721Semaste//===-- DWARFExpression.h ---------------------------------------*- C++ -*-===//
2254721Semaste//
3254721Semaste//                     The LLVM Compiler Infrastructure
4254721Semaste//
5254721Semaste// This file is distributed under the University of Illinois Open Source
6254721Semaste// License. See LICENSE.TXT for details.
7254721Semaste//
8254721Semaste//===----------------------------------------------------------------------===//
9254721Semaste
10254721Semaste#ifndef liblldb_DWARFExpression_h_
11254721Semaste#define liblldb_DWARFExpression_h_
12254721Semaste
13254721Semaste#include "lldb/lldb-private.h"
14254721Semaste#include "lldb/Core/ClangForward.h"
15254721Semaste#include "lldb/Core/Address.h"
16254721Semaste#include "lldb/Core/DataExtractor.h"
17254721Semaste#include "lldb/Core/Error.h"
18254721Semaste#include "lldb/Core/Scalar.h"
19254721Semaste
20254721Semastenamespace lldb_private {
21254721Semaste
22254721Semasteclass ClangExpressionVariable;
23254721Semasteclass ClangExpressionVariableList;
24254721Semaste
25254721Semasteclass ClangExpressionDeclMap;
26254721Semaste
27254721Semaste//----------------------------------------------------------------------
28254721Semaste/// @class DWARFExpression DWARFExpression.h "lldb/Expression/DWARFExpression.h"
29254721Semaste/// @brief Encapsulates a DWARF location expression and interprets it.
30254721Semaste///
31254721Semaste/// DWARF location expressions are used in two ways by LLDB.  The first
32254721Semaste/// use is to find entities specified in the debug information, since
33254721Semaste/// their locations are specified in precisely this language.  The second
34254721Semaste/// is to interpret expressions without having to run the target in cases
35254721Semaste/// where the overhead from copying JIT-compiled code into the target is
36254721Semaste/// too high or where the target cannot be run.  This class encapsulates
37254721Semaste/// a single DWARF location expression or a location list and interprets
38254721Semaste/// it.
39254721Semaste//----------------------------------------------------------------------
40254721Semasteclass DWARFExpression
41254721Semaste{
42254721Semastepublic:
43254721Semaste    //------------------------------------------------------------------
44254721Semaste    /// Constructor
45254721Semaste    //------------------------------------------------------------------
46254721Semaste    DWARFExpression();
47254721Semaste
48254721Semaste    //------------------------------------------------------------------
49254721Semaste    /// Constructor
50254721Semaste    ///
51254721Semaste    /// @param[in] data
52254721Semaste    ///     A data extractor configured to read the DWARF location expression's
53254721Semaste    ///     bytecode.
54254721Semaste    ///
55254721Semaste    /// @param[in] data_offset
56254721Semaste    ///     The offset of the location expression in the extractor.
57254721Semaste    ///
58254721Semaste    /// @param[in] data_length
59254721Semaste    ///     The byte length of the location expression.
60254721Semaste    //------------------------------------------------------------------
61263363Semaste    DWARFExpression(lldb::ModuleSP module,
62263363Semaste                    const DataExtractor& data,
63254721Semaste                    lldb::offset_t data_offset,
64254721Semaste                    lldb::offset_t data_length);
65254721Semaste
66254721Semaste    //------------------------------------------------------------------
67254721Semaste    /// Copy constructor
68254721Semaste    //------------------------------------------------------------------
69254721Semaste    DWARFExpression(const DWARFExpression& rhs);
70254721Semaste
71254721Semaste    //------------------------------------------------------------------
72254721Semaste    /// Destructor
73254721Semaste    //------------------------------------------------------------------
74254721Semaste    virtual
75254721Semaste    ~DWARFExpression();
76254721Semaste
77254721Semaste    //------------------------------------------------------------------
78254721Semaste    /// Print the description of the expression to a stream
79254721Semaste    ///
80254721Semaste    /// @param[in] s
81254721Semaste    ///     The stream to print to.
82254721Semaste    ///
83254721Semaste    /// @param[in] level
84254721Semaste    ///     The level of verbosity to use.
85254721Semaste    ///
86254721Semaste    /// @param[in] location_list_base_addr
87254721Semaste    ///     If this is a location list based expression, this is the
88254721Semaste    ///     address of the object that owns it. NOTE: this value is
89254721Semaste    ///     different from the DWARF version of the location list base
90254721Semaste    ///     address which is compile unit relative. This base address
91254721Semaste    ///     is the address of the object that owns the location list.
92254721Semaste    ///
93254721Semaste    /// @param[in] abi
94254721Semaste    ///     An optional ABI plug-in that can be used to resolve register
95254721Semaste    ///     names.
96254721Semaste    //------------------------------------------------------------------
97254721Semaste    void
98254721Semaste    GetDescription (Stream *s,
99254721Semaste                    lldb::DescriptionLevel level,
100254721Semaste                    lldb::addr_t location_list_base_addr,
101254721Semaste                    ABI *abi) const;
102254721Semaste
103254721Semaste    //------------------------------------------------------------------
104254721Semaste    /// Return true if the location expression contains data
105254721Semaste    //------------------------------------------------------------------
106254721Semaste    bool
107254721Semaste    IsValid() const;
108254721Semaste
109254721Semaste    //------------------------------------------------------------------
110254721Semaste    /// Return true if a location list was provided
111254721Semaste    //------------------------------------------------------------------
112254721Semaste    bool
113254721Semaste    IsLocationList() const;
114254721Semaste
115254721Semaste    //------------------------------------------------------------------
116254721Semaste    /// Search for a load address in the location list
117254721Semaste    ///
118254721Semaste    /// @param[in] process
119254721Semaste    ///     The process to use when resolving the load address
120254721Semaste    ///
121254721Semaste    /// @param[in] addr
122254721Semaste    ///     The address to resolve
123254721Semaste    ///
124254721Semaste    /// @return
125254721Semaste    ///     True if IsLocationList() is true and the address was found;
126254721Semaste    ///     false otherwise.
127254721Semaste    //------------------------------------------------------------------
128254721Semaste//    bool
129254721Semaste//    LocationListContainsLoadAddress (Process* process, const Address &addr) const;
130254721Semaste//
131254721Semaste    bool
132254721Semaste    LocationListContainsAddress (lldb::addr_t loclist_base_addr, lldb::addr_t addr) const;
133254721Semaste
134254721Semaste    //------------------------------------------------------------------
135254721Semaste    /// If a location is not a location list, return true if the location
136254721Semaste    /// contains a DW_OP_addr () opcode in the stream that matches \a
137254721Semaste    /// file_addr. If file_addr is LLDB_INVALID_ADDRESS, the this
138254721Semaste    /// function will return true if the variable there is any DW_OP_addr
139254721Semaste    /// in a location that (yet still is NOT a location list). This helps
140254721Semaste    /// us detect if a variable is a global or static variable since
141254721Semaste    /// there is no other indication from DWARF debug info.
142254721Semaste    ///
143254721Semaste    /// @param[in] op_addr_idx
144254721Semaste    ///     The DW_OP_addr index to retrieve in case there is more than
145254721Semaste    ///     one DW_OP_addr opcode in the location byte stream.
146254721Semaste    ///
147254721Semaste    /// @param[out] error
148254721Semaste    ///     If the location stream contains unknown DW_OP opcodes or the
149254721Semaste    ///     data is missing, \a error will be set to \b true.
150254721Semaste    ///
151254721Semaste    /// @return
152254721Semaste    ///     LLDB_INVALID_ADDRESS if the location doesn't contain a
153254721Semaste    ///     DW_OP_addr for \a op_addr_idx, otherwise a valid file address
154254721Semaste    //------------------------------------------------------------------
155254721Semaste    lldb::addr_t
156254721Semaste    GetLocation_DW_OP_addr (uint32_t op_addr_idx, bool &error) const;
157254721Semaste
158254721Semaste    bool
159254721Semaste    Update_DW_OP_addr (lldb::addr_t file_addr);
160254721Semaste
161254721Semaste    //------------------------------------------------------------------
162254721Semaste    /// Make the expression parser read its location information from a
163254721Semaste    /// given data source.  Does not change the offset and length
164254721Semaste    ///
165254721Semaste    /// @param[in] data
166254721Semaste    ///     A data extractor configured to read the DWARF location expression's
167254721Semaste    ///     bytecode.
168254721Semaste    //------------------------------------------------------------------
169254721Semaste    void
170254721Semaste    SetOpcodeData(const DataExtractor& data);
171254721Semaste
172254721Semaste    //------------------------------------------------------------------
173254721Semaste    /// Make the expression parser read its location information from a
174254721Semaste    /// given data source
175254721Semaste    ///
176263363Semaste    /// @param[in] module_sp
177263363Semaste    ///     The module that defines the DWARF expression.
178263363Semaste    ///
179254721Semaste    /// @param[in] data
180254721Semaste    ///     A data extractor configured to read the DWARF location expression's
181254721Semaste    ///     bytecode.
182254721Semaste    ///
183254721Semaste    /// @param[in] data_offset
184254721Semaste    ///     The offset of the location expression in the extractor.
185254721Semaste    ///
186254721Semaste    /// @param[in] data_length
187254721Semaste    ///     The byte length of the location expression.
188254721Semaste    //------------------------------------------------------------------
189254721Semaste    void
190263363Semaste    SetOpcodeData(lldb::ModuleSP module_sp, const DataExtractor& data, lldb::offset_t data_offset, lldb::offset_t data_length);
191254721Semaste
192254721Semaste    //------------------------------------------------------------------
193254721Semaste    /// Copy the DWARF location expression into a local buffer.
194254721Semaste    ///
195254721Semaste    /// It is a good idea to copy the data so we don't keep the entire
196254721Semaste    /// object file worth of data around just for a few bytes of location
197254721Semaste    /// expression. LLDB typically will mmap the entire contents of debug
198254721Semaste    /// information files, and if we use SetOpcodeData, it will get a
199254721Semaste    /// shared reference to all of this data for the and cause the object
200254721Semaste    /// file to have to stay around. Even worse, a very very large ".a"
201254721Semaste    /// that contains one or more .o files could end up being referenced.
202254721Semaste    /// Location lists are typically small so even though we are copying
203254721Semaste    /// the data, it shouldn't amount to that much for the variables we
204254721Semaste    /// end up parsing.
205254721Semaste    ///
206263363Semaste    /// @param[in] module_sp
207263363Semaste    ///     The module that defines the DWARF expression.
208263363Semaste    ///
209254721Semaste    /// @param[in] data
210254721Semaste    ///     A data extractor configured to read and copy the DWARF
211254721Semaste    ///     location expression's bytecode.
212254721Semaste    ///
213254721Semaste    /// @param[in] data_offset
214254721Semaste    ///     The offset of the location expression in the extractor.
215254721Semaste    ///
216254721Semaste    /// @param[in] data_length
217254721Semaste    ///     The byte length of the location expression.
218254721Semaste    //------------------------------------------------------------------
219254721Semaste    void
220263363Semaste    CopyOpcodeData (lldb::ModuleSP module_sp,
221263363Semaste                    const DataExtractor& data,
222254721Semaste                    lldb::offset_t data_offset,
223254721Semaste                    lldb::offset_t data_length);
224254721Semaste
225254721Semaste
226254721Semaste    //------------------------------------------------------------------
227254721Semaste    /// Tells the expression that it refers to a location list.
228254721Semaste    ///
229254721Semaste    /// @param[in] slide
230254721Semaste    ///     This value should be a slide that is applied to any values
231254721Semaste    ///     in the location list data so the values become zero based
232254721Semaste    ///     offsets into the object that owns the location list. We need
233254721Semaste    ///     to make location lists relative to the objects that own them
234254721Semaste    ///     so we can relink addresses on the fly.
235254721Semaste    //------------------------------------------------------------------
236254721Semaste    void
237254721Semaste    SetLocationListSlide (lldb::addr_t slide);
238254721Semaste
239254721Semaste    //------------------------------------------------------------------
240254721Semaste    /// Return the call-frame-info style register kind
241254721Semaste    //------------------------------------------------------------------
242254721Semaste    int
243254721Semaste    GetRegisterKind ();
244254721Semaste
245254721Semaste    //------------------------------------------------------------------
246254721Semaste    /// Set the call-frame-info style register kind
247254721Semaste    ///
248254721Semaste    /// @param[in] reg_kind
249254721Semaste    ///     The register kind.
250254721Semaste    //------------------------------------------------------------------
251254721Semaste    void
252254721Semaste    SetRegisterKind (lldb::RegisterKind reg_kind);
253254721Semaste
254254721Semaste    //------------------------------------------------------------------
255254721Semaste    /// Wrapper for the static evaluate function that accepts an
256254721Semaste    /// ExecutionContextScope instead of an ExecutionContext and uses
257254721Semaste    /// member variables to populate many operands
258254721Semaste    //------------------------------------------------------------------
259254721Semaste    bool
260254721Semaste    Evaluate (ExecutionContextScope *exe_scope,
261254721Semaste              ClangExpressionVariableList *expr_locals,
262254721Semaste              ClangExpressionDeclMap *decl_map,
263254721Semaste              lldb::addr_t loclist_base_load_addr,
264254721Semaste              const Value* initial_value_ptr,
265254721Semaste              Value& result,
266254721Semaste              Error *error_ptr) const;
267254721Semaste
268254721Semaste    //------------------------------------------------------------------
269254721Semaste    /// Wrapper for the static evaluate function that uses member
270254721Semaste    /// variables to populate many operands
271254721Semaste    //------------------------------------------------------------------
272254721Semaste    bool
273254721Semaste    Evaluate (ExecutionContext *exe_ctx,
274254721Semaste              ClangExpressionVariableList *expr_locals,
275254721Semaste              ClangExpressionDeclMap *decl_map,
276254721Semaste              RegisterContext *reg_ctx,
277254721Semaste              lldb::addr_t loclist_base_load_addr,
278254721Semaste              const Value* initial_value_ptr,
279254721Semaste              Value& result,
280254721Semaste              Error *error_ptr) const;
281254721Semaste
282254721Semaste    //------------------------------------------------------------------
283254721Semaste    /// Evaluate a DWARF location expression in a particular context
284254721Semaste    ///
285254721Semaste    /// @param[in] exe_ctx
286254721Semaste    ///     The execution context in which to evaluate the location
287254721Semaste    ///     expression.  The location expression may access the target's
288254721Semaste    ///     memory, especially if it comes from the expression parser.
289254721Semaste    ///
290263363Semaste    /// @param[in] opcode_ctx
291263363Semaste    ///     The module which defined the expression.
292263363Semaste    ///
293254721Semaste    /// @param[in] opcodes
294254721Semaste    ///     This is a static method so the opcodes need to be provided
295254721Semaste    ///     explicitly.
296254721Semaste    ///
297254721Semaste    /// @param[in] expr_locals
298254721Semaste    ///     If the location expression was produced by the expression parser,
299254721Semaste    ///     the list of local variables referenced by the DWARF expression.
300254721Semaste    ///     This list should already have been populated during parsing;
301254721Semaste    ///     the DWARF expression refers to variables by index.  Can be NULL if
302254721Semaste    ///     the location expression uses no locals.
303254721Semaste    ///
304254721Semaste    /// @param[in] decl_map
305254721Semaste    ///     If the location expression was produced by the expression parser,
306254721Semaste    ///     the list of external variables referenced by the location
307254721Semaste    ///     expression.  Can be NULL if the location expression uses no
308254721Semaste    ///     external variables.
309254721Semaste    ///
310254721Semaste    ///  @param[in] reg_ctx
311254721Semaste    ///     An optional parameter which provides a RegisterContext for use
312254721Semaste    ///     when evaluating the expression (i.e. for fetching register values).
313254721Semaste    ///     Normally this will come from the ExecutionContext's StackFrame but
314254721Semaste    ///     in the case where an expression needs to be evaluated while building
315254721Semaste    ///     the stack frame list, this short-cut is available.
316254721Semaste    ///
317254721Semaste    /// @param[in] offset
318254721Semaste    ///     The offset of the location expression in the data extractor.
319254721Semaste    ///
320254721Semaste    /// @param[in] length
321254721Semaste    ///     The length in bytes of the location expression.
322254721Semaste    ///
323254721Semaste    /// @param[in] reg_set
324254721Semaste    ///     The call-frame-info style register kind.
325254721Semaste    ///
326254721Semaste    /// @param[in] initial_value_ptr
327254721Semaste    ///     A value to put on top of the interpreter stack before evaluating
328254721Semaste    ///     the expression, if the expression is parametrized.  Can be NULL.
329254721Semaste    ///
330254721Semaste    /// @param[in] result
331254721Semaste    ///     A value into which the result of evaluating the expression is
332254721Semaste    ///     to be placed.
333254721Semaste    ///
334254721Semaste    /// @param[in] error_ptr
335254721Semaste    ///     If non-NULL, used to report errors in expression evaluation.
336254721Semaste    ///
337254721Semaste    /// @return
338254721Semaste    ///     True on success; false otherwise.  If error_ptr is non-NULL,
339254721Semaste    ///     details of the failure are provided through it.
340254721Semaste    //------------------------------------------------------------------
341254721Semaste    static bool
342254721Semaste    Evaluate (ExecutionContext *exe_ctx,
343254721Semaste              ClangExpressionVariableList *expr_locals,
344254721Semaste              ClangExpressionDeclMap *decl_map,
345254721Semaste              RegisterContext *reg_ctx,
346263363Semaste              lldb::ModuleSP opcode_ctx,
347254721Semaste              const DataExtractor& opcodes,
348254721Semaste              const lldb::offset_t offset,
349254721Semaste              const lldb::offset_t length,
350254721Semaste              const uint32_t reg_set,
351254721Semaste              const Value* initial_value_ptr,
352254721Semaste              Value& result,
353254721Semaste              Error *error_ptr);
354254721Semaste
355254721Semaste    //------------------------------------------------------------------
356254721Semaste    /// Loads a ClangExpressionVariableList into the object
357254721Semaste    ///
358254721Semaste    /// @param[in] locals
359254721Semaste    ///     If non-NULL, the list of locals used by this expression.
360254721Semaste    ///     See Evaluate().
361254721Semaste    //------------------------------------------------------------------
362254721Semaste    void
363254721Semaste    SetExpressionLocalVariableList (ClangExpressionVariableList *locals);
364254721Semaste
365254721Semaste    //------------------------------------------------------------------
366254721Semaste    /// Loads a ClangExpressionDeclMap into the object
367254721Semaste    ///
368254721Semaste    /// @param[in] locals
369254721Semaste    ///     If non-NULL, the list of external variables used by this
370254721Semaste    ///     expression.  See Evaluate().
371254721Semaste    //------------------------------------------------------------------
372254721Semaste    void
373254721Semaste    SetExpressionDeclMap (ClangExpressionDeclMap *decl_map);
374254721Semaste
375254721Semaste    bool
376254721Semaste    GetExpressionData (DataExtractor &data) const
377254721Semaste    {
378254721Semaste        data = m_data;
379254721Semaste        return data.GetByteSize() > 0;
380254721Semaste    }
381254721Semaste
382254721Semaste    bool
383254721Semaste    DumpLocationForAddress (Stream *s,
384254721Semaste                            lldb::DescriptionLevel level,
385254721Semaste                            lldb::addr_t loclist_base_load_addr,
386254721Semaste                            lldb::addr_t address,
387254721Semaste                            ABI *abi);
388254721Semaste
389254721Semasteprotected:
390254721Semaste    //------------------------------------------------------------------
391254721Semaste    /// Pretty-prints the location expression to a stream
392254721Semaste    ///
393254721Semaste    /// @param[in] stream
394254721Semaste    ///     The stream to use for pretty-printing.
395254721Semaste    ///
396254721Semaste    /// @param[in] offset
397254721Semaste    ///     The offset into the data buffer of the opcodes to be printed.
398254721Semaste    ///
399254721Semaste    /// @param[in] length
400254721Semaste    ///     The length in bytes of the opcodes to be printed.
401254721Semaste    ///
402254721Semaste    /// @param[in] level
403254721Semaste    ///     The level of detail to use in pretty-printing.
404254721Semaste    ///
405254721Semaste    /// @param[in] abi
406254721Semaste    ///     An optional ABI plug-in that can be used to resolve register
407254721Semaste    ///     names.
408254721Semaste    //------------------------------------------------------------------
409254721Semaste    void
410254721Semaste    DumpLocation(Stream *s,
411254721Semaste                 lldb::offset_t offset,
412254721Semaste                 lldb::offset_t length,
413254721Semaste                 lldb::DescriptionLevel level,
414254721Semaste                 ABI *abi) const;
415254721Semaste
416254721Semaste    bool
417254721Semaste    GetLocation (lldb::addr_t base_addr,
418254721Semaste                 lldb::addr_t pc,
419254721Semaste                 lldb::offset_t &offset,
420254721Semaste                 lldb::offset_t &len);
421254721Semaste
422254721Semaste    //------------------------------------------------------------------
423254721Semaste    /// Classes that inherit from DWARFExpression can see and modify these
424254721Semaste    //------------------------------------------------------------------
425263363Semaste
426263363Semaste    lldb::ModuleWP m_module_wp;                 ///< Module which defined this expression.
427254721Semaste    DataExtractor m_data;                       ///< A data extractor capable of reading opcode bytes
428254721Semaste    lldb::RegisterKind m_reg_kind;              ///< One of the defines that starts with LLDB_REGKIND_
429254721Semaste    lldb::addr_t m_loclist_slide;               ///< A value used to slide the location list offsets so that
430254721Semaste                                                ///< they are relative to the object that owns the location list
431254721Semaste                                                ///< (the function for frame base and variable location lists)
432254721Semaste
433254721Semaste};
434254721Semaste
435254721Semaste} // namespace lldb_private
436254721Semaste
437254721Semaste#endif  // liblldb_DWARFExpression_h_
438