]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - include/lldb/Expression/FunctionCaller.h
Vendor import of lldb release_39 branch r276489:
[FreeBSD/FreeBSD.git] / include / lldb / Expression / FunctionCaller.h
1 //===-- FunctionCaller.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_FunctionCaller_h_
11 #define liblldb_FunctionCaller_h_
12
13 // C Includes
14 // C++ Includes
15 #include <list>
16 #include <memory>
17 #include <string>
18 #include <vector>
19
20 // Other libraries and framework includes
21 // Project includes
22 #include "lldb/Core/Address.h"
23 #include "lldb/Core/Value.h"
24 #include "lldb/Expression/Expression.h"
25 #include "lldb/Expression/ExpressionParser.h"
26 #include "lldb/Symbol/CompilerType.h"
27
28 namespace lldb_private
29 {
30     
31 //----------------------------------------------------------------------
32 /// @class FunctionCaller FunctionCaller.h "lldb/Expression/FunctionCaller.h"
33 /// @brief Encapsulates a function that can be called.
34 ///
35 /// A given FunctionCaller object can handle a single function signature.
36 /// Once constructed, it can set up any number of concurrent calls to
37 /// functions with that signature.
38 ///
39 /// It performs the call by synthesizing a structure that contains the pointer
40 /// to the function and the arguments that should be passed to that function,
41 /// and producing a special-purpose JIT-compiled function that accepts a void*
42 /// pointing to this struct as its only argument and calls the function in the 
43 /// struct with the written arguments.  This method lets Clang handle the
44 /// vagaries of function calling conventions.
45 ///
46 /// The simplest use of the FunctionCaller is to construct it with a
47 /// function representative of the signature you want to use, then call
48 /// ExecuteFunction(ExecutionContext &, Stream &, Value &).
49 ///
50 /// If you need to reuse the arguments for several calls, you can call
51 /// InsertFunction() followed by WriteFunctionArguments(), which will return
52 /// the location of the args struct for the wrapper function in args_addr_ref.
53 ///
54 /// If you need to call the function on the thread plan stack, you can also 
55 /// call InsertFunction() followed by GetThreadPlanToCallFunction().
56 ///
57 /// Any of the methods that take arg_addr_ptr or arg_addr_ref can be passed
58 /// a pointer set to LLDB_INVALID_ADDRESS and new structure will be allocated
59 /// and its address returned in that variable.
60 /// 
61 /// Any of the methods that take arg_addr_ptr can be passed nullptr, and the
62 /// argument space will be managed for you.
63 //----------------------------------------------------------------------    
64 class FunctionCaller : public Expression
65 {
66 public:
67     //------------------------------------------------------------------
68     /// Constructor
69     ///
70     /// @param[in] exe_scope
71     ///     An execution context scope that gets us at least a target and 
72     ///     process.
73     ///
74     /// @param[in] ast_context
75     ///     The AST context to evaluate argument types in.
76     ///
77     /// @param[in] return_qualtype
78     ///     An opaque Clang QualType for the function result.  Should be
79     ///     defined in ast_context.
80     ///
81     /// @param[in] function_address
82     ///     The address of the function to call.
83     ///
84     /// @param[in] arg_value_list
85     ///     The default values to use when calling this function.  Can
86     ///     be overridden using WriteFunctionArguments().
87     //------------------------------------------------------------------
88     FunctionCaller (ExecutionContextScope &exe_scope,
89                    const CompilerType &return_type,
90                    const Address& function_address, 
91                    const ValueList &arg_value_list,
92                    const char *name);
93     
94     //------------------------------------------------------------------
95     /// Destructor
96     //------------------------------------------------------------------
97     ~FunctionCaller() override;
98
99     //------------------------------------------------------------------
100     /// Compile the wrapper function
101     ///
102     /// @param[in] thread_to_use_sp
103     ///     Compilation might end up calling functions.  Pass in the thread you
104     ///     want the compilation to use.  If you pass in an empty ThreadSP it will
105     ///     use the currently selected thread.
106     ///
107     /// @param[in] diagnostic_manager
108     ///     The diagnostic manager to report parser errors to.
109     ///
110     /// @return
111     ///     The number of errors.
112     //------------------------------------------------------------------
113     virtual unsigned
114     CompileFunction (lldb::ThreadSP thread_to_use_sp,
115                      DiagnosticManager &diagnostic_manager) = 0;
116
117     //------------------------------------------------------------------
118     /// Insert the default function wrapper and its default argument struct
119     ///
120     /// @param[in] exe_ctx
121     ///     The execution context to insert the function and its arguments
122     ///     into.
123     ///
124     /// @param[in,out] args_addr_ref
125     ///     The address of the structure to write the arguments into.  May
126     ///     be LLDB_INVALID_ADDRESS; if it is, a new structure is allocated
127     ///     and args_addr_ref is pointed to it.
128     ///
129     /// @param[in] diagnostic_manager
130     ///     The diagnostic manager to report errors to.
131     ///
132     /// @return
133     ///     True on success; false otherwise.
134     //------------------------------------------------------------------
135     bool
136     InsertFunction(ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref, DiagnosticManager &diagnostic_manager);
137
138     //------------------------------------------------------------------
139     /// Insert the default function wrapper (using the JIT)
140     ///
141     /// @param[in] exe_ctx
142     ///     The execution context to insert the function and its arguments
143     ///     into.
144     ///
145     /// @param[in] diagnostic_manager
146     ///     The diagnostic manager to report errors to.
147     ///
148     /// @return
149     ///     True on success; false otherwise.
150     //------------------------------------------------------------------
151     bool
152     WriteFunctionWrapper(ExecutionContext &exe_ctx, DiagnosticManager &diagnostic_manager);
153
154     //------------------------------------------------------------------
155     /// Insert the default function argument struct
156     ///
157     /// @param[in] exe_ctx
158     ///     The execution context to insert the function and its arguments
159     ///     into.
160     ///
161     /// @param[in,out] args_addr_ref
162     ///     The address of the structure to write the arguments into.  May
163     ///     be LLDB_INVALID_ADDRESS; if it is, a new structure is allocated
164     ///     and args_addr_ref is pointed to it.
165     ///
166     /// @param[in] diagnostic_manager
167     ///     The diagnostic manager to report errors to.
168     ///
169     /// @return
170     ///     True on success; false otherwise.
171     //------------------------------------------------------------------
172     bool
173     WriteFunctionArguments(ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref,
174                            DiagnosticManager &diagnostic_manager);
175
176     //------------------------------------------------------------------
177     /// Insert an argument struct with a non-default function address and
178     /// non-default argument values
179     ///
180     /// @param[in] exe_ctx
181     ///     The execution context to insert the function and its arguments
182     ///     into.
183     ///
184     /// @param[in,out] args_addr_ref
185     ///     The address of the structure to write the arguments into.  May
186     ///     be LLDB_INVALID_ADDRESS; if it is, a new structure is allocated
187     ///     and args_addr_ref is pointed at it.
188     ///
189     /// @param[in] arg_values
190     ///     The values of the function's arguments.
191     ///
192     /// @param[in] diagnostic_manager
193     ///     The diagnostic manager to report errors to.
194     ///
195     /// @return
196     ///     True on success; false otherwise.
197     //------------------------------------------------------------------
198     bool
199     WriteFunctionArguments(ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref, ValueList &arg_values,
200                            DiagnosticManager &diagnostic_manager);
201
202     //------------------------------------------------------------------
203     /// Run the function this FunctionCaller was created with.
204     ///
205     /// This is the full version.
206     ///
207     /// @param[in] exe_ctx
208     ///     The thread & process in which this function will run.
209     ///
210     /// @param[in] args_addr_ptr
211     ///     If nullptr, the function will take care of allocating & deallocating the wrapper
212     ///     args structure.  Otherwise, if set to LLDB_INVALID_ADDRESS, a new structure
213     ///     will be allocated, filled and the address returned to you.  You are responsible
214     ///     for deallocating it.  And if passed in with a value other than LLDB_INVALID_ADDRESS,
215     ///     this should point to an already allocated structure with the values already written.
216     ///
217     /// @param[in] diagnostic_manager
218     ///     The diagnostic manager to report errors to.
219     ///
220     /// @param[in] options
221     ///     The options for this expression execution.
222     ///
223     /// @param[out] results
224     ///     The result value will be put here after running the function.
225     ///
226     /// @return
227     ///     Returns one of the ExpressionResults enum indicating function call status.
228     //------------------------------------------------------------------
229     lldb::ExpressionResults
230     ExecuteFunction(ExecutionContext &exe_ctx, lldb::addr_t *args_addr_ptr, const EvaluateExpressionOptions &options,
231                     DiagnosticManager &diagnostic_manager, Value &results);
232
233     //------------------------------------------------------------------
234     /// Get a thread plan to run the function this FunctionCaller was created with.
235     ///
236     /// @param[in] exe_ctx
237     ///     The execution context to insert the function and its arguments
238     ///     into.
239     ///
240     /// @param[in] func_addr
241     ///     The address of the function in the target process.
242     ///
243     /// @param[in] args_addr
244     ///     The address of the argument struct.
245     ///
246     /// @param[in] diagnostic_manager
247     ///     The diagnostic manager to report errors to.
248     ///
249     /// @param[in] stop_others
250     ///     True if other threads should pause during execution.
251     ///
252     /// @param[in] unwind_on_error
253     ///     True if the thread plan may simply be discarded if an error occurs.
254     ///
255     /// @return
256     ///     A ThreadPlan shared pointer for executing the function.
257     //------------------------------------------------------------------
258     lldb::ThreadPlanSP
259     GetThreadPlanToCallFunction(ExecutionContext &exe_ctx, lldb::addr_t args_addr,
260                                 const EvaluateExpressionOptions &options, DiagnosticManager &diagnostic_manager);
261
262     //------------------------------------------------------------------
263     /// Get the result of the function from its struct
264     ///
265     /// @param[in] exe_ctx
266     ///     The execution context to retrieve the result from.
267     ///
268     /// @param[in] args_addr
269     ///     The address of the argument struct.
270     ///
271     /// @param[out] ret_value
272     ///     The value returned by the function.
273     ///
274     /// @return
275     ///     True on success; false otherwise.
276     //------------------------------------------------------------------
277     bool FetchFunctionResults (ExecutionContext &exe_ctx, 
278                                lldb::addr_t args_addr, 
279                                Value &ret_value);
280     
281     //------------------------------------------------------------------
282     /// Deallocate the arguments structure
283     ///
284     /// @param[in] exe_ctx
285     ///     The execution context to insert the function and its arguments
286     ///     into.
287     ///
288     /// @param[in] args_addr
289     ///     The address of the argument struct.
290     //------------------------------------------------------------------
291     void DeallocateFunctionResults (ExecutionContext &exe_ctx, 
292                                     lldb::addr_t args_addr);
293     
294     //------------------------------------------------------------------
295     /// Interface for ClangExpression
296     //------------------------------------------------------------------
297     
298     //------------------------------------------------------------------
299     /// Return the string that the parser should parse.  Must be a full
300     /// translation unit.
301     //------------------------------------------------------------------
302     const char *
303     Text() override
304     {
305         return m_wrapper_function_text.c_str();
306     }
307     
308     //------------------------------------------------------------------
309     /// Return the function name that should be used for executing the
310     /// expression.  Text() should contain the definition of this
311     /// function.
312     //------------------------------------------------------------------
313     const char *
314     FunctionName() override
315     {
316         return m_wrapper_function_name.c_str();
317     }
318     
319     //------------------------------------------------------------------
320     /// Return the object that the parser should use when registering
321     /// local variables. May be nullptr if the Expression doesn't care.
322     //------------------------------------------------------------------
323     ExpressionVariableList *
324     LocalVariables ()
325     {
326         return nullptr;
327     }
328     
329     //------------------------------------------------------------------
330     /// Return true if validation code should be inserted into the
331     /// expression.
332     //------------------------------------------------------------------
333     bool
334     NeedsValidation() override
335     {
336         return false;
337     }
338     
339     //------------------------------------------------------------------
340     /// Return true if external variables in the expression should be
341     /// resolved.
342     //------------------------------------------------------------------
343     bool
344     NeedsVariableResolution() override
345     {
346         return false;
347     }
348     
349     ValueList
350     GetArgumentValues () const
351     {
352         return m_arg_values;
353     }
354
355 protected:
356     // Note: the parser needs to be destructed before the execution unit, so
357     // declare the execution unit first.
358     std::shared_ptr<IRExecutionUnit> m_execution_unit_sp;
359     std::unique_ptr<ExpressionParser> m_parser;                     ///< The parser responsible for compiling the function.
360                                                                     ///< This will get made in CompileFunction, so it is
361                                                                     ///< safe to access it after that.
362
363     lldb::ModuleWP                  m_jit_module_wp;
364     std::string                     m_name;                         ///< The name of this clang function - for debugging purposes.
365     
366     Function                       *m_function_ptr;                 ///< The function we're going to call. May be nullptr if we don't have debug info for the function.
367     Address                         m_function_addr;                ///< If we don't have the FunctionSP, we at least need the address & return type.
368     CompilerType                    m_function_return_type;         ///< The opaque clang qual type for the function return type.
369
370     std::string                     m_wrapper_function_name;        ///< The name of the wrapper function.
371     std::string                     m_wrapper_function_text;        ///< The contents of the wrapper function.
372     std::string                     m_wrapper_struct_name;          ///< The name of the struct that contains the target function address, arguments, and result.
373     std::list<lldb::addr_t>         m_wrapper_args_addrs;           ///< The addresses of the arguments to the wrapper function.
374     
375     bool                            m_struct_valid;                 ///< True if the ASTStructExtractor has populated the variables below.
376     
377     //------------------------------------------------------------------
378     /// These values are populated by the ASTStructExtractor
379     size_t                          m_struct_size;                  ///< The size of the argument struct, in bytes.
380     std::vector<uint64_t>           m_member_offsets;               ///< The offset of each member in the struct, in bytes.
381     uint64_t                        m_return_size;                  ///< The size of the result variable, in bytes.
382     uint64_t                        m_return_offset;                ///< The offset of the result variable in the struct, in bytes.
383     //------------------------------------------------------------------
384
385     ValueList                       m_arg_values;                   ///< The default values of the arguments.
386     
387     bool                            m_compiled;                     ///< True if the wrapper function has already been parsed.
388     bool                            m_JITted;                       ///< True if the wrapper function has already been JIT-compiled.
389 };
390
391 } // namespace lldb_private
392
393 #endif // liblldb_FunctionCaller_h_