blob: a82394592c48212eda06cbe175435973f5301b5f [file] [log] [blame]
Jim Ingham151c0322015-09-15 21:13:50 +00001//===-- ClangFunctionCallerCaller.cpp ---------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Sean Callanan4dbb2712015-09-25 20:35:58 +000010#include "ClangFunctionCaller.h"
11
12#include "ASTStructExtractor.h"
13#include "ClangExpressionParser.h"
Jim Ingham151c0322015-09-15 21:13:50 +000014
15// C Includes
16// C++ Includes
17// Other libraries and framework includes
18#include "clang/AST/ASTContext.h"
19#include "clang/AST/RecordLayout.h"
20#include "clang/CodeGen/CodeGenAction.h"
21#include "clang/CodeGen/ModuleBuilder.h"
22#include "clang/Frontend/CompilerInstance.h"
23#include "llvm/ADT/StringRef.h"
24#include "llvm/ADT/Triple.h"
25#include "llvm/ExecutionEngine/ExecutionEngine.h"
26#include "llvm/IR/Module.h"
27
28// Project includes
29#include "lldb/Core/DataExtractor.h"
30#include "lldb/Core/Log.h"
31#include "lldb/Core/Module.h"
32#include "lldb/Core/State.h"
33#include "lldb/Core/ValueObject.h"
34#include "lldb/Core/ValueObjectList.h"
Jim Ingham151c0322015-09-15 21:13:50 +000035#include "lldb/Expression/IRExecutionUnit.h"
36#include "lldb/Interpreter/CommandReturnObject.h"
37#include "lldb/Symbol/ClangASTContext.h"
38#include "lldb/Symbol/Function.h"
39#include "lldb/Symbol/Type.h"
40#include "lldb/Target/ExecutionContext.h"
41#include "lldb/Target/Process.h"
42#include "lldb/Target/RegisterContext.h"
43#include "lldb/Target/Target.h"
44#include "lldb/Target/Thread.h"
45#include "lldb/Target/ThreadPlan.h"
46#include "lldb/Target/ThreadPlanCallFunction.h"
47
48using namespace lldb_private;
49
50//----------------------------------------------------------------------
51// ClangFunctionCaller constructor
52//----------------------------------------------------------------------
53ClangFunctionCaller::ClangFunctionCaller
54(
55 ExecutionContextScope &exe_scope,
56 const CompilerType &return_type,
57 const Address& functionAddress,
58 const ValueList &arg_value_list,
59 const char *name
60) :
61 FunctionCaller(exe_scope, return_type, functionAddress, arg_value_list, name),
62 m_type_system_helper (*this)
63{
64 m_jit_process_wp = lldb::ProcessWP(exe_scope.CalculateProcess());
65 // Can't make a ClangFunctionCaller without a process.
66 assert (m_jit_process_wp.lock());
67}
68
69//----------------------------------------------------------------------
70// Destructor
71//----------------------------------------------------------------------
72ClangFunctionCaller::~ClangFunctionCaller()
73{
74}
75
76unsigned
Sean Callanan579e70c2016-03-19 00:03:59 +000077ClangFunctionCaller::CompileFunction(DiagnosticManager &diagnostic_manager)
Jim Ingham151c0322015-09-15 21:13:50 +000078{
79 if (m_compiled)
80 return 0;
Sean Callanan579e70c2016-03-19 00:03:59 +000081
Jim Ingham151c0322015-09-15 21:13:50 +000082 // FIXME: How does clang tell us there's no return value? We need to handle that case.
83 unsigned num_errors = 0;
84
85 std::string return_type_str (m_function_return_type.GetTypeName().AsCString(""));
86
87 // Cons up the function we're going to wrap our call in, then compile it...
88 // We declare the function "extern "C"" because the compiler might be in C++
89 // mode which would mangle the name and then we couldn't find it again...
90 m_wrapper_function_text.clear();
91 m_wrapper_function_text.append ("extern \"C\" void ");
92 m_wrapper_function_text.append (m_wrapper_function_name);
93 m_wrapper_function_text.append (" (void *input)\n{\n struct ");
94 m_wrapper_function_text.append (m_wrapper_struct_name);
95 m_wrapper_function_text.append (" \n {\n");
96 m_wrapper_function_text.append (" ");
97 m_wrapper_function_text.append (return_type_str);
98 m_wrapper_function_text.append (" (*fn_ptr) (");
99
100 // Get the number of arguments. If we have a function type and it is prototyped,
101 // trust that, otherwise use the values we were given.
102
103 // FIXME: This will need to be extended to handle Variadic functions. We'll need
104 // to pull the defined arguments out of the function, then add the types from the
105 // arguments list for the variable arguments.
106
107 uint32_t num_args = UINT32_MAX;
108 bool trust_function = false;
109 // GetArgumentCount returns -1 for an unprototyped function.
110 CompilerType function_clang_type;
111 if (m_function_ptr)
112 {
113 function_clang_type = m_function_ptr->GetCompilerType();
114 if (function_clang_type)
115 {
116 int num_func_args = function_clang_type.GetFunctionArgumentCount();
117 if (num_func_args >= 0)
118 {
119 trust_function = true;
120 num_args = num_func_args;
121 }
122 }
123 }
124
125 if (num_args == UINT32_MAX)
126 num_args = m_arg_values.GetSize();
127
128 std::string args_buffer; // This one stores the definition of all the args in "struct caller".
129 std::string args_list_buffer; // This one stores the argument list called from the structure.
130 for (size_t i = 0; i < num_args; i++)
131 {
132 std::string type_name;
133
134 if (trust_function)
135 {
136 type_name = function_clang_type.GetFunctionArgumentTypeAtIndex(i).GetTypeName().AsCString("");
137 }
138 else
139 {
140 CompilerType clang_qual_type = m_arg_values.GetValueAtIndex(i)->GetCompilerType ();
141 if (clang_qual_type)
142 {
143 type_name = clang_qual_type.GetTypeName().AsCString("");
144 }
145 else
Sean Callanan579e70c2016-03-19 00:03:59 +0000146 {
147 diagnostic_manager.Printf(eDiagnosticSeverityError,
148 "Could not determine type of input value %" PRIu64 ".", (uint64_t)i);
Jim Ingham151c0322015-09-15 21:13:50 +0000149 return 1;
150 }
151 }
152
153 m_wrapper_function_text.append (type_name);
154 if (i < num_args - 1)
155 m_wrapper_function_text.append (", ");
156
157 char arg_buf[32];
158 args_buffer.append (" ");
159 args_buffer.append (type_name);
160 snprintf(arg_buf, 31, "arg_%" PRIu64, (uint64_t)i);
161 args_buffer.push_back (' ');
162 args_buffer.append (arg_buf);
163 args_buffer.append (";\n");
164
165 args_list_buffer.append ("__lldb_fn_data->");
166 args_list_buffer.append (arg_buf);
167 if (i < num_args - 1)
168 args_list_buffer.append (", ");
169
170 }
171 m_wrapper_function_text.append (");\n"); // Close off the function calling prototype.
172
173 m_wrapper_function_text.append (args_buffer);
174
175 m_wrapper_function_text.append (" ");
176 m_wrapper_function_text.append (return_type_str);
177 m_wrapper_function_text.append (" return_value;");
178 m_wrapper_function_text.append ("\n };\n struct ");
179 m_wrapper_function_text.append (m_wrapper_struct_name);
180 m_wrapper_function_text.append ("* __lldb_fn_data = (struct ");
181 m_wrapper_function_text.append (m_wrapper_struct_name);
182 m_wrapper_function_text.append (" *) input;\n");
183
184 m_wrapper_function_text.append (" __lldb_fn_data->return_value = __lldb_fn_data->fn_ptr (");
185 m_wrapper_function_text.append (args_list_buffer);
186 m_wrapper_function_text.append (");\n}\n");
187
188 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
189 if (log)
190 log->Printf ("Expression: \n\n%s\n\n", m_wrapper_function_text.c_str());
191
192 // Okay, now compile this expression
193
194 lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
195 if (jit_process_sp)
196 {
197 const bool generate_debug_info = true;
198 m_parser.reset(new ClangExpressionParser(jit_process_sp.get(), *this, generate_debug_info));
Sean Callanan579e70c2016-03-19 00:03:59 +0000199
200 num_errors = m_parser->Parse(diagnostic_manager);
Jim Ingham151c0322015-09-15 21:13:50 +0000201 }
202 else
203 {
Sean Callanan579e70c2016-03-19 00:03:59 +0000204 diagnostic_manager.PutCString(eDiagnosticSeverityError, "no process - unable to inject function");
Jim Ingham151c0322015-09-15 21:13:50 +0000205 num_errors = 1;
206 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000207
Jim Ingham151c0322015-09-15 21:13:50 +0000208 m_compiled = (num_errors == 0);
209
210 if (!m_compiled)
211 return num_errors;
212
213 return num_errors;
214}
215
216clang::ASTConsumer *
217ClangFunctionCaller::ClangFunctionCallerHelper::ASTTransformer (clang::ASTConsumer *passthrough)
218{
219 m_struct_extractor.reset(new ASTStructExtractor(passthrough, m_owner.GetWrapperStructName(), m_owner));
220
221 return m_struct_extractor.get();
222}