|
|
|||
File indexing completed on 2026-05-10 08:42:47
0001 //===-- FunctionCaller.h ----------------------------------------*- C++ -*-===// 0002 // 0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 0004 // See https://llvm.org/LICENSE.txt for license information. 0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 0006 // 0007 //===----------------------------------------------------------------------===// 0008 0009 #ifndef LLDB_EXPRESSION_FUNCTIONCALLER_H 0010 #define LLDB_EXPRESSION_FUNCTIONCALLER_H 0011 0012 #include <list> 0013 #include <memory> 0014 #include <string> 0015 #include <vector> 0016 0017 #include "lldb/Core/Address.h" 0018 #include "lldb/Core/Value.h" 0019 #include "lldb/Expression/Expression.h" 0020 #include "lldb/Expression/ExpressionParser.h" 0021 #include "lldb/Symbol/CompilerType.h" 0022 0023 namespace lldb_private { 0024 0025 /// \class FunctionCaller FunctionCaller.h "lldb/Expression/FunctionCaller.h" 0026 /// Encapsulates a function that can be called. 0027 /// 0028 /// A given FunctionCaller object can handle a single function signature. 0029 /// Once constructed, it can set up any number of concurrent calls to 0030 /// functions with that signature. 0031 /// 0032 /// It performs the call by synthesizing a structure that contains the pointer 0033 /// to the function and the arguments that should be passed to that function, 0034 /// and producing a special-purpose JIT-compiled function that accepts a void* 0035 /// pointing to this struct as its only argument and calls the function in the 0036 /// struct with the written arguments. This method lets Clang handle the 0037 /// vagaries of function calling conventions. 0038 /// 0039 /// The simplest use of the FunctionCaller is to construct it with a function 0040 /// representative of the signature you want to use, then call 0041 /// ExecuteFunction(ExecutionContext &, Stream &, Value &). 0042 /// 0043 /// If you need to reuse the arguments for several calls, you can call 0044 /// InsertFunction() followed by WriteFunctionArguments(), which will return 0045 /// the location of the args struct for the wrapper function in args_addr_ref. 0046 /// 0047 /// If you need to call the function on the thread plan stack, you can also 0048 /// call InsertFunction() followed by GetThreadPlanToCallFunction(). 0049 /// 0050 /// Any of the methods that take arg_addr_ptr or arg_addr_ref can be passed a 0051 /// pointer set to LLDB_INVALID_ADDRESS and new structure will be allocated 0052 /// and its address returned in that variable. 0053 /// 0054 /// Any of the methods that take arg_addr_ptr can be passed nullptr, and the 0055 /// argument space will be managed for you. 0056 class FunctionCaller : public Expression { 0057 // LLVM RTTI support 0058 static char ID; 0059 0060 public: 0061 bool isA(const void *ClassID) const override { return ClassID == &ID; } 0062 static bool classof(const Expression *obj) { return obj->isA(&ID); } 0063 0064 /// Constructor 0065 /// 0066 /// \param[in] exe_scope 0067 /// An execution context scope that gets us at least a target and 0068 /// process. 0069 /// 0070 /// \param[in] return_type 0071 /// An opaque Clang QualType for the function result. Should be 0072 /// defined in ast_context. 0073 /// 0074 /// \param[in] function_address 0075 /// The address of the function to call. 0076 /// 0077 /// \param[in] arg_value_list 0078 /// The default values to use when calling this function. Can 0079 /// be overridden using WriteFunctionArguments(). 0080 FunctionCaller(ExecutionContextScope &exe_scope, 0081 const CompilerType &return_type, 0082 const Address &function_address, 0083 const ValueList &arg_value_list, const char *name); 0084 0085 /// Destructor 0086 ~FunctionCaller() override; 0087 0088 /// Compile the wrapper function 0089 /// 0090 /// \param[in] thread_to_use_sp 0091 /// Compilation might end up calling functions. Pass in the thread you 0092 /// want the compilation to use. If you pass in an empty ThreadSP it will 0093 /// use the currently selected thread. 0094 /// 0095 /// \param[in] diagnostic_manager 0096 /// The diagnostic manager to report parser errors to. 0097 /// 0098 /// \return 0099 /// The number of errors. 0100 virtual unsigned CompileFunction(lldb::ThreadSP thread_to_use_sp, 0101 DiagnosticManager &diagnostic_manager) = 0; 0102 0103 /// Insert the default function wrapper and its default argument struct 0104 /// 0105 /// \param[in] exe_ctx 0106 /// The execution context to insert the function and its arguments 0107 /// into. 0108 /// 0109 /// \param[in,out] args_addr_ref 0110 /// The address of the structure to write the arguments into. May 0111 /// be LLDB_INVALID_ADDRESS; if it is, a new structure is allocated 0112 /// and args_addr_ref is pointed to it. 0113 /// 0114 /// \param[in] diagnostic_manager 0115 /// The diagnostic manager to report errors to. 0116 /// 0117 /// \return 0118 /// True on success; false otherwise. 0119 bool InsertFunction(ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref, 0120 DiagnosticManager &diagnostic_manager); 0121 0122 /// Insert the default function wrapper (using the JIT) 0123 /// 0124 /// \param[in] exe_ctx 0125 /// The execution context to insert the function and its arguments 0126 /// into. 0127 /// 0128 /// \param[in] diagnostic_manager 0129 /// The diagnostic manager to report errors to. 0130 /// 0131 /// \return 0132 /// True on success; false otherwise. 0133 bool WriteFunctionWrapper(ExecutionContext &exe_ctx, 0134 DiagnosticManager &diagnostic_manager); 0135 0136 /// Insert the default function argument struct 0137 /// 0138 /// \param[in] exe_ctx 0139 /// The execution context to insert the function and its arguments 0140 /// into. 0141 /// 0142 /// \param[in,out] args_addr_ref 0143 /// The address of the structure to write the arguments into. May 0144 /// be LLDB_INVALID_ADDRESS; if it is, a new structure is allocated 0145 /// and args_addr_ref is pointed to it. 0146 /// 0147 /// \param[in] diagnostic_manager 0148 /// The diagnostic manager to report errors to. 0149 /// 0150 /// \return 0151 /// True on success; false otherwise. 0152 bool WriteFunctionArguments(ExecutionContext &exe_ctx, 0153 lldb::addr_t &args_addr_ref, 0154 DiagnosticManager &diagnostic_manager); 0155 0156 /// Insert an argument struct with a non-default function address and non- 0157 /// default argument values 0158 /// 0159 /// \param[in] exe_ctx 0160 /// The execution context to insert the function and its arguments 0161 /// into. 0162 /// 0163 /// \param[in,out] args_addr_ref 0164 /// The address of the structure to write the arguments into. May 0165 /// be LLDB_INVALID_ADDRESS; if it is, a new structure is allocated 0166 /// and args_addr_ref is pointed at it. 0167 /// 0168 /// \param[in] arg_values 0169 /// The values of the function's arguments. 0170 /// 0171 /// \param[in] diagnostic_manager 0172 /// The diagnostic manager to report errors to. 0173 /// 0174 /// \return 0175 /// True on success; false otherwise. 0176 bool WriteFunctionArguments(ExecutionContext &exe_ctx, 0177 lldb::addr_t &args_addr_ref, 0178 ValueList &arg_values, 0179 DiagnosticManager &diagnostic_manager); 0180 0181 /// Run the function this FunctionCaller was created with. 0182 /// 0183 /// This is the full version. 0184 /// 0185 /// \param[in] exe_ctx 0186 /// The thread & process in which this function will run. 0187 /// 0188 /// \param[in] args_addr_ptr 0189 /// If nullptr, the function will take care of allocating & deallocating 0190 /// the wrapper 0191 /// args structure. Otherwise, if set to LLDB_INVALID_ADDRESS, a new 0192 /// structure 0193 /// will be allocated, filled and the address returned to you. You are 0194 /// responsible 0195 /// for deallocating it. And if passed in with a value other than 0196 /// LLDB_INVALID_ADDRESS, 0197 /// this should point to an already allocated structure with the values 0198 /// already written. 0199 /// 0200 /// \param[in] diagnostic_manager 0201 /// The diagnostic manager to report errors to. 0202 /// 0203 /// \param[in] options 0204 /// The options for this expression execution. 0205 /// 0206 /// \param[out] results 0207 /// The result value will be put here after running the function. 0208 /// 0209 /// \return 0210 /// Returns one of the ExpressionResults enum indicating function call 0211 /// status. 0212 lldb::ExpressionResults 0213 ExecuteFunction(ExecutionContext &exe_ctx, lldb::addr_t *args_addr_ptr, 0214 const EvaluateExpressionOptions &options, 0215 DiagnosticManager &diagnostic_manager, Value &results); 0216 0217 /// Get a thread plan to run the function this FunctionCaller was created 0218 /// with. 0219 /// 0220 /// \param[in] exe_ctx 0221 /// The execution context to insert the function and its arguments 0222 /// into. 0223 /// 0224 /// \param[in] args_addr 0225 /// The address of the argument struct. 0226 /// 0227 /// \param[in] diagnostic_manager 0228 /// The diagnostic manager to report errors to. 0229 /// 0230 /// \return 0231 /// A ThreadPlan shared pointer for executing the function. 0232 lldb::ThreadPlanSP 0233 GetThreadPlanToCallFunction(ExecutionContext &exe_ctx, lldb::addr_t args_addr, 0234 const EvaluateExpressionOptions &options, 0235 DiagnosticManager &diagnostic_manager); 0236 0237 /// Get the result of the function from its struct 0238 /// 0239 /// \param[in] exe_ctx 0240 /// The execution context to retrieve the result from. 0241 /// 0242 /// \param[in] args_addr 0243 /// The address of the argument struct. 0244 /// 0245 /// \param[out] ret_value 0246 /// The value returned by the function. 0247 /// 0248 /// \return 0249 /// True on success; false otherwise. 0250 bool FetchFunctionResults(ExecutionContext &exe_ctx, lldb::addr_t args_addr, 0251 Value &ret_value); 0252 0253 /// Deallocate the arguments structure 0254 /// 0255 /// \param[in] exe_ctx 0256 /// The execution context to insert the function and its arguments 0257 /// into. 0258 /// 0259 /// \param[in] args_addr 0260 /// The address of the argument struct. 0261 void DeallocateFunctionResults(ExecutionContext &exe_ctx, 0262 lldb::addr_t args_addr); 0263 0264 /// Interface for ClangExpression 0265 0266 /// Return the string that the parser should parse. Must be a full 0267 /// translation unit. 0268 const char *Text() override { return m_wrapper_function_text.c_str(); } 0269 0270 /// Return the function name that should be used for executing the 0271 /// expression. Text() should contain the definition of this function. 0272 const char *FunctionName() override { 0273 return m_wrapper_function_name.c_str(); 0274 } 0275 0276 /// Return the object that the parser should use when registering local 0277 /// variables. May be nullptr if the Expression doesn't care. 0278 ExpressionVariableList *LocalVariables() { return nullptr; } 0279 0280 /// Return true if validation code should be inserted into the expression. 0281 bool NeedsValidation() override { return false; } 0282 0283 /// Return true if external variables in the expression should be resolved. 0284 bool NeedsVariableResolution() override { return false; } 0285 0286 ValueList GetArgumentValues() const { return m_arg_values; } 0287 0288 protected: 0289 // Note: the parser needs to be destructed before the execution unit, so 0290 // declare the execution unit first. 0291 std::shared_ptr<IRExecutionUnit> m_execution_unit_sp; 0292 std::unique_ptr<ExpressionParser> 0293 m_parser; ///< The parser responsible for compiling the function. 0294 ///< This will get made in CompileFunction, so it is 0295 ///< safe to access it after that. 0296 0297 lldb::ModuleWP m_jit_module_wp; 0298 std::string 0299 m_name; ///< The name of this clang function - for debugging purposes. 0300 0301 Function *m_function_ptr; ///< The function we're going to call. May be 0302 ///nullptr if we don't have debug info for the 0303 ///function. 0304 Address m_function_addr; ///< If we don't have the FunctionSP, we at least 0305 ///need the address & return type. 0306 CompilerType m_function_return_type; ///< The opaque clang qual type for the 0307 ///function return type. 0308 0309 std::string m_wrapper_function_name; ///< The name of the wrapper function. 0310 std::string 0311 m_wrapper_function_text; ///< The contents of the wrapper function. 0312 std::string m_wrapper_struct_name; ///< The name of the struct that contains 0313 ///the target function address, arguments, 0314 ///and result. 0315 std::list<lldb::addr_t> m_wrapper_args_addrs; ///< The addresses of the 0316 ///arguments to the wrapper 0317 ///function. 0318 0319 bool m_struct_valid; ///< True if the ASTStructExtractor has populated the 0320 ///variables below. 0321 0322 /// These values are populated by the ASTStructExtractor 0323 size_t m_struct_size; ///< The size of the argument struct, in bytes. 0324 std::vector<uint64_t> 0325 m_member_offsets; ///< The offset of each member in the struct, in bytes. 0326 uint64_t m_return_size; ///< The size of the result variable, in bytes. 0327 uint64_t m_return_offset; ///< The offset of the result variable in the 0328 ///struct, in bytes. 0329 0330 ValueList m_arg_values; ///< The default values of the arguments. 0331 0332 bool m_compiled; ///< True if the wrapper function has already been parsed. 0333 bool 0334 m_JITted; ///< True if the wrapper function has already been JIT-compiled. 0335 }; 0336 0337 } // namespace lldb_private 0338 0339 #endif // LLDB_EXPRESSION_FUNCTIONCALLER_H
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|