blob: a7e7ac0106996e950c55600685ec0568ae23ff7b [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ClangFunction.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
10
11// C Includes
12// C++ Includes
13// Other libraries and framework includes
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014#include "clang/AST/ASTContext.h"
15#include "clang/AST/RecordLayout.h"
Greg Claytond2d60ce2010-07-02 18:39:06 +000016#include "clang/CodeGen/CodeGenAction.h"
17#include "clang/CodeGen/ModuleBuilder.h"
18#include "clang/Frontend/CompilerInstance.h"
19#include "llvm/ADT/StringRef.h"
Greg Clayton514487e2011-02-15 21:59:32 +000020#include "llvm/ADT/Triple.h"
Greg Claytond2d60ce2010-07-02 18:39:06 +000021#include "llvm/ExecutionEngine/ExecutionEngine.h"
Chandler Carruth1e157582013-01-02 12:20:07 +000022#include "llvm/IR/Module.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023
24// Project includes
Greg Clayton23f8c952014-03-24 23:10:19 +000025#include "lldb/Core/DataExtractor.h"
26#include "lldb/Core/Log.h"
27#include "lldb/Core/Module.h"
28#include "lldb/Core/State.h"
29#include "lldb/Core/ValueObject.h"
30#include "lldb/Core/ValueObjectList.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000031#include "lldb/Expression/ASTStructExtractor.h"
32#include "lldb/Expression/ClangExpressionParser.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033#include "lldb/Expression/ClangFunction.h"
Greg Claytone01e07b2013-04-18 18:10:51 +000034#include "lldb/Expression/IRExecutionUnit.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035#include "lldb/Interpreter/CommandReturnObject.h"
36#include "lldb/Symbol/ClangASTContext.h"
37#include "lldb/Symbol/Function.h"
Greg Clayton23f8c952014-03-24 23:10:19 +000038#include "lldb/Symbol/Type.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039#include "lldb/Target/ExecutionContext.h"
40#include "lldb/Target/Process.h"
Sean Callananebf77072010-07-23 00:16:21 +000041#include "lldb/Target/RegisterContext.h"
Greg Clayton514487e2011-02-15 21:59:32 +000042#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043#include "lldb/Target/Thread.h"
44#include "lldb/Target/ThreadPlan.h"
45#include "lldb/Target/ThreadPlanCallFunction.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046
47using namespace lldb_private;
Sean Callanan1a8d4092010-08-27 01:01:44 +000048
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049//----------------------------------------------------------------------
50// ClangFunction constructor
51//----------------------------------------------------------------------
Greg Clayton7b462cc2010-10-15 22:48:33 +000052ClangFunction::ClangFunction
53(
Jim Ingham35944dd2011-03-17 20:02:56 +000054 ExecutionContextScope &exe_scope,
Greg Clayton57ee3062013-07-11 22:46:58 +000055 const ClangASTType &return_type,
Greg Clayton7b462cc2010-10-15 22:48:33 +000056 const Address& functionAddress,
57 const ValueList &arg_value_list
58) :
Greg Clayton23f8c952014-03-24 23:10:19 +000059 m_parser(),
60 m_execution_unit_sp(),
61 m_jit_module_wp(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062 m_function_ptr (NULL),
Greg Claytonc982c762010-07-09 20:39:50 +000063 m_function_addr (functionAddress),
Greg Clayton57ee3062013-07-11 22:46:58 +000064 m_function_return_type(return_type),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000065 m_wrapper_function_name ("__lldb_caller_function"),
66 m_wrapper_struct_name ("__lldb_caller_struct"),
Greg Claytonc982c762010-07-09 20:39:50 +000067 m_wrapper_args_addrs (),
Greg Claytonc982c762010-07-09 20:39:50 +000068 m_arg_values (arg_value_list),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000069 m_compiled (false),
70 m_JITted (false)
71{
Enrico Granatadfc88a02012-09-18 00:08:47 +000072 m_jit_process_wp = lldb::ProcessWP(exe_scope.CalculateProcess());
Jim Ingham35944dd2011-03-17 20:02:56 +000073 // Can't make a ClangFunction without a process.
Enrico Granatadfc88a02012-09-18 00:08:47 +000074 assert (m_jit_process_wp.lock());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000075}
76
Greg Clayton7b462cc2010-10-15 22:48:33 +000077ClangFunction::ClangFunction
78(
Jim Ingham35944dd2011-03-17 20:02:56 +000079 ExecutionContextScope &exe_scope,
Greg Clayton7b462cc2010-10-15 22:48:33 +000080 Function &function,
81 ClangASTContext *ast_context,
82 const ValueList &arg_value_list
83) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +000084 m_function_ptr (&function),
Greg Claytonc982c762010-07-09 20:39:50 +000085 m_function_addr (),
Greg Clayton57ee3062013-07-11 22:46:58 +000086 m_function_return_type (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000087 m_wrapper_function_name ("__lldb_function_caller"),
88 m_wrapper_struct_name ("__lldb_caller_struct"),
Greg Claytonc982c762010-07-09 20:39:50 +000089 m_wrapper_args_addrs (),
Greg Claytonc982c762010-07-09 20:39:50 +000090 m_arg_values (arg_value_list),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000091 m_compiled (false),
92 m_JITted (false)
93{
Greg Clayton23f8c952014-03-24 23:10:19 +000094 m_jit_process_wp = exe_scope.CalculateProcess();
Jim Ingham35944dd2011-03-17 20:02:56 +000095 // Can't make a ClangFunction without a process.
Enrico Granatadfc88a02012-09-18 00:08:47 +000096 assert (m_jit_process_wp.lock());
Greg Clayton514487e2011-02-15 21:59:32 +000097
Chris Lattner30fdc8d2010-06-08 16:52:24 +000098 m_function_addr = m_function_ptr->GetAddressRange().GetBaseAddress();
Greg Clayton57ee3062013-07-11 22:46:58 +000099 m_function_return_type = m_function_ptr->GetClangType().GetFunctionReturnType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000100}
101
102//----------------------------------------------------------------------
103// Destructor
104//----------------------------------------------------------------------
105ClangFunction::~ClangFunction()
106{
Greg Clayton23f8c952014-03-24 23:10:19 +0000107 lldb::ProcessSP process_sp (m_jit_process_wp.lock());
108 if (process_sp)
109 {
110 lldb::ModuleSP jit_module_sp (m_jit_module_wp.lock());
111 if (jit_module_sp)
112 process_sp->GetTarget().GetImages().Remove(jit_module_sp);
113 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000114}
115
116unsigned
117ClangFunction::CompileFunction (Stream &errors)
118{
Sean Callanan1a8d4092010-08-27 01:01:44 +0000119 if (m_compiled)
120 return 0;
121
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000122 // FIXME: How does clang tell us there's no return value? We need to handle that case.
123 unsigned num_errors = 0;
124
Jim Inghamfb6fc0d2013-09-27 20:59:37 +0000125 std::string return_type_str (m_function_return_type.GetTypeName().AsCString(""));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000126
127 // Cons up the function we're going to wrap our call in, then compile it...
128 // We declare the function "extern "C"" because the compiler might be in C++
129 // mode which would mangle the name and then we couldn't find it again...
130 m_wrapper_function_text.clear();
131 m_wrapper_function_text.append ("extern \"C\" void ");
132 m_wrapper_function_text.append (m_wrapper_function_name);
133 m_wrapper_function_text.append (" (void *input)\n{\n struct ");
134 m_wrapper_function_text.append (m_wrapper_struct_name);
135 m_wrapper_function_text.append (" \n {\n");
136 m_wrapper_function_text.append (" ");
137 m_wrapper_function_text.append (return_type_str);
138 m_wrapper_function_text.append (" (*fn_ptr) (");
139
140 // Get the number of arguments. If we have a function type and it is prototyped,
141 // trust that, otherwise use the values we were given.
142
143 // FIXME: This will need to be extended to handle Variadic functions. We'll need
144 // to pull the defined arguments out of the function, then add the types from the
145 // arguments list for the variable arguments.
146
Greg Clayton57ee3062013-07-11 22:46:58 +0000147 uint32_t num_args = UINT32_MAX;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000148 bool trust_function = false;
149 // GetArgumentCount returns -1 for an unprototyped function.
Greg Clayton57ee3062013-07-11 22:46:58 +0000150 ClangASTType function_clang_type;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000151 if (m_function_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000152 {
Greg Clayton57ee3062013-07-11 22:46:58 +0000153 function_clang_type = m_function_ptr->GetClangType();
154 if (function_clang_type)
155 {
156 int num_func_args = function_clang_type.GetFunctionArgumentCount();
157 if (num_func_args >= 0)
158 {
159 trust_function = true;
160 num_args = num_func_args;
161 }
162 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000163 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000164
Sean Callanan1a8d4092010-08-27 01:01:44 +0000165 if (num_args == UINT32_MAX)
166 num_args = m_arg_values.GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000167
Sean Callanan1a8d4092010-08-27 01:01:44 +0000168 std::string args_buffer; // This one stores the definition of all the args in "struct caller".
169 std::string args_list_buffer; // This one stores the argument list called from the structure.
170 for (size_t i = 0; i < num_args; i++)
171 {
Greg Claytonf4ecaa52011-02-16 23:00:21 +0000172 std::string type_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000173
Sean Callanan1a8d4092010-08-27 01:01:44 +0000174 if (trust_function)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000175 {
Sean Callananddd7a2a2013-10-03 22:27:29 +0000176 type_name = function_clang_type.GetFunctionArgumentTypeAtIndex(i).GetTypeName().AsCString("");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000177 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000178 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179 {
Greg Clayton57ee3062013-07-11 22:46:58 +0000180 ClangASTType clang_qual_type = m_arg_values.GetValueAtIndex(i)->GetClangType ();
181 if (clang_qual_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000182 {
Sean Callananddd7a2a2013-10-03 22:27:29 +0000183 type_name = clang_qual_type.GetTypeName().AsCString("");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000184 }
185 else
Sean Callanan1a8d4092010-08-27 01:01:44 +0000186 {
Greg Clayton6fea17e2014-03-03 19:15:20 +0000187 errors.Printf("Could not determine type of input value %" PRIu64 ".", (uint64_t)i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000188 return 1;
189 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000190 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000191
Greg Claytonf4ecaa52011-02-16 23:00:21 +0000192 m_wrapper_function_text.append (type_name);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000193 if (i < num_args - 1)
194 m_wrapper_function_text.append (", ");
195
196 char arg_buf[32];
197 args_buffer.append (" ");
Greg Claytonf4ecaa52011-02-16 23:00:21 +0000198 args_buffer.append (type_name);
Daniel Malead01b2952012-11-29 21:49:15 +0000199 snprintf(arg_buf, 31, "arg_%" PRIu64, (uint64_t)i);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000200 args_buffer.push_back (' ');
201 args_buffer.append (arg_buf);
202 args_buffer.append (";\n");
203
204 args_list_buffer.append ("__lldb_fn_data->");
205 args_list_buffer.append (arg_buf);
206 if (i < num_args - 1)
207 args_list_buffer.append (", ");
208
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000209 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000210 m_wrapper_function_text.append (");\n"); // Close off the function calling prototype.
211
212 m_wrapper_function_text.append (args_buffer);
213
214 m_wrapper_function_text.append (" ");
215 m_wrapper_function_text.append (return_type_str);
216 m_wrapper_function_text.append (" return_value;");
217 m_wrapper_function_text.append ("\n };\n struct ");
218 m_wrapper_function_text.append (m_wrapper_struct_name);
219 m_wrapper_function_text.append ("* __lldb_fn_data = (struct ");
220 m_wrapper_function_text.append (m_wrapper_struct_name);
221 m_wrapper_function_text.append (" *) input;\n");
222
223 m_wrapper_function_text.append (" __lldb_fn_data->return_value = __lldb_fn_data->fn_ptr (");
224 m_wrapper_function_text.append (args_list_buffer);
225 m_wrapper_function_text.append (");\n}\n");
226
Greg Clayton5160ce52013-03-27 23:08:40 +0000227 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000228 if (log)
229 log->Printf ("Expression: \n\n%s\n\n", m_wrapper_function_text.c_str());
230
231 // Okay, now compile this expression
Sean Callanan1a8d4092010-08-27 01:01:44 +0000232
Enrico Granatadfc88a02012-09-18 00:08:47 +0000233 lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
234 if (jit_process_sp)
235 {
Greg Clayton23f8c952014-03-24 23:10:19 +0000236 const bool generate_debug_info = true;
237 m_parser.reset(new ClangExpressionParser(jit_process_sp.get(), *this, generate_debug_info));
Enrico Granatadfc88a02012-09-18 00:08:47 +0000238
239 num_errors = m_parser->Parse (errors);
240 }
241 else
242 {
243 errors.Printf("no process - unable to inject function");
244 num_errors = 1;
245 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000246
247 m_compiled = (num_errors == 0);
248
249 if (!m_compiled)
250 return num_errors;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000251
252 return num_errors;
253}
254
255bool
Sean Callanan138e74e2010-07-26 22:14:36 +0000256ClangFunction::WriteFunctionWrapper (ExecutionContext &exe_ctx, Stream &errors)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000257{
Greg Claytonc14ee322011-09-22 04:58:26 +0000258 Process *process = exe_ctx.GetProcessPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000259
Sean Callanan1a8d4092010-08-27 01:01:44 +0000260 if (!process)
261 return false;
Enrico Granatadfc88a02012-09-18 00:08:47 +0000262
263 lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
264
265 if (process != jit_process_sp.get())
Jim Ingham35944dd2011-03-17 20:02:56 +0000266 return false;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000267
268 if (!m_compiled)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000269 return false;
270
Sean Callanan1a8d4092010-08-27 01:01:44 +0000271 if (m_JITted)
272 return true;
Sean Callanan1582ee62013-04-18 22:06:33 +0000273
274 bool can_interpret = false; // should stay that way
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000275
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000276 Error jit_error (m_parser->PrepareForExecution (m_jit_start_addr,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000277 m_jit_end_addr,
Greg Clayton23f8c952014-03-24 23:10:19 +0000278 m_execution_unit_sp,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000279 exe_ctx,
Sean Callanan1582ee62013-04-18 22:06:33 +0000280 can_interpret,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000281 eExecutionPolicyAlways));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000282
283 if (!jit_error.Success())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000284 return false;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000285
Greg Clayton23f8c952014-03-24 23:10:19 +0000286 if (m_parser->GetGenerateDebugInfo())
287 {
288 lldb::ModuleSP jit_module_sp ( m_execution_unit_sp->GetJITModule());
289
290 if (jit_module_sp)
291 {
292 ConstString const_func_name(FunctionName());
293 FileSpec jit_file;
294 jit_file.GetFilename() = const_func_name;
295 jit_module_sp->SetFileSpecAndObjectName (jit_file, ConstString());
296 m_jit_module_wp = jit_module_sp;
297 process->GetTarget().GetImages().Append(jit_module_sp);
298 }
299 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000300 if (process && m_jit_start_addr)
Greg Clayton23f8c952014-03-24 23:10:19 +0000301 m_jit_process_wp = process->shared_from_this();
Sean Callanand14fac12013-01-14 21:45:38 +0000302
303 m_JITted = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000304
305 return true;
306}
307
308bool
Sean Callanan138e74e2010-07-26 22:14:36 +0000309ClangFunction::WriteFunctionArguments (ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref, Stream &errors)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000310{
Sean Callanan138e74e2010-07-26 22:14:36 +0000311 return WriteFunctionArguments(exe_ctx, args_addr_ref, m_function_addr, m_arg_values, errors);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000312}
313
314// FIXME: Assure that the ValueList we were passed in is consistent with the one that defined this function.
315
316bool
Jim Inghambaae1682010-09-10 23:07:48 +0000317ClangFunction::WriteFunctionArguments (ExecutionContext &exe_ctx,
318 lldb::addr_t &args_addr_ref,
319 Address function_address,
320 ValueList &arg_values,
321 Stream &errors)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000322{
Sean Callanan1a8d4092010-08-27 01:01:44 +0000323 // All the information to reconstruct the struct is provided by the
324 // StructExtractor.
325 if (!m_struct_valid)
326 {
327 errors.Printf("Argument information was not correctly parsed, so the function cannot be called.");
328 return false;
329 }
330
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000331 Error error;
332 using namespace clang;
Greg Claytone0d378b2011-03-24 21:19:54 +0000333 ExecutionResults return_value = eExecutionSetupError;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000334
Greg Claytonc14ee322011-09-22 04:58:26 +0000335 Process *process = exe_ctx.GetProcessPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000336
337 if (process == NULL)
338 return return_value;
Jim Ingham35944dd2011-03-17 20:02:56 +0000339
Enrico Granatadfc88a02012-09-18 00:08:47 +0000340 lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
341
342 if (process != jit_process_sp.get())
Jim Ingham35944dd2011-03-17 20:02:56 +0000343 return false;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000344
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000345 if (args_addr_ref == LLDB_INVALID_ADDRESS)
346 {
Sean Callanan1a8d4092010-08-27 01:01:44 +0000347 args_addr_ref = process->AllocateMemory(m_struct_size, lldb::ePermissionsReadable|lldb::ePermissionsWritable, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000348 if (args_addr_ref == LLDB_INVALID_ADDRESS)
349 return false;
350 m_wrapper_args_addrs.push_back (args_addr_ref);
351 }
352 else
353 {
354 // Make sure this is an address that we've already handed out.
355 if (find (m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(), args_addr_ref) == m_wrapper_args_addrs.end())
356 {
357 return false;
358 }
359 }
360
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000361 // TODO: verify fun_addr needs to be a callable address
Greg Claytonc14ee322011-09-22 04:58:26 +0000362 Scalar fun_addr (function_address.GetCallableLoadAddress(exe_ctx.GetTargetPtr()));
Greg Claytonc7bece562013-01-25 18:06:21 +0000363 uint64_t first_offset = m_member_offsets[0];
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000364 process->WriteScalarToMemory(args_addr_ref + first_offset, fun_addr, process->GetAddressByteSize(), error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000365
366 // FIXME: We will need to extend this for Variadic functions.
367
368 Error value_error;
369
370 size_t num_args = arg_values.GetSize();
371 if (num_args != m_arg_values.GetSize())
372 {
Greg Clayton6fea17e2014-03-03 19:15:20 +0000373 errors.Printf ("Wrong number of arguments - was: %" PRIu64 " should be: %" PRIu64 "", (uint64_t)num_args, (uint64_t)m_arg_values.GetSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000374 return false;
375 }
376
Greg Claytonc982c762010-07-09 20:39:50 +0000377 for (size_t i = 0; i < num_args; i++)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000378 {
379 // FIXME: We should sanity check sizes.
380
Greg Claytonc7bece562013-01-25 18:06:21 +0000381 uint64_t offset = m_member_offsets[i+1]; // Clang sizes are in bytes.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000382 Value *arg_value = arg_values.GetValueAtIndex(i);
383
384 // FIXME: For now just do scalars:
385
386 // Special case: if it's a pointer, don't do anything (the ABI supports passing cstrings)
387
388 if (arg_value->GetValueType() == Value::eValueTypeHostAddress &&
Greg Clayton57ee3062013-07-11 22:46:58 +0000389 arg_value->GetContextType() == Value::eContextTypeInvalid &&
390 arg_value->GetClangType().IsPointerType())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000391 continue;
392
Greg Clayton57ee3062013-07-11 22:46:58 +0000393 const Scalar &arg_scalar = arg_value->ResolveValue(&exe_ctx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000394
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000395 if (!process->WriteScalarToMemory(args_addr_ref + offset, arg_scalar, arg_scalar.GetByteSize(), error))
396 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000397 }
398
399 return true;
400}
401
402bool
Sean Callanan138e74e2010-07-26 22:14:36 +0000403ClangFunction::InsertFunction (ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref, Stream &errors)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000404{
405 using namespace clang;
406
407 if (CompileFunction(errors) != 0)
408 return false;
Sean Callanan138e74e2010-07-26 22:14:36 +0000409 if (!WriteFunctionWrapper(exe_ctx, errors))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000410 return false;
Sean Callanan138e74e2010-07-26 22:14:36 +0000411 if (!WriteFunctionArguments(exe_ctx, args_addr_ref, errors))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000412 return false;
413
Greg Clayton5160ce52013-03-27 23:08:40 +0000414 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000415 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000416 log->Printf ("Call Address: 0x%" PRIx64 " Struct Address: 0x%" PRIx64 ".\n", m_jit_start_addr, args_addr_ref);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000417
418 return true;
419}
420
421ThreadPlan *
Jim Ingham399f1ca2010-11-05 19:25:48 +0000422ClangFunction::GetThreadPlanToCallFunction (ExecutionContext &exe_ctx,
Sean Callanana464f3d2013-11-08 01:14:26 +0000423 lldb::addr_t args_addr,
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000424 const EvaluateExpressionOptions &options,
425 Stream &errors)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000426{
Greg Clayton5160ce52013-03-27 23:08:40 +0000427 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callananf58b12d2013-03-06 19:57:25 +0000428
429 if (log)
430 log->Printf("-- [ClangFunction::GetThreadPlanToCallFunction] Creating thread plan to call function --");
431
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000432 // FIXME: Use the errors Stream for better error reporting.
Greg Claytonc14ee322011-09-22 04:58:26 +0000433 Thread *thread = exe_ctx.GetThreadPtr();
434 if (thread == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000435 {
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000436 errors.Printf("Can't call a function without a valid thread.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000437 return NULL;
438 }
439
440 // Okay, now run the function:
441
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000442 Address wrapper_address (m_jit_start_addr);
Sean Callanana464f3d2013-11-08 01:14:26 +0000443
444 lldb::addr_t args = { args_addr };
445
Greg Claytonc14ee322011-09-22 04:58:26 +0000446 ThreadPlan *new_plan = new ThreadPlanCallFunction (*thread,
Sean Callanan17827832010-12-13 22:46:15 +0000447 wrapper_address,
Jim Inghamef651602011-12-22 19:12:40 +0000448 ClangASTType(),
Sean Callanana464f3d2013-11-08 01:14:26 +0000449 args,
450 options);
Jim Ingham923886c2012-05-11 18:43:38 +0000451 new_plan->SetIsMasterPlan(true);
452 new_plan->SetOkayToDiscard (false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000453 return new_plan;
454}
455
456bool
Sean Callanan138e74e2010-07-26 22:14:36 +0000457ClangFunction::FetchFunctionResults (ExecutionContext &exe_ctx, lldb::addr_t args_addr, Value &ret_value)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000458{
459 // Read the return value - it is the last field in the struct:
460 // FIXME: How does clang tell us there's no return value? We need to handle that case.
Jim Inghamef651602011-12-22 19:12:40 +0000461 // FIXME: Create our ThreadPlanCallFunction with the return ClangASTType, and then use GetReturnValueObject
462 // to fetch the value. That way we can fetch any values we need.
Sean Callananf58b12d2013-03-06 19:57:25 +0000463
Greg Clayton5160ce52013-03-27 23:08:40 +0000464 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callananf58b12d2013-03-06 19:57:25 +0000465
466 if (log)
467 log->Printf("-- [ClangFunction::FetchFunctionResults] Fetching function results --");
468
Greg Claytonc14ee322011-09-22 04:58:26 +0000469 Process *process = exe_ctx.GetProcessPtr();
Jim Ingham35944dd2011-03-17 20:02:56 +0000470
471 if (process == NULL)
472 return false;
Enrico Granatadfc88a02012-09-18 00:08:47 +0000473
474 lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
475
476 if (process != jit_process_sp.get())
Jim Ingham35944dd2011-03-17 20:02:56 +0000477 return false;
478
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000479 Error error;
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000480 ret_value.GetScalar() = process->ReadUnsignedIntegerFromMemory (args_addr + m_return_offset, m_return_size, 0, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000481
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000482 if (error.Fail())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000483 return false;
484
Greg Clayton57ee3062013-07-11 22:46:58 +0000485 ret_value.SetClangType(m_function_return_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000486 ret_value.SetValueType(Value::eValueTypeScalar);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000487 return true;
488}
489
490void
Sean Callanan138e74e2010-07-26 22:14:36 +0000491ClangFunction::DeallocateFunctionResults (ExecutionContext &exe_ctx, lldb::addr_t args_addr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000492{
493 std::list<lldb::addr_t>::iterator pos;
494 pos = std::find(m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(), args_addr);
495 if (pos != m_wrapper_args_addrs.end())
496 m_wrapper_args_addrs.erase(pos);
497
Greg Claytonc14ee322011-09-22 04:58:26 +0000498 exe_ctx.GetProcessRef().DeallocateMemory(args_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000499}
500
Greg Claytone0d378b2011-03-24 21:19:54 +0000501ExecutionResults
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000502ClangFunction::ExecuteFunction(
Sean Callanan138e74e2010-07-26 22:14:36 +0000503 ExecutionContext &exe_ctx,
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000504 lldb::addr_t *args_addr_ptr,
505 const EvaluateExpressionOptions &options,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000506 Stream &errors,
Sean Callananebf77072010-07-23 00:16:21 +0000507 Value &results)
508{
509 using namespace clang;
Greg Claytone0d378b2011-03-24 21:19:54 +0000510 ExecutionResults return_value = eExecutionSetupError;
Sean Callananebf77072010-07-23 00:16:21 +0000511
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000512 // ClangFunction::ExecuteFunction execution is always just to get the result. Do make sure we ignore
513 // breakpoints, unwind on error, and don't try to debug it.
514 EvaluateExpressionOptions real_options = options;
515 real_options.SetDebug(false);
516 real_options.SetUnwindOnError(true);
517 real_options.SetIgnoreBreakpoints(true);
518
Sean Callananebf77072010-07-23 00:16:21 +0000519 lldb::addr_t args_addr;
520
521 if (args_addr_ptr != NULL)
522 args_addr = *args_addr_ptr;
523 else
524 args_addr = LLDB_INVALID_ADDRESS;
525
526 if (CompileFunction(errors) != 0)
Greg Claytone0d378b2011-03-24 21:19:54 +0000527 return eExecutionSetupError;
Sean Callananebf77072010-07-23 00:16:21 +0000528
529 if (args_addr == LLDB_INVALID_ADDRESS)
530 {
Sean Callanan138e74e2010-07-26 22:14:36 +0000531 if (!InsertFunction(exe_ctx, args_addr, errors))
Greg Claytone0d378b2011-03-24 21:19:54 +0000532 return eExecutionSetupError;
Sean Callananebf77072010-07-23 00:16:21 +0000533 }
Sean Callananebf77072010-07-23 00:16:21 +0000534
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000535 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
536
537 if (log)
538 log->Printf("== [ClangFunction::ExecuteFunction] Executing function ==");
539
540 lldb::ThreadPlanSP call_plan_sp (GetThreadPlanToCallFunction (exe_ctx,
541 args_addr,
542 real_options,
543 errors));
544 if (!call_plan_sp)
545 return eExecutionSetupError;
546
Jim Ingham23b95f02014-03-12 23:43:15 +0000547 // We need to make sure we record the fact that we are running an expression here
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000548 // otherwise this fact will fail to be recorded when fetching an Objective-C object description
549 if (exe_ctx.GetProcessPtr())
550 exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
551
552 return_value = exe_ctx.GetProcessRef().RunThreadPlan (exe_ctx,
553 call_plan_sp,
554 real_options,
555 errors);
556
557 if (log)
558 {
559 if (return_value != eExecutionCompleted)
560 {
561 log->Printf("== [ClangFunction::ExecuteFunction] Execution completed abnormally ==");
562 }
563 else
564 {
565 log->Printf("== [ClangFunction::ExecuteFunction] Execution completed normally ==");
566 }
567 }
568
569 if (exe_ctx.GetProcessPtr())
570 exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
571
Sean Callananebf77072010-07-23 00:16:21 +0000572 if (args_addr_ptr != NULL)
573 *args_addr_ptr = args_addr;
574
Greg Claytone0d378b2011-03-24 21:19:54 +0000575 if (return_value != eExecutionCompleted)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000576 return return_value;
577
Sean Callanan138e74e2010-07-26 22:14:36 +0000578 FetchFunctionResults(exe_ctx, args_addr, results);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000579
580 if (args_addr_ptr == NULL)
Sean Callanan138e74e2010-07-26 22:14:36 +0000581 DeallocateFunctionResults(exe_ctx, args_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000582
Greg Claytone0d378b2011-03-24 21:19:54 +0000583 return eExecutionCompleted;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000584}
585
Sean Callanan1a8d4092010-08-27 01:01:44 +0000586clang::ASTConsumer *
587ClangFunction::ASTTransformer (clang::ASTConsumer *passthrough)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000588{
Sean Callanan394e36d2013-10-10 00:39:23 +0000589 m_struct_extractor.reset(new ASTStructExtractor(passthrough, m_wrapper_struct_name.c_str(), *this));
590
591 return m_struct_extractor.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000592}