blob: 171433d945b99876972571d3ca42bb7845178314 [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
Sean Callanan1a8d4092010-08-27 01:01:44 +000025#include "lldb/Expression/ASTStructExtractor.h"
26#include "lldb/Expression/ClangExpressionParser.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include "lldb/Expression/ClangFunction.h"
Greg Claytone01e07b2013-04-18 18:10:51 +000028#include "lldb/Expression/IRExecutionUnit.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029#include "lldb/Symbol/Type.h"
30#include "lldb/Core/DataExtractor.h"
Jim Ingham0d8bcc72010-11-17 02:32:00 +000031#include "lldb/Core/State.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032#include "lldb/Core/ValueObject.h"
33#include "lldb/Core/ValueObjectList.h"
34#include "lldb/Interpreter/CommandReturnObject.h"
35#include "lldb/Symbol/ClangASTContext.h"
36#include "lldb/Symbol/Function.h"
37#include "lldb/Target/ExecutionContext.h"
38#include "lldb/Target/Process.h"
Sean Callananebf77072010-07-23 00:16:21 +000039#include "lldb/Target/RegisterContext.h"
Greg Clayton514487e2011-02-15 21:59:32 +000040#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041#include "lldb/Target/Thread.h"
42#include "lldb/Target/ThreadPlan.h"
43#include "lldb/Target/ThreadPlanCallFunction.h"
44#include "lldb/Core/Log.h"
45
46using namespace lldb_private;
Sean Callanan1a8d4092010-08-27 01:01:44 +000047
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048//----------------------------------------------------------------------
49// ClangFunction constructor
50//----------------------------------------------------------------------
Greg Clayton7b462cc2010-10-15 22:48:33 +000051ClangFunction::ClangFunction
52(
Jim Ingham35944dd2011-03-17 20:02:56 +000053 ExecutionContextScope &exe_scope,
Greg Clayton57ee3062013-07-11 22:46:58 +000054 const ClangASTType &return_type,
Greg Clayton7b462cc2010-10-15 22:48:33 +000055 const Address& functionAddress,
56 const ValueList &arg_value_list
57) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058 m_function_ptr (NULL),
Greg Claytonc982c762010-07-09 20:39:50 +000059 m_function_addr (functionAddress),
Greg Clayton57ee3062013-07-11 22:46:58 +000060 m_function_return_type(return_type),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061 m_wrapper_function_name ("__lldb_caller_function"),
62 m_wrapper_struct_name ("__lldb_caller_struct"),
Greg Claytonc982c762010-07-09 20:39:50 +000063 m_wrapper_args_addrs (),
Greg Claytonc982c762010-07-09 20:39:50 +000064 m_arg_values (arg_value_list),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000065 m_compiled (false),
66 m_JITted (false)
67{
Enrico Granatadfc88a02012-09-18 00:08:47 +000068 m_jit_process_wp = lldb::ProcessWP(exe_scope.CalculateProcess());
Jim Ingham35944dd2011-03-17 20:02:56 +000069 // Can't make a ClangFunction without a process.
Enrico Granatadfc88a02012-09-18 00:08:47 +000070 assert (m_jit_process_wp.lock());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000071}
72
Greg Clayton7b462cc2010-10-15 22:48:33 +000073ClangFunction::ClangFunction
74(
Jim Ingham35944dd2011-03-17 20:02:56 +000075 ExecutionContextScope &exe_scope,
Greg Clayton7b462cc2010-10-15 22:48:33 +000076 Function &function,
77 ClangASTContext *ast_context,
78 const ValueList &arg_value_list
79) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +000080 m_function_ptr (&function),
Greg Claytonc982c762010-07-09 20:39:50 +000081 m_function_addr (),
Greg Clayton57ee3062013-07-11 22:46:58 +000082 m_function_return_type (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000083 m_wrapper_function_name ("__lldb_function_caller"),
84 m_wrapper_struct_name ("__lldb_caller_struct"),
Greg Claytonc982c762010-07-09 20:39:50 +000085 m_wrapper_args_addrs (),
Greg Claytonc982c762010-07-09 20:39:50 +000086 m_arg_values (arg_value_list),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000087 m_compiled (false),
88 m_JITted (false)
89{
Enrico Granatadfc88a02012-09-18 00:08:47 +000090 m_jit_process_wp = lldb::ProcessWP(exe_scope.CalculateProcess());
Jim Ingham35944dd2011-03-17 20:02:56 +000091 // Can't make a ClangFunction without a process.
Enrico Granatadfc88a02012-09-18 00:08:47 +000092 assert (m_jit_process_wp.lock());
Greg Clayton514487e2011-02-15 21:59:32 +000093
Chris Lattner30fdc8d2010-06-08 16:52:24 +000094 m_function_addr = m_function_ptr->GetAddressRange().GetBaseAddress();
Greg Clayton57ee3062013-07-11 22:46:58 +000095 m_function_return_type = m_function_ptr->GetClangType().GetFunctionReturnType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000096}
97
98//----------------------------------------------------------------------
99// Destructor
100//----------------------------------------------------------------------
101ClangFunction::~ClangFunction()
102{
103}
104
105unsigned
106ClangFunction::CompileFunction (Stream &errors)
107{
Sean Callanan1a8d4092010-08-27 01:01:44 +0000108 if (m_compiled)
109 return 0;
110
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000111 // FIXME: How does clang tell us there's no return value? We need to handle that case.
112 unsigned num_errors = 0;
113
Jim Inghamfb6fc0d2013-09-27 20:59:37 +0000114 std::string return_type_str (m_function_return_type.GetTypeName().AsCString(""));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000115
116 // Cons up the function we're going to wrap our call in, then compile it...
117 // We declare the function "extern "C"" because the compiler might be in C++
118 // mode which would mangle the name and then we couldn't find it again...
119 m_wrapper_function_text.clear();
120 m_wrapper_function_text.append ("extern \"C\" void ");
121 m_wrapper_function_text.append (m_wrapper_function_name);
122 m_wrapper_function_text.append (" (void *input)\n{\n struct ");
123 m_wrapper_function_text.append (m_wrapper_struct_name);
124 m_wrapper_function_text.append (" \n {\n");
125 m_wrapper_function_text.append (" ");
126 m_wrapper_function_text.append (return_type_str);
127 m_wrapper_function_text.append (" (*fn_ptr) (");
128
129 // Get the number of arguments. If we have a function type and it is prototyped,
130 // trust that, otherwise use the values we were given.
131
132 // FIXME: This will need to be extended to handle Variadic functions. We'll need
133 // to pull the defined arguments out of the function, then add the types from the
134 // arguments list for the variable arguments.
135
Greg Clayton57ee3062013-07-11 22:46:58 +0000136 uint32_t num_args = UINT32_MAX;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000137 bool trust_function = false;
138 // GetArgumentCount returns -1 for an unprototyped function.
Greg Clayton57ee3062013-07-11 22:46:58 +0000139 ClangASTType function_clang_type;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000140 if (m_function_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000141 {
Greg Clayton57ee3062013-07-11 22:46:58 +0000142 function_clang_type = m_function_ptr->GetClangType();
143 if (function_clang_type)
144 {
145 int num_func_args = function_clang_type.GetFunctionArgumentCount();
146 if (num_func_args >= 0)
147 {
148 trust_function = true;
149 num_args = num_func_args;
150 }
151 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000152 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000153
Sean Callanan1a8d4092010-08-27 01:01:44 +0000154 if (num_args == UINT32_MAX)
155 num_args = m_arg_values.GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000156
Sean Callanan1a8d4092010-08-27 01:01:44 +0000157 std::string args_buffer; // This one stores the definition of all the args in "struct caller".
158 std::string args_list_buffer; // This one stores the argument list called from the structure.
159 for (size_t i = 0; i < num_args; i++)
160 {
Greg Claytonf4ecaa52011-02-16 23:00:21 +0000161 std::string type_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000162
Sean Callanan1a8d4092010-08-27 01:01:44 +0000163 if (trust_function)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000164 {
Sean Callananddd7a2a2013-10-03 22:27:29 +0000165 type_name = function_clang_type.GetFunctionArgumentTypeAtIndex(i).GetTypeName().AsCString("");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000166 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000167 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000168 {
Greg Clayton57ee3062013-07-11 22:46:58 +0000169 ClangASTType clang_qual_type = m_arg_values.GetValueAtIndex(i)->GetClangType ();
170 if (clang_qual_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000171 {
Sean Callananddd7a2a2013-10-03 22:27:29 +0000172 type_name = clang_qual_type.GetTypeName().AsCString("");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000173 }
174 else
Sean Callanan1a8d4092010-08-27 01:01:44 +0000175 {
Jason Molendafd54b362011-09-20 21:44:10 +0000176 errors.Printf("Could not determine type of input value %lu.", i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000177 return 1;
178 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000180
Greg Claytonf4ecaa52011-02-16 23:00:21 +0000181 m_wrapper_function_text.append (type_name);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000182 if (i < num_args - 1)
183 m_wrapper_function_text.append (", ");
184
185 char arg_buf[32];
186 args_buffer.append (" ");
Greg Claytonf4ecaa52011-02-16 23:00:21 +0000187 args_buffer.append (type_name);
Daniel Malead01b2952012-11-29 21:49:15 +0000188 snprintf(arg_buf, 31, "arg_%" PRIu64, (uint64_t)i);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000189 args_buffer.push_back (' ');
190 args_buffer.append (arg_buf);
191 args_buffer.append (";\n");
192
193 args_list_buffer.append ("__lldb_fn_data->");
194 args_list_buffer.append (arg_buf);
195 if (i < num_args - 1)
196 args_list_buffer.append (", ");
197
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000198 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000199 m_wrapper_function_text.append (");\n"); // Close off the function calling prototype.
200
201 m_wrapper_function_text.append (args_buffer);
202
203 m_wrapper_function_text.append (" ");
204 m_wrapper_function_text.append (return_type_str);
205 m_wrapper_function_text.append (" return_value;");
206 m_wrapper_function_text.append ("\n };\n struct ");
207 m_wrapper_function_text.append (m_wrapper_struct_name);
208 m_wrapper_function_text.append ("* __lldb_fn_data = (struct ");
209 m_wrapper_function_text.append (m_wrapper_struct_name);
210 m_wrapper_function_text.append (" *) input;\n");
211
212 m_wrapper_function_text.append (" __lldb_fn_data->return_value = __lldb_fn_data->fn_ptr (");
213 m_wrapper_function_text.append (args_list_buffer);
214 m_wrapper_function_text.append (");\n}\n");
215
Greg Clayton5160ce52013-03-27 23:08:40 +0000216 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000217 if (log)
218 log->Printf ("Expression: \n\n%s\n\n", m_wrapper_function_text.c_str());
219
220 // Okay, now compile this expression
Sean Callanan1a8d4092010-08-27 01:01:44 +0000221
Enrico Granatadfc88a02012-09-18 00:08:47 +0000222 lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
223 if (jit_process_sp)
224 {
225 m_parser.reset(new ClangExpressionParser(jit_process_sp.get(), *this));
226
227 num_errors = m_parser->Parse (errors);
228 }
229 else
230 {
231 errors.Printf("no process - unable to inject function");
232 num_errors = 1;
233 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000234
235 m_compiled = (num_errors == 0);
236
237 if (!m_compiled)
238 return num_errors;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000239
240 return num_errors;
241}
242
243bool
Sean Callanan138e74e2010-07-26 22:14:36 +0000244ClangFunction::WriteFunctionWrapper (ExecutionContext &exe_ctx, Stream &errors)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000245{
Greg Claytonc14ee322011-09-22 04:58:26 +0000246 Process *process = exe_ctx.GetProcessPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000247
Sean Callanan1a8d4092010-08-27 01:01:44 +0000248 if (!process)
249 return false;
Enrico Granatadfc88a02012-09-18 00:08:47 +0000250
251 lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
252
253 if (process != jit_process_sp.get())
Jim Ingham35944dd2011-03-17 20:02:56 +0000254 return false;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000255
256 if (!m_compiled)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000257 return false;
258
Sean Callanan1a8d4092010-08-27 01:01:44 +0000259 if (m_JITted)
260 return true;
Sean Callanan1582ee62013-04-18 22:06:33 +0000261
262 bool can_interpret = false; // should stay that way
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000263
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000264 Error jit_error (m_parser->PrepareForExecution (m_jit_start_addr,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000265 m_jit_end_addr,
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000266 m_execution_unit_ap,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000267 exe_ctx,
Sean Callanan1582ee62013-04-18 22:06:33 +0000268 can_interpret,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000269 eExecutionPolicyAlways));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000270
271 if (!jit_error.Success())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000272 return false;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000273
274 if (process && m_jit_start_addr)
Enrico Granatadfc88a02012-09-18 00:08:47 +0000275 m_jit_process_wp = lldb::ProcessWP(process->shared_from_this());
Sean Callanand14fac12013-01-14 21:45:38 +0000276
277 m_JITted = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000278
279 return true;
280}
281
282bool
Sean Callanan138e74e2010-07-26 22:14:36 +0000283ClangFunction::WriteFunctionArguments (ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref, Stream &errors)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000284{
Sean Callanan138e74e2010-07-26 22:14:36 +0000285 return WriteFunctionArguments(exe_ctx, args_addr_ref, m_function_addr, m_arg_values, errors);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000286}
287
288// FIXME: Assure that the ValueList we were passed in is consistent with the one that defined this function.
289
290bool
Jim Inghambaae1682010-09-10 23:07:48 +0000291ClangFunction::WriteFunctionArguments (ExecutionContext &exe_ctx,
292 lldb::addr_t &args_addr_ref,
293 Address function_address,
294 ValueList &arg_values,
295 Stream &errors)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000296{
Sean Callanan1a8d4092010-08-27 01:01:44 +0000297 // All the information to reconstruct the struct is provided by the
298 // StructExtractor.
299 if (!m_struct_valid)
300 {
301 errors.Printf("Argument information was not correctly parsed, so the function cannot be called.");
302 return false;
303 }
304
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000305 Error error;
306 using namespace clang;
Greg Claytone0d378b2011-03-24 21:19:54 +0000307 ExecutionResults return_value = eExecutionSetupError;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000308
Greg Claytonc14ee322011-09-22 04:58:26 +0000309 Process *process = exe_ctx.GetProcessPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000310
311 if (process == NULL)
312 return return_value;
Jim Ingham35944dd2011-03-17 20:02:56 +0000313
Enrico Granatadfc88a02012-09-18 00:08:47 +0000314 lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
315
316 if (process != jit_process_sp.get())
Jim Ingham35944dd2011-03-17 20:02:56 +0000317 return false;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000318
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000319 if (args_addr_ref == LLDB_INVALID_ADDRESS)
320 {
Sean Callanan1a8d4092010-08-27 01:01:44 +0000321 args_addr_ref = process->AllocateMemory(m_struct_size, lldb::ePermissionsReadable|lldb::ePermissionsWritable, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000322 if (args_addr_ref == LLDB_INVALID_ADDRESS)
323 return false;
324 m_wrapper_args_addrs.push_back (args_addr_ref);
325 }
326 else
327 {
328 // Make sure this is an address that we've already handed out.
329 if (find (m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(), args_addr_ref) == m_wrapper_args_addrs.end())
330 {
331 return false;
332 }
333 }
334
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000335 // TODO: verify fun_addr needs to be a callable address
Greg Claytonc14ee322011-09-22 04:58:26 +0000336 Scalar fun_addr (function_address.GetCallableLoadAddress(exe_ctx.GetTargetPtr()));
Greg Claytonc7bece562013-01-25 18:06:21 +0000337 uint64_t first_offset = m_member_offsets[0];
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000338 process->WriteScalarToMemory(args_addr_ref + first_offset, fun_addr, process->GetAddressByteSize(), error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000339
340 // FIXME: We will need to extend this for Variadic functions.
341
342 Error value_error;
343
344 size_t num_args = arg_values.GetSize();
345 if (num_args != m_arg_values.GetSize())
346 {
Jason Molendafd54b362011-09-20 21:44:10 +0000347 errors.Printf ("Wrong number of arguments - was: %lu should be: %lu", num_args, m_arg_values.GetSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000348 return false;
349 }
350
Greg Claytonc982c762010-07-09 20:39:50 +0000351 for (size_t i = 0; i < num_args; i++)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000352 {
353 // FIXME: We should sanity check sizes.
354
Greg Claytonc7bece562013-01-25 18:06:21 +0000355 uint64_t offset = m_member_offsets[i+1]; // Clang sizes are in bytes.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000356 Value *arg_value = arg_values.GetValueAtIndex(i);
357
358 // FIXME: For now just do scalars:
359
360 // Special case: if it's a pointer, don't do anything (the ABI supports passing cstrings)
361
362 if (arg_value->GetValueType() == Value::eValueTypeHostAddress &&
Greg Clayton57ee3062013-07-11 22:46:58 +0000363 arg_value->GetContextType() == Value::eContextTypeInvalid &&
364 arg_value->GetClangType().IsPointerType())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000365 continue;
366
Greg Clayton57ee3062013-07-11 22:46:58 +0000367 const Scalar &arg_scalar = arg_value->ResolveValue(&exe_ctx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000368
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000369 if (!process->WriteScalarToMemory(args_addr_ref + offset, arg_scalar, arg_scalar.GetByteSize(), error))
370 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000371 }
372
373 return true;
374}
375
376bool
Sean Callanan138e74e2010-07-26 22:14:36 +0000377ClangFunction::InsertFunction (ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref, Stream &errors)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000378{
379 using namespace clang;
380
381 if (CompileFunction(errors) != 0)
382 return false;
Sean Callanan138e74e2010-07-26 22:14:36 +0000383 if (!WriteFunctionWrapper(exe_ctx, errors))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000384 return false;
Sean Callanan138e74e2010-07-26 22:14:36 +0000385 if (!WriteFunctionArguments(exe_ctx, args_addr_ref, errors))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000386 return false;
387
Greg Clayton5160ce52013-03-27 23:08:40 +0000388 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000389 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000390 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 +0000391
392 return true;
393}
394
395ThreadPlan *
Jim Ingham399f1ca2010-11-05 19:25:48 +0000396ClangFunction::GetThreadPlanToCallFunction (ExecutionContext &exe_ctx,
397 lldb::addr_t func_addr,
398 lldb::addr_t &args_addr,
399 Stream &errors,
400 bool stop_others,
Jim Ingham184e9812013-01-15 02:47:48 +0000401 bool unwind_on_error,
402 bool ignore_breakpoints,
Sean Callanan17827832010-12-13 22:46:15 +0000403 lldb::addr_t *this_arg,
404 lldb::addr_t *cmd_arg)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000405{
Greg Clayton5160ce52013-03-27 23:08:40 +0000406 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callananf58b12d2013-03-06 19:57:25 +0000407
408 if (log)
409 log->Printf("-- [ClangFunction::GetThreadPlanToCallFunction] Creating thread plan to call function --");
410
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000411 // FIXME: Use the errors Stream for better error reporting.
Greg Claytonc14ee322011-09-22 04:58:26 +0000412 Thread *thread = exe_ctx.GetThreadPtr();
413 if (thread == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000414 {
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000415 errors.Printf("Can't call a function without a valid thread.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000416 return NULL;
417 }
418
419 // Okay, now run the function:
420
Greg Claytone72dfb32012-02-24 01:59:29 +0000421 Address wrapper_address (func_addr);
Greg Claytonc14ee322011-09-22 04:58:26 +0000422 ThreadPlan *new_plan = new ThreadPlanCallFunction (*thread,
Sean Callanan17827832010-12-13 22:46:15 +0000423 wrapper_address,
Jim Inghamef651602011-12-22 19:12:40 +0000424 ClangASTType(),
Sean Callanan17827832010-12-13 22:46:15 +0000425 args_addr,
426 stop_others,
Jim Ingham184e9812013-01-15 02:47:48 +0000427 unwind_on_error,
428 ignore_breakpoints,
Sean Callanan17827832010-12-13 22:46:15 +0000429 this_arg,
430 cmd_arg);
Jim Ingham923886c2012-05-11 18:43:38 +0000431 new_plan->SetIsMasterPlan(true);
432 new_plan->SetOkayToDiscard (false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000433 return new_plan;
434}
435
436bool
Sean Callanan138e74e2010-07-26 22:14:36 +0000437ClangFunction::FetchFunctionResults (ExecutionContext &exe_ctx, lldb::addr_t args_addr, Value &ret_value)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000438{
439 // Read the return value - it is the last field in the struct:
440 // FIXME: How does clang tell us there's no return value? We need to handle that case.
Jim Inghamef651602011-12-22 19:12:40 +0000441 // FIXME: Create our ThreadPlanCallFunction with the return ClangASTType, and then use GetReturnValueObject
442 // to fetch the value. That way we can fetch any values we need.
Sean Callananf58b12d2013-03-06 19:57:25 +0000443
Greg Clayton5160ce52013-03-27 23:08:40 +0000444 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callananf58b12d2013-03-06 19:57:25 +0000445
446 if (log)
447 log->Printf("-- [ClangFunction::FetchFunctionResults] Fetching function results --");
448
Greg Claytonc14ee322011-09-22 04:58:26 +0000449 Process *process = exe_ctx.GetProcessPtr();
Jim Ingham35944dd2011-03-17 20:02:56 +0000450
451 if (process == NULL)
452 return false;
Enrico Granatadfc88a02012-09-18 00:08:47 +0000453
454 lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
455
456 if (process != jit_process_sp.get())
Jim Ingham35944dd2011-03-17 20:02:56 +0000457 return false;
458
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000459 Error error;
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000460 ret_value.GetScalar() = process->ReadUnsignedIntegerFromMemory (args_addr + m_return_offset, m_return_size, 0, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000461
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000462 if (error.Fail())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000463 return false;
464
Greg Clayton57ee3062013-07-11 22:46:58 +0000465 ret_value.SetClangType(m_function_return_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000466 ret_value.SetValueType(Value::eValueTypeScalar);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000467 return true;
468}
469
470void
Sean Callanan138e74e2010-07-26 22:14:36 +0000471ClangFunction::DeallocateFunctionResults (ExecutionContext &exe_ctx, lldb::addr_t args_addr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000472{
473 std::list<lldb::addr_t>::iterator pos;
474 pos = std::find(m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(), args_addr);
475 if (pos != m_wrapper_args_addrs.end())
476 m_wrapper_args_addrs.erase(pos);
477
Greg Claytonc14ee322011-09-22 04:58:26 +0000478 exe_ctx.GetProcessRef().DeallocateMemory(args_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000479}
480
Greg Claytone0d378b2011-03-24 21:19:54 +0000481ExecutionResults
Sean Callanan138e74e2010-07-26 22:14:36 +0000482ClangFunction::ExecuteFunction(ExecutionContext &exe_ctx, Stream &errors, Value &results)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000483{
Sean Callanan138e74e2010-07-26 22:14:36 +0000484 return ExecuteFunction (exe_ctx, errors, 1000, true, results);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000485}
486
Greg Claytone0d378b2011-03-24 21:19:54 +0000487ExecutionResults
Sean Callanan138e74e2010-07-26 22:14:36 +0000488ClangFunction::ExecuteFunction(ExecutionContext &exe_ctx, Stream &errors, bool stop_others, Value &results)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000489{
Jim Ingham399f1ca2010-11-05 19:25:48 +0000490 const bool try_all_threads = false;
Jim Ingham184e9812013-01-15 02:47:48 +0000491 const bool unwind_on_error = true;
492 const bool ignore_breakpoints = true;
493 return ExecuteFunction (exe_ctx, NULL, errors, stop_others, 0UL, try_all_threads,
494 unwind_on_error, ignore_breakpoints, results);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000495}
496
Greg Claytone0d378b2011-03-24 21:19:54 +0000497ExecutionResults
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000498ClangFunction::ExecuteFunction(
Sean Callanan138e74e2010-07-26 22:14:36 +0000499 ExecutionContext &exe_ctx,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000500 Stream &errors,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000501 uint32_t timeout_usec,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000502 bool try_all_threads,
503 Value &results)
504{
Jim Ingham399f1ca2010-11-05 19:25:48 +0000505 const bool stop_others = true;
Jim Ingham184e9812013-01-15 02:47:48 +0000506 const bool unwind_on_error = true;
507 const bool ignore_breakpoints = true;
Jim Ingham35e1bda2012-10-16 21:41:58 +0000508 return ExecuteFunction (exe_ctx, NULL, errors, stop_others, timeout_usec,
Jim Ingham184e9812013-01-15 02:47:48 +0000509 try_all_threads, unwind_on_error, ignore_breakpoints, results);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000510}
511
Sean Callananebf77072010-07-23 00:16:21 +0000512// This is the static function
Greg Claytone0d378b2011-03-24 21:19:54 +0000513ExecutionResults
Sean Callananebf77072010-07-23 00:16:21 +0000514ClangFunction::ExecuteFunction (
Sean Callanan138e74e2010-07-26 22:14:36 +0000515 ExecutionContext &exe_ctx,
Sean Callananebf77072010-07-23 00:16:21 +0000516 lldb::addr_t function_address,
517 lldb::addr_t &void_arg,
518 bool stop_others,
519 bool try_all_threads,
Jim Ingham184e9812013-01-15 02:47:48 +0000520 bool unwind_on_error,
521 bool ignore_breakpoints,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000522 uint32_t timeout_usec,
Sean Callananfc55f5d2010-09-21 00:44:12 +0000523 Stream &errors,
524 lldb::addr_t *this_arg)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000525{
Greg Clayton5160ce52013-03-27 23:08:40 +0000526 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callananf58b12d2013-03-06 19:57:25 +0000527
528 if (log)
529 log->Printf("== [ClangFunction::ExecuteFunction] Executing function ==");
530
531 lldb::ThreadPlanSP call_plan_sp (ClangFunction::GetThreadPlanToCallFunction (exe_ctx,
Greg Claytonc14ee322011-09-22 04:58:26 +0000532 function_address,
533 void_arg,
534 errors,
535 stop_others,
Jim Ingham184e9812013-01-15 02:47:48 +0000536 unwind_on_error,
537 ignore_breakpoints,
Greg Claytonc14ee322011-09-22 04:58:26 +0000538 this_arg));
Sean Callanan9a028512012-08-09 00:50:26 +0000539 if (!call_plan_sp)
Greg Claytone0d378b2011-03-24 21:19:54 +0000540 return eExecutionSetupError;
Jim Ingham95afbf52012-12-07 19:04:31 +0000541
Enrico Granatae2e091b2012-08-03 22:24:48 +0000542 // <rdar://problem/12027563> we need to make sure we record the fact that we are running an expression here
543 // otherwise this fact will fail to be recorded when fetching an Objective-C object description
544 if (exe_ctx.GetProcessPtr())
545 exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
546
547 ExecutionResults results = exe_ctx.GetProcessRef().RunThreadPlan (exe_ctx, call_plan_sp,
548 stop_others,
549 try_all_threads,
Jim Ingham184e9812013-01-15 02:47:48 +0000550 unwind_on_error,
551 ignore_breakpoints,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000552 timeout_usec,
Enrico Granatae2e091b2012-08-03 22:24:48 +0000553 errors);
554
Sean Callananf58b12d2013-03-06 19:57:25 +0000555 if (log)
556 {
557 if (results != eExecutionCompleted)
558 {
559 log->Printf("== [ClangFunction::ExecuteFunction] Execution completed abnormally ==");
560 }
561 else
562 {
563 log->Printf("== [ClangFunction::ExecuteFunction] Execution completed normally ==");
564 }
565 }
566
Enrico Granatae2e091b2012-08-03 22:24:48 +0000567 if (exe_ctx.GetProcessPtr())
568 exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
569
570 return results;
571}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000572
Greg Claytone0d378b2011-03-24 21:19:54 +0000573ExecutionResults
Sean Callananebf77072010-07-23 00:16:21 +0000574ClangFunction::ExecuteFunction(
Sean Callanan138e74e2010-07-26 22:14:36 +0000575 ExecutionContext &exe_ctx,
Sean Callananebf77072010-07-23 00:16:21 +0000576 lldb::addr_t *args_addr_ptr,
577 Stream &errors,
578 bool stop_others,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000579 uint32_t timeout_usec,
Jim Ingham399f1ca2010-11-05 19:25:48 +0000580 bool try_all_threads,
Jim Ingham184e9812013-01-15 02:47:48 +0000581 bool unwind_on_error,
582 bool ignore_breakpoints,
Sean Callananebf77072010-07-23 00:16:21 +0000583 Value &results)
584{
585 using namespace clang;
Greg Claytone0d378b2011-03-24 21:19:54 +0000586 ExecutionResults return_value = eExecutionSetupError;
Sean Callananebf77072010-07-23 00:16:21 +0000587
588 lldb::addr_t args_addr;
589
590 if (args_addr_ptr != NULL)
591 args_addr = *args_addr_ptr;
592 else
593 args_addr = LLDB_INVALID_ADDRESS;
594
595 if (CompileFunction(errors) != 0)
Greg Claytone0d378b2011-03-24 21:19:54 +0000596 return eExecutionSetupError;
Sean Callananebf77072010-07-23 00:16:21 +0000597
598 if (args_addr == LLDB_INVALID_ADDRESS)
599 {
Sean Callanan138e74e2010-07-26 22:14:36 +0000600 if (!InsertFunction(exe_ctx, args_addr, errors))
Greg Claytone0d378b2011-03-24 21:19:54 +0000601 return eExecutionSetupError;
Sean Callananebf77072010-07-23 00:16:21 +0000602 }
603
Greg Clayton22a939a2011-01-19 23:00:49 +0000604 return_value = ClangFunction::ExecuteFunction (exe_ctx,
605 m_jit_start_addr,
606 args_addr,
607 stop_others,
608 try_all_threads,
Jim Ingham184e9812013-01-15 02:47:48 +0000609 unwind_on_error,
610 ignore_breakpoints,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000611 timeout_usec,
Greg Clayton22a939a2011-01-19 23:00:49 +0000612 errors);
Sean Callananebf77072010-07-23 00:16:21 +0000613
614 if (args_addr_ptr != NULL)
615 *args_addr_ptr = args_addr;
616
Greg Claytone0d378b2011-03-24 21:19:54 +0000617 if (return_value != eExecutionCompleted)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000618 return return_value;
619
Sean Callanan138e74e2010-07-26 22:14:36 +0000620 FetchFunctionResults(exe_ctx, args_addr, results);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000621
622 if (args_addr_ptr == NULL)
Sean Callanan138e74e2010-07-26 22:14:36 +0000623 DeallocateFunctionResults(exe_ctx, args_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000624
Greg Claytone0d378b2011-03-24 21:19:54 +0000625 return eExecutionCompleted;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000626}
627
Sean Callanan1a8d4092010-08-27 01:01:44 +0000628clang::ASTConsumer *
629ClangFunction::ASTTransformer (clang::ASTConsumer *passthrough)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000630{
Sean Callanan1a8d4092010-08-27 01:01:44 +0000631 return new ASTStructExtractor(passthrough, m_wrapper_struct_name.c_str(), *this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000632}