1 //===-- ClangFunction.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 lldb_ClangFunction_h_
11 #define lldb_ClangFunction_h_
17 // Other libraries and framework includes
19 #include "lldb/Core/ClangForward.h"
20 #include "lldb/Core/Address.h"
21 #include "lldb/Core/ArchSpec.h"
22 #include "lldb/Core/Value.h"
23 #include "lldb/Core/ValueObjectList.h"
24 #include "lldb/Expression/ClangExpression.h"
25 #include "lldb/Target/Process.h"
27 namespace lldb_private
30 class ASTStructExtractor;
31 class ClangExpressionParser;
33 //----------------------------------------------------------------------
34 /// @class ClangFunction ClangFunction.h "lldb/Expression/ClangFunction.h"
35 /// @brief Encapsulates a function that can be called.
37 /// A given ClangFunction object can handle a single function signature.
38 /// Once constructed, it can set up any number of concurrent calls to
39 /// functions with that signature.
41 /// It performs the call by synthesizing a structure that contains the pointer
42 /// to the function and the arguments that should be passed to that function,
43 /// and producing a special-purpose JIT-compiled function that accepts a void*
44 /// pointing to this struct as its only argument and calls the function in the
45 /// struct with the written arguments. This method lets Clang handle the
46 /// vagaries of function calling conventions.
48 /// The simplest use of the ClangFunction is to construct it with a
49 /// function representative of the signature you want to use, then call
50 /// ExecuteFunction(ExecutionContext &, Stream &, Value &).
52 /// If you need to reuse the arguments for several calls, you can call
53 /// InsertFunction() followed by WriteFunctionArguments(), which will return
54 /// the location of the args struct for the wrapper function in args_addr_ref.
56 /// If you need to call the function on the thread plan stack, you can also
57 /// call InsertFunction() followed by GetThreadPlanToCallFunction().
59 /// Any of the methods that take arg_addr_ptr or arg_addr_ref can be passed
60 /// a pointer set to LLDB_INVALID_ADDRESS and new structure will be allocated
61 /// and its address returned in that variable.
63 /// Any of the methods that take arg_addr_ptr can be passed NULL, and the
64 /// argument space will be managed for you.
65 //----------------------------------------------------------------------
66 class ClangFunction : public ClangExpression
68 friend class ASTStructExtractor;
70 //------------------------------------------------------------------
73 /// @param[in] exe_scope
74 /// An execution context scope that gets us at least a target and
77 /// @param[in] function_ptr
78 /// The default function to be called. Can be overridden using
79 /// WriteFunctionArguments().
81 /// @param[in] ast_context
82 /// The AST context to evaluate argument types in.
84 /// @param[in] arg_value_list
85 /// The default values to use when calling this function. Can
86 /// be overridden using WriteFunctionArguments().
87 //------------------------------------------------------------------
88 ClangFunction (ExecutionContextScope &exe_scope,
89 Function &function_ptr,
90 ClangASTContext *ast_context,
91 const ValueList &arg_value_list,
94 //------------------------------------------------------------------
97 /// @param[in] exe_scope
98 /// An execution context scope that gets us at least a target and
101 /// @param[in] ast_context
102 /// The AST context to evaluate argument types in.
104 /// @param[in] return_qualtype
105 /// An opaque Clang QualType for the function result. Should be
106 /// defined in ast_context.
108 /// @param[in] function_address
109 /// The address of the function to call.
111 /// @param[in] arg_value_list
112 /// The default values to use when calling this function. Can
113 /// be overridden using WriteFunctionArguments().
114 //------------------------------------------------------------------
115 ClangFunction (ExecutionContextScope &exe_scope,
116 const ClangASTType &return_type,
117 const Address& function_address,
118 const ValueList &arg_value_list,
121 //------------------------------------------------------------------
123 //------------------------------------------------------------------
127 //------------------------------------------------------------------
128 /// Compile the wrapper function
130 /// @param[in] errors
131 /// The stream to print parser errors to.
134 /// The number of errors.
135 //------------------------------------------------------------------
137 CompileFunction (Stream &errors);
139 //------------------------------------------------------------------
140 /// Insert the default function wrapper and its default argument struct
142 /// @param[in] exe_ctx
143 /// The execution context to insert the function and its arguments
146 /// @param[in,out] args_addr_ref
147 /// The address of the structure to write the arguments into. May
148 /// be LLDB_INVALID_ADDRESS; if it is, a new structure is allocated
149 /// and args_addr_ref is pointed to it.
151 /// @param[in] errors
152 /// The stream to write errors to.
155 /// True on success; false otherwise.
156 //------------------------------------------------------------------
158 InsertFunction (ExecutionContext &exe_ctx,
159 lldb::addr_t &args_addr_ref,
162 //------------------------------------------------------------------
163 /// Insert the default function wrapper (using the JIT)
165 /// @param[in] exe_ctx
166 /// The execution context to insert the function and its arguments
169 /// @param[in] errors
170 /// The stream to write errors to.
173 /// True on success; false otherwise.
174 //------------------------------------------------------------------
175 bool WriteFunctionWrapper (ExecutionContext &exe_ctx,
178 //------------------------------------------------------------------
179 /// Insert the default function argument struct
181 /// @param[in] exe_ctx
182 /// The execution context to insert the function and its arguments
185 /// @param[in,out] args_addr_ref
186 /// The address of the structure to write the arguments into. May
187 /// be LLDB_INVALID_ADDRESS; if it is, a new structure is allocated
188 /// and args_addr_ref is pointed to it.
190 /// @param[in] errors
191 /// The stream to write errors to.
194 /// True on success; false otherwise.
195 //------------------------------------------------------------------
196 bool WriteFunctionArguments (ExecutionContext &exe_ctx,
197 lldb::addr_t &args_addr_ref,
200 //------------------------------------------------------------------
201 /// Insert an argument struct with a non-default function address and
202 /// non-default argument values
204 /// @param[in] exe_ctx
205 /// The execution context to insert the function and its arguments
208 /// @param[in,out] args_addr_ref
209 /// The address of the structure to write the arguments into. May
210 /// be LLDB_INVALID_ADDRESS; if it is, a new structure is allocated
211 /// and args_addr_ref is pointed to it.
213 /// @param[in] function_address
214 /// The address of the function to call.
216 /// @param[in] arg_values
217 /// The values of the function's arguments.
219 /// @param[in] errors
220 /// The stream to write errors to.
223 /// True on success; false otherwise.
224 //------------------------------------------------------------------
225 bool WriteFunctionArguments (ExecutionContext &exe_ctx,
226 lldb::addr_t &args_addr_ref,
227 Address function_address,
228 ValueList &arg_values,
231 //------------------------------------------------------------------
232 /// Run the function this ClangFunction was created with.
234 /// This is the full version.
236 /// @param[in] exe_ctx
237 /// The thread & process in which this function will run.
239 /// @param[in] args_addr_ptr
240 /// If NULL, the function will take care of allocating & deallocating the wrapper
241 /// args structure. Otherwise, if set to LLDB_INVALID_ADDRESS, a new structure
242 /// will be allocated, filled and the address returned to you. You are responsible
243 /// for deallocating it. And if passed in with a value other than LLDB_INVALID_ADDRESS,
244 /// this should point to an already allocated structure with the values already written.
246 /// @param[in] errors
247 /// Errors will be written here if there are any.
249 /// @param[in] options
250 /// The options for this expression execution.
252 /// @param[out] results
253 /// The result value will be put here after running the function.
256 /// Returns one of the ExpressionResults enum indicating function call status.
257 //------------------------------------------------------------------
258 lldb::ExpressionResults
259 ExecuteFunction(ExecutionContext &exe_ctx,
260 lldb::addr_t *args_addr_ptr,
261 const EvaluateExpressionOptions &options,
265 //------------------------------------------------------------------
266 /// Get a thread plan to run the function this ClangFunction was created with.
268 /// @param[in] exe_ctx
269 /// The execution context to insert the function and its arguments
272 /// @param[in] func_addr
273 /// The address of the function in the target process.
275 /// @param[in] args_addr
276 /// The address of the argument struct.
278 /// @param[in] errors
279 /// The stream to write errors to.
281 /// @param[in] stop_others
282 /// True if other threads should pause during execution.
284 /// @param[in] unwind_on_error
285 /// True if the thread plan may simply be discarded if an error occurs.
288 /// A ThreadPlan for executing the function.
289 //------------------------------------------------------------------
291 GetThreadPlanToCallFunction (ExecutionContext &exe_ctx,
292 lldb::addr_t args_addr,
293 const EvaluateExpressionOptions &options,
296 //------------------------------------------------------------------
297 /// Get the result of the function from its struct
299 /// @param[in] exe_ctx
300 /// The execution context to retrieve the result from.
302 /// @param[in] args_addr
303 /// The address of the argument struct.
305 /// @param[out] ret_value
306 /// The value returned by the function.
309 /// True on success; false otherwise.
310 //------------------------------------------------------------------
311 bool FetchFunctionResults (ExecutionContext &exe_ctx,
312 lldb::addr_t args_addr,
315 //------------------------------------------------------------------
316 /// Deallocate the arguments structure
318 /// @param[in] exe_ctx
319 /// The execution context to insert the function and its arguments
322 /// @param[in] args_addr
323 /// The address of the argument struct.
324 //------------------------------------------------------------------
325 void DeallocateFunctionResults (ExecutionContext &exe_ctx,
326 lldb::addr_t args_addr);
328 //------------------------------------------------------------------
329 /// Interface for ClangExpression
330 //------------------------------------------------------------------
332 //------------------------------------------------------------------
333 /// Return the string that the parser should parse. Must be a full
334 /// translation unit.
335 //------------------------------------------------------------------
339 return m_wrapper_function_text.c_str();
342 //------------------------------------------------------------------
343 /// Return the function name that should be used for executing the
344 /// expression. Text() should contain the definition of this
346 //------------------------------------------------------------------
350 return m_wrapper_function_name.c_str();
353 //------------------------------------------------------------------
354 /// Return the object that the parser should use when resolving external
355 /// values. May be NULL if everything should be self-contained.
356 //------------------------------------------------------------------
357 ClangExpressionDeclMap *
363 //------------------------------------------------------------------
364 /// Return the object that the parser should use when registering
365 /// local variables. May be NULL if the Expression doesn't care.
366 //------------------------------------------------------------------
367 ClangExpressionVariableList *
373 //------------------------------------------------------------------
374 /// Return the object that the parser should allow to access ASTs.
375 /// May be NULL if the ASTs do not need to be transformed.
377 /// @param[in] passthrough
378 /// The ASTConsumer that the returned transformer should send
379 /// the ASTs to after transformation.
380 //------------------------------------------------------------------
382 ASTTransformer (clang::ASTConsumer *passthrough);
384 //------------------------------------------------------------------
385 /// Return true if validation code should be inserted into the
387 //------------------------------------------------------------------
394 //------------------------------------------------------------------
395 /// Return true if external variables in the expression should be
397 //------------------------------------------------------------------
399 NeedsVariableResolution ()
405 GetArgumentValues () const
410 //------------------------------------------------------------------
411 // For ClangFunction only
412 //------------------------------------------------------------------
414 std::unique_ptr<ClangExpressionParser> m_parser; ///< The parser responsible for compiling the function.
415 std::shared_ptr<IRExecutionUnit> m_execution_unit_sp;
416 lldb::ModuleWP m_jit_module_wp;
417 std::string m_name; ///< The name of this clang function - for debugging purposes.
419 Function *m_function_ptr; ///< The function we're going to call. May be NULL if we don't have debug info for the function.
420 Address m_function_addr; ///< If we don't have the FunctionSP, we at least need the address & return type.
421 ClangASTType m_function_return_type; ///< The opaque clang qual type for the function return type.
423 std::string m_wrapper_function_name; ///< The name of the wrapper function.
424 std::string m_wrapper_function_text; ///< The contents of the wrapper function.
425 std::string m_wrapper_struct_name; ///< The name of the struct that contains the target function address, arguments, and result.
426 std::list<lldb::addr_t> m_wrapper_args_addrs; ///< The addresses of the arguments to the wrapper function.
428 std::unique_ptr<ASTStructExtractor> m_struct_extractor; ///< The class that generates the argument struct below.
430 bool m_struct_valid; ///< True if the ASTStructExtractor has populated the variables below.
432 //------------------------------------------------------------------
433 /// These values are populated by the ASTStructExtractor
434 size_t m_struct_size; ///< The size of the argument struct, in bytes.
435 std::vector<uint64_t> m_member_offsets; ///< The offset of each member in the struct, in bytes.
436 uint64_t m_return_size; ///< The size of the result variable, in bytes.
437 uint64_t m_return_offset; ///< The offset of the result variable in the struct, in bytes.
438 //------------------------------------------------------------------
440 ValueList m_arg_values; ///< The default values of the arguments.
442 bool m_compiled; ///< True if the wrapper function has already been parsed.
443 bool m_JITted; ///< True if the wrapper function has already been JIT-compiled.
446 } // Namespace lldb_private
448 #endif // lldb_ClangFunction_h_