1dda28197Spatrick //===-- ClangFunctionCaller.cpp -------------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick
9061da546Spatrick #include "ClangFunctionCaller.h"
10061da546Spatrick
11061da546Spatrick #include "ASTStructExtractor.h"
12061da546Spatrick #include "ClangExpressionParser.h"
13061da546Spatrick
14061da546Spatrick #include "clang/AST/ASTContext.h"
15061da546Spatrick #include "clang/AST/RecordLayout.h"
16061da546Spatrick #include "clang/CodeGen/CodeGenAction.h"
17061da546Spatrick #include "clang/CodeGen/ModuleBuilder.h"
18061da546Spatrick #include "clang/Frontend/CompilerInstance.h"
19061da546Spatrick #include "llvm/ADT/StringRef.h"
20061da546Spatrick #include "llvm/ADT/Triple.h"
21061da546Spatrick #include "llvm/ExecutionEngine/ExecutionEngine.h"
22061da546Spatrick #include "llvm/IR/Module.h"
23061da546Spatrick
24dda28197Spatrick #include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
25061da546Spatrick #include "lldb/Core/Module.h"
26061da546Spatrick #include "lldb/Core/ValueObject.h"
27061da546Spatrick #include "lldb/Core/ValueObjectList.h"
28061da546Spatrick #include "lldb/Expression/IRExecutionUnit.h"
29061da546Spatrick #include "lldb/Interpreter/CommandReturnObject.h"
30061da546Spatrick #include "lldb/Symbol/Function.h"
31061da546Spatrick #include "lldb/Symbol/Type.h"
32061da546Spatrick #include "lldb/Target/ExecutionContext.h"
33061da546Spatrick #include "lldb/Target/Process.h"
34061da546Spatrick #include "lldb/Target/RegisterContext.h"
35061da546Spatrick #include "lldb/Target/Target.h"
36061da546Spatrick #include "lldb/Target/Thread.h"
37061da546Spatrick #include "lldb/Target/ThreadPlan.h"
38061da546Spatrick #include "lldb/Target/ThreadPlanCallFunction.h"
39061da546Spatrick #include "lldb/Utility/DataExtractor.h"
40*f6aab3d8Srobert #include "lldb/Utility/LLDBLog.h"
41061da546Spatrick #include "lldb/Utility/Log.h"
42061da546Spatrick #include "lldb/Utility/State.h"
43061da546Spatrick
44061da546Spatrick using namespace lldb_private;
45061da546Spatrick
46061da546Spatrick char ClangFunctionCaller::ID;
47061da546Spatrick
48061da546Spatrick // ClangFunctionCaller constructor
ClangFunctionCaller(ExecutionContextScope & exe_scope,const CompilerType & return_type,const Address & functionAddress,const ValueList & arg_value_list,const char * name)49061da546Spatrick ClangFunctionCaller::ClangFunctionCaller(ExecutionContextScope &exe_scope,
50061da546Spatrick const CompilerType &return_type,
51061da546Spatrick const Address &functionAddress,
52061da546Spatrick const ValueList &arg_value_list,
53061da546Spatrick const char *name)
54061da546Spatrick : FunctionCaller(exe_scope, return_type, functionAddress, arg_value_list,
55061da546Spatrick name),
56061da546Spatrick m_type_system_helper(*this) {
57061da546Spatrick m_jit_process_wp = lldb::ProcessWP(exe_scope.CalculateProcess());
58061da546Spatrick // Can't make a ClangFunctionCaller without a process.
59061da546Spatrick assert(m_jit_process_wp.lock());
60061da546Spatrick }
61061da546Spatrick
62061da546Spatrick // Destructor
63be691f3bSpatrick ClangFunctionCaller::~ClangFunctionCaller() = default;
64061da546Spatrick
65061da546Spatrick unsigned
66061da546Spatrick
CompileFunction(lldb::ThreadSP thread_to_use_sp,DiagnosticManager & diagnostic_manager)67061da546Spatrick ClangFunctionCaller::CompileFunction(lldb::ThreadSP thread_to_use_sp,
68061da546Spatrick DiagnosticManager &diagnostic_manager) {
69061da546Spatrick if (m_compiled)
70061da546Spatrick return 0;
71061da546Spatrick
72061da546Spatrick // Compilation might call code, make sure to keep on the thread the caller
73061da546Spatrick // indicated.
74061da546Spatrick ThreadList::ExpressionExecutionThreadPusher execution_thread_pusher(
75061da546Spatrick thread_to_use_sp);
76061da546Spatrick
77061da546Spatrick // FIXME: How does clang tell us there's no return value? We need to handle
78061da546Spatrick // that case.
79061da546Spatrick unsigned num_errors = 0;
80061da546Spatrick
81061da546Spatrick std::string return_type_str(
82061da546Spatrick m_function_return_type.GetTypeName().AsCString(""));
83061da546Spatrick
84061da546Spatrick // Cons up the function we're going to wrap our call in, then compile it...
85061da546Spatrick // We declare the function "extern "C"" because the compiler might be in C++
86061da546Spatrick // mode which would mangle the name and then we couldn't find it again...
87061da546Spatrick m_wrapper_function_text.clear();
88061da546Spatrick m_wrapper_function_text.append("extern \"C\" void ");
89061da546Spatrick m_wrapper_function_text.append(m_wrapper_function_name);
90061da546Spatrick m_wrapper_function_text.append(" (void *input)\n{\n struct ");
91061da546Spatrick m_wrapper_function_text.append(m_wrapper_struct_name);
92061da546Spatrick m_wrapper_function_text.append(" \n {\n");
93061da546Spatrick m_wrapper_function_text.append(" ");
94061da546Spatrick m_wrapper_function_text.append(return_type_str);
95061da546Spatrick m_wrapper_function_text.append(" (*fn_ptr) (");
96061da546Spatrick
97061da546Spatrick // Get the number of arguments. If we have a function type and it is
98061da546Spatrick // prototyped, trust that, otherwise use the values we were given.
99061da546Spatrick
100061da546Spatrick // FIXME: This will need to be extended to handle Variadic functions. We'll
101061da546Spatrick // need
102061da546Spatrick // to pull the defined arguments out of the function, then add the types from
103061da546Spatrick // the arguments list for the variable arguments.
104061da546Spatrick
105061da546Spatrick uint32_t num_args = UINT32_MAX;
106061da546Spatrick bool trust_function = false;
107061da546Spatrick // GetArgumentCount returns -1 for an unprototyped function.
108061da546Spatrick CompilerType function_clang_type;
109061da546Spatrick if (m_function_ptr) {
110061da546Spatrick function_clang_type = m_function_ptr->GetCompilerType();
111061da546Spatrick if (function_clang_type) {
112061da546Spatrick int num_func_args = function_clang_type.GetFunctionArgumentCount();
113061da546Spatrick if (num_func_args >= 0) {
114061da546Spatrick trust_function = true;
115061da546Spatrick num_args = num_func_args;
116061da546Spatrick }
117061da546Spatrick }
118061da546Spatrick }
119061da546Spatrick
120061da546Spatrick if (num_args == UINT32_MAX)
121061da546Spatrick num_args = m_arg_values.GetSize();
122061da546Spatrick
123061da546Spatrick std::string args_buffer; // This one stores the definition of all the args in
124061da546Spatrick // "struct caller".
125061da546Spatrick std::string args_list_buffer; // This one stores the argument list called from
126061da546Spatrick // the structure.
127061da546Spatrick for (size_t i = 0; i < num_args; i++) {
128061da546Spatrick std::string type_name;
129061da546Spatrick
130061da546Spatrick if (trust_function) {
131061da546Spatrick type_name = function_clang_type.GetFunctionArgumentTypeAtIndex(i)
132061da546Spatrick .GetTypeName()
133061da546Spatrick .AsCString("");
134061da546Spatrick } else {
135061da546Spatrick CompilerType clang_qual_type =
136061da546Spatrick m_arg_values.GetValueAtIndex(i)->GetCompilerType();
137061da546Spatrick if (clang_qual_type) {
138061da546Spatrick type_name = clang_qual_type.GetTypeName().AsCString("");
139061da546Spatrick } else {
140061da546Spatrick diagnostic_manager.Printf(
141061da546Spatrick eDiagnosticSeverityError,
142061da546Spatrick "Could not determine type of input value %" PRIu64 ".",
143061da546Spatrick (uint64_t)i);
144061da546Spatrick return 1;
145061da546Spatrick }
146061da546Spatrick }
147061da546Spatrick
148061da546Spatrick m_wrapper_function_text.append(type_name);
149061da546Spatrick if (i < num_args - 1)
150061da546Spatrick m_wrapper_function_text.append(", ");
151061da546Spatrick
152061da546Spatrick char arg_buf[32];
153061da546Spatrick args_buffer.append(" ");
154061da546Spatrick args_buffer.append(type_name);
155061da546Spatrick snprintf(arg_buf, 31, "arg_%" PRIu64, (uint64_t)i);
156061da546Spatrick args_buffer.push_back(' ');
157061da546Spatrick args_buffer.append(arg_buf);
158061da546Spatrick args_buffer.append(";\n");
159061da546Spatrick
160061da546Spatrick args_list_buffer.append("__lldb_fn_data->");
161061da546Spatrick args_list_buffer.append(arg_buf);
162061da546Spatrick if (i < num_args - 1)
163061da546Spatrick args_list_buffer.append(", ");
164061da546Spatrick }
165061da546Spatrick m_wrapper_function_text.append(
166061da546Spatrick ");\n"); // Close off the function calling prototype.
167061da546Spatrick
168061da546Spatrick m_wrapper_function_text.append(args_buffer);
169061da546Spatrick
170061da546Spatrick m_wrapper_function_text.append(" ");
171061da546Spatrick m_wrapper_function_text.append(return_type_str);
172061da546Spatrick m_wrapper_function_text.append(" return_value;");
173061da546Spatrick m_wrapper_function_text.append("\n };\n struct ");
174061da546Spatrick m_wrapper_function_text.append(m_wrapper_struct_name);
175061da546Spatrick m_wrapper_function_text.append("* __lldb_fn_data = (struct ");
176061da546Spatrick m_wrapper_function_text.append(m_wrapper_struct_name);
177061da546Spatrick m_wrapper_function_text.append(" *) input;\n");
178061da546Spatrick
179061da546Spatrick m_wrapper_function_text.append(
180061da546Spatrick " __lldb_fn_data->return_value = __lldb_fn_data->fn_ptr (");
181061da546Spatrick m_wrapper_function_text.append(args_list_buffer);
182061da546Spatrick m_wrapper_function_text.append(");\n}\n");
183061da546Spatrick
184*f6aab3d8Srobert Log *log = GetLog(LLDBLog::Expressions);
185061da546Spatrick LLDB_LOGF(log, "Expression: \n\n%s\n\n", m_wrapper_function_text.c_str());
186061da546Spatrick
187061da546Spatrick // Okay, now compile this expression
188061da546Spatrick
189061da546Spatrick lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
190061da546Spatrick if (jit_process_sp) {
191061da546Spatrick const bool generate_debug_info = true;
192061da546Spatrick auto *clang_parser = new ClangExpressionParser(jit_process_sp.get(), *this,
193061da546Spatrick generate_debug_info);
194061da546Spatrick num_errors = clang_parser->Parse(diagnostic_manager);
195061da546Spatrick m_parser.reset(clang_parser);
196061da546Spatrick } else {
197061da546Spatrick diagnostic_manager.PutString(eDiagnosticSeverityError,
198061da546Spatrick "no process - unable to inject function");
199061da546Spatrick num_errors = 1;
200061da546Spatrick }
201061da546Spatrick
202061da546Spatrick m_compiled = (num_errors == 0);
203061da546Spatrick
204061da546Spatrick if (!m_compiled)
205061da546Spatrick return num_errors;
206061da546Spatrick
207061da546Spatrick return num_errors;
208061da546Spatrick }
209061da546Spatrick
210061da546Spatrick clang::ASTConsumer *
ASTTransformer(clang::ASTConsumer * passthrough)211061da546Spatrick ClangFunctionCaller::ClangFunctionCallerHelper::ASTTransformer(
212061da546Spatrick clang::ASTConsumer *passthrough) {
213dda28197Spatrick m_struct_extractor = std::make_unique<ASTStructExtractor>(
214dda28197Spatrick passthrough, m_owner.GetWrapperStructName(), m_owner);
215061da546Spatrick
216061da546Spatrick return m_struct_extractor.get();
217061da546Spatrick }
218