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
13
14#include "lldb/Core/Address.h"
15#include "lldb/Core/Value.h"
18#include "lldb/Target/Process.h"
20
21namespace lldb_private {
22
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 llvm::RTTIExtends<ClangFunctionCallerHelper,
62 ClangExpressionHelper> {
63 public:
64 // LLVM RTTI support
65 static char ID;
66
68
70
71 /// Return the object that the parser should use when resolving external
72 /// values. May be NULL if everything should be self-contained.
73 ClangExpressionDeclMap *DeclMap() override { return nullptr; }
74
75 /// Return the object that the parser should allow to access ASTs. May be
76 /// NULL if the ASTs do not need to be transformed.
77 ///
78 /// \param[in] passthrough
79 /// The ASTConsumer that the returned transformer should send
80 /// the ASTs to after transformation.
81 clang::ASTConsumer *
82 ASTTransformer(clang::ASTConsumer *passthrough) override;
83
84 private:
86 std::unique_ptr<ASTStructExtractor> m_struct_extractor; ///< The class that
87 ///generates the
88 ///argument struct
89 ///layout.
90 };
91
92 // LLVM RTTI support
93 static char ID;
94
95public:
96 bool isA(const void *ClassID) const override {
97 return ClassID == &ID || FunctionCaller::isA(ClassID);
98 }
99 static bool classof(const Expression *obj) { return obj->isA(&ID); }
100
101 /// Constructor
102 ///
103 /// \param[in] exe_scope
104 /// An execution context scope that gets us at least a target and
105 /// process.
106 ///
107 /// \param[in] return_type
108 /// A compiler type for the function result. Should be
109 /// defined in ast_context.
110 ///
111 /// \param[in] function_address
112 /// The address of the function to call.
113 ///
114 /// \param[in] arg_value_list
115 /// The default values to use when calling this function. Can
116 /// be overridden using WriteFunctionArguments().
118 const CompilerType &return_type,
119 const Address &function_address,
120 const ValueList &arg_value_list, const char *name);
121
123
124 /// Compile the wrapper function
125 ///
126 /// \param[in] thread_to_use_sp
127 /// Compilation might end up calling functions. Pass in the thread you
128 /// want the compilation to use. If you pass in an empty ThreadSP it will
129 /// use the currently selected thread.
130 ///
131 /// \param[in] diagnostic_manager
132 /// The diagnostic manager to report parser errors to.
133 ///
134 /// \return
135 /// The number of errors.
136 unsigned CompileFunction(lldb::ThreadSP thread_to_use_sp,
137 DiagnosticManager &diagnostic_manager) override;
138
142
143protected:
144 const char *GetWrapperStructName() { return m_wrapper_struct_name.c_str(); }
145
146private:
147 // For ClangFunctionCaller only
148
149 // Note: the parser needs to be destructed before the execution unit, so
150 // declare the execution unit first.
152};
153
154} // namespace lldb_private
155
156#endif // LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGFUNCTIONCALLER_H
"lldb/Expression/ASTStructExtractor.h" Extracts and describes the argument structure for a wrapped fu...
A section + offset based address class.
Definition Address.h:62
"lldb/Expression/ClangExpressionDeclMap.h" Manages named entities that are defined in LLDB's debug in...
ClangExpressionDeclMap * DeclMap() override
Return the object that the parser should use when resolving external values.
clang::ASTConsumer * ASTTransformer(clang::ASTConsumer *passthrough) override
Return the object that the parser should allow to access ASTs.
std::unique_ptr< ASTStructExtractor > m_struct_extractor
The class that generates the argument struct layout.
ClangFunctionCaller(ExecutionContextScope &exe_scope, const CompilerType &return_type, const Address &function_address, const ValueList &arg_value_list, const char *name)
Constructor.
static bool classof(const Expression *obj)
bool isA(const void *ClassID) const override
ClangFunctionCallerHelper m_type_system_helper
unsigned CompileFunction(lldb::ThreadSP thread_to_use_sp, DiagnosticManager &diagnostic_manager) override
Compile the wrapper function.
ExpressionTypeSystemHelper * GetTypeSystemHelper() override
Generic representation of a type in a programming language.
"lldb/Target/ExecutionContextScope.h" Inherit from this if your object can reconstruct its execution ...
"lldb/Expression/ExpressionTypeSystemHelper.h" A helper object that the Expression can pass to its Ex...
Encapsulates a single expression for use in lldb.
Definition Expression.h:32
virtual bool isA(const void *ClassID) const =0
std::string m_wrapper_struct_name
The name of the struct that contains the target function address, arguments, and result.
FunctionCaller(ExecutionContextScope &exe_scope, const CompilerType &return_type, const Address &function_address, const ValueList &arg_value_list, const char *name)
Constructor.
bool isA(const void *ClassID) const override
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::Thread > ThreadSP