LLDB  mainline
ClangFunctionCaller.h
Go to the documentation of this file.
1 //===-- ClangFunctionCaller.h -----------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGFUNCTIONCALLER_H
10 #define LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGFUNCTIONCALLER_H
11 
12 #include "ClangExpressionHelper.h"
13 
14 #include "lldb/Core/Address.h"
15 #include "lldb/Core/Value.h"
19 #include "lldb/Target/Process.h"
20 
21 namespace lldb_private {
22 
23 class ASTStructExtractor;
24 
25 /// \class ClangFunctionCaller ClangFunctionCaller.h
26 /// "lldb/Expression/ClangFunctionCaller.h" Encapsulates a function that can
27 /// be called.
28 ///
29 /// A given ClangFunctionCaller object can handle a single function signature.
30 /// Once constructed, it can set up any number of concurrent calls to
31 /// functions with that signature.
32 ///
33 /// It performs the call by synthesizing a structure that contains the pointer
34 /// to the function and the arguments that should be passed to that function,
35 /// and producing a special-purpose JIT-compiled function that accepts a void*
36 /// pointing to this struct as its only argument and calls the function in the
37 /// struct with the written arguments. This method lets Clang handle the
38 /// vagaries of function calling conventions.
39 ///
40 /// The simplest use of the ClangFunctionCaller is to construct it with a
41 /// function representative of the signature you want to use, then call
42 /// ExecuteFunction(ExecutionContext &, Stream &, Value &).
43 ///
44 /// If you need to reuse the arguments for several calls, you can call
45 /// InsertFunction() followed by WriteFunctionArguments(), which will return
46 /// the location of the args struct for the wrapper function in args_addr_ref.
47 ///
48 /// If you need to call the function on the thread plan stack, you can also
49 /// call InsertFunction() followed by GetThreadPlanToCallFunction().
50 ///
51 /// Any of the methods that take arg_addr_ptr or arg_addr_ref can be passed a
52 /// pointer set to LLDB_INVALID_ADDRESS and new structure will be allocated
53 /// and its address returned in that variable.
54 ///
55 /// Any of the methods that take arg_addr_ptr can be passed NULL, and the
56 /// argument space will be managed for you.
58  friend class ASTStructExtractor;
59 
61  public:
63 
64  ~ClangFunctionCallerHelper() override = default;
65 
66  /// Return the object that the parser should use when resolving external
67  /// values. May be NULL if everything should be self-contained.
68  ClangExpressionDeclMap *DeclMap() override { return nullptr; }
69 
70  /// Return the object that the parser should allow to access ASTs. May be
71  /// NULL if the ASTs do not need to be transformed.
72  ///
73  /// \param[in] passthrough
74  /// The ASTConsumer that the returned transformer should send
75  /// the ASTs to after transformation.
76  clang::ASTConsumer *
77  ASTTransformer(clang::ASTConsumer *passthrough) override;
78 
79  private:
81  std::unique_ptr<ASTStructExtractor> m_struct_extractor; ///< The class that
82  ///generates the
83  ///argument struct
84  ///layout.
85  };
86 
87  // LLVM RTTI support
88  static char ID;
89 
90 public:
91  bool isA(const void *ClassID) const override {
92  return ClassID == &ID || FunctionCaller::isA(ClassID);
93  }
94  static bool classof(const Expression *obj) { return obj->isA(&ID); }
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] return_type
103  /// A compiler type for the function result. Should be
104  /// defined in ast_context.
105  ///
106  /// \param[in] function_address
107  /// The address of the function to call.
108  ///
109  /// \param[in] arg_value_list
110  /// The default values to use when calling this function. Can
111  /// be overridden using WriteFunctionArguments().
113  const CompilerType &return_type,
114  const Address &function_address,
115  const ValueList &arg_value_list, const char *name);
116 
117  ~ClangFunctionCaller() override;
118 
119  /// Compile the wrapper function
120  ///
121  /// \param[in] thread_to_use_sp
122  /// Compilation might end up calling functions. Pass in the thread you
123  /// want the compilation to use. If you pass in an empty ThreadSP it will
124  /// use the currently selected thread.
125  ///
126  /// \param[in] diagnostic_manager
127  /// The diagnostic manager to report parser errors to.
128  ///
129  /// \return
130  /// The number of errors.
131  unsigned CompileFunction(lldb::ThreadSP thread_to_use_sp,
132  DiagnosticManager &diagnostic_manager) override;
133 
135  return &m_type_system_helper;
136  }
137 
138 protected:
139  const char *GetWrapperStructName() { return m_wrapper_struct_name.c_str(); }
140 
141 private:
142  // For ClangFunctionCaller only
143 
144  // Note: the parser needs to be destructed before the execution unit, so
145  // declare the execution unit first.
147 };
148 
149 } // namespace lldb_private
150 
151 #endif // LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGFUNCTIONCALLER_H
lldb_private::ClangFunctionCaller::ID
static char ID
Definition: ClangFunctionCaller.h:88
FunctionCaller.h
lldb_private::ClangFunctionCaller::GetTypeSystemHelper
ExpressionTypeSystemHelper * GetTypeSystemHelper() override
Definition: ClangFunctionCaller.h:134
lldb_private::ClangFunctionCaller::ClangFunctionCallerHelper::ASTTransformer
clang::ASTConsumer * ASTTransformer(clang::ASTConsumer *passthrough) override
Return the object that the parser should allow to access ASTs.
Definition: ClangFunctionCaller.cpp:211
lldb_private::ClangFunctionCaller::~ClangFunctionCaller
~ClangFunctionCaller() override
lldb_private::ValueList
Definition: Value.h:157
lldb_private::ClangFunctionCaller::ClangFunctionCallerHelper
Definition: ClangFunctionCaller.h:60
lldb_private::ExpressionTypeSystemHelper
Definition: ExpressionTypeSystemHelper.h:26
Process.h
lldb_private::Expression::isA
virtual bool isA(const void *ClassID) const =0
lldb_private::ClangFunctionCaller::classof
static bool classof(const Expression *obj)
Definition: ClangFunctionCaller.h:94
lldb_private::ClangFunctionCaller::isA
bool isA(const void *ClassID) const override
Definition: ClangFunctionCaller.h:91
lldb_private::FunctionCaller::m_wrapper_struct_name
std::string m_wrapper_struct_name
The name of the struct that contains the target function address, arguments, and result.
Definition: FunctionCaller.h:312
lldb_private::ExecutionContextScope
Definition: ExecutionContextScope.h:32
lldb_private::ASTStructExtractor
Definition: ASTStructExtractor.h:33
lldb_private::ClangFunctionCaller::ClangFunctionCallerHelper::~ClangFunctionCallerHelper
~ClangFunctionCallerHelper() override=default
lldb_private::ClangFunctionCaller::ClangFunctionCallerHelper::DeclMap
ClangExpressionDeclMap * DeclMap() override
Return the object that the parser should use when resolving external values.
Definition: ClangFunctionCaller.h:68
lldb_private::ClangFunctionCaller
Definition: ClangFunctionCaller.h:57
lldb_private::ClangFunctionCaller::CompileFunction
unsigned CompileFunction(lldb::ThreadSP thread_to_use_sp, DiagnosticManager &diagnostic_manager) override
Compile the wrapper function.
Definition: ClangFunctionCaller.cpp:67
lldb_private::ClangFunctionCaller::GetWrapperStructName
const char * GetWrapperStructName()
Definition: ClangFunctionCaller.h:139
CompilerType.h
Address.h
lldb_private::ClangFunctionCaller::ClangFunctionCallerHelper::m_owner
ClangFunctionCaller & m_owner
Definition: ClangFunctionCaller.h:80
lldb_private::ClangFunctionCaller::m_type_system_helper
ClangFunctionCallerHelper m_type_system_helper
Definition: ClangFunctionCaller.h:146
lldb_private::ClangFunctionCaller::ClangFunctionCallerHelper::ClangFunctionCallerHelper
ClangFunctionCallerHelper(ClangFunctionCaller &owner)
Definition: ClangFunctionCaller.h:62
lldb_private::Address
Definition: Address.h:59
lldb_private::DiagnosticManager
Definition: DiagnosticManager.h:93
lldb_private::ClangFunctionCaller::ClangFunctionCaller
ClangFunctionCaller(ExecutionContextScope &exe_scope, const CompilerType &return_type, const Address &function_address, const ValueList &arg_value_list, const char *name)
Constructor.
Definition: ClangFunctionCaller.cpp:49
lldb_private::ClangExpressionHelper
Definition: ClangExpressionHelper.h:30
lldb_private::CompilerType
Generic representation of a type in a programming language.
Definition: CompilerType.h:35
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::ClangFunctionCaller::ClangFunctionCallerHelper::m_struct_extractor
std::unique_ptr< ASTStructExtractor > m_struct_extractor
The class that generates the argument struct layout.
Definition: ClangFunctionCaller.h:81
lldb_private::FunctionCaller::isA
bool isA(const void *ClassID) const override
Definition: FunctionCaller.h:61
lldb_private::Expression
Definition: Expression.h:33
ValueObjectList.h
Value.h
ClangExpressionHelper.h
lldb_private::ClangExpressionDeclMap
Definition: ClangExpressionDeclMap.h:56
lldb_private::FunctionCaller
Definition: FunctionCaller.h:56