]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/llvm/tools/lldb/include/lldb/Expression/ClangFunction.h
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.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     
93     //------------------------------------------------------------------
94     /// Constructor
95     ///
96     /// @param[in] exe_scope
97     ///     An execution context scope that gets us at least a target and 
98     ///     process.
99     ///
100     /// @param[in] ast_context
101     ///     The AST context to evaluate argument types in.
102     ///
103     /// @param[in] return_qualtype
104     ///     An opaque Clang QualType for the function result.  Should be
105     ///     defined in ast_context.
106     ///
107     /// @param[in] function_address
108     ///     The address of the function to call.
109     ///
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);
118     
119     //------------------------------------------------------------------
120     /// Destructor
121     //------------------------------------------------------------------
122     virtual 
123     ~ClangFunction();
124
125     //------------------------------------------------------------------
126     /// Compile the wrapper function
127     ///
128     /// @param[in] errors
129     ///     The stream to print parser errors to.
130     ///
131     /// @return
132     ///     The number of errors.
133     //------------------------------------------------------------------
134     unsigned
135     CompileFunction (Stream &errors);
136     
137     //------------------------------------------------------------------
138     /// Insert the default function wrapper and its default argument struct  
139     ///
140     /// @param[in] exe_ctx
141     ///     The execution context to insert the function and its arguments
142     ///     into.
143     ///
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.
148     ///
149     /// @param[in] errors
150     ///     The stream to write errors to.
151     ///
152     /// @return
153     ///     True on success; false otherwise.
154     //------------------------------------------------------------------
155     bool
156     InsertFunction (ExecutionContext &exe_ctx,
157                     lldb::addr_t &args_addr_ref,
158                     Stream &errors);
159
160     //------------------------------------------------------------------
161     /// Insert the default function wrapper (using the JIT)
162     ///
163     /// @param[in] exe_ctx
164     ///     The execution context to insert the function and its arguments
165     ///     into.
166     ///
167     /// @param[in] errors
168     ///     The stream to write errors to.
169     ///
170     /// @return
171     ///     True on success; false otherwise.
172     //------------------------------------------------------------------
173     bool WriteFunctionWrapper (ExecutionContext &exe_ctx, 
174                                Stream &errors);
175     
176     //------------------------------------------------------------------
177     /// Insert the default function argument struct  
178     ///
179     /// @param[in] exe_ctx
180     ///     The execution context to insert the function and its arguments
181     ///     into.
182     ///
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.
187     ///
188     /// @param[in] errors
189     ///     The stream to write errors to.
190     ///
191     /// @return
192     ///     True on success; false otherwise.
193     //------------------------------------------------------------------
194     bool WriteFunctionArguments (ExecutionContext &exe_ctx, 
195                                  lldb::addr_t &args_addr_ref, 
196                                  Stream &errors);
197     
198     //------------------------------------------------------------------
199     /// Insert an argument struct with a non-default function address and
200     /// non-default argument values
201     ///
202     /// @param[in] exe_ctx
203     ///     The execution context to insert the function and its arguments
204     ///     into.
205     ///
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.
210     ///
211     /// @param[in] function_address
212     ///     The address of the function to call.
213     ///
214     /// @param[in] arg_values
215     ///     The values of the function's arguments.
216     ///
217     /// @param[in] errors
218     ///     The stream to write errors to.
219     ///
220     /// @return
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, 
227                                  Stream &errors);
228
229     //------------------------------------------------------------------
230     /// Run the function this ClangFunction was created with.
231     ///
232     /// This is the full version.
233     ///
234     /// @param[in] exe_ctx
235     ///     The thread & process in which this function will run.
236     ///
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.
243     ///
244     /// @param[in] errors
245     ///     Errors will be written here if there are any.
246     ///
247     /// @param[in] options
248     ///     The options for this expression execution.
249     ///
250     /// @param[out] results
251     ///     The result value will be put here after running the function.
252     ///
253     /// @return
254     ///     Returns one of the ExecutionResults enum indicating function call status.
255     //------------------------------------------------------------------
256     ExecutionResults 
257     ExecuteFunction(ExecutionContext &exe_ctx, 
258                     lldb::addr_t *args_addr_ptr, 
259                     const EvaluateExpressionOptions &options,
260                     Stream &errors,
261                     Value &results);
262     
263     //------------------------------------------------------------------
264     /// Get a thread plan to run the function this ClangFunction was created with.
265     ///
266     /// @param[in] exe_ctx
267     ///     The execution context to insert the function and its arguments
268     ///     into.
269     ///
270     /// @param[in] func_addr
271     ///     The address of the function in the target process.
272     ///
273     /// @param[in] args_addr
274     ///     The address of the argument struct.
275     ///
276     /// @param[in] errors
277     ///     The stream to write errors to.
278     ///
279     /// @param[in] stop_others
280     ///     True if other threads should pause during execution.
281     ///
282     /// @param[in] unwind_on_error
283     ///     True if the thread plan may simply be discarded if an error occurs.
284     ///
285     /// @return
286     ///     A ThreadPlan for executing the function.
287     //------------------------------------------------------------------
288     ThreadPlan *
289     GetThreadPlanToCallFunction (ExecutionContext &exe_ctx, 
290                                  lldb::addr_t args_addr,
291                                  const EvaluateExpressionOptions &options,
292                                  Stream &errors);
293     
294     //------------------------------------------------------------------
295     /// Get the result of the function from its struct
296     ///
297     /// @param[in] exe_ctx
298     ///     The execution context to retrieve the result from.
299     ///
300     /// @param[in] args_addr
301     ///     The address of the argument struct.
302     ///
303     /// @param[out] ret_value
304     ///     The value returned by the function.
305     ///
306     /// @return
307     ///     True on success; false otherwise.
308     //------------------------------------------------------------------
309     bool FetchFunctionResults (ExecutionContext &exe_ctx, 
310                                lldb::addr_t args_addr, 
311                                Value &ret_value);
312     
313     //------------------------------------------------------------------
314     /// Deallocate the arguments structure
315     ///
316     /// @param[in] exe_ctx
317     ///     The execution context to insert the function and its arguments
318     ///     into.
319     ///
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);
325     
326     //------------------------------------------------------------------
327     /// Interface for ClangExpression
328     //------------------------------------------------------------------
329     
330     //------------------------------------------------------------------
331     /// Return the string that the parser should parse.  Must be a full
332     /// translation unit.
333     //------------------------------------------------------------------
334     const char *
335     Text ()
336     {
337         return m_wrapper_function_text.c_str();
338     }
339     
340     //------------------------------------------------------------------
341     /// Return the function name that should be used for executing the
342     /// expression.  Text() should contain the definition of this
343     /// function.
344     //------------------------------------------------------------------
345     const char *
346     FunctionName ()
347     {
348         return m_wrapper_function_name.c_str();
349     }
350     
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 *
356     DeclMap ()
357     {
358         return NULL;
359     }
360     
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 *
366     LocalVariables ()
367     {
368         return NULL;
369     }
370     
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.
374     ///
375     /// @param[in] passthrough
376     ///     The ASTConsumer that the returned transformer should send
377     ///     the ASTs to after transformation.
378     //------------------------------------------------------------------
379     clang::ASTConsumer *
380     ASTTransformer (clang::ASTConsumer *passthrough);
381     
382     //------------------------------------------------------------------
383     /// Return true if validation code should be inserted into the
384     /// expression.
385     //------------------------------------------------------------------
386     bool
387     NeedsValidation ()
388     {
389         return false;
390     }
391     
392     //------------------------------------------------------------------
393     /// Return true if external variables in the expression should be
394     /// resolved.
395     //------------------------------------------------------------------
396     bool
397     NeedsVariableResolution ()
398     {
399         return false;
400     }
401     
402     ValueList
403     GetArgumentValues () const
404     {
405         return m_arg_values;
406     }
407 private:
408     //------------------------------------------------------------------
409     // For ClangFunction only
410     //------------------------------------------------------------------
411
412     std::unique_ptr<ClangExpressionParser> m_parser;                 ///< The parser responsible for compiling the function.
413     std::unique_ptr<IRExecutionUnit> m_execution_unit_ap;
414     
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.
418
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.
423     
424     std::unique_ptr<ASTStructExtractor> m_struct_extractor;         ///< The class that generates the argument struct below.
425
426     bool                            m_struct_valid;                 ///< True if the ASTStructExtractor has populated the variables below.
427     
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     //------------------------------------------------------------------
435
436     ValueList                       m_arg_values;                   ///< The default values of the arguments.
437     
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.
440 };
441
442 } // Namespace lldb_private
443
444 #endif  // lldb_ClangFunction_h_