blob: 49e09c4e7d2988b7f4075cdb66ee29e663d67679 [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"
28#include "lldb/Symbol/Type.h"
29#include "lldb/Core/DataExtractor.h"
Jim Ingham0d8bcc72010-11-17 02:32:00 +000030#include "lldb/Core/State.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031#include "lldb/Core/ValueObject.h"
32#include "lldb/Core/ValueObjectList.h"
33#include "lldb/Interpreter/CommandReturnObject.h"
34#include "lldb/Symbol/ClangASTContext.h"
35#include "lldb/Symbol/Function.h"
36#include "lldb/Target/ExecutionContext.h"
37#include "lldb/Target/Process.h"
Sean Callananebf77072010-07-23 00:16:21 +000038#include "lldb/Target/RegisterContext.h"
Greg Clayton514487e2011-02-15 21:59:32 +000039#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040#include "lldb/Target/Thread.h"
41#include "lldb/Target/ThreadPlan.h"
42#include "lldb/Target/ThreadPlanCallFunction.h"
43#include "lldb/Core/Log.h"
44
45using namespace lldb_private;
Sean Callanan1a8d4092010-08-27 01:01:44 +000046
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047//----------------------------------------------------------------------
48// ClangFunction constructor
49//----------------------------------------------------------------------
Greg Clayton7b462cc2010-10-15 22:48:33 +000050ClangFunction::ClangFunction
51(
Jim Ingham35944dd2011-03-17 20:02:56 +000052 ExecutionContextScope &exe_scope,
Greg Clayton7b462cc2010-10-15 22:48:33 +000053 ClangASTContext *ast_context,
54 void *return_qualtype,
55 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),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060 m_function_return_qual_type(return_qualtype),
Greg Claytonc982c762010-07-09 20:39:50 +000061 m_clang_ast_context (ast_context),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062 m_wrapper_function_name ("__lldb_caller_function"),
63 m_wrapper_struct_name ("__lldb_caller_struct"),
Greg Claytonc982c762010-07-09 20:39:50 +000064 m_wrapper_args_addrs (),
Greg Claytonc982c762010-07-09 20:39:50 +000065 m_arg_values (arg_value_list),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066 m_compiled (false),
67 m_JITted (false)
68{
Enrico Granatadfc88a02012-09-18 00:08:47 +000069 m_jit_process_wp = lldb::ProcessWP(exe_scope.CalculateProcess());
Jim Ingham35944dd2011-03-17 20:02:56 +000070 // Can't make a ClangFunction without a process.
Enrico Granatadfc88a02012-09-18 00:08:47 +000071 assert (m_jit_process_wp.lock());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072}
73
Greg Clayton7b462cc2010-10-15 22:48:33 +000074ClangFunction::ClangFunction
75(
Jim Ingham35944dd2011-03-17 20:02:56 +000076 ExecutionContextScope &exe_scope,
Greg Clayton7b462cc2010-10-15 22:48:33 +000077 Function &function,
78 ClangASTContext *ast_context,
79 const ValueList &arg_value_list
80) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +000081 m_function_ptr (&function),
Greg Claytonc982c762010-07-09 20:39:50 +000082 m_function_addr (),
83 m_function_return_qual_type (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000084 m_clang_ast_context (ast_context),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000085 m_wrapper_function_name ("__lldb_function_caller"),
86 m_wrapper_struct_name ("__lldb_caller_struct"),
Greg Claytonc982c762010-07-09 20:39:50 +000087 m_wrapper_args_addrs (),
Greg Claytonc982c762010-07-09 20:39:50 +000088 m_arg_values (arg_value_list),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000089 m_compiled (false),
90 m_JITted (false)
91{
Enrico Granatadfc88a02012-09-18 00:08:47 +000092 m_jit_process_wp = lldb::ProcessWP(exe_scope.CalculateProcess());
Jim Ingham35944dd2011-03-17 20:02:56 +000093 // Can't make a ClangFunction without a process.
Enrico Granatadfc88a02012-09-18 00:08:47 +000094 assert (m_jit_process_wp.lock());
Greg Clayton514487e2011-02-15 21:59:32 +000095
Chris Lattner30fdc8d2010-06-08 16:52:24 +000096 m_function_addr = m_function_ptr->GetAddressRange().GetBaseAddress();
Greg Claytonf4ecaa52011-02-16 23:00:21 +000097 m_function_return_qual_type = m_function_ptr->GetReturnClangType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000098}
99
100//----------------------------------------------------------------------
101// Destructor
102//----------------------------------------------------------------------
103ClangFunction::~ClangFunction()
104{
105}
106
107unsigned
108ClangFunction::CompileFunction (Stream &errors)
109{
Sean Callanan1a8d4092010-08-27 01:01:44 +0000110 if (m_compiled)
111 return 0;
112
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000113 // FIXME: How does clang tell us there's no return value? We need to handle that case.
114 unsigned num_errors = 0;
115
Greg Clayton84db9102012-03-26 23:03:23 +0000116 std::string return_type_str (ClangASTType::GetTypeNameForOpaqueQualType (m_clang_ast_context->getASTContext(),
117 m_function_return_qual_type));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000118
119 // Cons up the function we're going to wrap our call in, then compile it...
120 // We declare the function "extern "C"" because the compiler might be in C++
121 // mode which would mangle the name and then we couldn't find it again...
122 m_wrapper_function_text.clear();
123 m_wrapper_function_text.append ("extern \"C\" void ");
124 m_wrapper_function_text.append (m_wrapper_function_name);
125 m_wrapper_function_text.append (" (void *input)\n{\n struct ");
126 m_wrapper_function_text.append (m_wrapper_struct_name);
127 m_wrapper_function_text.append (" \n {\n");
128 m_wrapper_function_text.append (" ");
129 m_wrapper_function_text.append (return_type_str);
130 m_wrapper_function_text.append (" (*fn_ptr) (");
131
132 // Get the number of arguments. If we have a function type and it is prototyped,
133 // trust that, otherwise use the values we were given.
134
135 // FIXME: This will need to be extended to handle Variadic functions. We'll need
136 // to pull the defined arguments out of the function, then add the types from the
137 // arguments list for the variable arguments.
138
139 uint32_t num_args = UINT32_MAX;
140 bool trust_function = false;
141 // GetArgumentCount returns -1 for an unprototyped function.
142 if (m_function_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000143 {
Sean Callanan1a8d4092010-08-27 01:01:44 +0000144 int num_func_args = m_function_ptr->GetArgumentCount();
145 if (num_func_args >= 0)
146 trust_function = true;
147 else
148 num_args = num_func_args;
149 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000150
Sean Callanan1a8d4092010-08-27 01:01:44 +0000151 if (num_args == UINT32_MAX)
152 num_args = m_arg_values.GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000153
Sean Callanan1a8d4092010-08-27 01:01:44 +0000154 std::string args_buffer; // This one stores the definition of all the args in "struct caller".
155 std::string args_list_buffer; // This one stores the argument list called from the structure.
156 for (size_t i = 0; i < num_args; i++)
157 {
Greg Claytonf4ecaa52011-02-16 23:00:21 +0000158 std::string type_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000159
Sean Callanan1a8d4092010-08-27 01:01:44 +0000160 if (trust_function)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000161 {
Greg Claytonf4ecaa52011-02-16 23:00:21 +0000162 lldb::clang_type_t arg_clang_type = m_function_ptr->GetArgumentTypeAtIndex(i);
Greg Clayton84db9102012-03-26 23:03:23 +0000163 type_name = ClangASTType::GetTypeNameForOpaqueQualType (m_clang_ast_context->getASTContext(),
164 arg_clang_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000166 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000167 {
Sean Callanan1a8d4092010-08-27 01:01:44 +0000168 Value *arg_value = m_arg_values.GetValueAtIndex(i);
Greg Claytone3055942011-06-30 02:28:26 +0000169 lldb::clang_type_t clang_qual_type = arg_value->GetClangType ();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000170 if (clang_qual_type != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000171 {
Greg Clayton84db9102012-03-26 23:03:23 +0000172 type_name = ClangASTType::GetTypeNameForOpaqueQualType (m_clang_ast_context->getASTContext(),
173 clang_qual_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000174 }
175 else
Sean Callanan1a8d4092010-08-27 01:01:44 +0000176 {
Jason Molendafd54b362011-09-20 21:44:10 +0000177 errors.Printf("Could not determine type of input value %lu.", i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000178 return 1;
179 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000180 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000181
Greg Claytonf4ecaa52011-02-16 23:00:21 +0000182 m_wrapper_function_text.append (type_name);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000183 if (i < num_args - 1)
184 m_wrapper_function_text.append (", ");
185
186 char arg_buf[32];
187 args_buffer.append (" ");
Greg Claytonf4ecaa52011-02-16 23:00:21 +0000188 args_buffer.append (type_name);
Daniel Malead01b2952012-11-29 21:49:15 +0000189 snprintf(arg_buf, 31, "arg_%" PRIu64, (uint64_t)i);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000190 args_buffer.push_back (' ');
191 args_buffer.append (arg_buf);
192 args_buffer.append (";\n");
193
194 args_list_buffer.append ("__lldb_fn_data->");
195 args_list_buffer.append (arg_buf);
196 if (i < num_args - 1)
197 args_list_buffer.append (", ");
198
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000199 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000200 m_wrapper_function_text.append (");\n"); // Close off the function calling prototype.
201
202 m_wrapper_function_text.append (args_buffer);
203
204 m_wrapper_function_text.append (" ");
205 m_wrapper_function_text.append (return_type_str);
206 m_wrapper_function_text.append (" return_value;");
207 m_wrapper_function_text.append ("\n };\n struct ");
208 m_wrapper_function_text.append (m_wrapper_struct_name);
209 m_wrapper_function_text.append ("* __lldb_fn_data = (struct ");
210 m_wrapper_function_text.append (m_wrapper_struct_name);
211 m_wrapper_function_text.append (" *) input;\n");
212
213 m_wrapper_function_text.append (" __lldb_fn_data->return_value = __lldb_fn_data->fn_ptr (");
214 m_wrapper_function_text.append (args_list_buffer);
215 m_wrapper_function_text.append (");\n}\n");
216
Jim Inghamb086ff72011-01-18 22:20:08 +0000217 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000218 if (log)
219 log->Printf ("Expression: \n\n%s\n\n", m_wrapper_function_text.c_str());
220
221 // Okay, now compile this expression
Sean Callanan1a8d4092010-08-27 01:01:44 +0000222
Enrico Granatadfc88a02012-09-18 00:08:47 +0000223 lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
224 if (jit_process_sp)
225 {
226 m_parser.reset(new ClangExpressionParser(jit_process_sp.get(), *this));
227
228 num_errors = m_parser->Parse (errors);
229 }
230 else
231 {
232 errors.Printf("no process - unable to inject function");
233 num_errors = 1;
234 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000235
236 m_compiled = (num_errors == 0);
237
238 if (!m_compiled)
239 return num_errors;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000240
241 return num_errors;
242}
243
244bool
Sean Callanan138e74e2010-07-26 22:14:36 +0000245ClangFunction::WriteFunctionWrapper (ExecutionContext &exe_ctx, Stream &errors)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000246{
Greg Claytonc14ee322011-09-22 04:58:26 +0000247 Process *process = exe_ctx.GetProcessPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000248
Sean Callanan1a8d4092010-08-27 01:01:44 +0000249 if (!process)
250 return false;
Enrico Granatadfc88a02012-09-18 00:08:47 +0000251
252 lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
253
254 if (process != jit_process_sp.get())
Jim Ingham35944dd2011-03-17 20:02:56 +0000255 return false;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000256
257 if (!m_compiled)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000258 return false;
259
Sean Callanan1a8d4092010-08-27 01:01:44 +0000260 if (m_JITted)
261 return true;
262
Sean Callanan63697e52011-05-07 01:06:41 +0000263 lldb::ClangExpressionVariableSP const_result;
264
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000265 bool evaluated_statically = false; // should stay that way
266
267 Error jit_error (m_parser->PrepareForExecution (m_jit_alloc,
268 m_jit_start_addr,
269 m_jit_end_addr,
270 exe_ctx,
271 NULL,
272 evaluated_statically,
273 const_result,
274 eExecutionPolicyAlways));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000275
276 if (!jit_error.Success())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000277 return false;
Greg Claytonc14ee322011-09-22 04:58:26 +0000278 if (process && m_jit_alloc != LLDB_INVALID_ADDRESS)
Enrico Granatadfc88a02012-09-18 00:08:47 +0000279 m_jit_process_wp = lldb::ProcessWP(process->shared_from_this());
Sean Callanand14fac12013-01-14 21:45:38 +0000280
281 m_JITted = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000282
283 return true;
284}
285
286bool
Sean Callanan138e74e2010-07-26 22:14:36 +0000287ClangFunction::WriteFunctionArguments (ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref, Stream &errors)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000288{
Sean Callanan138e74e2010-07-26 22:14:36 +0000289 return WriteFunctionArguments(exe_ctx, args_addr_ref, m_function_addr, m_arg_values, errors);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000290}
291
292// FIXME: Assure that the ValueList we were passed in is consistent with the one that defined this function.
293
294bool
Jim Inghambaae1682010-09-10 23:07:48 +0000295ClangFunction::WriteFunctionArguments (ExecutionContext &exe_ctx,
296 lldb::addr_t &args_addr_ref,
297 Address function_address,
298 ValueList &arg_values,
299 Stream &errors)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000300{
Sean Callanan1a8d4092010-08-27 01:01:44 +0000301 // All the information to reconstruct the struct is provided by the
302 // StructExtractor.
303 if (!m_struct_valid)
304 {
305 errors.Printf("Argument information was not correctly parsed, so the function cannot be called.");
306 return false;
307 }
308
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000309 Error error;
310 using namespace clang;
Greg Claytone0d378b2011-03-24 21:19:54 +0000311 ExecutionResults return_value = eExecutionSetupError;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000312
Greg Claytonc14ee322011-09-22 04:58:26 +0000313 Process *process = exe_ctx.GetProcessPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000314
315 if (process == NULL)
316 return return_value;
Jim Ingham35944dd2011-03-17 20:02:56 +0000317
Enrico Granatadfc88a02012-09-18 00:08:47 +0000318 lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
319
320 if (process != jit_process_sp.get())
Jim Ingham35944dd2011-03-17 20:02:56 +0000321 return false;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000322
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000323 if (args_addr_ref == LLDB_INVALID_ADDRESS)
324 {
Sean Callanan1a8d4092010-08-27 01:01:44 +0000325 args_addr_ref = process->AllocateMemory(m_struct_size, lldb::ePermissionsReadable|lldb::ePermissionsWritable, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000326 if (args_addr_ref == LLDB_INVALID_ADDRESS)
327 return false;
328 m_wrapper_args_addrs.push_back (args_addr_ref);
329 }
330 else
331 {
332 // Make sure this is an address that we've already handed out.
333 if (find (m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(), args_addr_ref) == m_wrapper_args_addrs.end())
334 {
335 return false;
336 }
337 }
338
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000339 // TODO: verify fun_addr needs to be a callable address
Greg Claytonc14ee322011-09-22 04:58:26 +0000340 Scalar fun_addr (function_address.GetCallableLoadAddress(exe_ctx.GetTargetPtr()));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000341 int first_offset = m_member_offsets[0];
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000342 process->WriteScalarToMemory(args_addr_ref + first_offset, fun_addr, process->GetAddressByteSize(), error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000343
344 // FIXME: We will need to extend this for Variadic functions.
345
346 Error value_error;
347
348 size_t num_args = arg_values.GetSize();
349 if (num_args != m_arg_values.GetSize())
350 {
Jason Molendafd54b362011-09-20 21:44:10 +0000351 errors.Printf ("Wrong number of arguments - was: %lu should be: %lu", num_args, m_arg_values.GetSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000352 return false;
353 }
354
Greg Claytonc982c762010-07-09 20:39:50 +0000355 for (size_t i = 0; i < num_args; i++)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000356 {
357 // FIXME: We should sanity check sizes.
358
Sean Callanan1a8d4092010-08-27 01:01:44 +0000359 int offset = m_member_offsets[i+1]; // Clang sizes are in bytes.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000360 Value *arg_value = arg_values.GetValueAtIndex(i);
361
362 // FIXME: For now just do scalars:
363
364 // Special case: if it's a pointer, don't do anything (the ABI supports passing cstrings)
365
366 if (arg_value->GetValueType() == Value::eValueTypeHostAddress &&
Greg Clayton526e5af2010-11-13 03:52:47 +0000367 arg_value->GetContextType() == Value::eContextTypeClangType &&
Greg Clayton1be10fc2010-09-29 01:12:09 +0000368 ClangASTContext::IsPointerType(arg_value->GetClangType()))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000369 continue;
370
Sean Callanan138e74e2010-07-26 22:14:36 +0000371 const Scalar &arg_scalar = arg_value->ResolveValue(&exe_ctx, m_clang_ast_context->getASTContext());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000372
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000373 if (!process->WriteScalarToMemory(args_addr_ref + offset, arg_scalar, arg_scalar.GetByteSize(), error))
374 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000375 }
376
377 return true;
378}
379
380bool
Sean Callanan138e74e2010-07-26 22:14:36 +0000381ClangFunction::InsertFunction (ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref, Stream &errors)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000382{
383 using namespace clang;
384
385 if (CompileFunction(errors) != 0)
386 return false;
Sean Callanan138e74e2010-07-26 22:14:36 +0000387 if (!WriteFunctionWrapper(exe_ctx, errors))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000388 return false;
Sean Callanan138e74e2010-07-26 22:14:36 +0000389 if (!WriteFunctionArguments(exe_ctx, args_addr_ref, errors))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000390 return false;
391
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000392 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000393 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000394 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 +0000395
396 return true;
397}
398
399ThreadPlan *
Jim Ingham399f1ca2010-11-05 19:25:48 +0000400ClangFunction::GetThreadPlanToCallFunction (ExecutionContext &exe_ctx,
401 lldb::addr_t func_addr,
402 lldb::addr_t &args_addr,
403 Stream &errors,
404 bool stop_others,
405 bool discard_on_error,
Sean Callanan17827832010-12-13 22:46:15 +0000406 lldb::addr_t *this_arg,
407 lldb::addr_t *cmd_arg)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000408{
409 // FIXME: Use the errors Stream for better error reporting.
Greg Claytonc14ee322011-09-22 04:58:26 +0000410 Thread *thread = exe_ctx.GetThreadPtr();
411 if (thread == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000412 {
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000413 errors.Printf("Can't call a function without a valid thread.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000414 return NULL;
415 }
416
417 // Okay, now run the function:
418
Greg Claytone72dfb32012-02-24 01:59:29 +0000419 Address wrapper_address (func_addr);
Greg Claytonc14ee322011-09-22 04:58:26 +0000420 ThreadPlan *new_plan = new ThreadPlanCallFunction (*thread,
Sean Callanan17827832010-12-13 22:46:15 +0000421 wrapper_address,
Jim Inghamef651602011-12-22 19:12:40 +0000422 ClangASTType(),
Sean Callanan17827832010-12-13 22:46:15 +0000423 args_addr,
424 stop_others,
425 discard_on_error,
426 this_arg,
427 cmd_arg);
Jim Ingham923886c2012-05-11 18:43:38 +0000428 new_plan->SetIsMasterPlan(true);
429 new_plan->SetOkayToDiscard (false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000430 return new_plan;
431}
432
433bool
Sean Callanan138e74e2010-07-26 22:14:36 +0000434ClangFunction::FetchFunctionResults (ExecutionContext &exe_ctx, lldb::addr_t args_addr, Value &ret_value)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000435{
436 // Read the return value - it is the last field in the struct:
437 // FIXME: How does clang tell us there's no return value? We need to handle that case.
Jim Inghamef651602011-12-22 19:12:40 +0000438 // FIXME: Create our ThreadPlanCallFunction with the return ClangASTType, and then use GetReturnValueObject
439 // to fetch the value. That way we can fetch any values we need.
Greg Claytonc14ee322011-09-22 04:58:26 +0000440 Process *process = exe_ctx.GetProcessPtr();
Jim Ingham35944dd2011-03-17 20:02:56 +0000441
442 if (process == NULL)
443 return false;
Enrico Granatadfc88a02012-09-18 00:08:47 +0000444
445 lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
446
447 if (process != jit_process_sp.get())
Jim Ingham35944dd2011-03-17 20:02:56 +0000448 return false;
449
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000450 Error error;
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000451 ret_value.GetScalar() = process->ReadUnsignedIntegerFromMemory (args_addr + m_return_offset, m_return_size, 0, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000452
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000453 if (error.Fail())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000454 return false;
455
Greg Clayton526e5af2010-11-13 03:52:47 +0000456 ret_value.SetContext (Value::eContextTypeClangType, m_function_return_qual_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000457 ret_value.SetValueType(Value::eValueTypeScalar);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000458 return true;
459}
460
461void
Sean Callanan138e74e2010-07-26 22:14:36 +0000462ClangFunction::DeallocateFunctionResults (ExecutionContext &exe_ctx, lldb::addr_t args_addr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000463{
464 std::list<lldb::addr_t>::iterator pos;
465 pos = std::find(m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(), args_addr);
466 if (pos != m_wrapper_args_addrs.end())
467 m_wrapper_args_addrs.erase(pos);
468
Greg Claytonc14ee322011-09-22 04:58:26 +0000469 exe_ctx.GetProcessRef().DeallocateMemory(args_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000470}
471
Greg Claytone0d378b2011-03-24 21:19:54 +0000472ExecutionResults
Sean Callanan138e74e2010-07-26 22:14:36 +0000473ClangFunction::ExecuteFunction(ExecutionContext &exe_ctx, Stream &errors, Value &results)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000474{
Sean Callanan138e74e2010-07-26 22:14:36 +0000475 return ExecuteFunction (exe_ctx, errors, 1000, true, results);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000476}
477
Greg Claytone0d378b2011-03-24 21:19:54 +0000478ExecutionResults
Sean Callanan138e74e2010-07-26 22:14:36 +0000479ClangFunction::ExecuteFunction(ExecutionContext &exe_ctx, Stream &errors, bool stop_others, Value &results)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000480{
Jim Ingham399f1ca2010-11-05 19:25:48 +0000481 const bool try_all_threads = false;
482 const bool discard_on_error = true;
Filipe Cabecinhasd0b87d82012-09-11 18:11:16 +0000483 return ExecuteFunction (exe_ctx, NULL, errors, stop_others, 0UL, try_all_threads, discard_on_error, results);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000484}
485
Greg Claytone0d378b2011-03-24 21:19:54 +0000486ExecutionResults
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000487ClangFunction::ExecuteFunction(
Sean Callanan138e74e2010-07-26 22:14:36 +0000488 ExecutionContext &exe_ctx,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000489 Stream &errors,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000490 uint32_t timeout_usec,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000491 bool try_all_threads,
492 Value &results)
493{
Jim Ingham399f1ca2010-11-05 19:25:48 +0000494 const bool stop_others = true;
495 const bool discard_on_error = true;
Jim Ingham35e1bda2012-10-16 21:41:58 +0000496 return ExecuteFunction (exe_ctx, NULL, errors, stop_others, timeout_usec,
Jim Ingham399f1ca2010-11-05 19:25:48 +0000497 try_all_threads, discard_on_error, results);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000498}
499
Sean Callananebf77072010-07-23 00:16:21 +0000500// This is the static function
Greg Claytone0d378b2011-03-24 21:19:54 +0000501ExecutionResults
Sean Callananebf77072010-07-23 00:16:21 +0000502ClangFunction::ExecuteFunction (
Sean Callanan138e74e2010-07-26 22:14:36 +0000503 ExecutionContext &exe_ctx,
Sean Callananebf77072010-07-23 00:16:21 +0000504 lldb::addr_t function_address,
505 lldb::addr_t &void_arg,
506 bool stop_others,
507 bool try_all_threads,
Jim Ingham399f1ca2010-11-05 19:25:48 +0000508 bool discard_on_error,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000509 uint32_t timeout_usec,
Sean Callananfc55f5d2010-09-21 00:44:12 +0000510 Stream &errors,
511 lldb::addr_t *this_arg)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000512{
Greg Claytonc14ee322011-09-22 04:58:26 +0000513 lldb::ThreadPlanSP call_plan_sp (ClangFunction::GetThreadPlanToCallFunction (exe_ctx,
514 function_address,
515 void_arg,
516 errors,
517 stop_others,
518 discard_on_error,
519 this_arg));
Sean Callanan9a028512012-08-09 00:50:26 +0000520 if (!call_plan_sp)
Greg Claytone0d378b2011-03-24 21:19:54 +0000521 return eExecutionSetupError;
Jim Ingham95afbf52012-12-07 19:04:31 +0000522
Enrico Granatae2e091b2012-08-03 22:24:48 +0000523 // <rdar://problem/12027563> we need to make sure we record the fact that we are running an expression here
524 // otherwise this fact will fail to be recorded when fetching an Objective-C object description
525 if (exe_ctx.GetProcessPtr())
526 exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
527
528 ExecutionResults results = exe_ctx.GetProcessRef().RunThreadPlan (exe_ctx, call_plan_sp,
529 stop_others,
530 try_all_threads,
531 discard_on_error,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000532 timeout_usec,
Enrico Granatae2e091b2012-08-03 22:24:48 +0000533 errors);
534
535 if (exe_ctx.GetProcessPtr())
536 exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
537
538 return results;
539}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000540
Greg Claytone0d378b2011-03-24 21:19:54 +0000541ExecutionResults
Sean Callananebf77072010-07-23 00:16:21 +0000542ClangFunction::ExecuteFunction(
Sean Callanan138e74e2010-07-26 22:14:36 +0000543 ExecutionContext &exe_ctx,
Sean Callananebf77072010-07-23 00:16:21 +0000544 lldb::addr_t *args_addr_ptr,
545 Stream &errors,
546 bool stop_others,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000547 uint32_t timeout_usec,
Jim Ingham399f1ca2010-11-05 19:25:48 +0000548 bool try_all_threads,
549 bool discard_on_error,
Sean Callananebf77072010-07-23 00:16:21 +0000550 Value &results)
551{
552 using namespace clang;
Greg Claytone0d378b2011-03-24 21:19:54 +0000553 ExecutionResults return_value = eExecutionSetupError;
Sean Callananebf77072010-07-23 00:16:21 +0000554
555 lldb::addr_t args_addr;
556
557 if (args_addr_ptr != NULL)
558 args_addr = *args_addr_ptr;
559 else
560 args_addr = LLDB_INVALID_ADDRESS;
561
562 if (CompileFunction(errors) != 0)
Greg Claytone0d378b2011-03-24 21:19:54 +0000563 return eExecutionSetupError;
Sean Callananebf77072010-07-23 00:16:21 +0000564
565 if (args_addr == LLDB_INVALID_ADDRESS)
566 {
Sean Callanan138e74e2010-07-26 22:14:36 +0000567 if (!InsertFunction(exe_ctx, args_addr, errors))
Greg Claytone0d378b2011-03-24 21:19:54 +0000568 return eExecutionSetupError;
Sean Callananebf77072010-07-23 00:16:21 +0000569 }
570
Greg Clayton22a939a2011-01-19 23:00:49 +0000571 return_value = ClangFunction::ExecuteFunction (exe_ctx,
572 m_jit_start_addr,
573 args_addr,
574 stop_others,
575 try_all_threads,
576 discard_on_error,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000577 timeout_usec,
Greg Clayton22a939a2011-01-19 23:00:49 +0000578 errors);
Sean Callananebf77072010-07-23 00:16:21 +0000579
580 if (args_addr_ptr != NULL)
581 *args_addr_ptr = args_addr;
582
Greg Claytone0d378b2011-03-24 21:19:54 +0000583 if (return_value != eExecutionCompleted)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000584 return return_value;
585
Sean Callanan138e74e2010-07-26 22:14:36 +0000586 FetchFunctionResults(exe_ctx, args_addr, results);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000587
588 if (args_addr_ptr == NULL)
Sean Callanan138e74e2010-07-26 22:14:36 +0000589 DeallocateFunctionResults(exe_ctx, args_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000590
Greg Claytone0d378b2011-03-24 21:19:54 +0000591 return eExecutionCompleted;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000592}
593
Sean Callanan1a8d4092010-08-27 01:01:44 +0000594clang::ASTConsumer *
595ClangFunction::ASTTransformer (clang::ASTConsumer *passthrough)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000596{
Sean Callanan1a8d4092010-08-27 01:01:44 +0000597 return new ASTStructExtractor(passthrough, m_wrapper_struct_name.c_str(), *this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000598}