blob: 31b74c6e5e97ee735162dd35bb1abae78e15f90b [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"
20#include "llvm/ExecutionEngine/ExecutionEngine.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021#include "llvm/Module.h"
Chris Lattner24943d22010-06-08 16:52:24 +000022
23// Project includes
Sean Callanan65dafa82010-08-27 01:01:44 +000024#include "lldb/Expression/ASTStructExtractor.h"
25#include "lldb/Expression/ClangExpressionParser.h"
Chris Lattner24943d22010-06-08 16:52:24 +000026#include "lldb/Expression/ClangFunction.h"
27#include "lldb/Symbol/Type.h"
28#include "lldb/Core/DataExtractor.h"
Jim Ingham3ae449a2010-11-17 02:32:00 +000029#include "lldb/Core/State.h"
Chris Lattner24943d22010-06-08 16:52:24 +000030#include "lldb/Core/ValueObject.h"
31#include "lldb/Core/ValueObjectList.h"
32#include "lldb/Interpreter/CommandReturnObject.h"
33#include "lldb/Symbol/ClangASTContext.h"
34#include "lldb/Symbol/Function.h"
35#include "lldb/Target/ExecutionContext.h"
36#include "lldb/Target/Process.h"
Sean Callanan841026f2010-07-23 00:16:21 +000037#include "lldb/Target/RegisterContext.h"
Greg Clayton643ee732010-08-04 01:40:35 +000038#include "lldb/Target/StopInfo.h"
Chris Lattner24943d22010-06-08 16:52:24 +000039#include "lldb/Target/Thread.h"
40#include "lldb/Target/ThreadPlan.h"
41#include "lldb/Target/ThreadPlanCallFunction.h"
42#include "lldb/Core/Log.h"
43
44using namespace lldb_private;
Sean Callanan65dafa82010-08-27 01:01:44 +000045
Chris Lattner24943d22010-06-08 16:52:24 +000046//----------------------------------------------------------------------
47// ClangFunction constructor
48//----------------------------------------------------------------------
Greg Clayton8de27c72010-10-15 22:48:33 +000049ClangFunction::ClangFunction
50(
51 const char *target_triple,
52 ClangASTContext *ast_context,
53 void *return_qualtype,
54 const Address& functionAddress,
55 const ValueList &arg_value_list
56) :
Sean Callanan65dafa82010-08-27 01:01:44 +000057 m_target_triple (target_triple),
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{
69}
70
Greg Clayton8de27c72010-10-15 22:48:33 +000071ClangFunction::ClangFunction
72(
73 const char *target_triple,
74 Function &function,
75 ClangASTContext *ast_context,
76 const ValueList &arg_value_list
77) :
Sean Callanan65dafa82010-08-27 01:01:44 +000078 m_target_triple (target_triple),
Chris Lattner24943d22010-06-08 16:52:24 +000079 m_function_ptr (&function),
Greg Clayton54e7afa2010-07-09 20:39:50 +000080 m_function_addr (),
81 m_function_return_qual_type (),
Chris Lattner24943d22010-06-08 16:52:24 +000082 m_clang_ast_context (ast_context),
Chris Lattner24943d22010-06-08 16:52:24 +000083 m_wrapper_function_name ("__lldb_function_caller"),
84 m_wrapper_struct_name ("__lldb_caller_struct"),
Greg Clayton54e7afa2010-07-09 20:39:50 +000085 m_wrapper_args_addrs (),
Greg Clayton54e7afa2010-07-09 20:39:50 +000086 m_arg_values (arg_value_list),
Chris Lattner24943d22010-06-08 16:52:24 +000087 m_compiled (false),
88 m_JITted (false)
89{
90 m_function_addr = m_function_ptr->GetAddressRange().GetBaseAddress();
Greg Clayton462d4142010-09-29 01:12:09 +000091 m_function_return_qual_type = m_function_ptr->GetReturnType().GetClangType();
Chris Lattner24943d22010-06-08 16:52:24 +000092}
93
94//----------------------------------------------------------------------
95// Destructor
96//----------------------------------------------------------------------
97ClangFunction::~ClangFunction()
98{
99}
100
101unsigned
102ClangFunction::CompileFunction (Stream &errors)
103{
Sean Callanan65dafa82010-08-27 01:01:44 +0000104 if (m_compiled)
105 return 0;
106
Chris Lattner24943d22010-06-08 16:52:24 +0000107 // FIXME: How does clang tell us there's no return value? We need to handle that case.
108 unsigned num_errors = 0;
109
Sean Callanan65dafa82010-08-27 01:01:44 +0000110 std::string return_type_str = ClangASTContext::GetTypeName(m_function_return_qual_type);
111
112 // Cons up the function we're going to wrap our call in, then compile it...
113 // We declare the function "extern "C"" because the compiler might be in C++
114 // mode which would mangle the name and then we couldn't find it again...
115 m_wrapper_function_text.clear();
116 m_wrapper_function_text.append ("extern \"C\" void ");
117 m_wrapper_function_text.append (m_wrapper_function_name);
118 m_wrapper_function_text.append (" (void *input)\n{\n struct ");
119 m_wrapper_function_text.append (m_wrapper_struct_name);
120 m_wrapper_function_text.append (" \n {\n");
121 m_wrapper_function_text.append (" ");
122 m_wrapper_function_text.append (return_type_str);
123 m_wrapper_function_text.append (" (*fn_ptr) (");
124
125 // Get the number of arguments. If we have a function type and it is prototyped,
126 // trust that, otherwise use the values we were given.
127
128 // FIXME: This will need to be extended to handle Variadic functions. We'll need
129 // to pull the defined arguments out of the function, then add the types from the
130 // arguments list for the variable arguments.
131
132 uint32_t num_args = UINT32_MAX;
133 bool trust_function = false;
134 // GetArgumentCount returns -1 for an unprototyped function.
135 if (m_function_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +0000136 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000137 int num_func_args = m_function_ptr->GetArgumentCount();
138 if (num_func_args >= 0)
139 trust_function = true;
140 else
141 num_args = num_func_args;
142 }
Chris Lattner24943d22010-06-08 16:52:24 +0000143
Sean Callanan65dafa82010-08-27 01:01:44 +0000144 if (num_args == UINT32_MAX)
145 num_args = m_arg_values.GetSize();
Chris Lattner24943d22010-06-08 16:52:24 +0000146
Sean Callanan65dafa82010-08-27 01:01:44 +0000147 std::string args_buffer; // This one stores the definition of all the args in "struct caller".
148 std::string args_list_buffer; // This one stores the argument list called from the structure.
149 for (size_t i = 0; i < num_args; i++)
150 {
151 const char *type_string;
152 std::string type_stdstr;
Chris Lattner24943d22010-06-08 16:52:24 +0000153
Sean Callanan65dafa82010-08-27 01:01:44 +0000154 if (trust_function)
Chris Lattner24943d22010-06-08 16:52:24 +0000155 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000156 type_string = m_function_ptr->GetArgumentTypeAtIndex(i).GetName().AsCString();
Chris Lattner24943d22010-06-08 16:52:24 +0000157 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000158 else
Chris Lattner24943d22010-06-08 16:52:24 +0000159 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000160 Value *arg_value = m_arg_values.GetValueAtIndex(i);
Greg Clayton462d4142010-09-29 01:12:09 +0000161 void *clang_qual_type = arg_value->GetClangType ();
Sean Callanan65dafa82010-08-27 01:01:44 +0000162 if (clang_qual_type != NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000163 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000164 type_stdstr = ClangASTContext::GetTypeName(clang_qual_type);
165 type_string = type_stdstr.c_str();
Chris Lattner24943d22010-06-08 16:52:24 +0000166 }
167 else
Sean Callanan65dafa82010-08-27 01:01:44 +0000168 {
169 errors.Printf("Could not determine type of input value %d.", i);
Chris Lattner24943d22010-06-08 16:52:24 +0000170 return 1;
171 }
Chris Lattner24943d22010-06-08 16:52:24 +0000172 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000173
174 m_wrapper_function_text.append (type_string);
175 if (i < num_args - 1)
176 m_wrapper_function_text.append (", ");
177
178 char arg_buf[32];
179 args_buffer.append (" ");
180 args_buffer.append (type_string);
181 snprintf(arg_buf, 31, "arg_%zd", i);
182 args_buffer.push_back (' ');
183 args_buffer.append (arg_buf);
184 args_buffer.append (";\n");
185
186 args_list_buffer.append ("__lldb_fn_data->");
187 args_list_buffer.append (arg_buf);
188 if (i < num_args - 1)
189 args_list_buffer.append (", ");
190
Chris Lattner24943d22010-06-08 16:52:24 +0000191 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000192 m_wrapper_function_text.append (");\n"); // Close off the function calling prototype.
193
194 m_wrapper_function_text.append (args_buffer);
195
196 m_wrapper_function_text.append (" ");
197 m_wrapper_function_text.append (return_type_str);
198 m_wrapper_function_text.append (" return_value;");
199 m_wrapper_function_text.append ("\n };\n struct ");
200 m_wrapper_function_text.append (m_wrapper_struct_name);
201 m_wrapper_function_text.append ("* __lldb_fn_data = (struct ");
202 m_wrapper_function_text.append (m_wrapper_struct_name);
203 m_wrapper_function_text.append (" *) input;\n");
204
205 m_wrapper_function_text.append (" __lldb_fn_data->return_value = __lldb_fn_data->fn_ptr (");
206 m_wrapper_function_text.append (args_list_buffer);
207 m_wrapper_function_text.append (");\n}\n");
208
Jim Ingham7812e012011-01-18 22:20:08 +0000209 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan65dafa82010-08-27 01:01:44 +0000210 if (log)
211 log->Printf ("Expression: \n\n%s\n\n", m_wrapper_function_text.c_str());
212
213 // Okay, now compile this expression
214
Sean Callananc7674af2011-01-17 23:42:46 +0000215 m_parser.reset(new ClangExpressionParser(m_target_triple.c_str(), NULL, *this));
Sean Callanan65dafa82010-08-27 01:01:44 +0000216
217 num_errors = m_parser->Parse (errors);
218
219 m_compiled = (num_errors == 0);
220
221 if (!m_compiled)
222 return num_errors;
Chris Lattner24943d22010-06-08 16:52:24 +0000223
224 return num_errors;
225}
226
227bool
Sean Callananc78d6482010-07-26 22:14:36 +0000228ClangFunction::WriteFunctionWrapper (ExecutionContext &exe_ctx, Stream &errors)
Chris Lattner24943d22010-06-08 16:52:24 +0000229{
Sean Callananc78d6482010-07-26 22:14:36 +0000230 Process *process = exe_ctx.process;
Chris Lattner24943d22010-06-08 16:52:24 +0000231
Sean Callanan65dafa82010-08-27 01:01:44 +0000232 if (!process)
233 return false;
234
235 if (!m_compiled)
Chris Lattner24943d22010-06-08 16:52:24 +0000236 return false;
237
Sean Callanan65dafa82010-08-27 01:01:44 +0000238 if (m_JITted)
239 return true;
240
Greg Claytond0882d02011-01-19 23:00:49 +0000241 Error jit_error (m_parser->MakeJIT (m_jit_alloc, m_jit_start_addr, m_jit_end_addr, exe_ctx));
Sean Callanan65dafa82010-08-27 01:01:44 +0000242
243 if (!jit_error.Success())
Chris Lattner24943d22010-06-08 16:52:24 +0000244 return false;
Greg Claytond0882d02011-01-19 23:00:49 +0000245 if (exe_ctx.process && m_jit_alloc != LLDB_INVALID_ADDRESS)
246 m_jit_process_sp = exe_ctx.process->GetSP();
Chris Lattner24943d22010-06-08 16:52:24 +0000247
248 return true;
249}
250
251bool
Sean Callananc78d6482010-07-26 22:14:36 +0000252ClangFunction::WriteFunctionArguments (ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref, Stream &errors)
Chris Lattner24943d22010-06-08 16:52:24 +0000253{
Sean Callananc78d6482010-07-26 22:14:36 +0000254 return WriteFunctionArguments(exe_ctx, args_addr_ref, m_function_addr, m_arg_values, errors);
Chris Lattner24943d22010-06-08 16:52:24 +0000255}
256
257// FIXME: Assure that the ValueList we were passed in is consistent with the one that defined this function.
258
259bool
Jim Ingham681778e2010-09-10 23:07:48 +0000260ClangFunction::WriteFunctionArguments (ExecutionContext &exe_ctx,
261 lldb::addr_t &args_addr_ref,
262 Address function_address,
263 ValueList &arg_values,
264 Stream &errors)
Chris Lattner24943d22010-06-08 16:52:24 +0000265{
Sean Callanan65dafa82010-08-27 01:01:44 +0000266 // All the information to reconstruct the struct is provided by the
267 // StructExtractor.
268 if (!m_struct_valid)
269 {
270 errors.Printf("Argument information was not correctly parsed, so the function cannot be called.");
271 return false;
272 }
273
Chris Lattner24943d22010-06-08 16:52:24 +0000274 Error error;
275 using namespace clang;
Greg Clayton427f2902010-12-14 02:59:59 +0000276 lldb::ExecutionResults return_value = lldb::eExecutionSetupError;
Chris Lattner24943d22010-06-08 16:52:24 +0000277
Sean Callananc78d6482010-07-26 22:14:36 +0000278 Process *process = exe_ctx.process;
Chris Lattner24943d22010-06-08 16:52:24 +0000279
280 if (process == NULL)
281 return return_value;
Sean Callanan65dafa82010-08-27 01:01:44 +0000282
Chris Lattner24943d22010-06-08 16:52:24 +0000283 if (args_addr_ref == LLDB_INVALID_ADDRESS)
284 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000285 args_addr_ref = process->AllocateMemory(m_struct_size, lldb::ePermissionsReadable|lldb::ePermissionsWritable, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000286 if (args_addr_ref == LLDB_INVALID_ADDRESS)
287 return false;
288 m_wrapper_args_addrs.push_back (args_addr_ref);
289 }
290 else
291 {
292 // Make sure this is an address that we've already handed out.
293 if (find (m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(), args_addr_ref) == m_wrapper_args_addrs.end())
294 {
295 return false;
296 }
297 }
298
299 // FIXME: This is fake, and just assumes that it matches that architecture.
300 // Make a data extractor and put the address into the right byte order & size.
301
Greg Claytoneea26402010-09-14 23:36:40 +0000302 uint64_t fun_addr = function_address.GetLoadAddress(exe_ctx.target);
Sean Callanan65dafa82010-08-27 01:01:44 +0000303 int first_offset = m_member_offsets[0];
Chris Lattner24943d22010-06-08 16:52:24 +0000304 process->WriteMemory(args_addr_ref + first_offset, &fun_addr, 8, error);
305
306 // FIXME: We will need to extend this for Variadic functions.
307
308 Error value_error;
309
310 size_t num_args = arg_values.GetSize();
311 if (num_args != m_arg_values.GetSize())
312 {
313 errors.Printf ("Wrong number of arguments - was: %d should be: %d", num_args, m_arg_values.GetSize());
314 return false;
315 }
316
Greg Clayton54e7afa2010-07-09 20:39:50 +0000317 for (size_t i = 0; i < num_args; i++)
Chris Lattner24943d22010-06-08 16:52:24 +0000318 {
319 // FIXME: We should sanity check sizes.
320
Sean Callanan65dafa82010-08-27 01:01:44 +0000321 int offset = m_member_offsets[i+1]; // Clang sizes are in bytes.
Chris Lattner24943d22010-06-08 16:52:24 +0000322 Value *arg_value = arg_values.GetValueAtIndex(i);
323
324 // FIXME: For now just do scalars:
325
326 // Special case: if it's a pointer, don't do anything (the ABI supports passing cstrings)
327
328 if (arg_value->GetValueType() == Value::eValueTypeHostAddress &&
Greg Clayton6916e352010-11-13 03:52:47 +0000329 arg_value->GetContextType() == Value::eContextTypeClangType &&
Greg Clayton462d4142010-09-29 01:12:09 +0000330 ClangASTContext::IsPointerType(arg_value->GetClangType()))
Chris Lattner24943d22010-06-08 16:52:24 +0000331 continue;
332
Sean Callananc78d6482010-07-26 22:14:36 +0000333 const Scalar &arg_scalar = arg_value->ResolveValue(&exe_ctx, m_clang_ast_context->getASTContext());
Chris Lattner24943d22010-06-08 16:52:24 +0000334
335 int byte_size = arg_scalar.GetByteSize();
336 std::vector<uint8_t> buffer;
337 buffer.resize(byte_size);
338 DataExtractor value_data;
339 arg_scalar.GetData (value_data);
Greg Clayton53d68e72010-07-20 22:52:08 +0000340 value_data.ExtractBytes(0, byte_size, process->GetByteOrder(), &buffer.front());
341 process->WriteMemory(args_addr_ref + offset, &buffer.front(), byte_size, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000342 }
343
344 return true;
345}
346
347bool
Sean Callananc78d6482010-07-26 22:14:36 +0000348ClangFunction::InsertFunction (ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref, Stream &errors)
Chris Lattner24943d22010-06-08 16:52:24 +0000349{
350 using namespace clang;
351
352 if (CompileFunction(errors) != 0)
353 return false;
Sean Callananc78d6482010-07-26 22:14:36 +0000354 if (!WriteFunctionWrapper(exe_ctx, errors))
Chris Lattner24943d22010-06-08 16:52:24 +0000355 return false;
Sean Callananc78d6482010-07-26 22:14:36 +0000356 if (!WriteFunctionArguments(exe_ctx, args_addr_ref, errors))
Chris Lattner24943d22010-06-08 16:52:24 +0000357 return false;
358
Greg Claytone005f2c2010-11-06 01:53:30 +0000359 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000360 if (log)
Greg Claytond0882d02011-01-19 23:00:49 +0000361 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 +0000362
363 return true;
364}
365
366ThreadPlan *
Jim Inghamea9d4262010-11-05 19:25:48 +0000367ClangFunction::GetThreadPlanToCallFunction (ExecutionContext &exe_ctx,
368 lldb::addr_t func_addr,
369 lldb::addr_t &args_addr,
370 Stream &errors,
371 bool stop_others,
372 bool discard_on_error,
Sean Callanan3aa7da52010-12-13 22:46:15 +0000373 lldb::addr_t *this_arg,
374 lldb::addr_t *cmd_arg)
Chris Lattner24943d22010-06-08 16:52:24 +0000375{
376 // FIXME: Use the errors Stream for better error reporting.
377
Sean Callananc78d6482010-07-26 22:14:36 +0000378 Process *process = exe_ctx.process;
Chris Lattner24943d22010-06-08 16:52:24 +0000379
380 if (process == NULL)
381 {
382 errors.Printf("Can't call a function without a process.");
383 return NULL;
384 }
385
386 // Okay, now run the function:
387
Sean Callanan841026f2010-07-23 00:16:21 +0000388 Address wrapper_address (NULL, func_addr);
Sean Callananc78d6482010-07-26 22:14:36 +0000389 ThreadPlan *new_plan = new ThreadPlanCallFunction (*exe_ctx.thread,
Sean Callanan3aa7da52010-12-13 22:46:15 +0000390 wrapper_address,
391 args_addr,
392 stop_others,
393 discard_on_error,
394 this_arg,
395 cmd_arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000396 return new_plan;
397}
398
399bool
Sean Callananc78d6482010-07-26 22:14:36 +0000400ClangFunction::FetchFunctionResults (ExecutionContext &exe_ctx, lldb::addr_t args_addr, Value &ret_value)
Chris Lattner24943d22010-06-08 16:52:24 +0000401{
402 // Read the return value - it is the last field in the struct:
403 // FIXME: How does clang tell us there's no return value? We need to handle that case.
404
405 std::vector<uint8_t> data_buffer;
406 data_buffer.resize(m_return_size);
Sean Callananc78d6482010-07-26 22:14:36 +0000407 Process *process = exe_ctx.process;
Chris Lattner24943d22010-06-08 16:52:24 +0000408 Error error;
Sean Callanan65dafa82010-08-27 01:01:44 +0000409 size_t bytes_read = process->ReadMemory(args_addr + m_return_offset, &data_buffer.front(), m_return_size, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000410
411 if (bytes_read == 0)
412 {
413 return false;
414 }
415
416 if (bytes_read < m_return_size)
417 return false;
418
Greg Clayton53d68e72010-07-20 22:52:08 +0000419 DataExtractor data(&data_buffer.front(), m_return_size, process->GetByteOrder(), process->GetAddressByteSize());
Chris Lattner24943d22010-06-08 16:52:24 +0000420 // FIXME: Assuming an integer scalar for now:
421
422 uint32_t offset = 0;
423 uint64_t return_integer = data.GetMaxU64(&offset, m_return_size);
424
Greg Clayton6916e352010-11-13 03:52:47 +0000425 ret_value.SetContext (Value::eContextTypeClangType, m_function_return_qual_type);
Chris Lattner24943d22010-06-08 16:52:24 +0000426 ret_value.SetValueType(Value::eValueTypeScalar);
427 ret_value.GetScalar() = return_integer;
428 return true;
429}
430
431void
Sean Callananc78d6482010-07-26 22:14:36 +0000432ClangFunction::DeallocateFunctionResults (ExecutionContext &exe_ctx, lldb::addr_t args_addr)
Chris Lattner24943d22010-06-08 16:52:24 +0000433{
434 std::list<lldb::addr_t>::iterator pos;
435 pos = std::find(m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(), args_addr);
436 if (pos != m_wrapper_args_addrs.end())
437 m_wrapper_args_addrs.erase(pos);
438
Sean Callananc78d6482010-07-26 22:14:36 +0000439 exe_ctx.process->DeallocateMemory(args_addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000440}
441
Greg Clayton427f2902010-12-14 02:59:59 +0000442lldb::ExecutionResults
Sean Callananc78d6482010-07-26 22:14:36 +0000443ClangFunction::ExecuteFunction(ExecutionContext &exe_ctx, Stream &errors, Value &results)
Chris Lattner24943d22010-06-08 16:52:24 +0000444{
Sean Callananc78d6482010-07-26 22:14:36 +0000445 return ExecuteFunction (exe_ctx, errors, 1000, true, results);
Chris Lattner24943d22010-06-08 16:52:24 +0000446}
447
Greg Clayton427f2902010-12-14 02:59:59 +0000448lldb::ExecutionResults
Sean Callananc78d6482010-07-26 22:14:36 +0000449ClangFunction::ExecuteFunction(ExecutionContext &exe_ctx, Stream &errors, bool stop_others, Value &results)
Chris Lattner24943d22010-06-08 16:52:24 +0000450{
Jim Inghamea9d4262010-11-05 19:25:48 +0000451 const bool try_all_threads = false;
452 const bool discard_on_error = true;
453 return ExecuteFunction (exe_ctx, NULL, errors, stop_others, NULL, try_all_threads, discard_on_error, results);
Chris Lattner24943d22010-06-08 16:52:24 +0000454}
455
Greg Clayton427f2902010-12-14 02:59:59 +0000456lldb::ExecutionResults
Chris Lattner24943d22010-06-08 16:52:24 +0000457ClangFunction::ExecuteFunction(
Sean Callananc78d6482010-07-26 22:14:36 +0000458 ExecutionContext &exe_ctx,
Chris Lattner24943d22010-06-08 16:52:24 +0000459 Stream &errors,
460 uint32_t single_thread_timeout_usec,
461 bool try_all_threads,
462 Value &results)
463{
Jim Inghamea9d4262010-11-05 19:25:48 +0000464 const bool stop_others = true;
465 const bool discard_on_error = true;
466 return ExecuteFunction (exe_ctx, NULL, errors, stop_others, single_thread_timeout_usec,
467 try_all_threads, discard_on_error, results);
Chris Lattner24943d22010-06-08 16:52:24 +0000468}
469
Sean Callanan841026f2010-07-23 00:16:21 +0000470// This is the static function
Greg Clayton427f2902010-12-14 02:59:59 +0000471lldb::ExecutionResults
Sean Callanan841026f2010-07-23 00:16:21 +0000472ClangFunction::ExecuteFunction (
Sean Callananc78d6482010-07-26 22:14:36 +0000473 ExecutionContext &exe_ctx,
Sean Callanan841026f2010-07-23 00:16:21 +0000474 lldb::addr_t function_address,
475 lldb::addr_t &void_arg,
476 bool stop_others,
477 bool try_all_threads,
Jim Inghamea9d4262010-11-05 19:25:48 +0000478 bool discard_on_error,
Sean Callanan841026f2010-07-23 00:16:21 +0000479 uint32_t single_thread_timeout_usec,
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000480 Stream &errors,
481 lldb::addr_t *this_arg)
Chris Lattner24943d22010-06-08 16:52:24 +0000482{
Jim Inghamea9d4262010-11-05 19:25:48 +0000483 lldb::ThreadPlanSP call_plan_sp(ClangFunction::GetThreadPlanToCallFunction(exe_ctx, function_address, void_arg,
484 errors, stop_others, discard_on_error,
485 this_arg));
Chris Lattner24943d22010-06-08 16:52:24 +0000486 if (call_plan_sp == NULL)
Greg Clayton427f2902010-12-14 02:59:59 +0000487 return lldb::eExecutionSetupError;
Sean Callanan841026f2010-07-23 00:16:21 +0000488
Chris Lattner24943d22010-06-08 16:52:24 +0000489 call_plan_sp->SetPrivate(true);
Jim Ingham3ae449a2010-11-17 02:32:00 +0000490
Jim Ingham360f53f2010-11-30 02:22:11 +0000491 return exe_ctx.process->RunThreadPlan (exe_ctx, call_plan_sp, stop_others, try_all_threads, discard_on_error,
492 single_thread_timeout_usec, errors);
Sean Callanan841026f2010-07-23 00:16:21 +0000493}
Chris Lattner24943d22010-06-08 16:52:24 +0000494
Greg Clayton427f2902010-12-14 02:59:59 +0000495lldb::ExecutionResults
Sean Callanan841026f2010-07-23 00:16:21 +0000496ClangFunction::ExecuteFunction(
Sean Callananc78d6482010-07-26 22:14:36 +0000497 ExecutionContext &exe_ctx,
Sean Callanan841026f2010-07-23 00:16:21 +0000498 lldb::addr_t *args_addr_ptr,
499 Stream &errors,
500 bool stop_others,
501 uint32_t single_thread_timeout_usec,
Jim Inghamea9d4262010-11-05 19:25:48 +0000502 bool try_all_threads,
503 bool discard_on_error,
Sean Callanan841026f2010-07-23 00:16:21 +0000504 Value &results)
505{
506 using namespace clang;
Greg Clayton427f2902010-12-14 02:59:59 +0000507 lldb::ExecutionResults return_value = lldb::eExecutionSetupError;
Sean Callanan841026f2010-07-23 00:16:21 +0000508
509 lldb::addr_t args_addr;
510
511 if (args_addr_ptr != NULL)
512 args_addr = *args_addr_ptr;
513 else
514 args_addr = LLDB_INVALID_ADDRESS;
515
516 if (CompileFunction(errors) != 0)
Greg Clayton427f2902010-12-14 02:59:59 +0000517 return lldb::eExecutionSetupError;
Sean Callanan841026f2010-07-23 00:16:21 +0000518
519 if (args_addr == LLDB_INVALID_ADDRESS)
520 {
Sean Callananc78d6482010-07-26 22:14:36 +0000521 if (!InsertFunction(exe_ctx, args_addr, errors))
Greg Clayton427f2902010-12-14 02:59:59 +0000522 return lldb::eExecutionSetupError;
Sean Callanan841026f2010-07-23 00:16:21 +0000523 }
524
Greg Claytond0882d02011-01-19 23:00:49 +0000525 return_value = ClangFunction::ExecuteFunction (exe_ctx,
526 m_jit_start_addr,
527 args_addr,
528 stop_others,
529 try_all_threads,
530 discard_on_error,
531 single_thread_timeout_usec,
532 errors);
Sean Callanan841026f2010-07-23 00:16:21 +0000533
534 if (args_addr_ptr != NULL)
535 *args_addr_ptr = args_addr;
536
Greg Clayton427f2902010-12-14 02:59:59 +0000537 if (return_value != lldb::eExecutionCompleted)
Chris Lattner24943d22010-06-08 16:52:24 +0000538 return return_value;
539
Sean Callananc78d6482010-07-26 22:14:36 +0000540 FetchFunctionResults(exe_ctx, args_addr, results);
Chris Lattner24943d22010-06-08 16:52:24 +0000541
542 if (args_addr_ptr == NULL)
Sean Callananc78d6482010-07-26 22:14:36 +0000543 DeallocateFunctionResults(exe_ctx, args_addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000544
Greg Clayton427f2902010-12-14 02:59:59 +0000545 return lldb::eExecutionCompleted;
Chris Lattner24943d22010-06-08 16:52:24 +0000546}
547
Sean Callanan65dafa82010-08-27 01:01:44 +0000548clang::ASTConsumer *
549ClangFunction::ASTTransformer (clang::ASTConsumer *passthrough)
Chris Lattner24943d22010-06-08 16:52:24 +0000550{
Sean Callanan65dafa82010-08-27 01:01:44 +0000551 return new ASTStructExtractor(passthrough, m_wrapper_struct_name.c_str(), *this);
Chris Lattner24943d22010-06-08 16:52:24 +0000552}