blob: a3d177cf4c13d79d5edbd93a0d8e883626607e3f [file] [log] [blame]
Chris Lattner24943d22010-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 Lattner24943d22010-06-08 16:52:24 +000014#include "clang/AST/ASTContext.h"
15#include "clang/AST/RecordLayout.h"
Greg Claytonc4f51102010-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 Clayton395fc332011-02-15 21:59:32 +000020#include "llvm/ADT/Triple.h"
Greg Claytonc4f51102010-07-02 18:39:06 +000021#include "llvm/ExecutionEngine/ExecutionEngine.h"
Chris Lattner24943d22010-06-08 16:52:24 +000022#include "llvm/Module.h"
Chris Lattner24943d22010-06-08 16:52:24 +000023
24// Project includes
Sean Callanan65dafa82010-08-27 01:01:44 +000025#include "lldb/Expression/ASTStructExtractor.h"
26#include "lldb/Expression/ClangExpressionParser.h"
Chris Lattner24943d22010-06-08 16:52:24 +000027#include "lldb/Expression/ClangFunction.h"
28#include "lldb/Symbol/Type.h"
29#include "lldb/Core/DataExtractor.h"
Jim Ingham3ae449a2010-11-17 02:32:00 +000030#include "lldb/Core/State.h"
Chris Lattner24943d22010-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 Callanan841026f2010-07-23 00:16:21 +000038#include "lldb/Target/RegisterContext.h"
Greg Clayton395fc332011-02-15 21:59:32 +000039#include "lldb/Target/Target.h"
Chris Lattner24943d22010-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 Callanan65dafa82010-08-27 01:01:44 +000046
Chris Lattner24943d22010-06-08 16:52:24 +000047//----------------------------------------------------------------------
48// ClangFunction constructor
49//----------------------------------------------------------------------
Greg Clayton8de27c72010-10-15 22:48:33 +000050ClangFunction::ClangFunction
51(
Jim Ingham53bfd062011-03-17 20:02:56 +000052 ExecutionContextScope &exe_scope,
Greg Clayton8de27c72010-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 Lattner24943d22010-06-08 16:52:24 +000058 m_function_ptr (NULL),
Greg Clayton54e7afa2010-07-09 20:39:50 +000059 m_function_addr (functionAddress),
Chris Lattner24943d22010-06-08 16:52:24 +000060 m_function_return_qual_type(return_qualtype),
Greg Clayton54e7afa2010-07-09 20:39:50 +000061 m_clang_ast_context (ast_context),
Chris Lattner24943d22010-06-08 16:52:24 +000062 m_wrapper_function_name ("__lldb_caller_function"),
63 m_wrapper_struct_name ("__lldb_caller_struct"),
Greg Clayton54e7afa2010-07-09 20:39:50 +000064 m_wrapper_args_addrs (),
Greg Clayton54e7afa2010-07-09 20:39:50 +000065 m_arg_values (arg_value_list),
Chris Lattner24943d22010-06-08 16:52:24 +000066 m_compiled (false),
67 m_JITted (false)
68{
Greg Clayton289afcb2012-02-18 05:35:26 +000069 m_jit_process_sp = exe_scope.CalculateProcess();
Jim Ingham53bfd062011-03-17 20:02:56 +000070 // Can't make a ClangFunction without a process.
Greg Clayton289afcb2012-02-18 05:35:26 +000071 assert (m_jit_process_sp);
Chris Lattner24943d22010-06-08 16:52:24 +000072}
73
Greg Clayton8de27c72010-10-15 22:48:33 +000074ClangFunction::ClangFunction
75(
Jim Ingham53bfd062011-03-17 20:02:56 +000076 ExecutionContextScope &exe_scope,
Greg Clayton8de27c72010-10-15 22:48:33 +000077 Function &function,
78 ClangASTContext *ast_context,
79 const ValueList &arg_value_list
80) :
Chris Lattner24943d22010-06-08 16:52:24 +000081 m_function_ptr (&function),
Greg Clayton54e7afa2010-07-09 20:39:50 +000082 m_function_addr (),
83 m_function_return_qual_type (),
Chris Lattner24943d22010-06-08 16:52:24 +000084 m_clang_ast_context (ast_context),
Chris Lattner24943d22010-06-08 16:52:24 +000085 m_wrapper_function_name ("__lldb_function_caller"),
86 m_wrapper_struct_name ("__lldb_caller_struct"),
Greg Clayton54e7afa2010-07-09 20:39:50 +000087 m_wrapper_args_addrs (),
Greg Clayton54e7afa2010-07-09 20:39:50 +000088 m_arg_values (arg_value_list),
Chris Lattner24943d22010-06-08 16:52:24 +000089 m_compiled (false),
90 m_JITted (false)
91{
Greg Clayton289afcb2012-02-18 05:35:26 +000092 m_jit_process_sp = exe_scope.CalculateProcess();
Jim Ingham53bfd062011-03-17 20:02:56 +000093 // Can't make a ClangFunction without a process.
Greg Clayton289afcb2012-02-18 05:35:26 +000094 assert (m_jit_process_sp);
Greg Clayton395fc332011-02-15 21:59:32 +000095
Chris Lattner24943d22010-06-08 16:52:24 +000096 m_function_addr = m_function_ptr->GetAddressRange().GetBaseAddress();
Greg Clayton04c9c7b2011-02-16 23:00:21 +000097 m_function_return_qual_type = m_function_ptr->GetReturnClangType();
Chris Lattner24943d22010-06-08 16:52:24 +000098}
99
100//----------------------------------------------------------------------
101// Destructor
102//----------------------------------------------------------------------
103ClangFunction::~ClangFunction()
104{
105}
106
107unsigned
108ClangFunction::CompileFunction (Stream &errors)
109{
Sean Callanan65dafa82010-08-27 01:01:44 +0000110 if (m_compiled)
111 return 0;
112
Chris Lattner24943d22010-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 Claytonb302b2f2011-06-30 02:28:26 +0000116 std::string return_type_str (ClangASTType::GetTypeNameForOpaqueQualType (m_function_return_qual_type));
Sean Callanan65dafa82010-08-27 01:01:44 +0000117
118 // Cons up the function we're going to wrap our call in, then compile it...
119 // We declare the function "extern "C"" because the compiler might be in C++
120 // mode which would mangle the name and then we couldn't find it again...
121 m_wrapper_function_text.clear();
122 m_wrapper_function_text.append ("extern \"C\" void ");
123 m_wrapper_function_text.append (m_wrapper_function_name);
124 m_wrapper_function_text.append (" (void *input)\n{\n struct ");
125 m_wrapper_function_text.append (m_wrapper_struct_name);
126 m_wrapper_function_text.append (" \n {\n");
127 m_wrapper_function_text.append (" ");
128 m_wrapper_function_text.append (return_type_str);
129 m_wrapper_function_text.append (" (*fn_ptr) (");
130
131 // Get the number of arguments. If we have a function type and it is prototyped,
132 // trust that, otherwise use the values we were given.
133
134 // FIXME: This will need to be extended to handle Variadic functions. We'll need
135 // to pull the defined arguments out of the function, then add the types from the
136 // arguments list for the variable arguments.
137
138 uint32_t num_args = UINT32_MAX;
139 bool trust_function = false;
140 // GetArgumentCount returns -1 for an unprototyped function.
141 if (m_function_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +0000142 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000143 int num_func_args = m_function_ptr->GetArgumentCount();
144 if (num_func_args >= 0)
145 trust_function = true;
146 else
147 num_args = num_func_args;
148 }
Chris Lattner24943d22010-06-08 16:52:24 +0000149
Sean Callanan65dafa82010-08-27 01:01:44 +0000150 if (num_args == UINT32_MAX)
151 num_args = m_arg_values.GetSize();
Chris Lattner24943d22010-06-08 16:52:24 +0000152
Sean Callanan65dafa82010-08-27 01:01:44 +0000153 std::string args_buffer; // This one stores the definition of all the args in "struct caller".
154 std::string args_list_buffer; // This one stores the argument list called from the structure.
155 for (size_t i = 0; i < num_args; i++)
156 {
Greg Clayton04c9c7b2011-02-16 23:00:21 +0000157 std::string type_name;
Chris Lattner24943d22010-06-08 16:52:24 +0000158
Sean Callanan65dafa82010-08-27 01:01:44 +0000159 if (trust_function)
Chris Lattner24943d22010-06-08 16:52:24 +0000160 {
Greg Clayton04c9c7b2011-02-16 23:00:21 +0000161 lldb::clang_type_t arg_clang_type = m_function_ptr->GetArgumentTypeAtIndex(i);
Greg Claytonb302b2f2011-06-30 02:28:26 +0000162 type_name = ClangASTType::GetTypeNameForOpaqueQualType (arg_clang_type);
Chris Lattner24943d22010-06-08 16:52:24 +0000163 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000164 else
Chris Lattner24943d22010-06-08 16:52:24 +0000165 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000166 Value *arg_value = m_arg_values.GetValueAtIndex(i);
Greg Claytonb302b2f2011-06-30 02:28:26 +0000167 lldb::clang_type_t clang_qual_type = arg_value->GetClangType ();
Sean Callanan65dafa82010-08-27 01:01:44 +0000168 if (clang_qual_type != NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000169 {
Greg Claytonb302b2f2011-06-30 02:28:26 +0000170 type_name = ClangASTType::GetTypeNameForOpaqueQualType (clang_qual_type);
Chris Lattner24943d22010-06-08 16:52:24 +0000171 }
172 else
Sean Callanan65dafa82010-08-27 01:01:44 +0000173 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000174 errors.Printf("Could not determine type of input value %lu.", i);
Chris Lattner24943d22010-06-08 16:52:24 +0000175 return 1;
176 }
Chris Lattner24943d22010-06-08 16:52:24 +0000177 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000178
Greg Clayton04c9c7b2011-02-16 23:00:21 +0000179 m_wrapper_function_text.append (type_name);
Sean Callanan65dafa82010-08-27 01:01:44 +0000180 if (i < num_args - 1)
181 m_wrapper_function_text.append (", ");
182
183 char arg_buf[32];
184 args_buffer.append (" ");
Greg Clayton04c9c7b2011-02-16 23:00:21 +0000185 args_buffer.append (type_name);
Sean Callanan65dafa82010-08-27 01:01:44 +0000186 snprintf(arg_buf, 31, "arg_%zd", i);
187 args_buffer.push_back (' ');
188 args_buffer.append (arg_buf);
189 args_buffer.append (";\n");
190
191 args_list_buffer.append ("__lldb_fn_data->");
192 args_list_buffer.append (arg_buf);
193 if (i < num_args - 1)
194 args_list_buffer.append (", ");
195
Chris Lattner24943d22010-06-08 16:52:24 +0000196 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000197 m_wrapper_function_text.append (");\n"); // Close off the function calling prototype.
198
199 m_wrapper_function_text.append (args_buffer);
200
201 m_wrapper_function_text.append (" ");
202 m_wrapper_function_text.append (return_type_str);
203 m_wrapper_function_text.append (" return_value;");
204 m_wrapper_function_text.append ("\n };\n struct ");
205 m_wrapper_function_text.append (m_wrapper_struct_name);
206 m_wrapper_function_text.append ("* __lldb_fn_data = (struct ");
207 m_wrapper_function_text.append (m_wrapper_struct_name);
208 m_wrapper_function_text.append (" *) input;\n");
209
210 m_wrapper_function_text.append (" __lldb_fn_data->return_value = __lldb_fn_data->fn_ptr (");
211 m_wrapper_function_text.append (args_list_buffer);
212 m_wrapper_function_text.append (");\n}\n");
213
Jim Ingham7812e012011-01-18 22:20:08 +0000214 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan65dafa82010-08-27 01:01:44 +0000215 if (log)
216 log->Printf ("Expression: \n\n%s\n\n", m_wrapper_function_text.c_str());
217
218 // Okay, now compile this expression
Jim Ingham53bfd062011-03-17 20:02:56 +0000219
220 m_parser.reset(new ClangExpressionParser(m_jit_process_sp.get(), *this));
Sean Callanan65dafa82010-08-27 01:01:44 +0000221
222 num_errors = m_parser->Parse (errors);
223
224 m_compiled = (num_errors == 0);
225
226 if (!m_compiled)
227 return num_errors;
Chris Lattner24943d22010-06-08 16:52:24 +0000228
229 return num_errors;
230}
231
232bool
Sean Callananc78d6482010-07-26 22:14:36 +0000233ClangFunction::WriteFunctionWrapper (ExecutionContext &exe_ctx, Stream &errors)
Chris Lattner24943d22010-06-08 16:52:24 +0000234{
Greg Clayton567e7f32011-09-22 04:58:26 +0000235 Process *process = exe_ctx.GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000236
Sean Callanan65dafa82010-08-27 01:01:44 +0000237 if (!process)
238 return false;
Jim Ingham53bfd062011-03-17 20:02:56 +0000239
240 if (process != m_jit_process_sp.get())
241 return false;
Sean Callanan65dafa82010-08-27 01:01:44 +0000242
243 if (!m_compiled)
Chris Lattner24943d22010-06-08 16:52:24 +0000244 return false;
245
Sean Callanan65dafa82010-08-27 01:01:44 +0000246 if (m_JITted)
247 return true;
248
Sean Callanan696cf5f2011-05-07 01:06:41 +0000249 lldb::ClangExpressionVariableSP const_result;
250
Sean Callanan47dc4572011-09-15 02:13:07 +0000251 bool evaluated_statically = false; // should stay that way
252
253 Error jit_error (m_parser->PrepareForExecution (m_jit_alloc,
254 m_jit_start_addr,
255 m_jit_end_addr,
256 exe_ctx,
257 NULL,
258 evaluated_statically,
259 const_result,
260 eExecutionPolicyAlways));
Sean Callanan65dafa82010-08-27 01:01:44 +0000261
262 if (!jit_error.Success())
Chris Lattner24943d22010-06-08 16:52:24 +0000263 return false;
Greg Clayton567e7f32011-09-22 04:58:26 +0000264 if (process && m_jit_alloc != LLDB_INVALID_ADDRESS)
Greg Clayton13d24fb2012-01-29 20:56:30 +0000265 m_jit_process_sp = process->shared_from_this();
Chris Lattner24943d22010-06-08 16:52:24 +0000266
267 return true;
268}
269
270bool
Sean Callananc78d6482010-07-26 22:14:36 +0000271ClangFunction::WriteFunctionArguments (ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref, Stream &errors)
Chris Lattner24943d22010-06-08 16:52:24 +0000272{
Sean Callananc78d6482010-07-26 22:14:36 +0000273 return WriteFunctionArguments(exe_ctx, args_addr_ref, m_function_addr, m_arg_values, errors);
Chris Lattner24943d22010-06-08 16:52:24 +0000274}
275
276// FIXME: Assure that the ValueList we were passed in is consistent with the one that defined this function.
277
278bool
Jim Ingham681778e2010-09-10 23:07:48 +0000279ClangFunction::WriteFunctionArguments (ExecutionContext &exe_ctx,
280 lldb::addr_t &args_addr_ref,
281 Address function_address,
282 ValueList &arg_values,
283 Stream &errors)
Chris Lattner24943d22010-06-08 16:52:24 +0000284{
Sean Callanan65dafa82010-08-27 01:01:44 +0000285 // All the information to reconstruct the struct is provided by the
286 // StructExtractor.
287 if (!m_struct_valid)
288 {
289 errors.Printf("Argument information was not correctly parsed, so the function cannot be called.");
290 return false;
291 }
292
Chris Lattner24943d22010-06-08 16:52:24 +0000293 Error error;
294 using namespace clang;
Greg Claytonb3448432011-03-24 21:19:54 +0000295 ExecutionResults return_value = eExecutionSetupError;
Chris Lattner24943d22010-06-08 16:52:24 +0000296
Greg Clayton567e7f32011-09-22 04:58:26 +0000297 Process *process = exe_ctx.GetProcessPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000298
299 if (process == NULL)
300 return return_value;
Jim Ingham53bfd062011-03-17 20:02:56 +0000301
302 if (process != m_jit_process_sp.get())
303 return false;
Sean Callanan65dafa82010-08-27 01:01:44 +0000304
Chris Lattner24943d22010-06-08 16:52:24 +0000305 if (args_addr_ref == LLDB_INVALID_ADDRESS)
306 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000307 args_addr_ref = process->AllocateMemory(m_struct_size, lldb::ePermissionsReadable|lldb::ePermissionsWritable, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000308 if (args_addr_ref == LLDB_INVALID_ADDRESS)
309 return false;
310 m_wrapper_args_addrs.push_back (args_addr_ref);
311 }
312 else
313 {
314 // Make sure this is an address that we've already handed out.
315 if (find (m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(), args_addr_ref) == m_wrapper_args_addrs.end())
316 {
317 return false;
318 }
319 }
320
Greg Claytonc0fa5332011-05-22 22:46:53 +0000321 // TODO: verify fun_addr needs to be a callable address
Greg Clayton567e7f32011-09-22 04:58:26 +0000322 Scalar fun_addr (function_address.GetCallableLoadAddress(exe_ctx.GetTargetPtr()));
Sean Callanan65dafa82010-08-27 01:01:44 +0000323 int first_offset = m_member_offsets[0];
Greg Claytonc0fa5332011-05-22 22:46:53 +0000324 process->WriteScalarToMemory(args_addr_ref + first_offset, fun_addr, process->GetAddressByteSize(), error);
Chris Lattner24943d22010-06-08 16:52:24 +0000325
326 // FIXME: We will need to extend this for Variadic functions.
327
328 Error value_error;
329
330 size_t num_args = arg_values.GetSize();
331 if (num_args != m_arg_values.GetSize())
332 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000333 errors.Printf ("Wrong number of arguments - was: %lu should be: %lu", num_args, m_arg_values.GetSize());
Chris Lattner24943d22010-06-08 16:52:24 +0000334 return false;
335 }
336
Greg Clayton54e7afa2010-07-09 20:39:50 +0000337 for (size_t i = 0; i < num_args; i++)
Chris Lattner24943d22010-06-08 16:52:24 +0000338 {
339 // FIXME: We should sanity check sizes.
340
Sean Callanan65dafa82010-08-27 01:01:44 +0000341 int offset = m_member_offsets[i+1]; // Clang sizes are in bytes.
Chris Lattner24943d22010-06-08 16:52:24 +0000342 Value *arg_value = arg_values.GetValueAtIndex(i);
343
344 // FIXME: For now just do scalars:
345
346 // Special case: if it's a pointer, don't do anything (the ABI supports passing cstrings)
347
348 if (arg_value->GetValueType() == Value::eValueTypeHostAddress &&
Greg Clayton6916e352010-11-13 03:52:47 +0000349 arg_value->GetContextType() == Value::eContextTypeClangType &&
Greg Clayton462d4142010-09-29 01:12:09 +0000350 ClangASTContext::IsPointerType(arg_value->GetClangType()))
Chris Lattner24943d22010-06-08 16:52:24 +0000351 continue;
352
Sean Callananc78d6482010-07-26 22:14:36 +0000353 const Scalar &arg_scalar = arg_value->ResolveValue(&exe_ctx, m_clang_ast_context->getASTContext());
Chris Lattner24943d22010-06-08 16:52:24 +0000354
Greg Claytonc0fa5332011-05-22 22:46:53 +0000355 if (!process->WriteScalarToMemory(args_addr_ref + offset, arg_scalar, arg_scalar.GetByteSize(), error))
356 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000357 }
358
359 return true;
360}
361
362bool
Sean Callananc78d6482010-07-26 22:14:36 +0000363ClangFunction::InsertFunction (ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref, Stream &errors)
Chris Lattner24943d22010-06-08 16:52:24 +0000364{
365 using namespace clang;
366
367 if (CompileFunction(errors) != 0)
368 return false;
Sean Callananc78d6482010-07-26 22:14:36 +0000369 if (!WriteFunctionWrapper(exe_ctx, errors))
Chris Lattner24943d22010-06-08 16:52:24 +0000370 return false;
Sean Callananc78d6482010-07-26 22:14:36 +0000371 if (!WriteFunctionArguments(exe_ctx, args_addr_ref, errors))
Chris Lattner24943d22010-06-08 16:52:24 +0000372 return false;
373
Greg Claytone005f2c2010-11-06 01:53:30 +0000374 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000375 if (log)
Greg Claytond0882d02011-01-19 23:00:49 +0000376 log->Printf ("Call Address: 0x%llx Struct Address: 0x%llx.\n", m_jit_start_addr, args_addr_ref);
Chris Lattner24943d22010-06-08 16:52:24 +0000377
378 return true;
379}
380
381ThreadPlan *
Jim Inghamea9d4262010-11-05 19:25:48 +0000382ClangFunction::GetThreadPlanToCallFunction (ExecutionContext &exe_ctx,
383 lldb::addr_t func_addr,
384 lldb::addr_t &args_addr,
385 Stream &errors,
386 bool stop_others,
387 bool discard_on_error,
Sean Callanan3aa7da52010-12-13 22:46:15 +0000388 lldb::addr_t *this_arg,
389 lldb::addr_t *cmd_arg)
Chris Lattner24943d22010-06-08 16:52:24 +0000390{
391 // FIXME: Use the errors Stream for better error reporting.
Greg Clayton567e7f32011-09-22 04:58:26 +0000392 Thread *thread = exe_ctx.GetThreadPtr();
393 if (thread == NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000394 {
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000395 errors.Printf("Can't call a function without a valid thread.");
Chris Lattner24943d22010-06-08 16:52:24 +0000396 return NULL;
397 }
398
399 // Okay, now run the function:
400
Greg Clayton3508c382012-02-24 01:59:29 +0000401 Address wrapper_address (func_addr);
Greg Clayton567e7f32011-09-22 04:58:26 +0000402 ThreadPlan *new_plan = new ThreadPlanCallFunction (*thread,
Sean Callanan3aa7da52010-12-13 22:46:15 +0000403 wrapper_address,
Jim Ingham016ef882011-12-22 19:12:40 +0000404 ClangASTType(),
Sean Callanan3aa7da52010-12-13 22:46:15 +0000405 args_addr,
406 stop_others,
407 discard_on_error,
408 this_arg,
409 cmd_arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000410 return new_plan;
411}
412
413bool
Sean Callananc78d6482010-07-26 22:14:36 +0000414ClangFunction::FetchFunctionResults (ExecutionContext &exe_ctx, lldb::addr_t args_addr, Value &ret_value)
Chris Lattner24943d22010-06-08 16:52:24 +0000415{
416 // Read the return value - it is the last field in the struct:
417 // FIXME: How does clang tell us there's no return value? We need to handle that case.
Jim Ingham016ef882011-12-22 19:12:40 +0000418 // FIXME: Create our ThreadPlanCallFunction with the return ClangASTType, and then use GetReturnValueObject
419 // to fetch the value. That way we can fetch any values we need.
Greg Clayton567e7f32011-09-22 04:58:26 +0000420 Process *process = exe_ctx.GetProcessPtr();
Jim Ingham53bfd062011-03-17 20:02:56 +0000421
422 if (process == NULL)
423 return false;
424 if (process != m_jit_process_sp.get())
425 return false;
426
Chris Lattner24943d22010-06-08 16:52:24 +0000427 Error error;
Greg Claytonc0fa5332011-05-22 22:46:53 +0000428 ret_value.GetScalar() = process->ReadUnsignedIntegerFromMemory (args_addr + m_return_offset, m_return_size, 0, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000429
Greg Claytonc0fa5332011-05-22 22:46:53 +0000430 if (error.Fail())
Chris Lattner24943d22010-06-08 16:52:24 +0000431 return false;
432
Greg Clayton6916e352010-11-13 03:52:47 +0000433 ret_value.SetContext (Value::eContextTypeClangType, m_function_return_qual_type);
Chris Lattner24943d22010-06-08 16:52:24 +0000434 ret_value.SetValueType(Value::eValueTypeScalar);
Chris Lattner24943d22010-06-08 16:52:24 +0000435 return true;
436}
437
438void
Sean Callananc78d6482010-07-26 22:14:36 +0000439ClangFunction::DeallocateFunctionResults (ExecutionContext &exe_ctx, lldb::addr_t args_addr)
Chris Lattner24943d22010-06-08 16:52:24 +0000440{
441 std::list<lldb::addr_t>::iterator pos;
442 pos = std::find(m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(), args_addr);
443 if (pos != m_wrapper_args_addrs.end())
444 m_wrapper_args_addrs.erase(pos);
445
Greg Clayton567e7f32011-09-22 04:58:26 +0000446 exe_ctx.GetProcessRef().DeallocateMemory(args_addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000447}
448
Greg Claytonb3448432011-03-24 21:19:54 +0000449ExecutionResults
Sean Callananc78d6482010-07-26 22:14:36 +0000450ClangFunction::ExecuteFunction(ExecutionContext &exe_ctx, Stream &errors, Value &results)
Chris Lattner24943d22010-06-08 16:52:24 +0000451{
Sean Callananc78d6482010-07-26 22:14:36 +0000452 return ExecuteFunction (exe_ctx, errors, 1000, true, results);
Chris Lattner24943d22010-06-08 16:52:24 +0000453}
454
Greg Claytonb3448432011-03-24 21:19:54 +0000455ExecutionResults
Sean Callananc78d6482010-07-26 22:14:36 +0000456ClangFunction::ExecuteFunction(ExecutionContext &exe_ctx, Stream &errors, bool stop_others, Value &results)
Chris Lattner24943d22010-06-08 16:52:24 +0000457{
Jim Inghamea9d4262010-11-05 19:25:48 +0000458 const bool try_all_threads = false;
459 const bool discard_on_error = true;
460 return ExecuteFunction (exe_ctx, NULL, errors, stop_others, NULL, try_all_threads, discard_on_error, results);
Chris Lattner24943d22010-06-08 16:52:24 +0000461}
462
Greg Claytonb3448432011-03-24 21:19:54 +0000463ExecutionResults
Chris Lattner24943d22010-06-08 16:52:24 +0000464ClangFunction::ExecuteFunction(
Sean Callananc78d6482010-07-26 22:14:36 +0000465 ExecutionContext &exe_ctx,
Chris Lattner24943d22010-06-08 16:52:24 +0000466 Stream &errors,
467 uint32_t single_thread_timeout_usec,
468 bool try_all_threads,
469 Value &results)
470{
Jim Inghamea9d4262010-11-05 19:25:48 +0000471 const bool stop_others = true;
472 const bool discard_on_error = true;
473 return ExecuteFunction (exe_ctx, NULL, errors, stop_others, single_thread_timeout_usec,
474 try_all_threads, discard_on_error, results);
Chris Lattner24943d22010-06-08 16:52:24 +0000475}
476
Sean Callanan841026f2010-07-23 00:16:21 +0000477// This is the static function
Greg Claytonb3448432011-03-24 21:19:54 +0000478ExecutionResults
Sean Callanan841026f2010-07-23 00:16:21 +0000479ClangFunction::ExecuteFunction (
Sean Callananc78d6482010-07-26 22:14:36 +0000480 ExecutionContext &exe_ctx,
Sean Callanan841026f2010-07-23 00:16:21 +0000481 lldb::addr_t function_address,
482 lldb::addr_t &void_arg,
483 bool stop_others,
484 bool try_all_threads,
Jim Inghamea9d4262010-11-05 19:25:48 +0000485 bool discard_on_error,
Sean Callanan841026f2010-07-23 00:16:21 +0000486 uint32_t single_thread_timeout_usec,
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000487 Stream &errors,
488 lldb::addr_t *this_arg)
Chris Lattner24943d22010-06-08 16:52:24 +0000489{
Greg Clayton567e7f32011-09-22 04:58:26 +0000490 lldb::ThreadPlanSP call_plan_sp (ClangFunction::GetThreadPlanToCallFunction (exe_ctx,
491 function_address,
492 void_arg,
493 errors,
494 stop_others,
495 discard_on_error,
496 this_arg));
Chris Lattner24943d22010-06-08 16:52:24 +0000497 if (call_plan_sp == NULL)
Greg Claytonb3448432011-03-24 21:19:54 +0000498 return eExecutionSetupError;
Sean Callanan841026f2010-07-23 00:16:21 +0000499
Chris Lattner24943d22010-06-08 16:52:24 +0000500 call_plan_sp->SetPrivate(true);
Jim Ingham3ae449a2010-11-17 02:32:00 +0000501
Greg Clayton567e7f32011-09-22 04:58:26 +0000502 return exe_ctx.GetProcessRef().RunThreadPlan (exe_ctx, call_plan_sp,
503 stop_others,
504 try_all_threads,
505 discard_on_error,
506 single_thread_timeout_usec,
507 errors);
Sean Callanan841026f2010-07-23 00:16:21 +0000508}
Chris Lattner24943d22010-06-08 16:52:24 +0000509
Greg Claytonb3448432011-03-24 21:19:54 +0000510ExecutionResults
Sean Callanan841026f2010-07-23 00:16:21 +0000511ClangFunction::ExecuteFunction(
Sean Callananc78d6482010-07-26 22:14:36 +0000512 ExecutionContext &exe_ctx,
Sean Callanan841026f2010-07-23 00:16:21 +0000513 lldb::addr_t *args_addr_ptr,
514 Stream &errors,
515 bool stop_others,
516 uint32_t single_thread_timeout_usec,
Jim Inghamea9d4262010-11-05 19:25:48 +0000517 bool try_all_threads,
518 bool discard_on_error,
Sean Callanan841026f2010-07-23 00:16:21 +0000519 Value &results)
520{
521 using namespace clang;
Greg Claytonb3448432011-03-24 21:19:54 +0000522 ExecutionResults return_value = eExecutionSetupError;
Sean Callanan841026f2010-07-23 00:16:21 +0000523
524 lldb::addr_t args_addr;
525
526 if (args_addr_ptr != NULL)
527 args_addr = *args_addr_ptr;
528 else
529 args_addr = LLDB_INVALID_ADDRESS;
530
531 if (CompileFunction(errors) != 0)
Greg Claytonb3448432011-03-24 21:19:54 +0000532 return eExecutionSetupError;
Sean Callanan841026f2010-07-23 00:16:21 +0000533
534 if (args_addr == LLDB_INVALID_ADDRESS)
535 {
Sean Callananc78d6482010-07-26 22:14:36 +0000536 if (!InsertFunction(exe_ctx, args_addr, errors))
Greg Claytonb3448432011-03-24 21:19:54 +0000537 return eExecutionSetupError;
Sean Callanan841026f2010-07-23 00:16:21 +0000538 }
539
Greg Claytond0882d02011-01-19 23:00:49 +0000540 return_value = ClangFunction::ExecuteFunction (exe_ctx,
541 m_jit_start_addr,
542 args_addr,
543 stop_others,
544 try_all_threads,
545 discard_on_error,
546 single_thread_timeout_usec,
547 errors);
Sean Callanan841026f2010-07-23 00:16:21 +0000548
549 if (args_addr_ptr != NULL)
550 *args_addr_ptr = args_addr;
551
Greg Claytonb3448432011-03-24 21:19:54 +0000552 if (return_value != eExecutionCompleted)
Chris Lattner24943d22010-06-08 16:52:24 +0000553 return return_value;
554
Sean Callananc78d6482010-07-26 22:14:36 +0000555 FetchFunctionResults(exe_ctx, args_addr, results);
Chris Lattner24943d22010-06-08 16:52:24 +0000556
557 if (args_addr_ptr == NULL)
Sean Callananc78d6482010-07-26 22:14:36 +0000558 DeallocateFunctionResults(exe_ctx, args_addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000559
Greg Claytonb3448432011-03-24 21:19:54 +0000560 return eExecutionCompleted;
Chris Lattner24943d22010-06-08 16:52:24 +0000561}
562
Sean Callanan65dafa82010-08-27 01:01:44 +0000563clang::ASTConsumer *
564ClangFunction::ASTTransformer (clang::ASTConsumer *passthrough)
Chris Lattner24943d22010-06-08 16:52:24 +0000565{
Sean Callanan65dafa82010-08-27 01:01:44 +0000566 return new ASTStructExtractor(passthrough, m_wrapper_struct_name.c_str(), *this);
Chris Lattner24943d22010-06-08 16:52:24 +0000567}