]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Expression/ClangFunction.h
Merge ^/head r274961 through r276301.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / include / lldb / Expression / ClangFunction.h
1 //===-- ClangFunction.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 lldb_ClangFunction_h_
11 #define lldb_ClangFunction_h_
12
13 // C Includes
14 // C++ Includes
15 #include <vector>
16 #include <list>
17 // Other libraries and framework includes
18 // Project 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"
26
27 namespace lldb_private
28 {
29     
30 class ASTStructExtractor;
31 class ClangExpressionParser;
32
33 //----------------------------------------------------------------------
34 /// @class ClangFunction ClangFunction.h "lldb/Expression/ClangFunction.h"
35 /// @brief Encapsulates a function that can be called.
36 ///
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.
40 ///
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.
47 ///
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 &).
51 ///
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.
55 ///
56 /// If you need to call the function on the thread plan stack, you can also 
57 /// call InsertFunction() followed by GetThreadPlanToCallFunction().
58 ///
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.
62 /// 
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
67 {
68     friend class ASTStructExtractor;
69 public:
70     //------------------------------------------------------------------
71     /// Constructor
72     ///
73     /// @param[in] exe_scope
74     ///     An execution context scope that gets us at least a target and 
75     ///     process.
76     ///
77     /// @param[in] function_ptr
78     ///     The default function to be called.  Can be overridden using
79     ///     WriteFunctionArguments().
80     ///
81     /// @param[in] ast_context
82     ///     The AST context to evaluate argument types in.
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     ClangFunction (ExecutionContextScope &exe_scope,
89                    Function &function_ptr, 
90                    ClangASTContext *ast_context, 
91                    const ValueList &arg_value_list,
92                    const char *name);
93     
94     //------------------------------------------------------------------
95     /// Constructor
96     ///
97     /// @param[in] exe_scope
98     ///     An execution context scope that gets us at least a target and 
99     ///     process.
100     ///
101     /// @param[in] ast_context
102     ///     The AST context to evaluate argument types in.
103     ///
104     /// @param[in] return_qualtype
105     ///     An opaque Clang QualType for the function result.  Should be
106     ///     defined in ast_context.
107     ///
108     /// @param[in] function_address
109     ///     The address of the function to call.
110     ///
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,
119                    const char *name);
120     
121     //------------------------------------------------------------------
122     /// Destructor
123     //------------------------------------------------------------------
124     virtual 
125     ~ClangFunction();
126
127     //------------------------------------------------------------------
128     /// Compile the wrapper function
129     ///
130     /// @param[in] errors
131     ///     The stream to print parser errors to.
132     ///
133     /// @return
134     ///     The number of errors.
135     //------------------------------------------------------------------
136     unsigned
137     CompileFunction (Stream &errors);
138     
139     //------------------------------------------------------------------
140     /// Insert the default function wrapper and its default argument struct  
141     ///
142     /// @param[in] exe_ctx
143     ///     The execution context to insert the function and its arguments
144     ///     into.
145     ///
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.
150     ///
151     /// @param[in] errors
152     ///     The stream to write errors to.
153     ///
154     /// @return
155     ///     True on success; false otherwise.
156     //------------------------------------------------------------------
157     bool
158     InsertFunction (ExecutionContext &exe_ctx,
159                     lldb::addr_t &args_addr_ref,
160                     Stream &errors);
161
162     //------------------------------------------------------------------
163     /// Insert the default function wrapper (using the JIT)
164     ///
165     /// @param[in] exe_ctx
166     ///     The execution context to insert the function and its arguments
167     ///     into.
168     ///
169     /// @param[in] errors
170     ///     The stream to write errors to.
171     ///
172     /// @return
173     ///     True on success; false otherwise.
174     //------------------------------------------------------------------
175     bool WriteFunctionWrapper (ExecutionContext &exe_ctx, 
176                                Stream &errors);
177     
178     //------------------------------------------------------------------
179     /// Insert the default function argument struct  
180     ///
181     /// @param[in] exe_ctx
182     ///     The execution context to insert the function and its arguments
183     ///     into.
184     ///
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.
189     ///
190     /// @param[in] errors
191     ///     The stream to write errors to.
192     ///
193     /// @return
194     ///     True on success; false otherwise.
195     //------------------------------------------------------------------
196     bool WriteFunctionArguments (ExecutionContext &exe_ctx, 
197                                  lldb::addr_t &args_addr_ref, 
198                                  Stream &errors);
199     
200     //------------------------------------------------------------------
201     /// Insert an argument struct with a non-default function address and
202     /// non-default argument values
203     ///
204     /// @param[in] exe_ctx
205     ///     The execution context to insert the function and its arguments
206     ///     into.
207     ///
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.
212     ///
213     /// @param[in] function_address
214     ///     The address of the function to call.
215     ///
216     /// @param[in] arg_values
217     ///     The values of the function's arguments.
218     ///
219     /// @param[in] errors
220     ///     The stream to write errors to.
221     ///
222     /// @return
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, 
229                                  Stream &errors);
230
231     //------------------------------------------------------------------
232     /// Run the function this ClangFunction was created with.
233     ///
234     /// This is the full version.
235     ///
236     /// @param[in] exe_ctx
237     ///     The thread & process in which this function will run.
238     ///
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.
245     ///
246     /// @param[in] errors
247     ///     Errors will be written here if there are any.
248     ///
249     /// @param[in] options
250     ///     The options for this expression execution.
251     ///
252     /// @param[out] results
253     ///     The result value will be put here after running the function.
254     ///
255     /// @return
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,
262                     Stream &errors,
263                     Value &results);
264     
265     //------------------------------------------------------------------
266     /// Get a thread plan to run the function this ClangFunction was created with.
267     ///
268     /// @param[in] exe_ctx
269     ///     The execution context to insert the function and its arguments
270     ///     into.
271     ///
272     /// @param[in] func_addr
273     ///     The address of the function in the target process.
274     ///
275     /// @param[in] args_addr
276     ///     The address of the argument struct.
277     ///
278     /// @param[in] errors
279     ///     The stream to write errors to.
280     ///
281     /// @param[in] stop_others
282     ///     True if other threads should pause during execution.
283     ///
284     /// @param[in] unwind_on_error
285     ///     True if the thread plan may simply be discarded if an error occurs.
286     ///
287     /// @return
288     ///     A ThreadPlan for executing the function.
289     //------------------------------------------------------------------
290     ThreadPlan *
291     GetThreadPlanToCallFunction (ExecutionContext &exe_ctx, 
292                                  lldb::addr_t args_addr,
293                                  const EvaluateExpressionOptions &options,
294                                  Stream &errors);
295     
296     //------------------------------------------------------------------
297     /// Get the result of the function from its struct
298     ///
299     /// @param[in] exe_ctx
300     ///     The execution context to retrieve the result from.
301     ///
302     /// @param[in] args_addr
303     ///     The address of the argument struct.
304     ///
305     /// @param[out] ret_value
306     ///     The value returned by the function.
307     ///
308     /// @return
309     ///     True on success; false otherwise.
310     //------------------------------------------------------------------
311     bool FetchFunctionResults (ExecutionContext &exe_ctx, 
312                                lldb::addr_t args_addr, 
313                                Value &ret_value);
314     
315     //------------------------------------------------------------------
316     /// Deallocate the arguments structure
317     ///
318     /// @param[in] exe_ctx
319     ///     The execution context to insert the function and its arguments
320     ///     into.
321     ///
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);
327     
328     //------------------------------------------------------------------
329     /// Interface for ClangExpression
330     //------------------------------------------------------------------
331     
332     //------------------------------------------------------------------
333     /// Return the string that the parser should parse.  Must be a full
334     /// translation unit.
335     //------------------------------------------------------------------
336     const char *
337     Text ()
338     {
339         return m_wrapper_function_text.c_str();
340     }
341     
342     //------------------------------------------------------------------
343     /// Return the function name that should be used for executing the
344     /// expression.  Text() should contain the definition of this
345     /// function.
346     //------------------------------------------------------------------
347     const char *
348     FunctionName ()
349     {
350         return m_wrapper_function_name.c_str();
351     }
352     
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 *
358     DeclMap ()
359     {
360         return NULL;
361     }
362     
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 *
368     LocalVariables ()
369     {
370         return NULL;
371     }
372     
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.
376     ///
377     /// @param[in] passthrough
378     ///     The ASTConsumer that the returned transformer should send
379     ///     the ASTs to after transformation.
380     //------------------------------------------------------------------
381     clang::ASTConsumer *
382     ASTTransformer (clang::ASTConsumer *passthrough);
383     
384     //------------------------------------------------------------------
385     /// Return true if validation code should be inserted into the
386     /// expression.
387     //------------------------------------------------------------------
388     bool
389     NeedsValidation ()
390     {
391         return false;
392     }
393     
394     //------------------------------------------------------------------
395     /// Return true if external variables in the expression should be
396     /// resolved.
397     //------------------------------------------------------------------
398     bool
399     NeedsVariableResolution ()
400     {
401         return false;
402     }
403     
404     ValueList
405     GetArgumentValues () const
406     {
407         return m_arg_values;
408     }
409 private:
410     //------------------------------------------------------------------
411     // For ClangFunction only
412     //------------------------------------------------------------------
413
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.
418     
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.
422
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.
427     
428     std::unique_ptr<ASTStructExtractor> m_struct_extractor;         ///< The class that generates the argument struct below.
429
430     bool                            m_struct_valid;                 ///< True if the ASTStructExtractor has populated the variables below.
431     
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     //------------------------------------------------------------------
439
440     ValueList                       m_arg_values;                   ///< The default values of the arguments.
441     
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.
444 };
445
446 } // Namespace lldb_private
447
448 #endif  // lldb_ClangFunction_h_