blob: e37a16a4fb76be86d6fc59a63acb5561f4b6f0df [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 Clayton7b462cc2010-10-15 22:48:33 +000054 ClangASTContext *ast_context,
55 void *return_qualtype,
56 const Address& functionAddress,
57 const ValueList &arg_value_list
58) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059 m_function_ptr (NULL),
Greg Claytonc982c762010-07-09 20:39:50 +000060 m_function_addr (functionAddress),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061 m_function_return_qual_type(return_qualtype),
Greg Claytonc982c762010-07-09 20:39:50 +000062 m_clang_ast_context (ast_context),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063 m_wrapper_function_name ("__lldb_caller_function"),
64 m_wrapper_struct_name ("__lldb_caller_struct"),
Greg Claytonc982c762010-07-09 20:39:50 +000065 m_wrapper_args_addrs (),
Greg Claytonc982c762010-07-09 20:39:50 +000066 m_arg_values (arg_value_list),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000067 m_compiled (false),
68 m_JITted (false)
69{
Enrico Granatadfc88a02012-09-18 00:08:47 +000070 m_jit_process_wp = lldb::ProcessWP(exe_scope.CalculateProcess());
Jim Ingham35944dd2011-03-17 20:02:56 +000071 // Can't make a ClangFunction without a process.
Enrico Granatadfc88a02012-09-18 00:08:47 +000072 assert (m_jit_process_wp.lock());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000073}
74
Greg Clayton7b462cc2010-10-15 22:48:33 +000075ClangFunction::ClangFunction
76(
Jim Ingham35944dd2011-03-17 20:02:56 +000077 ExecutionContextScope &exe_scope,
Greg Clayton7b462cc2010-10-15 22:48:33 +000078 Function &function,
79 ClangASTContext *ast_context,
80 const ValueList &arg_value_list
81) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +000082 m_function_ptr (&function),
Greg Claytonc982c762010-07-09 20:39:50 +000083 m_function_addr (),
84 m_function_return_qual_type (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000085 m_clang_ast_context (ast_context),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000086 m_wrapper_function_name ("__lldb_function_caller"),
87 m_wrapper_struct_name ("__lldb_caller_struct"),
Greg Claytonc982c762010-07-09 20:39:50 +000088 m_wrapper_args_addrs (),
Greg Claytonc982c762010-07-09 20:39:50 +000089 m_arg_values (arg_value_list),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000090 m_compiled (false),
91 m_JITted (false)
92{
Enrico Granatadfc88a02012-09-18 00:08:47 +000093 m_jit_process_wp = lldb::ProcessWP(exe_scope.CalculateProcess());
Jim Ingham35944dd2011-03-17 20:02:56 +000094 // Can't make a ClangFunction without a process.
Enrico Granatadfc88a02012-09-18 00:08:47 +000095 assert (m_jit_process_wp.lock());
Greg Clayton514487e2011-02-15 21:59:32 +000096
Chris Lattner30fdc8d2010-06-08 16:52:24 +000097 m_function_addr = m_function_ptr->GetAddressRange().GetBaseAddress();
Greg Claytonf4ecaa52011-02-16 23:00:21 +000098 m_function_return_qual_type = m_function_ptr->GetReturnClangType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000099}
100
101//----------------------------------------------------------------------
102// Destructor
103//----------------------------------------------------------------------
104ClangFunction::~ClangFunction()
105{
106}
107
108unsigned
109ClangFunction::CompileFunction (Stream &errors)
110{
Sean Callanan1a8d4092010-08-27 01:01:44 +0000111 if (m_compiled)
112 return 0;
113
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000114 // FIXME: How does clang tell us there's no return value? We need to handle that case.
115 unsigned num_errors = 0;
116
Greg Clayton84db9102012-03-26 23:03:23 +0000117 std::string return_type_str (ClangASTType::GetTypeNameForOpaqueQualType (m_clang_ast_context->getASTContext(),
118 m_function_return_qual_type));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000119
120 // Cons up the function we're going to wrap our call in, then compile it...
121 // We declare the function "extern "C"" because the compiler might be in C++
122 // mode which would mangle the name and then we couldn't find it again...
123 m_wrapper_function_text.clear();
124 m_wrapper_function_text.append ("extern \"C\" void ");
125 m_wrapper_function_text.append (m_wrapper_function_name);
126 m_wrapper_function_text.append (" (void *input)\n{\n struct ");
127 m_wrapper_function_text.append (m_wrapper_struct_name);
128 m_wrapper_function_text.append (" \n {\n");
129 m_wrapper_function_text.append (" ");
130 m_wrapper_function_text.append (return_type_str);
131 m_wrapper_function_text.append (" (*fn_ptr) (");
132
133 // Get the number of arguments. If we have a function type and it is prototyped,
134 // trust that, otherwise use the values we were given.
135
136 // FIXME: This will need to be extended to handle Variadic functions. We'll need
137 // to pull the defined arguments out of the function, then add the types from the
138 // arguments list for the variable arguments.
139
Greg Claytonc7bece562013-01-25 18:06:21 +0000140 size_t num_args = UINT32_MAX;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000141 bool trust_function = false;
142 // GetArgumentCount returns -1 for an unprototyped function.
143 if (m_function_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000144 {
Sean Callanan1a8d4092010-08-27 01:01:44 +0000145 int num_func_args = m_function_ptr->GetArgumentCount();
146 if (num_func_args >= 0)
147 trust_function = true;
148 else
149 num_args = num_func_args;
150 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000151
Sean Callanan1a8d4092010-08-27 01:01:44 +0000152 if (num_args == UINT32_MAX)
153 num_args = m_arg_values.GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000154
Sean Callanan1a8d4092010-08-27 01:01:44 +0000155 std::string args_buffer; // This one stores the definition of all the args in "struct caller".
156 std::string args_list_buffer; // This one stores the argument list called from the structure.
157 for (size_t i = 0; i < num_args; i++)
158 {
Greg Claytonf4ecaa52011-02-16 23:00:21 +0000159 std::string type_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000160
Sean Callanan1a8d4092010-08-27 01:01:44 +0000161 if (trust_function)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000162 {
Greg Claytonf4ecaa52011-02-16 23:00:21 +0000163 lldb::clang_type_t arg_clang_type = m_function_ptr->GetArgumentTypeAtIndex(i);
Greg Clayton84db9102012-03-26 23:03:23 +0000164 type_name = ClangASTType::GetTypeNameForOpaqueQualType (m_clang_ast_context->getASTContext(),
165 arg_clang_type);
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 {
Sean Callanan1a8d4092010-08-27 01:01:44 +0000169 Value *arg_value = m_arg_values.GetValueAtIndex(i);
Greg Claytone3055942011-06-30 02:28:26 +0000170 lldb::clang_type_t clang_qual_type = arg_value->GetClangType ();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000171 if (clang_qual_type != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000172 {
Greg Clayton84db9102012-03-26 23:03:23 +0000173 type_name = ClangASTType::GetTypeNameForOpaqueQualType (m_clang_ast_context->getASTContext(),
174 clang_qual_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000175 }
176 else
Sean Callanan1a8d4092010-08-27 01:01:44 +0000177 {
Jason Molendafd54b362011-09-20 21:44:10 +0000178 errors.Printf("Could not determine type of input value %lu.", i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179 return 1;
180 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000181 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000182
Greg Claytonf4ecaa52011-02-16 23:00:21 +0000183 m_wrapper_function_text.append (type_name);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000184 if (i < num_args - 1)
185 m_wrapper_function_text.append (", ");
186
187 char arg_buf[32];
188 args_buffer.append (" ");
Greg Claytonf4ecaa52011-02-16 23:00:21 +0000189 args_buffer.append (type_name);
Daniel Malead01b2952012-11-29 21:49:15 +0000190 snprintf(arg_buf, 31, "arg_%" PRIu64, (uint64_t)i);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000191 args_buffer.push_back (' ');
192 args_buffer.append (arg_buf);
193 args_buffer.append (";\n");
194
195 args_list_buffer.append ("__lldb_fn_data->");
196 args_list_buffer.append (arg_buf);
197 if (i < num_args - 1)
198 args_list_buffer.append (", ");
199
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000200 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000201 m_wrapper_function_text.append (");\n"); // Close off the function calling prototype.
202
203 m_wrapper_function_text.append (args_buffer);
204
205 m_wrapper_function_text.append (" ");
206 m_wrapper_function_text.append (return_type_str);
207 m_wrapper_function_text.append (" return_value;");
208 m_wrapper_function_text.append ("\n };\n struct ");
209 m_wrapper_function_text.append (m_wrapper_struct_name);
210 m_wrapper_function_text.append ("* __lldb_fn_data = (struct ");
211 m_wrapper_function_text.append (m_wrapper_struct_name);
212 m_wrapper_function_text.append (" *) input;\n");
213
214 m_wrapper_function_text.append (" __lldb_fn_data->return_value = __lldb_fn_data->fn_ptr (");
215 m_wrapper_function_text.append (args_list_buffer);
216 m_wrapper_function_text.append (");\n}\n");
217
Greg Clayton5160ce52013-03-27 23:08:40 +0000218 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000219 if (log)
220 log->Printf ("Expression: \n\n%s\n\n", m_wrapper_function_text.c_str());
221
222 // Okay, now compile this expression
Sean Callanan1a8d4092010-08-27 01:01:44 +0000223
Enrico Granatadfc88a02012-09-18 00:08:47 +0000224 lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
225 if (jit_process_sp)
226 {
227 m_parser.reset(new ClangExpressionParser(jit_process_sp.get(), *this));
228
229 num_errors = m_parser->Parse (errors);
230 }
231 else
232 {
233 errors.Printf("no process - unable to inject function");
234 num_errors = 1;
235 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000236
237 m_compiled = (num_errors == 0);
238
239 if (!m_compiled)
240 return num_errors;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000241
242 return num_errors;
243}
244
245bool
Sean Callanan138e74e2010-07-26 22:14:36 +0000246ClangFunction::WriteFunctionWrapper (ExecutionContext &exe_ctx, Stream &errors)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000247{
Greg Claytonc14ee322011-09-22 04:58:26 +0000248 Process *process = exe_ctx.GetProcessPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000249
Sean Callanan1a8d4092010-08-27 01:01:44 +0000250 if (!process)
251 return false;
Enrico Granatadfc88a02012-09-18 00:08:47 +0000252
253 lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
254
255 if (process != jit_process_sp.get())
Jim Ingham35944dd2011-03-17 20:02:56 +0000256 return false;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000257
258 if (!m_compiled)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000259 return false;
260
Sean Callanan1a8d4092010-08-27 01:01:44 +0000261 if (m_JITted)
262 return true;
Sean Callanan1582ee62013-04-18 22:06:33 +0000263
264 bool can_interpret = false; // should stay that way
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000265
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000266 Error jit_error (m_parser->PrepareForExecution (m_jit_start_addr,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000267 m_jit_end_addr,
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000268 m_execution_unit_ap,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000269 exe_ctx,
Sean Callanan1582ee62013-04-18 22:06:33 +0000270 can_interpret,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000271 eExecutionPolicyAlways));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000272
273 if (!jit_error.Success())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000274 return false;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000275
276 if (process && m_jit_start_addr)
Enrico Granatadfc88a02012-09-18 00:08:47 +0000277 m_jit_process_wp = lldb::ProcessWP(process->shared_from_this());
Sean Callanand14fac12013-01-14 21:45:38 +0000278
279 m_JITted = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000280
281 return true;
282}
283
284bool
Sean Callanan138e74e2010-07-26 22:14:36 +0000285ClangFunction::WriteFunctionArguments (ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref, Stream &errors)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000286{
Sean Callanan138e74e2010-07-26 22:14:36 +0000287 return WriteFunctionArguments(exe_ctx, args_addr_ref, m_function_addr, m_arg_values, errors);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000288}
289
290// FIXME: Assure that the ValueList we were passed in is consistent with the one that defined this function.
291
292bool
Jim Inghambaae1682010-09-10 23:07:48 +0000293ClangFunction::WriteFunctionArguments (ExecutionContext &exe_ctx,
294 lldb::addr_t &args_addr_ref,
295 Address function_address,
296 ValueList &arg_values,
297 Stream &errors)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000298{
Sean Callanan1a8d4092010-08-27 01:01:44 +0000299 // All the information to reconstruct the struct is provided by the
300 // StructExtractor.
301 if (!m_struct_valid)
302 {
303 errors.Printf("Argument information was not correctly parsed, so the function cannot be called.");
304 return false;
305 }
306
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000307 Error error;
308 using namespace clang;
Greg Claytone0d378b2011-03-24 21:19:54 +0000309 ExecutionResults return_value = eExecutionSetupError;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000310
Greg Claytonc14ee322011-09-22 04:58:26 +0000311 Process *process = exe_ctx.GetProcessPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000312
313 if (process == NULL)
314 return return_value;
Jim Ingham35944dd2011-03-17 20:02:56 +0000315
Enrico Granatadfc88a02012-09-18 00:08:47 +0000316 lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
317
318 if (process != jit_process_sp.get())
Jim Ingham35944dd2011-03-17 20:02:56 +0000319 return false;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000320
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000321 if (args_addr_ref == LLDB_INVALID_ADDRESS)
322 {
Sean Callanan1a8d4092010-08-27 01:01:44 +0000323 args_addr_ref = process->AllocateMemory(m_struct_size, lldb::ePermissionsReadable|lldb::ePermissionsWritable, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000324 if (args_addr_ref == LLDB_INVALID_ADDRESS)
325 return false;
326 m_wrapper_args_addrs.push_back (args_addr_ref);
327 }
328 else
329 {
330 // Make sure this is an address that we've already handed out.
331 if (find (m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(), args_addr_ref) == m_wrapper_args_addrs.end())
332 {
333 return false;
334 }
335 }
336
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000337 // TODO: verify fun_addr needs to be a callable address
Greg Claytonc14ee322011-09-22 04:58:26 +0000338 Scalar fun_addr (function_address.GetCallableLoadAddress(exe_ctx.GetTargetPtr()));
Greg Claytonc7bece562013-01-25 18:06:21 +0000339 uint64_t first_offset = m_member_offsets[0];
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000340 process->WriteScalarToMemory(args_addr_ref + first_offset, fun_addr, process->GetAddressByteSize(), error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000341
342 // FIXME: We will need to extend this for Variadic functions.
343
344 Error value_error;
345
346 size_t num_args = arg_values.GetSize();
347 if (num_args != m_arg_values.GetSize())
348 {
Jason Molendafd54b362011-09-20 21:44:10 +0000349 errors.Printf ("Wrong number of arguments - was: %lu should be: %lu", num_args, m_arg_values.GetSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000350 return false;
351 }
352
Greg Claytonc982c762010-07-09 20:39:50 +0000353 for (size_t i = 0; i < num_args; i++)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000354 {
355 // FIXME: We should sanity check sizes.
356
Greg Claytonc7bece562013-01-25 18:06:21 +0000357 uint64_t offset = m_member_offsets[i+1]; // Clang sizes are in bytes.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000358 Value *arg_value = arg_values.GetValueAtIndex(i);
359
360 // FIXME: For now just do scalars:
361
362 // Special case: if it's a pointer, don't do anything (the ABI supports passing cstrings)
363
364 if (arg_value->GetValueType() == Value::eValueTypeHostAddress &&
Greg Clayton526e5af2010-11-13 03:52:47 +0000365 arg_value->GetContextType() == Value::eContextTypeClangType &&
Greg Clayton1be10fc2010-09-29 01:12:09 +0000366 ClangASTContext::IsPointerType(arg_value->GetClangType()))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000367 continue;
368
Sean Callanan138e74e2010-07-26 22:14:36 +0000369 const Scalar &arg_scalar = arg_value->ResolveValue(&exe_ctx, m_clang_ast_context->getASTContext());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000370
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000371 if (!process->WriteScalarToMemory(args_addr_ref + offset, arg_scalar, arg_scalar.GetByteSize(), error))
372 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000373 }
374
375 return true;
376}
377
378bool
Sean Callanan138e74e2010-07-26 22:14:36 +0000379ClangFunction::InsertFunction (ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref, Stream &errors)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000380{
381 using namespace clang;
382
383 if (CompileFunction(errors) != 0)
384 return false;
Sean Callanan138e74e2010-07-26 22:14:36 +0000385 if (!WriteFunctionWrapper(exe_ctx, errors))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000386 return false;
Sean Callanan138e74e2010-07-26 22:14:36 +0000387 if (!WriteFunctionArguments(exe_ctx, args_addr_ref, errors))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000388 return false;
389
Greg Clayton5160ce52013-03-27 23:08:40 +0000390 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000391 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000392 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 +0000393
394 return true;
395}
396
397ThreadPlan *
Jim Ingham399f1ca2010-11-05 19:25:48 +0000398ClangFunction::GetThreadPlanToCallFunction (ExecutionContext &exe_ctx,
399 lldb::addr_t func_addr,
400 lldb::addr_t &args_addr,
401 Stream &errors,
402 bool stop_others,
Jim Ingham184e9812013-01-15 02:47:48 +0000403 bool unwind_on_error,
404 bool ignore_breakpoints,
Sean Callanan17827832010-12-13 22:46:15 +0000405 lldb::addr_t *this_arg,
406 lldb::addr_t *cmd_arg)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000407{
Greg Clayton5160ce52013-03-27 23:08:40 +0000408 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callananf58b12d2013-03-06 19:57:25 +0000409
410 if (log)
411 log->Printf("-- [ClangFunction::GetThreadPlanToCallFunction] Creating thread plan to call function --");
412
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000413 // FIXME: Use the errors Stream for better error reporting.
Greg Claytonc14ee322011-09-22 04:58:26 +0000414 Thread *thread = exe_ctx.GetThreadPtr();
415 if (thread == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000416 {
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000417 errors.Printf("Can't call a function without a valid thread.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000418 return NULL;
419 }
420
421 // Okay, now run the function:
422
Greg Claytone72dfb32012-02-24 01:59:29 +0000423 Address wrapper_address (func_addr);
Greg Claytonc14ee322011-09-22 04:58:26 +0000424 ThreadPlan *new_plan = new ThreadPlanCallFunction (*thread,
Sean Callanan17827832010-12-13 22:46:15 +0000425 wrapper_address,
Jim Inghamef651602011-12-22 19:12:40 +0000426 ClangASTType(),
Sean Callanan17827832010-12-13 22:46:15 +0000427 args_addr,
428 stop_others,
Jim Ingham184e9812013-01-15 02:47:48 +0000429 unwind_on_error,
430 ignore_breakpoints,
Sean Callanan17827832010-12-13 22:46:15 +0000431 this_arg,
432 cmd_arg);
Jim Ingham923886c2012-05-11 18:43:38 +0000433 new_plan->SetIsMasterPlan(true);
434 new_plan->SetOkayToDiscard (false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000435 return new_plan;
436}
437
438bool
Sean Callanan138e74e2010-07-26 22:14:36 +0000439ClangFunction::FetchFunctionResults (ExecutionContext &exe_ctx, lldb::addr_t args_addr, Value &ret_value)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000440{
441 // Read the return value - it is the last field in the struct:
442 // FIXME: How does clang tell us there's no return value? We need to handle that case.
Jim Inghamef651602011-12-22 19:12:40 +0000443 // FIXME: Create our ThreadPlanCallFunction with the return ClangASTType, and then use GetReturnValueObject
444 // to fetch the value. That way we can fetch any values we need.
Sean Callananf58b12d2013-03-06 19:57:25 +0000445
Greg Clayton5160ce52013-03-27 23:08:40 +0000446 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callananf58b12d2013-03-06 19:57:25 +0000447
448 if (log)
449 log->Printf("-- [ClangFunction::FetchFunctionResults] Fetching function results --");
450
Greg Claytonc14ee322011-09-22 04:58:26 +0000451 Process *process = exe_ctx.GetProcessPtr();
Jim Ingham35944dd2011-03-17 20:02:56 +0000452
453 if (process == NULL)
454 return false;
Enrico Granatadfc88a02012-09-18 00:08:47 +0000455
456 lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
457
458 if (process != jit_process_sp.get())
Jim Ingham35944dd2011-03-17 20:02:56 +0000459 return false;
460
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000461 Error error;
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000462 ret_value.GetScalar() = process->ReadUnsignedIntegerFromMemory (args_addr + m_return_offset, m_return_size, 0, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000463
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000464 if (error.Fail())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000465 return false;
466
Greg Clayton526e5af2010-11-13 03:52:47 +0000467 ret_value.SetContext (Value::eContextTypeClangType, m_function_return_qual_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000468 ret_value.SetValueType(Value::eValueTypeScalar);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000469 return true;
470}
471
472void
Sean Callanan138e74e2010-07-26 22:14:36 +0000473ClangFunction::DeallocateFunctionResults (ExecutionContext &exe_ctx, lldb::addr_t args_addr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000474{
475 std::list<lldb::addr_t>::iterator pos;
476 pos = std::find(m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(), args_addr);
477 if (pos != m_wrapper_args_addrs.end())
478 m_wrapper_args_addrs.erase(pos);
479
Greg Claytonc14ee322011-09-22 04:58:26 +0000480 exe_ctx.GetProcessRef().DeallocateMemory(args_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000481}
482
Greg Claytone0d378b2011-03-24 21:19:54 +0000483ExecutionResults
Sean Callanan138e74e2010-07-26 22:14:36 +0000484ClangFunction::ExecuteFunction(ExecutionContext &exe_ctx, Stream &errors, Value &results)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000485{
Sean Callanan138e74e2010-07-26 22:14:36 +0000486 return ExecuteFunction (exe_ctx, errors, 1000, true, results);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000487}
488
Greg Claytone0d378b2011-03-24 21:19:54 +0000489ExecutionResults
Sean Callanan138e74e2010-07-26 22:14:36 +0000490ClangFunction::ExecuteFunction(ExecutionContext &exe_ctx, Stream &errors, bool stop_others, Value &results)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000491{
Jim Ingham399f1ca2010-11-05 19:25:48 +0000492 const bool try_all_threads = false;
Jim Ingham184e9812013-01-15 02:47:48 +0000493 const bool unwind_on_error = true;
494 const bool ignore_breakpoints = true;
495 return ExecuteFunction (exe_ctx, NULL, errors, stop_others, 0UL, try_all_threads,
496 unwind_on_error, ignore_breakpoints, results);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000497}
498
Greg Claytone0d378b2011-03-24 21:19:54 +0000499ExecutionResults
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000500ClangFunction::ExecuteFunction(
Sean Callanan138e74e2010-07-26 22:14:36 +0000501 ExecutionContext &exe_ctx,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000502 Stream &errors,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000503 uint32_t timeout_usec,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000504 bool try_all_threads,
505 Value &results)
506{
Jim Ingham399f1ca2010-11-05 19:25:48 +0000507 const bool stop_others = true;
Jim Ingham184e9812013-01-15 02:47:48 +0000508 const bool unwind_on_error = true;
509 const bool ignore_breakpoints = true;
Jim Ingham35e1bda2012-10-16 21:41:58 +0000510 return ExecuteFunction (exe_ctx, NULL, errors, stop_others, timeout_usec,
Jim Ingham184e9812013-01-15 02:47:48 +0000511 try_all_threads, unwind_on_error, ignore_breakpoints, results);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000512}
513
Sean Callananebf77072010-07-23 00:16:21 +0000514// This is the static function
Greg Claytone0d378b2011-03-24 21:19:54 +0000515ExecutionResults
Sean Callananebf77072010-07-23 00:16:21 +0000516ClangFunction::ExecuteFunction (
Sean Callanan138e74e2010-07-26 22:14:36 +0000517 ExecutionContext &exe_ctx,
Sean Callananebf77072010-07-23 00:16:21 +0000518 lldb::addr_t function_address,
519 lldb::addr_t &void_arg,
520 bool stop_others,
521 bool try_all_threads,
Jim Ingham184e9812013-01-15 02:47:48 +0000522 bool unwind_on_error,
523 bool ignore_breakpoints,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000524 uint32_t timeout_usec,
Sean Callananfc55f5d2010-09-21 00:44:12 +0000525 Stream &errors,
526 lldb::addr_t *this_arg)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000527{
Greg Clayton5160ce52013-03-27 23:08:40 +0000528 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callananf58b12d2013-03-06 19:57:25 +0000529
530 if (log)
531 log->Printf("== [ClangFunction::ExecuteFunction] Executing function ==");
532
533 lldb::ThreadPlanSP call_plan_sp (ClangFunction::GetThreadPlanToCallFunction (exe_ctx,
Greg Claytonc14ee322011-09-22 04:58:26 +0000534 function_address,
535 void_arg,
536 errors,
537 stop_others,
Jim Ingham184e9812013-01-15 02:47:48 +0000538 unwind_on_error,
539 ignore_breakpoints,
Greg Claytonc14ee322011-09-22 04:58:26 +0000540 this_arg));
Sean Callanan9a028512012-08-09 00:50:26 +0000541 if (!call_plan_sp)
Greg Claytone0d378b2011-03-24 21:19:54 +0000542 return eExecutionSetupError;
Jim Ingham95afbf52012-12-07 19:04:31 +0000543
Enrico Granatae2e091b2012-08-03 22:24:48 +0000544 // <rdar://problem/12027563> we need to make sure we record the fact that we are running an expression here
545 // otherwise this fact will fail to be recorded when fetching an Objective-C object description
546 if (exe_ctx.GetProcessPtr())
547 exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
548
549 ExecutionResults results = exe_ctx.GetProcessRef().RunThreadPlan (exe_ctx, call_plan_sp,
550 stop_others,
551 try_all_threads,
Jim Ingham184e9812013-01-15 02:47:48 +0000552 unwind_on_error,
553 ignore_breakpoints,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000554 timeout_usec,
Enrico Granatae2e091b2012-08-03 22:24:48 +0000555 errors);
556
Sean Callananf58b12d2013-03-06 19:57:25 +0000557 if (log)
558 {
559 if (results != 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
Enrico Granatae2e091b2012-08-03 22:24:48 +0000569 if (exe_ctx.GetProcessPtr())
570 exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
571
572 return results;
573}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000574
Greg Claytone0d378b2011-03-24 21:19:54 +0000575ExecutionResults
Sean Callananebf77072010-07-23 00:16:21 +0000576ClangFunction::ExecuteFunction(
Sean Callanan138e74e2010-07-26 22:14:36 +0000577 ExecutionContext &exe_ctx,
Sean Callananebf77072010-07-23 00:16:21 +0000578 lldb::addr_t *args_addr_ptr,
579 Stream &errors,
580 bool stop_others,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000581 uint32_t timeout_usec,
Jim Ingham399f1ca2010-11-05 19:25:48 +0000582 bool try_all_threads,
Jim Ingham184e9812013-01-15 02:47:48 +0000583 bool unwind_on_error,
584 bool ignore_breakpoints,
Sean Callananebf77072010-07-23 00:16:21 +0000585 Value &results)
586{
587 using namespace clang;
Greg Claytone0d378b2011-03-24 21:19:54 +0000588 ExecutionResults return_value = eExecutionSetupError;
Sean Callananebf77072010-07-23 00:16:21 +0000589
590 lldb::addr_t args_addr;
591
592 if (args_addr_ptr != NULL)
593 args_addr = *args_addr_ptr;
594 else
595 args_addr = LLDB_INVALID_ADDRESS;
596
597 if (CompileFunction(errors) != 0)
Greg Claytone0d378b2011-03-24 21:19:54 +0000598 return eExecutionSetupError;
Sean Callananebf77072010-07-23 00:16:21 +0000599
600 if (args_addr == LLDB_INVALID_ADDRESS)
601 {
Sean Callanan138e74e2010-07-26 22:14:36 +0000602 if (!InsertFunction(exe_ctx, args_addr, errors))
Greg Claytone0d378b2011-03-24 21:19:54 +0000603 return eExecutionSetupError;
Sean Callananebf77072010-07-23 00:16:21 +0000604 }
605
Greg Clayton22a939a2011-01-19 23:00:49 +0000606 return_value = ClangFunction::ExecuteFunction (exe_ctx,
607 m_jit_start_addr,
608 args_addr,
609 stop_others,
610 try_all_threads,
Jim Ingham184e9812013-01-15 02:47:48 +0000611 unwind_on_error,
612 ignore_breakpoints,
Jim Ingham35e1bda2012-10-16 21:41:58 +0000613 timeout_usec,
Greg Clayton22a939a2011-01-19 23:00:49 +0000614 errors);
Sean Callananebf77072010-07-23 00:16:21 +0000615
616 if (args_addr_ptr != NULL)
617 *args_addr_ptr = args_addr;
618
Greg Claytone0d378b2011-03-24 21:19:54 +0000619 if (return_value != eExecutionCompleted)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000620 return return_value;
621
Sean Callanan138e74e2010-07-26 22:14:36 +0000622 FetchFunctionResults(exe_ctx, args_addr, results);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000623
624 if (args_addr_ptr == NULL)
Sean Callanan138e74e2010-07-26 22:14:36 +0000625 DeallocateFunctionResults(exe_ctx, args_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000626
Greg Claytone0d378b2011-03-24 21:19:54 +0000627 return eExecutionCompleted;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000628}
629
Sean Callanan1a8d4092010-08-27 01:01:44 +0000630clang::ASTConsumer *
631ClangFunction::ASTTransformer (clang::ASTConsumer *passthrough)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000632{
Sean Callanan1a8d4092010-08-27 01:01:44 +0000633 return new ASTStructExtractor(passthrough, m_wrapper_struct_name.c_str(), *this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000634}