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);
93 //------------------------------------------------------------------
96 /// @param[in] exe_scope
97 /// An execution context scope that gets us at least a target and
100 /// @param[in] ast_context
101 /// The AST context to evaluate argument types in.
103 /// @param[in] return_qualtype
104 /// An opaque Clang QualType for the function result. Should be
105 /// defined in ast_context.
107 /// @param[in] function_address
108 /// The address of the function to call.
110 /// @param[in] arg_value_list
111 /// The default values to use when calling this function. Can
112 /// be overridden using WriteFunctionArguments().
113 //------------------------------------------------------------------
114 ClangFunction (ExecutionContextScope &exe_scope,
115 const ClangASTType &return_type,
116 const Address& function_address,
117 const ValueList &arg_value_list);
119 //------------------------------------------------------------------
121 //------------------------------------------------------------------
125 //------------------------------------------------------------------
126 /// Compile the wrapper function
128 /// @param[in] errors
129 /// The stream to print parser errors to.
132 /// The number of errors.
133 //------------------------------------------------------------------
135 CompileFunction (Stream &errors);
137 //------------------------------------------------------------------
138 /// Insert the default function wrapper and its default argument struct
140 /// @param[in] exe_ctx
141 /// The execution context to insert the function and its arguments
144 /// @param[in,out] args_addr_ref
145 /// The address of the structure to write the arguments into. May
146 /// be LLDB_INVALID_ADDRESS; if it is, a new structure is allocated
147 /// and args_addr_ref is pointed to it.
149 /// @param[in] errors
150 /// The stream to write errors to.
153 /// True on success; false otherwise.
154 //------------------------------------------------------------------
156 InsertFunction (ExecutionContext &exe_ctx,
157 lldb::addr_t &args_addr_ref,
160 //------------------------------------------------------------------
161 /// Insert the default function wrapper (using the JIT)
163 /// @param[in] exe_ctx
164 /// The execution context to insert the function and its arguments
167 /// @param[in] errors
168 /// The stream to write errors to.
171 /// True on success; false otherwise.
172 //------------------------------------------------------------------
173 bool WriteFunctionWrapper (ExecutionContext &exe_ctx,
176 //------------------------------------------------------------------
177 /// Insert the default function argument struct
179 /// @param[in] exe_ctx
180 /// The execution context to insert the function and its arguments
183 /// @param[in,out] args_addr_ref
184 /// The address of the structure to write the arguments into. May
185 /// be LLDB_INVALID_ADDRESS; if it is, a new structure is allocated
186 /// and args_addr_ref is pointed to it.
188 /// @param[in] errors
189 /// The stream to write errors to.
192 /// True on success; false otherwise.
193 //------------------------------------------------------------------
194 bool WriteFunctionArguments (ExecutionContext &exe_ctx,
195 lldb::addr_t &args_addr_ref,
198 //------------------------------------------------------------------
199 /// Insert an argument struct with a non-default function address and
200 /// non-default argument values
202 /// @param[in] exe_ctx
203 /// The execution context to insert the function and its arguments
206 /// @param[in,out] args_addr_ref
207 /// The address of the structure to write the arguments into. May
208 /// be LLDB_INVALID_ADDRESS; if it is, a new structure is allocated
209 /// and args_addr_ref is pointed to it.
211 /// @param[in] function_address
212 /// The address of the function to call.
214 /// @param[in] arg_values
215 /// The values of the function's arguments.
217 /// @param[in] errors
218 /// The stream to write errors to.
221 /// True on success; false otherwise.
222 //------------------------------------------------------------------
223 bool WriteFunctionArguments (ExecutionContext &exe_ctx,
224 lldb::addr_t &args_addr_ref,
225 Address function_address,
226 ValueList &arg_values,
229 //------------------------------------------------------------------
230 /// Run the function this ClangFunction was created with.
232 /// This is the full version.
234 /// @param[in] exe_ctx
235 /// The thread & process in which this function will run.
237 /// @param[in] args_addr_ptr
238 /// If NULL, the function will take care of allocating & deallocating the wrapper
239 /// args structure. Otherwise, if set to LLDB_INVALID_ADDRESS, a new structure
240 /// will be allocated, filled and the address returned to you. You are responsible
241 /// for deallocating it. And if passed in with a value other than LLDB_INVALID_ADDRESS,
242 /// this should point to an already allocated structure with the values already written.
244 /// @param[in] errors
245 /// Errors will be written here if there are any.
247 /// @param[in] options
248 /// The options for this expression execution.
250 /// @param[out] results
251 /// The result value will be put here after running the function.
254 /// Returns one of the ExecutionResults enum indicating function call status.
255 //------------------------------------------------------------------
257 ExecuteFunction(ExecutionContext &exe_ctx,
258 lldb::addr_t *args_addr_ptr,
259 const EvaluateExpressionOptions &options,
263 //------------------------------------------------------------------
264 /// Get a thread plan to run the function this ClangFunction was created with.
266 /// @param[in] exe_ctx
267 /// The execution context to insert the function and its arguments
270 /// @param[in] func_addr
271 /// The address of the function in the target process.
273 /// @param[in] args_addr
274 /// The address of the argument struct.
276 /// @param[in] errors
277 /// The stream to write errors to.
279 /// @param[in] stop_others
280 /// True if other threads should pause during execution.
282 /// @param[in] unwind_on_error
283 /// True if the thread plan may simply be discarded if an error occurs.
286 /// A ThreadPlan for executing the function.
287 //------------------------------------------------------------------
289 GetThreadPlanToCallFunction (ExecutionContext &exe_ctx,
290 lldb::addr_t args_addr,
291 const EvaluateExpressionOptions &options,
294 //------------------------------------------------------------------
295 /// Get the result of the function from its struct
297 /// @param[in] exe_ctx
298 /// The execution context to retrieve the result from.
300 /// @param[in] args_addr
301 /// The address of the argument struct.
303 /// @param[out] ret_value
304 /// The value returned by the function.
307 /// True on success; false otherwise.
308 //------------------------------------------------------------------
309 bool FetchFunctionResults (ExecutionContext &exe_ctx,
310 lldb::addr_t args_addr,
313 //------------------------------------------------------------------
314 /// Deallocate the arguments structure
316 /// @param[in] exe_ctx
317 /// The execution context to insert the function and its arguments
320 /// @param[in] args_addr
321 /// The address of the argument struct.
322 //------------------------------------------------------------------
323 void DeallocateFunctionResults (ExecutionContext &exe_ctx,
324 lldb::addr_t args_addr);
326 //------------------------------------------------------------------
327 /// Interface for ClangExpression
328 //------------------------------------------------------------------
330 //------------------------------------------------------------------
331 /// Return the string that the parser should parse. Must be a full
332 /// translation unit.
333 //------------------------------------------------------------------
337 return m_wrapper_function_text.c_str();
340 //------------------------------------------------------------------
341 /// Return the function name that should be used for executing the
342 /// expression. Text() should contain the definition of this
344 //------------------------------------------------------------------
348 return m_wrapper_function_name.c_str();
351 //------------------------------------------------------------------
352 /// Return the object that the parser should use when resolving external
353 /// values. May be NULL if everything should be self-contained.
354 //------------------------------------------------------------------
355 ClangExpressionDeclMap *
361 //------------------------------------------------------------------
362 /// Return the object that the parser should use when registering
363 /// local variables. May be NULL if the Expression doesn't care.
364 //------------------------------------------------------------------
365 ClangExpressionVariableList *
371 //------------------------------------------------------------------
372 /// Return the object that the parser should allow to access ASTs.
373 /// May be NULL if the ASTs do not need to be transformed.
375 /// @param[in] passthrough
376 /// The ASTConsumer that the returned transformer should send
377 /// the ASTs to after transformation.
378 //------------------------------------------------------------------
380 ASTTransformer (clang::ASTConsumer *passthrough);
382 //------------------------------------------------------------------
383 /// Return true if validation code should be inserted into the
385 //------------------------------------------------------------------
392 //------------------------------------------------------------------
393 /// Return true if external variables in the expression should be
395 //------------------------------------------------------------------
397 NeedsVariableResolution ()
403 GetArgumentValues () const
408 //------------------------------------------------------------------
409 // For ClangFunction only
410 //------------------------------------------------------------------
412 std::unique_ptr<ClangExpressionParser> m_parser; ///< The parser responsible for compiling the function.
413 std::unique_ptr<IRExecutionUnit> m_execution_unit_ap;
415 Function *m_function_ptr; ///< The function we're going to call. May be NULL if we don't have debug info for the function.
416 Address m_function_addr; ///< If we don't have the FunctionSP, we at least need the address & return type.
417 ClangASTType m_function_return_type; ///< The opaque clang qual type for the function return type.
419 std::string m_wrapper_function_name; ///< The name of the wrapper function.
420 std::string m_wrapper_function_text; ///< The contents of the wrapper function.
421 std::string m_wrapper_struct_name; ///< The name of the struct that contains the target function address, arguments, and result.
422 std::list<lldb::addr_t> m_wrapper_args_addrs; ///< The addresses of the arguments to the wrapper function.
424 std::unique_ptr<ASTStructExtractor> m_struct_extractor; ///< The class that generates the argument struct below.
426 bool m_struct_valid; ///< True if the ASTStructExtractor has populated the variables below.
428 //------------------------------------------------------------------
429 /// These values are populated by the ASTStructExtractor
430 size_t m_struct_size; ///< The size of the argument struct, in bytes.
431 std::vector<uint64_t> m_member_offsets; ///< The offset of each member in the struct, in bytes.
432 uint64_t m_return_size; ///< The size of the result variable, in bytes.
433 uint64_t m_return_offset; ///< The offset of the result variable in the struct, in bytes.
434 //------------------------------------------------------------------
436 ValueList m_arg_values; ///< The default values of the arguments.
438 bool m_compiled; ///< True if the wrapper function has already been parsed.
439 bool m_JITted; ///< True if the wrapper function has already been JIT-compiled.
442 } // Namespace lldb_private
444 #endif // lldb_ClangFunction_h_