18d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt//===-- ClangFunction.h -----------------------------------------*- C++ -*-===//
28d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt//
3fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt//                     The LLVM Compiler Infrastructure
48d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt//
5c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt// This file is distributed under the University of Illinois Open Source
6c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt// License. See LICENSE.TXT for details.
78d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt//
88d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt//===----------------------------------------------------------------------===//
98d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifndef lldb_ClangFunction_h_
118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define lldb_ClangFunction_h_
128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt// C Includes
148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt// C++ Includes
158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include <vector>
16a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt#include <list>
1704f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt// Other libraries and framework includes
188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt// Project includes
198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "lldb/Core/ClangForward.h"
201f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#include "lldb/Core/Address.h"
211f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#include "lldb/Core/ArchSpec.h"
221f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#include "lldb/Core/Value.h"
231f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#include "lldb/Core/ValueObjectList.h"
241f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#include "lldb/Expression/ClangExpression.h"
251f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt#include "lldb/Target/Process.h"
261f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtnamespace lldb_private
288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtclass ASTStructExtractor;
318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtclass ClangExpressionParser;
328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt//----------------------------------------------------------------------
348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/// @class ClangFunction ClangFunction.h "lldb/Expression/ClangFunction.h"
35c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt/// @brief Encapsulates a function that can be called.
36c2ebb4b85d69b65f552fee71ac68f44e8d87b39eDmitry Shmidt///
378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/// A given ClangFunction object can handle a single function signature.
388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/// Once constructed, it can set up any number of concurrent calls to
398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/// functions with that signature.
408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt///
411f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt/// It performs the call by synthesizing a structure that contains the pointer
42a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt/// to the function and the arguments that should be passed to that function,
43bd14a57187b024f49f5b9ace55ef457d8d04650aDmitry Shmidt/// and producing a special-purpose JIT-compiled function that accepts a void*
448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/// pointing to this struct as its only argument and calls the function in the
458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/// struct with the written arguments.  This method lets Clang handle the
468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/// vagaries of function calling conventions.
478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt///
488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/// The simplest use of the ClangFunction is to construct it with a
498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/// function representative of the signature you want to use, then call
508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/// ExecuteFunction(ExecutionContext &, Stream &, Value &).
518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt///
52cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt/// If you need to reuse the arguments for several calls, you can call
538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/// InsertFunction() followed by WriteFunctionArguments(), which will return
548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/// the location of the args struct for the wrapper function in args_addr_ref.
558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt///
568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/// If you need to call the function on the thread plan stack, you can also
578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/// call InsertFunction() followed by GetThreadPlanToCallFunction().
588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt///
598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/// Any of the methods that take arg_addr_ptr or arg_addr_ref can be passed
608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/// a pointer set to LLDB_INVALID_ADDRESS and new structure will be allocated
61a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt/// and its address returned in that variable.
62a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt///
63a54fa5fb807eaeff45464139b5a7759f060cec68Dmitry Shmidt/// Any of the methods that take arg_addr_ptr can be passed NULL, and the
648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/// argument space will be managed for you.
658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt//----------------------------------------------------------------------
668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtclass ClangFunction : public ClangExpression
678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    friend class ASTStructExtractor;
698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtpublic:
708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	//------------------------------------------------------------------
718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/// Constructor
728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///
738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    /// @param[in] exe_scope
748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///     An execution context scope that gets us at least a target and
758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///     process.
768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///
778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    /// @param[in] function_ptr
788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///     The default function to be called.  Can be overridden using
798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///     WriteFunctionArguments().
808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///
818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    /// @param[in] ast_context
828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///     The AST context to evaluate argument types in.
838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///
848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    /// @param[in] arg_value_list
858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///     The default values to use when calling this function.  Can
868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///     be overridden using WriteFunctionArguments().
878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	//------------------------------------------------------------------
888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ClangFunction (ExecutionContextScope &exe_scope,
891f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt                   Function &function_ptr,
908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt                   ClangASTContext *ast_context,
918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt                   const ValueList &arg_value_list);
928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    //------------------------------------------------------------------
9404949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	/// Constructor
9504949598a23f501be6eec21697465fd46a28840aDmitry Shmidt    ///
9604949598a23f501be6eec21697465fd46a28840aDmitry Shmidt    /// @param[in] exe_scope
971f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt    ///     An execution context scope that gets us at least a target and
981f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt    ///     process.
991f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt    ///
1001f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt    /// @param[in] ast_context
1011f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt    ///     The AST context to evaluate argument types in.
1021f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt    ///
1031f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt    /// @param[in] return_qualtype
1041f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt    ///     An opaque Clang QualType for the function result.  Should be
105cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt    ///     defined in ast_context.
106051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    ///
107051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    /// @param[in] function_address
108051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    ///     The address of the function to call.
10904f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt    ///
11004f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt    /// @param[in] arg_value_list
11104f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt    ///     The default values to use when calling this function.  Can
11204f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt    ///     be overridden using WriteFunctionArguments().
11304f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt	//------------------------------------------------------------------
1148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ClangFunction (ExecutionContextScope &exe_scope,
1158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt                   const ClangASTType &return_type,
1168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt                   const Address& function_address,
1178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt                   const ValueList &arg_value_list);
11861d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
11961d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt    //------------------------------------------------------------------
12061d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	/// Destructor
12161d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	//------------------------------------------------------------------
122051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt	virtual
123051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    ~ClangFunction();
124051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
1258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    //------------------------------------------------------------------
1268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/// Compile the wrapper function
1278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///
1288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    /// @param[in] errors
1298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///     The stream to print parser errors to.
1308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///
1318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    /// @return
1328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///     The number of errors.
1338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	//------------------------------------------------------------------
1348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    unsigned
1358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    CompileFunction (Stream &errors);
1368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    //------------------------------------------------------------------
1388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/// Insert the default function wrapper and its default argument struct
1398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///
1408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    /// @param[in] exe_ctx
1418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///     The execution context to insert the function and its arguments
1428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///     into.
1438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///
1448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    /// @param[in,out] args_addr_ref
1458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///     The address of the structure to write the arguments into.  May
1468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///     be LLDB_INVALID_ADDRESS; if it is, a new structure is allocated
1478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///     and args_addr_ref is pointed to it.
1488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///
1498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    /// @param[in] errors
1508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///     The stream to write errors to.
1518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///
1528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    /// @return
1538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///     True on success; false otherwise.
1548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	//------------------------------------------------------------------
1558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    bool
1568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    InsertFunction (ExecutionContext &exe_ctx,
1578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt                    lldb::addr_t &args_addr_ref,
1588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt                    Stream &errors);
1598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    //------------------------------------------------------------------
1618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/// Insert the default function wrapper (using the JIT)
1628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///
1638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    /// @param[in] exe_ctx
1648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///     The execution context to insert the function and its arguments
1658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///     into.
1668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///
1678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    /// @param[in] errors
1688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///     The stream to write errors to.
1698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///
1708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    /// @return
1718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///     True on success; false otherwise.
1728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	//------------------------------------------------------------------
1738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    bool WriteFunctionWrapper (ExecutionContext &exe_ctx,
1748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt                               Stream &errors);
1758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    //------------------------------------------------------------------
1778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/// Insert the default function argument struct
1788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///
1798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    /// @param[in] exe_ctx
1808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///     The execution context to insert the function and its arguments
1818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///     into.
1828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///
1838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    /// @param[in,out] args_addr_ref
1848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///     The address of the structure to write the arguments into.  May
1858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///     be LLDB_INVALID_ADDRESS; if it is, a new structure is allocated
1868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///     and args_addr_ref is pointed to it.
1878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///
1888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    /// @param[in] errors
1898bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt    ///     The stream to write errors to.
1908bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt    ///
1918bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt    /// @return
1928bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt    ///     True on success; false otherwise.
1938bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt	//------------------------------------------------------------------
1948bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt    bool WriteFunctionArguments (ExecutionContext &exe_ctx,
1958bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt                                 lldb::addr_t &args_addr_ref,
1968bae4138a0356709720a96f3e50b4d734e532c12Dmitry Shmidt                                 Stream &errors);
1971f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
1981f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt    //------------------------------------------------------------------
1998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/// Insert an argument struct with a non-default function address and
2001f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt    /// non-default argument values
2018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///
2021f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt    /// @param[in] exe_ctx
2038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///     The execution context to insert the function and its arguments
2048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///     into.
2058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///
2068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    /// @param[in,out] args_addr_ref
2078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///     The address of the structure to write the arguments into.  May
2088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///     be LLDB_INVALID_ADDRESS; if it is, a new structure is allocated
2098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///     and args_addr_ref is pointed to it.
2108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///
2118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    /// @param[in] function_address
2128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///     The address of the function to call.
2138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///
2148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    /// @param[in] arg_values
2158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///     The values of the function's arguments.
2168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///
2178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    /// @param[in] errors
2188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///     The stream to write errors to.
2198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///
2208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    /// @return
2218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///     True on success; false otherwise.
2228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	//------------------------------------------------------------------
2238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    bool WriteFunctionArguments (ExecutionContext &exe_ctx,
22475ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen                                 lldb::addr_t &args_addr_ref,
22575ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen                                 Address function_address,
22675ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen                                 ValueList &arg_values,
22775ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen                                 Stream &errors);
22875ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen
22975ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen    //------------------------------------------------------------------
23075ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen	/// [Static] Execute a function, passing it a single void* parameter.
23175ecf5267604f166b85a7ee2cf0d9cb682966680Jouni Malinen    /// ClangFunction uses this to call the wrapper function.
2321f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt    ///
2331f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt    /// @param[in] exe_ctx
2341f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt    ///     The execution context to insert the function and its arguments
2351f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt    ///     into.
2361f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt    ///
2371f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt    /// @param[in] function_address
2381f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt    ///     The address of the function in the target process.
2391f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt    ///
2401f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt    /// @param[in] void_arg
241b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt    ///     The value of the void* parameter.
242b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt    ///
243b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt    /// @param[in] stop_others
244b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt    ///     True if other threads should pause during execution.
245b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt    ///
246b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt    /// @param[in] try_all_threads
247b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt    ///     If the timeout expires, true if other threads should run.  If
248b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt    ///     the function may try to take locks, this is useful.
249b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt    ///
250b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt    /// @param[in] unwind_on_error
251cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt    ///     If true, and the execution stops before completion, we unwind the
252cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt    ///     function call, and return the program state to what it was before the
253cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt    ///     execution.  If false, we leave the program in the stopped state.
254cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt    ///
255cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt    /// @param[in] timeout_usec
256cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt    ///     Timeout value (0 for no timeout). If try_all_threads is true, then we
257cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt    ///     will try on one thread for the lesser of .25 sec and half the total timeout.
258cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt    ///     then switch to running all threads, otherwise this will be the total timeout.
259cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt    ///
260cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt    /// @param[in] errors
261cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt    ///     The stream to write errors to.
262cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt    ///
263cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt    /// @param[in] this_arg
264cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt    ///     If non-NULL, the function is invoked like a C++ method, with the
265cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt    ///     value pointed to by the pointer as its 'this' argument.
266e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt    ///
267e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt    /// @return
268e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt    ///     Returns one of the ExecutionResults enum indicating function call status.
269e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt	//------------------------------------------------------------------
270e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt    static ExecutionResults
271e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt    ExecuteFunction (ExecutionContext &exe_ctx,
272e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt                     lldb::addr_t function_address,
273e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt                     lldb::addr_t &void_arg,
274e0e48dc666fb14a7bb60264ca87463ba7bc1fe0bDmitry Shmidt                     bool stop_others,
275fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt                     bool try_all_threads,
276fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt                     bool unwind_on_error,
277fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt                     bool ignore_breakpoints,
278fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt                     uint32_t timeout_usec,
279fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt                     Stream &errors,
280fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt                     lldb::addr_t* this_arg = 0);
281fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt
282fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt    //------------------------------------------------------------------
283df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt    /// Run the function this ClangFunction was created with.
284df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt    ///
285df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt    /// This simple version will run the function stopping other threads
286df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt    /// for a fixed timeout period (1000 usec) and if it does not complete,
287df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt    /// we halt the process and try with all threads running.
288df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt    ///
289df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt    /// @param[in] exe_ctx
290df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt    ///     The thread & process in which this function will run.
291df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt    ///
292df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt    /// @param[in] errors
293df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt    ///     Errors will be written here if there are any.
2948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt    ///
295    /// @param[out] results
296    ///     The result value will be put here after running the function.
297    ///
298    /// @return
299    ///     Returns one of the ExecutionResults enum indicating function call status.
300    //------------------------------------------------------------------
301    ExecutionResults
302    ExecuteFunction(ExecutionContext &exe_ctx,
303                     Stream &errors,
304                     Value &results);
305
306    //------------------------------------------------------------------
307    /// Run the function this ClangFunction was created with.
308    ///
309    /// This simple version will run the function obeying the stop_others
310    /// argument.  There is no timeout.
311    ///
312    /// @param[in] exe_ctx
313    ///     The thread & process in which this function will run.
314    ///
315    /// @param[in] errors
316    ///     Errors will be written here if there are any.
317    ///
318    /// @param[in] stop_others
319    ///     If \b true, run only this thread, if \b false let all threads run.
320    ///
321    /// @param[out] results
322    ///     The result value will be put here after running the function.
323    ///
324    /// @return
325    ///     Returns one of the ExecutionResults enum indicating function call status.
326    //------------------------------------------------------------------
327    ExecutionResults
328    ExecuteFunction(ExecutionContext &exe_ctx,
329                     Stream &errors, bool stop_others,
330                     Value &results);
331
332    //------------------------------------------------------------------
333    /// Run the function this ClangFunction was created with.
334    ///
335    /// This simple version will run the function on one thread.  If \a timeout_usec
336    /// is not zero, we time out after that timeout.  If \a try_all_threads is true, then we will
337    /// resume with all threads on, otherwise we halt the process, and eExecutionInterrupted will be returned.
338    ///
339    /// @param[in] exe_ctx
340    ///     The thread & process in which this function will run.
341    ///
342    /// @param[in] errors
343    ///     Errors will be written here if there are any.
344    ///
345    /// @param[in] timeout_usec
346    ///     Timeout value (0 for no timeout). If try_all_threads is true, then we
347    ///     will try on one thread for the lesser of .25 sec and half the total timeout.
348    ///     then switch to running all threads, otherwise this will be the total timeout.
349    ///
350    /// @param[in] try_all_threads
351    ///     If \b true, run only this thread, if \b false let all threads run.
352    ///
353    /// @param[out] results
354    ///     The result value will be put here after running the function.
355    ///
356    /// @return
357    ///     Returns one of the ExecutionResults enum indicating function call status.
358    //------------------------------------------------------------------
359    ExecutionResults
360    ExecuteFunction(ExecutionContext &exe_ctx,
361                    Stream &errors,
362                    uint32_t single_thread_timeout_usec,
363                    bool try_all_threads,
364                    Value &results);
365
366    //------------------------------------------------------------------
367    /// Run the function this ClangFunction was created with.
368    ///
369    /// This is the full version.
370    ///
371    /// @param[in] exe_ctx
372    ///     The thread & process in which this function will run.
373    ///
374    /// @param[in] args_addr_ptr
375    ///     If NULL, the function will take care of allocating & deallocating the wrapper
376    ///     args structure.  Otherwise, if set to LLDB_INVALID_ADDRESS, a new structure
377    ///     will be allocated, filled and the address returned to you.  You are responsible
378    ///     for deallocating it.  And if passed in with a value other than LLDB_INVALID_ADDRESS,
379    ///     this should point to an already allocated structure with the values already written.
380    ///
381    /// @param[in] errors
382    ///     Errors will be written here if there are any.
383    ///
384    /// @param[in] stop_others
385    ///     If \b true, run only this thread, if \b false let all threads run.
386    ///
387    /// @param[in] timeout_usec
388    ///     Timeout value (0 for no timeout). If try_all_threads is true, then we
389    ///     will try on one thread for the lesser of .25 sec and half the total timeout.
390    ///     then switch to running all threads, otherwise this will be the total timeout.
391    ///
392    ///
393    /// @param[in] try_all_threads
394    ///     If \b true, run only this thread, if \b false let all threads run.
395    ///
396    /// @param[out] results
397    ///     The result value will be put here after running the function.
398    ///
399    /// @return
400    ///     Returns one of the ExecutionResults enum indicating function call status.
401    //------------------------------------------------------------------
402    ExecutionResults
403    ExecuteFunction(ExecutionContext &exe_ctx,
404                    lldb::addr_t *args_addr_ptr,
405                    Stream &errors,
406                    bool stop_others,
407                    uint32_t timeout_usec,
408                    bool try_all_threads,
409                    bool unwind_on_error,
410                    bool ignore_breakpoints,
411                    Value &results);
412
413    //------------------------------------------------------------------
414    /// [static] Get a thread plan to run a function.
415    ///
416    /// @param[in] exe_ctx
417    ///     The execution context to insert the function and its arguments
418    ///     into.
419    ///
420    /// @param[in] func_addr
421    ///     The address of the function in the target process.
422    ///
423    /// @param[in] args_addr_ref
424    ///     The value of the void* parameter.
425    ///
426    /// @param[in] errors
427    ///     The stream to write errors to.
428    ///
429    /// @param[in] stop_others
430    ///     True if other threads should pause during execution.
431    ///
432    /// @param[in] unwind_on_error
433    ///     True if the thread plan may simply be discarded if an error occurs.
434    ///
435    /// @param[in] ignore_breakpoints
436    ///     True if the expression execution will ignore breakpoint hits and continue executing.
437    ///
438    /// @param[in] this_arg
439    ///     If non-NULL (and cmd_arg is NULL), the function is invoked like a C++
440    ///     method, with the value pointed to by the pointer as its 'this'
441    ///     argument.
442    ///
443    /// @param[in] cmd_arg
444    ///     If non-NULL, the function is invoked like an Objective-C method, with
445    ///     this_arg in the 'self' slot and cmd_arg in the '_cmd' slot
446    ///
447    /// @return
448    ///     A ThreadPlan for executing the function.
449	//------------------------------------------------------------------
450    static ThreadPlan *
451    GetThreadPlanToCallFunction (ExecutionContext &exe_ctx,
452                                 lldb::addr_t func_addr,
453                                 lldb::addr_t &args_addr_ref,
454                                 Stream &errors,
455                                 bool stop_others,
456                                 bool unwind_on_error,
457                                 bool ignore_breakpoints,
458                                 lldb::addr_t *this_arg = 0,
459                                 lldb::addr_t *cmd_arg = 0);
460
461    //------------------------------------------------------------------
462    /// Get a thread plan to run the function this ClangFunction was created with.
463    ///
464    /// @param[in] exe_ctx
465    ///     The execution context to insert the function and its arguments
466    ///     into.
467    ///
468    /// @param[in] func_addr
469    ///     The address of the function in the target process.
470    ///
471    /// @param[in] args_addr_ref
472    ///     The value of the void* parameter.
473    ///
474    /// @param[in] errors
475    ///     The stream to write errors to.
476    ///
477    /// @param[in] stop_others
478    ///     True if other threads should pause during execution.
479    ///
480    /// @param[in] unwind_on_error
481    ///     True if the thread plan may simply be discarded if an error occurs.
482    ///
483    /// @return
484    ///     A ThreadPlan for executing the function.
485	//------------------------------------------------------------------
486    ThreadPlan *
487    GetThreadPlanToCallFunction (ExecutionContext &exe_ctx,
488                                 lldb::addr_t &args_addr_ref,
489                                 Stream &errors,
490                                 bool stop_others,
491                                 bool unwind_on_error = true,
492                                 bool ignore_breakpoints = true)
493    {
494        return ClangFunction::GetThreadPlanToCallFunction (exe_ctx,
495                                                           m_jit_start_addr,
496                                                           args_addr_ref,
497                                                           errors,
498                                                           stop_others,
499                                                           unwind_on_error,
500                                                           ignore_breakpoints);
501    }
502
503    //------------------------------------------------------------------
504    /// Get the result of the function from its struct
505    ///
506    /// @param[in] exe_ctx
507    ///     The execution context to retrieve the result from.
508    ///
509    /// @param[in] args_addr
510    ///     The address of the argument struct.
511    ///
512    /// @param[in] ret_value
513    ///     The value returned by the function.
514    ///
515    /// @return
516    ///     True on success; false otherwise.
517	//------------------------------------------------------------------
518    bool FetchFunctionResults (ExecutionContext &exe_ctx,
519                               lldb::addr_t args_addr,
520                               Value &ret_value);
521
522    //------------------------------------------------------------------
523    /// Deallocate the arguments structure
524    ///
525    /// @param[in] exe_ctx
526    ///     The execution context to insert the function and its arguments
527    ///     into.
528    ///
529    /// @param[in] args_addr
530    ///     The address of the argument struct.
531	//------------------------------------------------------------------
532    void DeallocateFunctionResults (ExecutionContext &exe_ctx,
533                                    lldb::addr_t args_addr);
534
535    //------------------------------------------------------------------
536    /// Interface for ClangExpression
537    //------------------------------------------------------------------
538
539    //------------------------------------------------------------------
540    /// Return the string that the parser should parse.  Must be a full
541    /// translation unit.
542    //------------------------------------------------------------------
543    const char *
544    Text ()
545    {
546        return m_wrapper_function_text.c_str();
547    }
548
549    //------------------------------------------------------------------
550    /// Return the function name that should be used for executing the
551    /// expression.  Text() should contain the definition of this
552    /// function.
553    //------------------------------------------------------------------
554    const char *
555    FunctionName ()
556    {
557        return m_wrapper_function_name.c_str();
558    }
559
560    //------------------------------------------------------------------
561    /// Return the object that the parser should use when resolving external
562    /// values.  May be NULL if everything should be self-contained.
563    //------------------------------------------------------------------
564    ClangExpressionDeclMap *
565    DeclMap ()
566    {
567        return NULL;
568    }
569
570    //------------------------------------------------------------------
571    /// Return the object that the parser should use when registering
572    /// local variables.  May be NULL if the Expression doesn't care.
573    //------------------------------------------------------------------
574    ClangExpressionVariableList *
575    LocalVariables ()
576    {
577        return NULL;
578    }
579
580    //------------------------------------------------------------------
581    /// Return the object that the parser should allow to access ASTs.
582    /// May be NULL if the ASTs do not need to be transformed.
583    ///
584    /// @param[in] passthrough
585    ///     The ASTConsumer that the returned transformer should send
586    ///     the ASTs to after transformation.
587    //------------------------------------------------------------------
588    clang::ASTConsumer *
589    ASTTransformer (clang::ASTConsumer *passthrough);
590
591    //------------------------------------------------------------------
592    /// Return true if validation code should be inserted into the
593    /// expression.
594    //------------------------------------------------------------------
595    bool
596    NeedsValidation ()
597    {
598        return false;
599    }
600
601    //------------------------------------------------------------------
602    /// Return true if external variables in the expression should be
603    /// resolved.
604    //------------------------------------------------------------------
605    bool
606    NeedsVariableResolution ()
607    {
608        return false;
609    }
610
611    ValueList
612    GetArgumentValues () const
613    {
614        return m_arg_values;
615    }
616private:
617	//------------------------------------------------------------------
618	// For ClangFunction only
619	//------------------------------------------------------------------
620
621    std::unique_ptr<ClangExpressionParser> m_parser;                 ///< The parser responsible for compiling the function.
622    std::unique_ptr<IRExecutionUnit> m_execution_unit_ap;
623
624    Function                       *m_function_ptr;                 ///< The function we're going to call.  May be NULL if we don't have debug info for the function.
625    Address                         m_function_addr;                ///< If we don't have the FunctionSP, we at least need the address & return type.
626    ClangASTType                    m_function_return_type;         ///< The opaque clang qual type for the function return type.
627    ClangASTContext                *m_clang_ast_context;            ///< This is the clang_ast_context that we're getting types from the and value, and the function return the function pointer is NULL.
628
629    std::string                     m_wrapper_function_name;        ///< The name of the wrapper function.
630    std::string                     m_wrapper_function_text;        ///< The contents of the wrapper function.
631    std::string                     m_wrapper_struct_name;          ///< The name of the struct that contains the target function address, arguments, and result.
632    std::list<lldb::addr_t>         m_wrapper_args_addrs;           ///< The addresses of the arguments to the wrapper function.
633
634    bool                            m_struct_valid;                 ///< True if the ASTStructExtractor has populated the variables below.
635
636	//------------------------------------------------------------------
637	/// These values are populated by the ASTStructExtractor
638    size_t                          m_struct_size;                  ///< The size of the argument struct, in bytes.
639    std::vector<uint64_t>           m_member_offsets;               ///< The offset of each member in the struct, in bytes.
640    uint64_t                        m_return_size;                  ///< The size of the result variable, in bytes.
641    uint64_t                        m_return_offset;                ///< The offset of the result variable in the struct, in bytes.
642    //------------------------------------------------------------------
643
644    ValueList                       m_arg_values;                   ///< The default values of the arguments.
645
646    bool                            m_compiled;                     ///< True if the wrapper function has already been parsed.
647    bool                            m_JITted;                       ///< True if the wrapper function has already been JIT-compiled.
648};
649
650} // Namespace lldb_private
651
652#endif  // lldb_ClangFunction_h_
653