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