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/Symbol/ClangASTType.h"
26 #include "lldb/Target/Process.h"
28 namespace lldb_private
31 class ASTStructExtractor;
32 class ClangExpressionParser;
34 //----------------------------------------------------------------------
35 /// @class ClangFunction ClangFunction.h "lldb/Expression/ClangFunction.h"
36 /// @brief Encapsulates a function that can be called.
38 /// A given ClangFunction object can handle a single function signature.
39 /// Once constructed, it can set up any number of concurrent calls to
40 /// functions with that signature.
42 /// It performs the call by synthesizing a structure that contains the pointer
43 /// to the function and the arguments that should be passed to that function,
44 /// and producing a special-purpose JIT-compiled function that accepts a void*
45 /// pointing to this struct as its only argument and calls the function in the
46 /// struct with the written arguments. This method lets Clang handle the
47 /// vagaries of function calling conventions.
49 /// The simplest use of the ClangFunction is to construct it with a
50 /// function representative of the signature you want to use, then call
51 /// ExecuteFunction(ExecutionContext &, Stream &, Value &).
53 /// If you need to reuse the arguments for several calls, you can call
54 /// InsertFunction() followed by WriteFunctionArguments(), which will return
55 /// the location of the args struct for the wrapper function in args_addr_ref.
57 /// If you need to call the function on the thread plan stack, you can also
58 /// call InsertFunction() followed by GetThreadPlanToCallFunction().
60 /// Any of the methods that take arg_addr_ptr or arg_addr_ref can be passed
61 /// a pointer set to LLDB_INVALID_ADDRESS and new structure will be allocated
62 /// and its address returned in that variable.
64 /// Any of the methods that take arg_addr_ptr can be passed NULL, and the
65 /// argument space will be managed for you.
66 //----------------------------------------------------------------------
67 class ClangFunction : public ClangExpression
69 friend class ASTStructExtractor;
71 //------------------------------------------------------------------
74 /// @param[in] exe_scope
75 /// An execution context scope that gets us at least a target and
78 /// @param[in] function_ptr
79 /// The default function to be called. Can be overridden using
80 /// WriteFunctionArguments().
82 /// @param[in] ast_context
83 /// The AST context to evaluate argument types in.
85 /// @param[in] arg_value_list
86 /// The default values to use when calling this function. Can
87 /// be overridden using WriteFunctionArguments().
88 //------------------------------------------------------------------
89 ClangFunction (ExecutionContextScope &exe_scope,
90 Function &function_ptr,
91 ClangASTContext *ast_context,
92 const ValueList &arg_value_list,
95 //------------------------------------------------------------------
98 /// @param[in] exe_scope
99 /// An execution context scope that gets us at least a target and
102 /// @param[in] ast_context
103 /// The AST context to evaluate argument types in.
105 /// @param[in] return_qualtype
106 /// An opaque Clang QualType for the function result. Should be
107 /// defined in ast_context.
109 /// @param[in] function_address
110 /// The address of the function to call.
112 /// @param[in] arg_value_list
113 /// The default values to use when calling this function. Can
114 /// be overridden using WriteFunctionArguments().
115 //------------------------------------------------------------------
116 ClangFunction (ExecutionContextScope &exe_scope,
117 const ClangASTType &return_type,
118 const Address& function_address,
119 const ValueList &arg_value_list,
122 //------------------------------------------------------------------
124 //------------------------------------------------------------------
128 //------------------------------------------------------------------
129 /// Compile the wrapper function
131 /// @param[in] errors
132 /// The stream to print parser errors to.
135 /// The number of errors.
136 //------------------------------------------------------------------
138 CompileFunction (Stream &errors);
140 //------------------------------------------------------------------
141 /// Insert the default function wrapper and its default argument struct
143 /// @param[in] exe_ctx
144 /// The execution context to insert the function and its arguments
147 /// @param[in,out] args_addr_ref
148 /// The address of the structure to write the arguments into. May
149 /// be LLDB_INVALID_ADDRESS; if it is, a new structure is allocated
150 /// and args_addr_ref is pointed to it.
152 /// @param[in] errors
153 /// The stream to write errors to.
156 /// True on success; false otherwise.
157 //------------------------------------------------------------------
159 InsertFunction (ExecutionContext &exe_ctx,
160 lldb::addr_t &args_addr_ref,
163 //------------------------------------------------------------------
164 /// Insert the default function wrapper (using the JIT)
166 /// @param[in] exe_ctx
167 /// The execution context to insert the function and its arguments
170 /// @param[in] errors
171 /// The stream to write errors to.
174 /// True on success; false otherwise.
175 //------------------------------------------------------------------
176 bool WriteFunctionWrapper (ExecutionContext &exe_ctx,
179 //------------------------------------------------------------------
180 /// Insert the default function argument struct
182 /// @param[in] exe_ctx
183 /// The execution context to insert the function and its arguments
186 /// @param[in,out] args_addr_ref
187 /// The address of the structure to write the arguments into. May
188 /// be LLDB_INVALID_ADDRESS; if it is, a new structure is allocated
189 /// and args_addr_ref is pointed to it.
191 /// @param[in] errors
192 /// The stream to write errors to.
195 /// True on success; false otherwise.
196 //------------------------------------------------------------------
197 bool WriteFunctionArguments (ExecutionContext &exe_ctx,
198 lldb::addr_t &args_addr_ref,
201 //------------------------------------------------------------------
202 /// Insert an argument struct with a non-default function address and
203 /// non-default argument values
205 /// @param[in] exe_ctx
206 /// The execution context to insert the function and its arguments
209 /// @param[in,out] args_addr_ref
210 /// The address of the structure to write the arguments into. May
211 /// be LLDB_INVALID_ADDRESS; if it is, a new structure is allocated
212 /// and args_addr_ref is pointed to it.
214 /// @param[in] function_address
215 /// The address of the function to call.
217 /// @param[in] arg_values
218 /// The values of the function's arguments.
220 /// @param[in] errors
221 /// The stream to write errors to.
224 /// True on success; false otherwise.
225 //------------------------------------------------------------------
226 bool WriteFunctionArguments (ExecutionContext &exe_ctx,
227 lldb::addr_t &args_addr_ref,
228 Address function_address,
229 ValueList &arg_values,
232 //------------------------------------------------------------------
233 /// Run the function this ClangFunction was created with.
235 /// This is the full version.
237 /// @param[in] exe_ctx
238 /// The thread & process in which this function will run.
240 /// @param[in] args_addr_ptr
241 /// If NULL, the function will take care of allocating & deallocating the wrapper
242 /// args structure. Otherwise, if set to LLDB_INVALID_ADDRESS, a new structure
243 /// will be allocated, filled and the address returned to you. You are responsible
244 /// for deallocating it. And if passed in with a value other than LLDB_INVALID_ADDRESS,
245 /// this should point to an already allocated structure with the values already written.
247 /// @param[in] errors
248 /// Errors will be written here if there are any.
250 /// @param[in] options
251 /// The options for this expression execution.
253 /// @param[out] results
254 /// The result value will be put here after running the function.
257 /// Returns one of the ExpressionResults enum indicating function call status.
258 //------------------------------------------------------------------
259 lldb::ExpressionResults
260 ExecuteFunction(ExecutionContext &exe_ctx,
261 lldb::addr_t *args_addr_ptr,
262 const EvaluateExpressionOptions &options,
266 //------------------------------------------------------------------
267 /// Get a thread plan to run the function this ClangFunction was created with.
269 /// @param[in] exe_ctx
270 /// The execution context to insert the function and its arguments
273 /// @param[in] func_addr
274 /// The address of the function in the target process.
276 /// @param[in] args_addr
277 /// The address of the argument struct.
279 /// @param[in] errors
280 /// The stream to write errors to.
282 /// @param[in] stop_others
283 /// True if other threads should pause during execution.
285 /// @param[in] unwind_on_error
286 /// True if the thread plan may simply be discarded if an error occurs.
289 /// A ThreadPlan shared pointer for executing the function.
290 //------------------------------------------------------------------
292 GetThreadPlanToCallFunction (ExecutionContext &exe_ctx,
293 lldb::addr_t args_addr,
294 const EvaluateExpressionOptions &options,
297 //------------------------------------------------------------------
298 /// Get the result of the function from its struct
300 /// @param[in] exe_ctx
301 /// The execution context to retrieve the result from.
303 /// @param[in] args_addr
304 /// The address of the argument struct.
306 /// @param[out] ret_value
307 /// The value returned by the function.
310 /// True on success; false otherwise.
311 //------------------------------------------------------------------
312 bool FetchFunctionResults (ExecutionContext &exe_ctx,
313 lldb::addr_t args_addr,
316 //------------------------------------------------------------------
317 /// Deallocate the arguments structure
319 /// @param[in] exe_ctx
320 /// The execution context to insert the function and its arguments
323 /// @param[in] args_addr
324 /// The address of the argument struct.
325 //------------------------------------------------------------------
326 void DeallocateFunctionResults (ExecutionContext &exe_ctx,
327 lldb::addr_t args_addr);
329 //------------------------------------------------------------------
330 /// Interface for ClangExpression
331 //------------------------------------------------------------------
333 //------------------------------------------------------------------
334 /// Return the string that the parser should parse. Must be a full
335 /// translation unit.
336 //------------------------------------------------------------------
340 return m_wrapper_function_text.c_str();
343 //------------------------------------------------------------------
344 /// Return the function name that should be used for executing the
345 /// expression. Text() should contain the definition of this
347 //------------------------------------------------------------------
351 return m_wrapper_function_name.c_str();
354 //------------------------------------------------------------------
355 /// Return the object that the parser should use when resolving external
356 /// values. May be NULL if everything should be self-contained.
357 //------------------------------------------------------------------
358 ClangExpressionDeclMap *
364 //------------------------------------------------------------------
365 /// Return the object that the parser should use when registering
366 /// local variables. May be NULL if the Expression doesn't care.
367 //------------------------------------------------------------------
368 ClangExpressionVariableList *
374 //------------------------------------------------------------------
375 /// Return the object that the parser should allow to access ASTs.
376 /// May be NULL if the ASTs do not need to be transformed.
378 /// @param[in] passthrough
379 /// The ASTConsumer that the returned transformer should send
380 /// the ASTs to after transformation.
381 //------------------------------------------------------------------
383 ASTTransformer (clang::ASTConsumer *passthrough);
385 //------------------------------------------------------------------
386 /// Return true if validation code should be inserted into the
388 //------------------------------------------------------------------
395 //------------------------------------------------------------------
396 /// Return true if external variables in the expression should be
398 //------------------------------------------------------------------
400 NeedsVariableResolution ()
406 GetArgumentValues () const
411 //------------------------------------------------------------------
412 // For ClangFunction only
413 //------------------------------------------------------------------
415 // Note: the parser needs to be destructed before the execution unit, so
416 // declare the execution unit first.
417 std::shared_ptr<IRExecutionUnit> m_execution_unit_sp;
418 std::unique_ptr<ClangExpressionParser> m_parser; ///< The parser responsible for compiling the function.
419 lldb::ModuleWP m_jit_module_wp;
420 std::string m_name; ///< The name of this clang function - for debugging purposes.
422 Function *m_function_ptr; ///< The function we're going to call. May be NULL if we don't have debug info for the function.
423 Address m_function_addr; ///< If we don't have the FunctionSP, we at least need the address & return type.
424 ClangASTType m_function_return_type; ///< The opaque clang qual type for the function return type.
426 std::string m_wrapper_function_name; ///< The name of the wrapper function.
427 std::string m_wrapper_function_text; ///< The contents of the wrapper function.
428 std::string m_wrapper_struct_name; ///< The name of the struct that contains the target function address, arguments, and result.
429 std::list<lldb::addr_t> m_wrapper_args_addrs; ///< The addresses of the arguments to the wrapper function.
431 std::unique_ptr<ASTStructExtractor> m_struct_extractor; ///< The class that generates the argument struct below.
433 bool m_struct_valid; ///< True if the ASTStructExtractor has populated the variables below.
435 //------------------------------------------------------------------
436 /// These values are populated by the ASTStructExtractor
437 size_t m_struct_size; ///< The size of the argument struct, in bytes.
438 std::vector<uint64_t> m_member_offsets; ///< The offset of each member in the struct, in bytes.
439 uint64_t m_return_size; ///< The size of the result variable, in bytes.
440 uint64_t m_return_offset; ///< The offset of the result variable in the struct, in bytes.
441 //------------------------------------------------------------------
443 ValueList m_arg_values; ///< The default values of the arguments.
445 bool m_compiled; ///< True if the wrapper function has already been parsed.
446 bool m_JITted; ///< True if the wrapper function has already been JIT-compiled.
449 } // Namespace lldb_private
451 #endif // lldb_ClangFunction_h_