blob: e7b6f77ea5bdaeb99006dc57596596d0d2843f3f [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_function_addr (),
65 m_wrapper_args_addrs (),
Greg Clayton54e7afa2010-07-09 20:39:50 +000066 m_arg_values (arg_value_list),
Chris Lattner24943d22010-06-08 16:52:24 +000067 m_compiled (false),
68 m_JITted (false)
69{
70}
71
Greg Clayton8de27c72010-10-15 22:48:33 +000072ClangFunction::ClangFunction
73(
74 const char *target_triple,
75 Function &function,
76 ClangASTContext *ast_context,
77 const ValueList &arg_value_list
78) :
Sean Callanan65dafa82010-08-27 01:01:44 +000079 m_target_triple (target_triple),
Chris Lattner24943d22010-06-08 16:52:24 +000080 m_function_ptr (&function),
Greg Clayton54e7afa2010-07-09 20:39:50 +000081 m_function_addr (),
82 m_function_return_qual_type (),
Chris Lattner24943d22010-06-08 16:52:24 +000083 m_clang_ast_context (ast_context),
Chris Lattner24943d22010-06-08 16:52:24 +000084 m_wrapper_function_name ("__lldb_function_caller"),
85 m_wrapper_struct_name ("__lldb_caller_struct"),
Greg Clayton54e7afa2010-07-09 20:39:50 +000086 m_wrapper_function_addr (),
87 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{
92 m_function_addr = m_function_ptr->GetAddressRange().GetBaseAddress();
Greg Clayton462d4142010-09-29 01:12:09 +000093 m_function_return_qual_type = m_function_ptr->GetReturnType().GetClangType();
Chris Lattner24943d22010-06-08 16:52:24 +000094}
95
96//----------------------------------------------------------------------
97// Destructor
98//----------------------------------------------------------------------
99ClangFunction::~ClangFunction()
100{
101}
102
103unsigned
104ClangFunction::CompileFunction (Stream &errors)
105{
Sean Callanan65dafa82010-08-27 01:01:44 +0000106 if (m_compiled)
107 return 0;
108
Chris Lattner24943d22010-06-08 16:52:24 +0000109 // FIXME: How does clang tell us there's no return value? We need to handle that case.
110 unsigned num_errors = 0;
111
Sean Callanan65dafa82010-08-27 01:01:44 +0000112 std::string return_type_str = ClangASTContext::GetTypeName(m_function_return_qual_type);
113
114 // Cons up the function we're going to wrap our call in, then compile it...
115 // We declare the function "extern "C"" because the compiler might be in C++
116 // mode which would mangle the name and then we couldn't find it again...
117 m_wrapper_function_text.clear();
118 m_wrapper_function_text.append ("extern \"C\" void ");
119 m_wrapper_function_text.append (m_wrapper_function_name);
120 m_wrapper_function_text.append (" (void *input)\n{\n struct ");
121 m_wrapper_function_text.append (m_wrapper_struct_name);
122 m_wrapper_function_text.append (" \n {\n");
123 m_wrapper_function_text.append (" ");
124 m_wrapper_function_text.append (return_type_str);
125 m_wrapper_function_text.append (" (*fn_ptr) (");
126
127 // Get the number of arguments. If we have a function type and it is prototyped,
128 // trust that, otherwise use the values we were given.
129
130 // FIXME: This will need to be extended to handle Variadic functions. We'll need
131 // to pull the defined arguments out of the function, then add the types from the
132 // arguments list for the variable arguments.
133
134 uint32_t num_args = UINT32_MAX;
135 bool trust_function = false;
136 // GetArgumentCount returns -1 for an unprototyped function.
137 if (m_function_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +0000138 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000139 int num_func_args = m_function_ptr->GetArgumentCount();
140 if (num_func_args >= 0)
141 trust_function = true;
142 else
143 num_args = num_func_args;
144 }
Chris Lattner24943d22010-06-08 16:52:24 +0000145
Sean Callanan65dafa82010-08-27 01:01:44 +0000146 if (num_args == UINT32_MAX)
147 num_args = m_arg_values.GetSize();
Chris Lattner24943d22010-06-08 16:52:24 +0000148
Sean Callanan65dafa82010-08-27 01:01:44 +0000149 std::string args_buffer; // This one stores the definition of all the args in "struct caller".
150 std::string args_list_buffer; // This one stores the argument list called from the structure.
151 for (size_t i = 0; i < num_args; i++)
152 {
153 const char *type_string;
154 std::string type_stdstr;
Chris Lattner24943d22010-06-08 16:52:24 +0000155
Sean Callanan65dafa82010-08-27 01:01:44 +0000156 if (trust_function)
Chris Lattner24943d22010-06-08 16:52:24 +0000157 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000158 type_string = m_function_ptr->GetArgumentTypeAtIndex(i).GetName().AsCString();
Chris Lattner24943d22010-06-08 16:52:24 +0000159 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000160 else
Chris Lattner24943d22010-06-08 16:52:24 +0000161 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000162 Value *arg_value = m_arg_values.GetValueAtIndex(i);
Greg Clayton462d4142010-09-29 01:12:09 +0000163 void *clang_qual_type = arg_value->GetClangType ();
Sean Callanan65dafa82010-08-27 01:01:44 +0000164 if (clang_qual_type != NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000165 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000166 type_stdstr = ClangASTContext::GetTypeName(clang_qual_type);
167 type_string = type_stdstr.c_str();
Chris Lattner24943d22010-06-08 16:52:24 +0000168 }
169 else
Sean Callanan65dafa82010-08-27 01:01:44 +0000170 {
171 errors.Printf("Could not determine type of input value %d.", i);
Chris Lattner24943d22010-06-08 16:52:24 +0000172 return 1;
173 }
Chris Lattner24943d22010-06-08 16:52:24 +0000174 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000175
176 m_wrapper_function_text.append (type_string);
177 if (i < num_args - 1)
178 m_wrapper_function_text.append (", ");
179
180 char arg_buf[32];
181 args_buffer.append (" ");
182 args_buffer.append (type_string);
183 snprintf(arg_buf, 31, "arg_%zd", i);
184 args_buffer.push_back (' ');
185 args_buffer.append (arg_buf);
186 args_buffer.append (";\n");
187
188 args_list_buffer.append ("__lldb_fn_data->");
189 args_list_buffer.append (arg_buf);
190 if (i < num_args - 1)
191 args_list_buffer.append (", ");
192
Chris Lattner24943d22010-06-08 16:52:24 +0000193 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000194 m_wrapper_function_text.append (");\n"); // Close off the function calling prototype.
195
196 m_wrapper_function_text.append (args_buffer);
197
198 m_wrapper_function_text.append (" ");
199 m_wrapper_function_text.append (return_type_str);
200 m_wrapper_function_text.append (" return_value;");
201 m_wrapper_function_text.append ("\n };\n struct ");
202 m_wrapper_function_text.append (m_wrapper_struct_name);
203 m_wrapper_function_text.append ("* __lldb_fn_data = (struct ");
204 m_wrapper_function_text.append (m_wrapper_struct_name);
205 m_wrapper_function_text.append (" *) input;\n");
206
207 m_wrapper_function_text.append (" __lldb_fn_data->return_value = __lldb_fn_data->fn_ptr (");
208 m_wrapper_function_text.append (args_list_buffer);
209 m_wrapper_function_text.append (");\n}\n");
210
Jim Ingham7812e012011-01-18 22:20:08 +0000211 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan65dafa82010-08-27 01:01:44 +0000212 if (log)
213 log->Printf ("Expression: \n\n%s\n\n", m_wrapper_function_text.c_str());
214
215 // Okay, now compile this expression
216
Sean Callananc7674af2011-01-17 23:42:46 +0000217 m_parser.reset(new ClangExpressionParser(m_target_triple.c_str(), NULL, *this));
Sean Callanan65dafa82010-08-27 01:01:44 +0000218
219 num_errors = m_parser->Parse (errors);
220
221 m_compiled = (num_errors == 0);
222
223 if (!m_compiled)
224 return num_errors;
Chris Lattner24943d22010-06-08 16:52:24 +0000225
226 return num_errors;
227}
228
229bool
Sean Callananc78d6482010-07-26 22:14:36 +0000230ClangFunction::WriteFunctionWrapper (ExecutionContext &exe_ctx, Stream &errors)
Chris Lattner24943d22010-06-08 16:52:24 +0000231{
Sean Callananc78d6482010-07-26 22:14:36 +0000232 Process *process = exe_ctx.process;
Chris Lattner24943d22010-06-08 16:52:24 +0000233
Sean Callanan65dafa82010-08-27 01:01:44 +0000234 if (!process)
235 return false;
236
237 if (!m_compiled)
Chris Lattner24943d22010-06-08 16:52:24 +0000238 return false;
239
Sean Callanan65dafa82010-08-27 01:01:44 +0000240 if (m_JITted)
241 return true;
242
Sean Callanan830a9032010-08-27 23:31:21 +0000243 lldb::addr_t wrapper_function_end;
244
245 Error jit_error = m_parser->MakeJIT(m_wrapper_function_addr, wrapper_function_end, exe_ctx);
Sean Callanan65dafa82010-08-27 01:01:44 +0000246
247 if (!jit_error.Success())
Chris Lattner24943d22010-06-08 16:52:24 +0000248 return false;
249
250 return true;
251}
252
253bool
Sean Callananc78d6482010-07-26 22:14:36 +0000254ClangFunction::WriteFunctionArguments (ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref, Stream &errors)
Chris Lattner24943d22010-06-08 16:52:24 +0000255{
Sean Callananc78d6482010-07-26 22:14:36 +0000256 return WriteFunctionArguments(exe_ctx, args_addr_ref, m_function_addr, m_arg_values, errors);
Chris Lattner24943d22010-06-08 16:52:24 +0000257}
258
259// FIXME: Assure that the ValueList we were passed in is consistent with the one that defined this function.
260
261bool
Jim Ingham681778e2010-09-10 23:07:48 +0000262ClangFunction::WriteFunctionArguments (ExecutionContext &exe_ctx,
263 lldb::addr_t &args_addr_ref,
264 Address function_address,
265 ValueList &arg_values,
266 Stream &errors)
Chris Lattner24943d22010-06-08 16:52:24 +0000267{
Sean Callanan65dafa82010-08-27 01:01:44 +0000268 // All the information to reconstruct the struct is provided by the
269 // StructExtractor.
270 if (!m_struct_valid)
271 {
272 errors.Printf("Argument information was not correctly parsed, so the function cannot be called.");
273 return false;
274 }
275
Chris Lattner24943d22010-06-08 16:52:24 +0000276 Error error;
277 using namespace clang;
Greg Clayton427f2902010-12-14 02:59:59 +0000278 lldb::ExecutionResults return_value = lldb::eExecutionSetupError;
Chris Lattner24943d22010-06-08 16:52:24 +0000279
Sean Callananc78d6482010-07-26 22:14:36 +0000280 Process *process = exe_ctx.process;
Chris Lattner24943d22010-06-08 16:52:24 +0000281
282 if (process == NULL)
283 return return_value;
Sean Callanan65dafa82010-08-27 01:01:44 +0000284
Chris Lattner24943d22010-06-08 16:52:24 +0000285 if (args_addr_ref == LLDB_INVALID_ADDRESS)
286 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000287 args_addr_ref = process->AllocateMemory(m_struct_size, lldb::ePermissionsReadable|lldb::ePermissionsWritable, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000288 if (args_addr_ref == LLDB_INVALID_ADDRESS)
289 return false;
290 m_wrapper_args_addrs.push_back (args_addr_ref);
291 }
292 else
293 {
294 // Make sure this is an address that we've already handed out.
295 if (find (m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(), args_addr_ref) == m_wrapper_args_addrs.end())
296 {
297 return false;
298 }
299 }
300
301 // FIXME: This is fake, and just assumes that it matches that architecture.
302 // Make a data extractor and put the address into the right byte order & size.
303
Greg Claytoneea26402010-09-14 23:36:40 +0000304 uint64_t fun_addr = function_address.GetLoadAddress(exe_ctx.target);
Sean Callanan65dafa82010-08-27 01:01:44 +0000305 int first_offset = m_member_offsets[0];
Chris Lattner24943d22010-06-08 16:52:24 +0000306 process->WriteMemory(args_addr_ref + first_offset, &fun_addr, 8, error);
307
308 // FIXME: We will need to extend this for Variadic functions.
309
310 Error value_error;
311
312 size_t num_args = arg_values.GetSize();
313 if (num_args != m_arg_values.GetSize())
314 {
315 errors.Printf ("Wrong number of arguments - was: %d should be: %d", num_args, m_arg_values.GetSize());
316 return false;
317 }
318
Greg Clayton54e7afa2010-07-09 20:39:50 +0000319 for (size_t i = 0; i < num_args; i++)
Chris Lattner24943d22010-06-08 16:52:24 +0000320 {
321 // FIXME: We should sanity check sizes.
322
Sean Callanan65dafa82010-08-27 01:01:44 +0000323 int offset = m_member_offsets[i+1]; // Clang sizes are in bytes.
Chris Lattner24943d22010-06-08 16:52:24 +0000324 Value *arg_value = arg_values.GetValueAtIndex(i);
325
326 // FIXME: For now just do scalars:
327
328 // Special case: if it's a pointer, don't do anything (the ABI supports passing cstrings)
329
330 if (arg_value->GetValueType() == Value::eValueTypeHostAddress &&
Greg Clayton6916e352010-11-13 03:52:47 +0000331 arg_value->GetContextType() == Value::eContextTypeClangType &&
Greg Clayton462d4142010-09-29 01:12:09 +0000332 ClangASTContext::IsPointerType(arg_value->GetClangType()))
Chris Lattner24943d22010-06-08 16:52:24 +0000333 continue;
334
Sean Callananc78d6482010-07-26 22:14:36 +0000335 const Scalar &arg_scalar = arg_value->ResolveValue(&exe_ctx, m_clang_ast_context->getASTContext());
Chris Lattner24943d22010-06-08 16:52:24 +0000336
337 int byte_size = arg_scalar.GetByteSize();
338 std::vector<uint8_t> buffer;
339 buffer.resize(byte_size);
340 DataExtractor value_data;
341 arg_scalar.GetData (value_data);
Greg Clayton53d68e72010-07-20 22:52:08 +0000342 value_data.ExtractBytes(0, byte_size, process->GetByteOrder(), &buffer.front());
343 process->WriteMemory(args_addr_ref + offset, &buffer.front(), byte_size, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000344 }
345
346 return true;
347}
348
349bool
Sean Callananc78d6482010-07-26 22:14:36 +0000350ClangFunction::InsertFunction (ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref, Stream &errors)
Chris Lattner24943d22010-06-08 16:52:24 +0000351{
352 using namespace clang;
353
354 if (CompileFunction(errors) != 0)
355 return false;
Sean Callananc78d6482010-07-26 22:14:36 +0000356 if (!WriteFunctionWrapper(exe_ctx, errors))
Chris Lattner24943d22010-06-08 16:52:24 +0000357 return false;
Sean Callananc78d6482010-07-26 22:14:36 +0000358 if (!WriteFunctionArguments(exe_ctx, args_addr_ref, errors))
Chris Lattner24943d22010-06-08 16:52:24 +0000359 return false;
360
Greg Claytone005f2c2010-11-06 01:53:30 +0000361 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000362 if (log)
Greg Clayton54e7afa2010-07-09 20:39:50 +0000363 log->Printf ("Call Address: 0x%llx Struct Address: 0x%llx.\n", m_wrapper_function_addr, args_addr_ref);
Chris Lattner24943d22010-06-08 16:52:24 +0000364
365 return true;
366}
367
368ThreadPlan *
Jim Inghamea9d4262010-11-05 19:25:48 +0000369ClangFunction::GetThreadPlanToCallFunction (ExecutionContext &exe_ctx,
370 lldb::addr_t func_addr,
371 lldb::addr_t &args_addr,
372 Stream &errors,
373 bool stop_others,
374 bool discard_on_error,
Sean Callanan3aa7da52010-12-13 22:46:15 +0000375 lldb::addr_t *this_arg,
376 lldb::addr_t *cmd_arg)
Chris Lattner24943d22010-06-08 16:52:24 +0000377{
378 // FIXME: Use the errors Stream for better error reporting.
379
Sean Callananc78d6482010-07-26 22:14:36 +0000380 Process *process = exe_ctx.process;
Chris Lattner24943d22010-06-08 16:52:24 +0000381
382 if (process == NULL)
383 {
384 errors.Printf("Can't call a function without a process.");
385 return NULL;
386 }
387
388 // Okay, now run the function:
389
Sean Callanan841026f2010-07-23 00:16:21 +0000390 Address wrapper_address (NULL, func_addr);
Sean Callananc78d6482010-07-26 22:14:36 +0000391 ThreadPlan *new_plan = new ThreadPlanCallFunction (*exe_ctx.thread,
Sean Callanan3aa7da52010-12-13 22:46:15 +0000392 wrapper_address,
393 args_addr,
394 stop_others,
395 discard_on_error,
396 this_arg,
397 cmd_arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000398 return new_plan;
399}
400
401bool
Sean Callananc78d6482010-07-26 22:14:36 +0000402ClangFunction::FetchFunctionResults (ExecutionContext &exe_ctx, lldb::addr_t args_addr, Value &ret_value)
Chris Lattner24943d22010-06-08 16:52:24 +0000403{
404 // Read the return value - it is the last field in the struct:
405 // FIXME: How does clang tell us there's no return value? We need to handle that case.
406
407 std::vector<uint8_t> data_buffer;
408 data_buffer.resize(m_return_size);
Sean Callananc78d6482010-07-26 22:14:36 +0000409 Process *process = exe_ctx.process;
Chris Lattner24943d22010-06-08 16:52:24 +0000410 Error error;
Sean Callanan65dafa82010-08-27 01:01:44 +0000411 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 +0000412
413 if (bytes_read == 0)
414 {
415 return false;
416 }
417
418 if (bytes_read < m_return_size)
419 return false;
420
Greg Clayton53d68e72010-07-20 22:52:08 +0000421 DataExtractor data(&data_buffer.front(), m_return_size, process->GetByteOrder(), process->GetAddressByteSize());
Chris Lattner24943d22010-06-08 16:52:24 +0000422 // FIXME: Assuming an integer scalar for now:
423
424 uint32_t offset = 0;
425 uint64_t return_integer = data.GetMaxU64(&offset, m_return_size);
426
Greg Clayton6916e352010-11-13 03:52:47 +0000427 ret_value.SetContext (Value::eContextTypeClangType, m_function_return_qual_type);
Chris Lattner24943d22010-06-08 16:52:24 +0000428 ret_value.SetValueType(Value::eValueTypeScalar);
429 ret_value.GetScalar() = return_integer;
430 return true;
431}
432
433void
Sean Callananc78d6482010-07-26 22:14:36 +0000434ClangFunction::DeallocateFunctionResults (ExecutionContext &exe_ctx, lldb::addr_t args_addr)
Chris Lattner24943d22010-06-08 16:52:24 +0000435{
436 std::list<lldb::addr_t>::iterator pos;
437 pos = std::find(m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(), args_addr);
438 if (pos != m_wrapper_args_addrs.end())
439 m_wrapper_args_addrs.erase(pos);
440
Sean Callananc78d6482010-07-26 22:14:36 +0000441 exe_ctx.process->DeallocateMemory(args_addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000442}
443
Greg Clayton427f2902010-12-14 02:59:59 +0000444lldb::ExecutionResults
Sean Callananc78d6482010-07-26 22:14:36 +0000445ClangFunction::ExecuteFunction(ExecutionContext &exe_ctx, Stream &errors, Value &results)
Chris Lattner24943d22010-06-08 16:52:24 +0000446{
Sean Callananc78d6482010-07-26 22:14:36 +0000447 return ExecuteFunction (exe_ctx, errors, 1000, true, results);
Chris Lattner24943d22010-06-08 16:52:24 +0000448}
449
Greg Clayton427f2902010-12-14 02:59:59 +0000450lldb::ExecutionResults
Sean Callananc78d6482010-07-26 22:14:36 +0000451ClangFunction::ExecuteFunction(ExecutionContext &exe_ctx, Stream &errors, bool stop_others, Value &results)
Chris Lattner24943d22010-06-08 16:52:24 +0000452{
Jim Inghamea9d4262010-11-05 19:25:48 +0000453 const bool try_all_threads = false;
454 const bool discard_on_error = true;
455 return ExecuteFunction (exe_ctx, NULL, errors, stop_others, NULL, try_all_threads, discard_on_error, results);
Chris Lattner24943d22010-06-08 16:52:24 +0000456}
457
Greg Clayton427f2902010-12-14 02:59:59 +0000458lldb::ExecutionResults
Chris Lattner24943d22010-06-08 16:52:24 +0000459ClangFunction::ExecuteFunction(
Sean Callananc78d6482010-07-26 22:14:36 +0000460 ExecutionContext &exe_ctx,
Chris Lattner24943d22010-06-08 16:52:24 +0000461 Stream &errors,
462 uint32_t single_thread_timeout_usec,
463 bool try_all_threads,
464 Value &results)
465{
Jim Inghamea9d4262010-11-05 19:25:48 +0000466 const bool stop_others = true;
467 const bool discard_on_error = true;
468 return ExecuteFunction (exe_ctx, NULL, errors, stop_others, single_thread_timeout_usec,
469 try_all_threads, discard_on_error, results);
Chris Lattner24943d22010-06-08 16:52:24 +0000470}
471
Sean Callanan841026f2010-07-23 00:16:21 +0000472// This is the static function
Greg Clayton427f2902010-12-14 02:59:59 +0000473lldb::ExecutionResults
Sean Callanan841026f2010-07-23 00:16:21 +0000474ClangFunction::ExecuteFunction (
Sean Callananc78d6482010-07-26 22:14:36 +0000475 ExecutionContext &exe_ctx,
Sean Callanan841026f2010-07-23 00:16:21 +0000476 lldb::addr_t function_address,
477 lldb::addr_t &void_arg,
478 bool stop_others,
479 bool try_all_threads,
Jim Inghamea9d4262010-11-05 19:25:48 +0000480 bool discard_on_error,
Sean Callanan841026f2010-07-23 00:16:21 +0000481 uint32_t single_thread_timeout_usec,
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000482 Stream &errors,
483 lldb::addr_t *this_arg)
Chris Lattner24943d22010-06-08 16:52:24 +0000484{
Jim Inghamea9d4262010-11-05 19:25:48 +0000485 lldb::ThreadPlanSP call_plan_sp(ClangFunction::GetThreadPlanToCallFunction(exe_ctx, function_address, void_arg,
486 errors, stop_others, discard_on_error,
487 this_arg));
Chris Lattner24943d22010-06-08 16:52:24 +0000488 if (call_plan_sp == NULL)
Greg Clayton427f2902010-12-14 02:59:59 +0000489 return lldb::eExecutionSetupError;
Sean Callanan841026f2010-07-23 00:16:21 +0000490
Chris Lattner24943d22010-06-08 16:52:24 +0000491 call_plan_sp->SetPrivate(true);
Jim Ingham3ae449a2010-11-17 02:32:00 +0000492
Jim Ingham360f53f2010-11-30 02:22:11 +0000493 return exe_ctx.process->RunThreadPlan (exe_ctx, call_plan_sp, stop_others, try_all_threads, discard_on_error,
494 single_thread_timeout_usec, errors);
Sean Callanan841026f2010-07-23 00:16:21 +0000495}
Chris Lattner24943d22010-06-08 16:52:24 +0000496
Greg Clayton427f2902010-12-14 02:59:59 +0000497lldb::ExecutionResults
Sean Callanan841026f2010-07-23 00:16:21 +0000498ClangFunction::ExecuteFunction(
Sean Callananc78d6482010-07-26 22:14:36 +0000499 ExecutionContext &exe_ctx,
Sean Callanan841026f2010-07-23 00:16:21 +0000500 lldb::addr_t *args_addr_ptr,
501 Stream &errors,
502 bool stop_others,
503 uint32_t single_thread_timeout_usec,
Jim Inghamea9d4262010-11-05 19:25:48 +0000504 bool try_all_threads,
505 bool discard_on_error,
Sean Callanan841026f2010-07-23 00:16:21 +0000506 Value &results)
507{
508 using namespace clang;
Greg Clayton427f2902010-12-14 02:59:59 +0000509 lldb::ExecutionResults return_value = lldb::eExecutionSetupError;
Sean Callanan841026f2010-07-23 00:16:21 +0000510
511 lldb::addr_t args_addr;
512
513 if (args_addr_ptr != NULL)
514 args_addr = *args_addr_ptr;
515 else
516 args_addr = LLDB_INVALID_ADDRESS;
517
518 if (CompileFunction(errors) != 0)
Greg Clayton427f2902010-12-14 02:59:59 +0000519 return lldb::eExecutionSetupError;
Sean Callanan841026f2010-07-23 00:16:21 +0000520
521 if (args_addr == LLDB_INVALID_ADDRESS)
522 {
Sean Callananc78d6482010-07-26 22:14:36 +0000523 if (!InsertFunction(exe_ctx, args_addr, errors))
Greg Clayton427f2902010-12-14 02:59:59 +0000524 return lldb::eExecutionSetupError;
Sean Callanan841026f2010-07-23 00:16:21 +0000525 }
526
Jim Ingham681778e2010-09-10 23:07:48 +0000527 return_value = ClangFunction::ExecuteFunction(exe_ctx, m_wrapper_function_addr, args_addr, stop_others,
Jim Inghamea9d4262010-11-05 19:25:48 +0000528 try_all_threads, discard_on_error, single_thread_timeout_usec, errors);
Sean Callanan841026f2010-07-23 00:16:21 +0000529
530 if (args_addr_ptr != NULL)
531 *args_addr_ptr = args_addr;
532
Greg Clayton427f2902010-12-14 02:59:59 +0000533 if (return_value != lldb::eExecutionCompleted)
Chris Lattner24943d22010-06-08 16:52:24 +0000534 return return_value;
535
Sean Callananc78d6482010-07-26 22:14:36 +0000536 FetchFunctionResults(exe_ctx, args_addr, results);
Chris Lattner24943d22010-06-08 16:52:24 +0000537
538 if (args_addr_ptr == NULL)
Sean Callananc78d6482010-07-26 22:14:36 +0000539 DeallocateFunctionResults(exe_ctx, args_addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000540
Greg Clayton427f2902010-12-14 02:59:59 +0000541 return lldb::eExecutionCompleted;
Chris Lattner24943d22010-06-08 16:52:24 +0000542}
543
Sean Callanan65dafa82010-08-27 01:01:44 +0000544clang::ASTConsumer *
545ClangFunction::ASTTransformer (clang::ASTConsumer *passthrough)
Chris Lattner24943d22010-06-08 16:52:24 +0000546{
Sean Callanan65dafa82010-08-27 01:01:44 +0000547 return new ASTStructExtractor(passthrough, m_wrapper_struct_name.c_str(), *this);
Chris Lattner24943d22010-06-08 16:52:24 +0000548}