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 class ClangExpressionParser;
25 
26 /// \class ClangFunctionCaller ClangFunctionCaller.h
27 /// "lldb/Expression/ClangFunctionCaller.h" Encapsulates a function that can
28 /// be called.
29 ///
30 /// A given ClangFunctionCaller object can handle a single function signature.
31 /// Once constructed, it can set up any number of concurrent calls to
32 /// functions with that signature.
33 ///
34 /// It performs the call by synthesizing a structure that contains the pointer
35 /// to the function and the arguments that should be passed to that function,
36 /// and producing a special-purpose JIT-compiled function that accepts a void*
37 /// pointing to this struct as its only argument and calls the function in the
38 /// struct with the written arguments. This method lets Clang handle the
39 /// vagaries of function calling conventions.
40 ///
41 /// The simplest use of the ClangFunctionCaller is to construct it with a
42 /// function representative of the signature you want to use, then call
43 /// ExecuteFunction(ExecutionContext &, Stream &, Value &).
44 ///
45 /// If you need to reuse the arguments for several calls, you can call
46 /// InsertFunction() followed by WriteFunctionArguments(), which will return
47 /// the location of the args struct for the wrapper function in args_addr_ref.
48 ///
49 /// If you need to call the function on the thread plan stack, you can also
50 /// call InsertFunction() followed by GetThreadPlanToCallFunction().
51 ///
52 /// Any of the methods that take arg_addr_ptr or arg_addr_ref can be passed a
53 /// pointer set to LLDB_INVALID_ADDRESS and new structure will be allocated
54 /// and its address returned in that variable.
55 ///
56 /// Any of the methods that take arg_addr_ptr can be passed NULL, and the
57 /// argument space will be managed for you.
59  friend class ASTStructExtractor;
60 
62  public:
64 
65  ~ClangFunctionCallerHelper() override = default;
66 
67  /// Return the object that the parser should use when resolving external
68  /// values. May be NULL if everything should be self-contained.
69  ClangExpressionDeclMap *DeclMap() override { return nullptr; }
70 
71  /// Return the object that the parser should allow to access ASTs. May be
72  /// NULL if the ASTs do not need to be transformed.
73  ///
74  /// \param[in] passthrough
75  /// The ASTConsumer that the returned transformer should send
76  /// the ASTs to after transformation.
77  clang::ASTConsumer *
78  ASTTransformer(clang::ASTConsumer *passthrough) override;
79 
80  private:
82  std::unique_ptr<ASTStructExtractor> m_struct_extractor; ///< The class that
83  ///generates the
84  ///argument struct
85  ///layout.
86  };
87 
88  // LLVM RTTI support
89  static char ID;
90 
91 public:
92  bool isA(const void *ClassID) const override {
93  return ClassID == &ID || FunctionCaller::isA(ClassID);
94  }
95  static bool classof(const Expression *obj) { return obj->isA(&ID); }
96 
97  /// Constructor
98  ///
99  /// \param[in] exe_scope
100  /// An execution context scope that gets us at least a target and
101  /// process.
102  ///
103  /// \param[in] return_type
104  /// A compiler type 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().
114  const CompilerType &return_type,
115  const Address &function_address,
116  const ValueList &arg_value_list, const char *name);
117 
118  ~ClangFunctionCaller() override;
119 
120  /// Compile the wrapper function
121  ///
122  /// \param[in] thread_to_use_sp
123  /// Compilation might end up calling functions. Pass in the thread you
124  /// want the compilation to use. If you pass in an empty ThreadSP it will
125  /// use the currently selected thread.
126  ///
127  /// \param[in] diagnostic_manager
128  /// The diagnostic manager to report parser errors to.
129  ///
130  /// \return
131  /// The number of errors.
132  unsigned CompileFunction(lldb::ThreadSP thread_to_use_sp,
133  DiagnosticManager &diagnostic_manager) override;
134 
136  return &m_type_system_helper;
137  }
138 
139 protected:
140  const char *GetWrapperStructName() { return m_wrapper_struct_name.c_str(); }
141 
142 private:
143  // For ClangFunctionCaller only
144 
145  // Note: the parser needs to be destructed before the execution unit, so
146  // declare the execution unit first.
148 };
149 
150 } // namespace lldb_private
151 
152 #endif // LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGFUNCTIONCALLER_H
lldb_private::ClangFunctionCaller::ID
static char ID
Definition: ClangFunctionCaller.h:89
FunctionCaller.h
lldb_private::ClangFunctionCaller::GetTypeSystemHelper
ExpressionTypeSystemHelper * GetTypeSystemHelper() override
Definition: ClangFunctionCaller.h:135
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:210
lldb_private::ClangFunctionCaller::~ClangFunctionCaller
~ClangFunctionCaller() override
lldb_private::ValueList
Definition: Value.h:157
lldb_private::ClangFunctionCaller::ClangFunctionCallerHelper
Definition: ClangFunctionCaller.h:61
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:95
lldb_private::ClangFunctionCaller::isA
bool isA(const void *ClassID) const override
Definition: ClangFunctionCaller.h:92
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:69
lldb_private::ClangFunctionCaller
Definition: ClangFunctionCaller.h:58
lldb_private::ClangFunctionCaller::CompileFunction
unsigned CompileFunction(lldb::ThreadSP thread_to_use_sp, DiagnosticManager &diagnostic_manager) override
Compile the wrapper function.
Definition: ClangFunctionCaller.cpp:66
lldb_private::ClangFunctionCaller::GetWrapperStructName
const char * GetWrapperStructName()
Definition: ClangFunctionCaller.h:140
CompilerType.h
Address.h
lldb_private::ClangFunctionCaller::ClangFunctionCallerHelper::m_owner
ClangFunctionCaller & m_owner
Definition: ClangFunctionCaller.h:81
lldb_private::ClangFunctionCaller::m_type_system_helper
ClangFunctionCallerHelper m_type_system_helper
Definition: ClangFunctionCaller.h:147
lldb_private::ClangFunctionCaller::ClangFunctionCallerHelper::ClangFunctionCallerHelper
ClangFunctionCallerHelper(ClangFunctionCaller &owner)
Definition: ClangFunctionCaller.h:63
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:48
lldb_private::ClangExpressionHelper
Definition: ClangExpressionHelper.h:30
lldb_private::CompilerType
Generic representation of a type in a programming language.
Definition: CompilerType.h:33
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:82
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