1 //===-- FunctionCaller.h ----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_FunctionCaller_h_
11 #define liblldb_FunctionCaller_h_
20 // Other libraries and framework 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"
28 namespace lldb_private
31 //----------------------------------------------------------------------
32 /// @class FunctionCaller FunctionCaller.h "lldb/Expression/FunctionCaller.h"
33 /// @brief Encapsulates a function that can be called.
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.
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.
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 &).
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.
54 /// If you need to call the function on the thread plan stack, you can also
55 /// call InsertFunction() followed by GetThreadPlanToCallFunction().
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.
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
67 //------------------------------------------------------------------
70 /// @param[in] exe_scope
71 /// An execution context scope that gets us at least a target and
74 /// @param[in] ast_context
75 /// The AST context to evaluate argument types in.
77 /// @param[in] return_qualtype
78 /// An opaque Clang QualType for the function result. Should be
79 /// defined in ast_context.
81 /// @param[in] function_address
82 /// The address of the function to call.
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,
94 //------------------------------------------------------------------
96 //------------------------------------------------------------------
97 ~FunctionCaller() override;
99 //------------------------------------------------------------------
100 /// Compile the wrapper function
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.
107 /// @param[in] diagnostic_manager
108 /// The diagnostic manager to report parser errors to.
111 /// The number of errors.
112 //------------------------------------------------------------------
114 CompileFunction (lldb::ThreadSP thread_to_use_sp,
115 DiagnosticManager &diagnostic_manager) = 0;
117 //------------------------------------------------------------------
118 /// Insert the default function wrapper and its default argument struct
120 /// @param[in] exe_ctx
121 /// The execution context to insert the function and its arguments
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.
129 /// @param[in] diagnostic_manager
130 /// The diagnostic manager to report errors to.
133 /// True on success; false otherwise.
134 //------------------------------------------------------------------
136 InsertFunction(ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref, DiagnosticManager &diagnostic_manager);
138 //------------------------------------------------------------------
139 /// Insert the default function wrapper (using the JIT)
141 /// @param[in] exe_ctx
142 /// The execution context to insert the function and its arguments
145 /// @param[in] diagnostic_manager
146 /// The diagnostic manager to report errors to.
149 /// True on success; false otherwise.
150 //------------------------------------------------------------------
152 WriteFunctionWrapper(ExecutionContext &exe_ctx, DiagnosticManager &diagnostic_manager);
154 //------------------------------------------------------------------
155 /// Insert the default function argument struct
157 /// @param[in] exe_ctx
158 /// The execution context to insert the function and its arguments
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.
166 /// @param[in] diagnostic_manager
167 /// The diagnostic manager to report errors to.
170 /// True on success; false otherwise.
171 //------------------------------------------------------------------
173 WriteFunctionArguments(ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref,
174 DiagnosticManager &diagnostic_manager);
176 //------------------------------------------------------------------
177 /// Insert an argument struct with a non-default function address and
178 /// non-default argument values
180 /// @param[in] exe_ctx
181 /// The execution context to insert the function and its arguments
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.
189 /// @param[in] arg_values
190 /// The values of the function's arguments.
192 /// @param[in] diagnostic_manager
193 /// The diagnostic manager to report errors to.
196 /// True on success; false otherwise.
197 //------------------------------------------------------------------
199 WriteFunctionArguments(ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref, ValueList &arg_values,
200 DiagnosticManager &diagnostic_manager);
202 //------------------------------------------------------------------
203 /// Run the function this FunctionCaller was created with.
205 /// This is the full version.
207 /// @param[in] exe_ctx
208 /// The thread & process in which this function will run.
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.
217 /// @param[in] diagnostic_manager
218 /// The diagnostic manager to report errors to.
220 /// @param[in] options
221 /// The options for this expression execution.
223 /// @param[out] results
224 /// The result value will be put here after running the function.
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);
233 //------------------------------------------------------------------
234 /// Get a thread plan to run the function this FunctionCaller was created with.
236 /// @param[in] exe_ctx
237 /// The execution context to insert the function and its arguments
240 /// @param[in] func_addr
241 /// The address of the function in the target process.
243 /// @param[in] args_addr
244 /// The address of the argument struct.
246 /// @param[in] diagnostic_manager
247 /// The diagnostic manager to report errors to.
249 /// @param[in] stop_others
250 /// True if other threads should pause during execution.
252 /// @param[in] unwind_on_error
253 /// True if the thread plan may simply be discarded if an error occurs.
256 /// A ThreadPlan shared pointer for executing the function.
257 //------------------------------------------------------------------
259 GetThreadPlanToCallFunction(ExecutionContext &exe_ctx, lldb::addr_t args_addr,
260 const EvaluateExpressionOptions &options, DiagnosticManager &diagnostic_manager);
262 //------------------------------------------------------------------
263 /// Get the result of the function from its struct
265 /// @param[in] exe_ctx
266 /// The execution context to retrieve the result from.
268 /// @param[in] args_addr
269 /// The address of the argument struct.
271 /// @param[out] ret_value
272 /// The value returned by the function.
275 /// True on success; false otherwise.
276 //------------------------------------------------------------------
277 bool FetchFunctionResults (ExecutionContext &exe_ctx,
278 lldb::addr_t args_addr,
281 //------------------------------------------------------------------
282 /// Deallocate the arguments structure
284 /// @param[in] exe_ctx
285 /// The execution context to insert the function and its arguments
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);
294 //------------------------------------------------------------------
295 /// Interface for ClangExpression
296 //------------------------------------------------------------------
298 //------------------------------------------------------------------
299 /// Return the string that the parser should parse. Must be a full
300 /// translation unit.
301 //------------------------------------------------------------------
305 return m_wrapper_function_text.c_str();
308 //------------------------------------------------------------------
309 /// Return the function name that should be used for executing the
310 /// expression. Text() should contain the definition of this
312 //------------------------------------------------------------------
314 FunctionName() override
316 return m_wrapper_function_name.c_str();
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 *
329 //------------------------------------------------------------------
330 /// Return true if validation code should be inserted into the
332 //------------------------------------------------------------------
334 NeedsValidation() override
339 //------------------------------------------------------------------
340 /// Return true if external variables in the expression should be
342 //------------------------------------------------------------------
344 NeedsVariableResolution() override
350 GetArgumentValues () const
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.
363 lldb::ModuleWP m_jit_module_wp;
364 std::string m_name; ///< The name of this clang function - for debugging purposes.
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.
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.
375 bool m_struct_valid; ///< True if the ASTStructExtractor has populated the variables below.
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 //------------------------------------------------------------------
385 ValueList m_arg_values; ///< The default values of the arguments.
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.
391 } // namespace lldb_private
393 #endif // liblldb_FunctionCaller_h_