blob: 9ccb4fb0197838df269d06af079194dc3a437a5b [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 Clayton643ee732010-08-04 01:40:35 +000039#include "lldb/Target/StopInfo.h"
Greg Clayton395fc332011-02-15 21:59:32 +000040#include "lldb/Target/Target.h"
Chris Lattner24943d22010-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 Callanan65dafa82010-08-27 01:01:44 +000047
Chris Lattner24943d22010-06-08 16:52:24 +000048//----------------------------------------------------------------------
49// ClangFunction constructor
50//----------------------------------------------------------------------
Greg Clayton8de27c72010-10-15 22:48:33 +000051ClangFunction::ClangFunction
52(
Greg Clayton395fc332011-02-15 21:59:32 +000053 ExecutionContextScope *exe_scope,
Greg Clayton8de27c72010-10-15 22:48:33 +000054 ClangASTContext *ast_context,
55 void *return_qualtype,
56 const Address& functionAddress,
57 const ValueList &arg_value_list
58) :
Greg Clayton395fc332011-02-15 21:59:32 +000059 m_arch (),
Chris Lattner24943d22010-06-08 16:52:24 +000060 m_function_ptr (NULL),
Greg Clayton54e7afa2010-07-09 20:39:50 +000061 m_function_addr (functionAddress),
Chris Lattner24943d22010-06-08 16:52:24 +000062 m_function_return_qual_type(return_qualtype),
Greg Clayton54e7afa2010-07-09 20:39:50 +000063 m_clang_ast_context (ast_context),
Chris Lattner24943d22010-06-08 16:52:24 +000064 m_wrapper_function_name ("__lldb_caller_function"),
65 m_wrapper_struct_name ("__lldb_caller_struct"),
Greg Clayton54e7afa2010-07-09 20:39:50 +000066 m_wrapper_args_addrs (),
Greg Clayton54e7afa2010-07-09 20:39:50 +000067 m_arg_values (arg_value_list),
Chris Lattner24943d22010-06-08 16:52:24 +000068 m_compiled (false),
69 m_JITted (false)
70{
Greg Clayton395fc332011-02-15 21:59:32 +000071 if (exe_scope)
72 {
73 Target *target = exe_scope->CalculateTarget();
74 if (target)
75 m_arch = target->GetArchitecture();
76 }
Chris Lattner24943d22010-06-08 16:52:24 +000077}
78
Greg Clayton8de27c72010-10-15 22:48:33 +000079ClangFunction::ClangFunction
80(
Greg Clayton395fc332011-02-15 21:59:32 +000081 ExecutionContextScope *exe_scope,
Greg Clayton8de27c72010-10-15 22:48:33 +000082 Function &function,
83 ClangASTContext *ast_context,
84 const ValueList &arg_value_list
85) :
Greg Clayton395fc332011-02-15 21:59:32 +000086 m_arch (),
Chris Lattner24943d22010-06-08 16:52:24 +000087 m_function_ptr (&function),
Greg Clayton54e7afa2010-07-09 20:39:50 +000088 m_function_addr (),
89 m_function_return_qual_type (),
Chris Lattner24943d22010-06-08 16:52:24 +000090 m_clang_ast_context (ast_context),
Chris Lattner24943d22010-06-08 16:52:24 +000091 m_wrapper_function_name ("__lldb_function_caller"),
92 m_wrapper_struct_name ("__lldb_caller_struct"),
Greg Clayton54e7afa2010-07-09 20:39:50 +000093 m_wrapper_args_addrs (),
Greg Clayton54e7afa2010-07-09 20:39:50 +000094 m_arg_values (arg_value_list),
Chris Lattner24943d22010-06-08 16:52:24 +000095 m_compiled (false),
96 m_JITted (false)
97{
Greg Clayton395fc332011-02-15 21:59:32 +000098 if (exe_scope)
99 {
100 Target *target = exe_scope->CalculateTarget();
101 if (target)
102 m_arch = target->GetArchitecture();
103 }
104
Chris Lattner24943d22010-06-08 16:52:24 +0000105 m_function_addr = m_function_ptr->GetAddressRange().GetBaseAddress();
Greg Clayton462d4142010-09-29 01:12:09 +0000106 m_function_return_qual_type = m_function_ptr->GetReturnType().GetClangType();
Chris Lattner24943d22010-06-08 16:52:24 +0000107}
108
109//----------------------------------------------------------------------
110// Destructor
111//----------------------------------------------------------------------
112ClangFunction::~ClangFunction()
113{
114}
115
116unsigned
117ClangFunction::CompileFunction (Stream &errors)
118{
Sean Callanan65dafa82010-08-27 01:01:44 +0000119 if (m_compiled)
120 return 0;
121
Chris Lattner24943d22010-06-08 16:52:24 +0000122 // FIXME: How does clang tell us there's no return value? We need to handle that case.
123 unsigned num_errors = 0;
124
Sean Callanan65dafa82010-08-27 01:01:44 +0000125 std::string return_type_str = ClangASTContext::GetTypeName(m_function_return_qual_type);
126
127 // Cons up the function we're going to wrap our call in, then compile it...
128 // We declare the function "extern "C"" because the compiler might be in C++
129 // mode which would mangle the name and then we couldn't find it again...
130 m_wrapper_function_text.clear();
131 m_wrapper_function_text.append ("extern \"C\" void ");
132 m_wrapper_function_text.append (m_wrapper_function_name);
133 m_wrapper_function_text.append (" (void *input)\n{\n struct ");
134 m_wrapper_function_text.append (m_wrapper_struct_name);
135 m_wrapper_function_text.append (" \n {\n");
136 m_wrapper_function_text.append (" ");
137 m_wrapper_function_text.append (return_type_str);
138 m_wrapper_function_text.append (" (*fn_ptr) (");
139
140 // Get the number of arguments. If we have a function type and it is prototyped,
141 // trust that, otherwise use the values we were given.
142
143 // FIXME: This will need to be extended to handle Variadic functions. We'll need
144 // to pull the defined arguments out of the function, then add the types from the
145 // arguments list for the variable arguments.
146
147 uint32_t num_args = UINT32_MAX;
148 bool trust_function = false;
149 // GetArgumentCount returns -1 for an unprototyped function.
150 if (m_function_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +0000151 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000152 int num_func_args = m_function_ptr->GetArgumentCount();
153 if (num_func_args >= 0)
154 trust_function = true;
155 else
156 num_args = num_func_args;
157 }
Chris Lattner24943d22010-06-08 16:52:24 +0000158
Sean Callanan65dafa82010-08-27 01:01:44 +0000159 if (num_args == UINT32_MAX)
160 num_args = m_arg_values.GetSize();
Chris Lattner24943d22010-06-08 16:52:24 +0000161
Sean Callanan65dafa82010-08-27 01:01:44 +0000162 std::string args_buffer; // This one stores the definition of all the args in "struct caller".
163 std::string args_list_buffer; // This one stores the argument list called from the structure.
164 for (size_t i = 0; i < num_args; i++)
165 {
166 const char *type_string;
167 std::string type_stdstr;
Chris Lattner24943d22010-06-08 16:52:24 +0000168
Sean Callanan65dafa82010-08-27 01:01:44 +0000169 if (trust_function)
Chris Lattner24943d22010-06-08 16:52:24 +0000170 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000171 type_string = m_function_ptr->GetArgumentTypeAtIndex(i).GetName().AsCString();
Chris Lattner24943d22010-06-08 16:52:24 +0000172 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000173 else
Chris Lattner24943d22010-06-08 16:52:24 +0000174 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000175 Value *arg_value = m_arg_values.GetValueAtIndex(i);
Greg Clayton462d4142010-09-29 01:12:09 +0000176 void *clang_qual_type = arg_value->GetClangType ();
Sean Callanan65dafa82010-08-27 01:01:44 +0000177 if (clang_qual_type != NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000178 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000179 type_stdstr = ClangASTContext::GetTypeName(clang_qual_type);
180 type_string = type_stdstr.c_str();
Chris Lattner24943d22010-06-08 16:52:24 +0000181 }
182 else
Sean Callanan65dafa82010-08-27 01:01:44 +0000183 {
184 errors.Printf("Could not determine type of input value %d.", i);
Chris Lattner24943d22010-06-08 16:52:24 +0000185 return 1;
186 }
Chris Lattner24943d22010-06-08 16:52:24 +0000187 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000188
189 m_wrapper_function_text.append (type_string);
190 if (i < num_args - 1)
191 m_wrapper_function_text.append (", ");
192
193 char arg_buf[32];
194 args_buffer.append (" ");
195 args_buffer.append (type_string);
196 snprintf(arg_buf, 31, "arg_%zd", i);
197 args_buffer.push_back (' ');
198 args_buffer.append (arg_buf);
199 args_buffer.append (";\n");
200
201 args_list_buffer.append ("__lldb_fn_data->");
202 args_list_buffer.append (arg_buf);
203 if (i < num_args - 1)
204 args_list_buffer.append (", ");
205
Chris Lattner24943d22010-06-08 16:52:24 +0000206 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000207 m_wrapper_function_text.append (");\n"); // Close off the function calling prototype.
208
209 m_wrapper_function_text.append (args_buffer);
210
211 m_wrapper_function_text.append (" ");
212 m_wrapper_function_text.append (return_type_str);
213 m_wrapper_function_text.append (" return_value;");
214 m_wrapper_function_text.append ("\n };\n struct ");
215 m_wrapper_function_text.append (m_wrapper_struct_name);
216 m_wrapper_function_text.append ("* __lldb_fn_data = (struct ");
217 m_wrapper_function_text.append (m_wrapper_struct_name);
218 m_wrapper_function_text.append (" *) input;\n");
219
220 m_wrapper_function_text.append (" __lldb_fn_data->return_value = __lldb_fn_data->fn_ptr (");
221 m_wrapper_function_text.append (args_list_buffer);
222 m_wrapper_function_text.append (");\n}\n");
223
Jim Ingham7812e012011-01-18 22:20:08 +0000224 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan65dafa82010-08-27 01:01:44 +0000225 if (log)
226 log->Printf ("Expression: \n\n%s\n\n", m_wrapper_function_text.c_str());
227
228 // Okay, now compile this expression
229
Greg Clayton395fc332011-02-15 21:59:32 +0000230 m_parser.reset(new ClangExpressionParser(NULL, *this));
Sean Callanan65dafa82010-08-27 01:01:44 +0000231
232 num_errors = m_parser->Parse (errors);
233
234 m_compiled = (num_errors == 0);
235
236 if (!m_compiled)
237 return num_errors;
Chris Lattner24943d22010-06-08 16:52:24 +0000238
239 return num_errors;
240}
241
242bool
Sean Callananc78d6482010-07-26 22:14:36 +0000243ClangFunction::WriteFunctionWrapper (ExecutionContext &exe_ctx, Stream &errors)
Chris Lattner24943d22010-06-08 16:52:24 +0000244{
Sean Callananc78d6482010-07-26 22:14:36 +0000245 Process *process = exe_ctx.process;
Chris Lattner24943d22010-06-08 16:52:24 +0000246
Sean Callanan65dafa82010-08-27 01:01:44 +0000247 if (!process)
248 return false;
249
250 if (!m_compiled)
Chris Lattner24943d22010-06-08 16:52:24 +0000251 return false;
252
Sean Callanan65dafa82010-08-27 01:01:44 +0000253 if (m_JITted)
254 return true;
255
Greg Claytond0882d02011-01-19 23:00:49 +0000256 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 +0000257
258 if (!jit_error.Success())
Chris Lattner24943d22010-06-08 16:52:24 +0000259 return false;
Greg Claytond0882d02011-01-19 23:00:49 +0000260 if (exe_ctx.process && m_jit_alloc != LLDB_INVALID_ADDRESS)
261 m_jit_process_sp = exe_ctx.process->GetSP();
Chris Lattner24943d22010-06-08 16:52:24 +0000262
263 return true;
264}
265
266bool
Sean Callananc78d6482010-07-26 22:14:36 +0000267ClangFunction::WriteFunctionArguments (ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref, Stream &errors)
Chris Lattner24943d22010-06-08 16:52:24 +0000268{
Sean Callananc78d6482010-07-26 22:14:36 +0000269 return WriteFunctionArguments(exe_ctx, args_addr_ref, m_function_addr, m_arg_values, errors);
Chris Lattner24943d22010-06-08 16:52:24 +0000270}
271
272// FIXME: Assure that the ValueList we were passed in is consistent with the one that defined this function.
273
274bool
Jim Ingham681778e2010-09-10 23:07:48 +0000275ClangFunction::WriteFunctionArguments (ExecutionContext &exe_ctx,
276 lldb::addr_t &args_addr_ref,
277 Address function_address,
278 ValueList &arg_values,
279 Stream &errors)
Chris Lattner24943d22010-06-08 16:52:24 +0000280{
Sean Callanan65dafa82010-08-27 01:01:44 +0000281 // All the information to reconstruct the struct is provided by the
282 // StructExtractor.
283 if (!m_struct_valid)
284 {
285 errors.Printf("Argument information was not correctly parsed, so the function cannot be called.");
286 return false;
287 }
288
Chris Lattner24943d22010-06-08 16:52:24 +0000289 Error error;
290 using namespace clang;
Greg Clayton427f2902010-12-14 02:59:59 +0000291 lldb::ExecutionResults return_value = lldb::eExecutionSetupError;
Chris Lattner24943d22010-06-08 16:52:24 +0000292
Sean Callananc78d6482010-07-26 22:14:36 +0000293 Process *process = exe_ctx.process;
Chris Lattner24943d22010-06-08 16:52:24 +0000294
295 if (process == NULL)
296 return return_value;
Sean Callanan65dafa82010-08-27 01:01:44 +0000297
Chris Lattner24943d22010-06-08 16:52:24 +0000298 if (args_addr_ref == LLDB_INVALID_ADDRESS)
299 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000300 args_addr_ref = process->AllocateMemory(m_struct_size, lldb::ePermissionsReadable|lldb::ePermissionsWritable, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000301 if (args_addr_ref == LLDB_INVALID_ADDRESS)
302 return false;
303 m_wrapper_args_addrs.push_back (args_addr_ref);
304 }
305 else
306 {
307 // Make sure this is an address that we've already handed out.
308 if (find (m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(), args_addr_ref) == m_wrapper_args_addrs.end())
309 {
310 return false;
311 }
312 }
313
314 // FIXME: This is fake, and just assumes that it matches that architecture.
315 // Make a data extractor and put the address into the right byte order & size.
316
Greg Claytoneea26402010-09-14 23:36:40 +0000317 uint64_t fun_addr = function_address.GetLoadAddress(exe_ctx.target);
Sean Callanan65dafa82010-08-27 01:01:44 +0000318 int first_offset = m_member_offsets[0];
Chris Lattner24943d22010-06-08 16:52:24 +0000319 process->WriteMemory(args_addr_ref + first_offset, &fun_addr, 8, error);
320
321 // FIXME: We will need to extend this for Variadic functions.
322
323 Error value_error;
324
325 size_t num_args = arg_values.GetSize();
326 if (num_args != m_arg_values.GetSize())
327 {
328 errors.Printf ("Wrong number of arguments - was: %d should be: %d", num_args, m_arg_values.GetSize());
329 return false;
330 }
331
Greg Clayton54e7afa2010-07-09 20:39:50 +0000332 for (size_t i = 0; i < num_args; i++)
Chris Lattner24943d22010-06-08 16:52:24 +0000333 {
334 // FIXME: We should sanity check sizes.
335
Sean Callanan65dafa82010-08-27 01:01:44 +0000336 int offset = m_member_offsets[i+1]; // Clang sizes are in bytes.
Chris Lattner24943d22010-06-08 16:52:24 +0000337 Value *arg_value = arg_values.GetValueAtIndex(i);
338
339 // FIXME: For now just do scalars:
340
341 // Special case: if it's a pointer, don't do anything (the ABI supports passing cstrings)
342
343 if (arg_value->GetValueType() == Value::eValueTypeHostAddress &&
Greg Clayton6916e352010-11-13 03:52:47 +0000344 arg_value->GetContextType() == Value::eContextTypeClangType &&
Greg Clayton462d4142010-09-29 01:12:09 +0000345 ClangASTContext::IsPointerType(arg_value->GetClangType()))
Chris Lattner24943d22010-06-08 16:52:24 +0000346 continue;
347
Sean Callananc78d6482010-07-26 22:14:36 +0000348 const Scalar &arg_scalar = arg_value->ResolveValue(&exe_ctx, m_clang_ast_context->getASTContext());
Chris Lattner24943d22010-06-08 16:52:24 +0000349
350 int byte_size = arg_scalar.GetByteSize();
351 std::vector<uint8_t> buffer;
352 buffer.resize(byte_size);
353 DataExtractor value_data;
354 arg_scalar.GetData (value_data);
Greg Clayton53d68e72010-07-20 22:52:08 +0000355 value_data.ExtractBytes(0, byte_size, process->GetByteOrder(), &buffer.front());
356 process->WriteMemory(args_addr_ref + offset, &buffer.front(), byte_size, error);
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.
392
Sean Callananc78d6482010-07-26 22:14:36 +0000393 Process *process = exe_ctx.process;
Chris Lattner24943d22010-06-08 16:52:24 +0000394
395 if (process == NULL)
396 {
397 errors.Printf("Can't call a function without a process.");
398 return NULL;
399 }
400
401 // Okay, now run the function:
402
Sean Callanan841026f2010-07-23 00:16:21 +0000403 Address wrapper_address (NULL, func_addr);
Sean Callananc78d6482010-07-26 22:14:36 +0000404 ThreadPlan *new_plan = new ThreadPlanCallFunction (*exe_ctx.thread,
Sean Callanan3aa7da52010-12-13 22:46:15 +0000405 wrapper_address,
406 args_addr,
407 stop_others,
408 discard_on_error,
409 this_arg,
410 cmd_arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000411 return new_plan;
412}
413
414bool
Sean Callananc78d6482010-07-26 22:14:36 +0000415ClangFunction::FetchFunctionResults (ExecutionContext &exe_ctx, lldb::addr_t args_addr, Value &ret_value)
Chris Lattner24943d22010-06-08 16:52:24 +0000416{
417 // Read the return value - it is the last field in the struct:
418 // FIXME: How does clang tell us there's no return value? We need to handle that case.
419
420 std::vector<uint8_t> data_buffer;
421 data_buffer.resize(m_return_size);
Sean Callananc78d6482010-07-26 22:14:36 +0000422 Process *process = exe_ctx.process;
Chris Lattner24943d22010-06-08 16:52:24 +0000423 Error error;
Sean Callanan65dafa82010-08-27 01:01:44 +0000424 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 +0000425
426 if (bytes_read == 0)
427 {
428 return false;
429 }
430
431 if (bytes_read < m_return_size)
432 return false;
433
Greg Clayton53d68e72010-07-20 22:52:08 +0000434 DataExtractor data(&data_buffer.front(), m_return_size, process->GetByteOrder(), process->GetAddressByteSize());
Chris Lattner24943d22010-06-08 16:52:24 +0000435 // FIXME: Assuming an integer scalar for now:
436
437 uint32_t offset = 0;
438 uint64_t return_integer = data.GetMaxU64(&offset, m_return_size);
439
Greg Clayton6916e352010-11-13 03:52:47 +0000440 ret_value.SetContext (Value::eContextTypeClangType, m_function_return_qual_type);
Chris Lattner24943d22010-06-08 16:52:24 +0000441 ret_value.SetValueType(Value::eValueTypeScalar);
442 ret_value.GetScalar() = return_integer;
443 return true;
444}
445
446void
Sean Callananc78d6482010-07-26 22:14:36 +0000447ClangFunction::DeallocateFunctionResults (ExecutionContext &exe_ctx, lldb::addr_t args_addr)
Chris Lattner24943d22010-06-08 16:52:24 +0000448{
449 std::list<lldb::addr_t>::iterator pos;
450 pos = std::find(m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(), args_addr);
451 if (pos != m_wrapper_args_addrs.end())
452 m_wrapper_args_addrs.erase(pos);
453
Sean Callananc78d6482010-07-26 22:14:36 +0000454 exe_ctx.process->DeallocateMemory(args_addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000455}
456
Greg Clayton427f2902010-12-14 02:59:59 +0000457lldb::ExecutionResults
Sean Callananc78d6482010-07-26 22:14:36 +0000458ClangFunction::ExecuteFunction(ExecutionContext &exe_ctx, Stream &errors, Value &results)
Chris Lattner24943d22010-06-08 16:52:24 +0000459{
Sean Callananc78d6482010-07-26 22:14:36 +0000460 return ExecuteFunction (exe_ctx, errors, 1000, true, results);
Chris Lattner24943d22010-06-08 16:52:24 +0000461}
462
Greg Clayton427f2902010-12-14 02:59:59 +0000463lldb::ExecutionResults
Sean Callananc78d6482010-07-26 22:14:36 +0000464ClangFunction::ExecuteFunction(ExecutionContext &exe_ctx, Stream &errors, bool stop_others, Value &results)
Chris Lattner24943d22010-06-08 16:52:24 +0000465{
Jim Inghamea9d4262010-11-05 19:25:48 +0000466 const bool try_all_threads = false;
467 const bool discard_on_error = true;
468 return ExecuteFunction (exe_ctx, NULL, errors, stop_others, NULL, try_all_threads, discard_on_error, results);
Chris Lattner24943d22010-06-08 16:52:24 +0000469}
470
Greg Clayton427f2902010-12-14 02:59:59 +0000471lldb::ExecutionResults
Chris Lattner24943d22010-06-08 16:52:24 +0000472ClangFunction::ExecuteFunction(
Sean Callananc78d6482010-07-26 22:14:36 +0000473 ExecutionContext &exe_ctx,
Chris Lattner24943d22010-06-08 16:52:24 +0000474 Stream &errors,
475 uint32_t single_thread_timeout_usec,
476 bool try_all_threads,
477 Value &results)
478{
Jim Inghamea9d4262010-11-05 19:25:48 +0000479 const bool stop_others = true;
480 const bool discard_on_error = true;
481 return ExecuteFunction (exe_ctx, NULL, errors, stop_others, single_thread_timeout_usec,
482 try_all_threads, discard_on_error, results);
Chris Lattner24943d22010-06-08 16:52:24 +0000483}
484
Sean Callanan841026f2010-07-23 00:16:21 +0000485// This is the static function
Greg Clayton427f2902010-12-14 02:59:59 +0000486lldb::ExecutionResults
Sean Callanan841026f2010-07-23 00:16:21 +0000487ClangFunction::ExecuteFunction (
Sean Callananc78d6482010-07-26 22:14:36 +0000488 ExecutionContext &exe_ctx,
Sean Callanan841026f2010-07-23 00:16:21 +0000489 lldb::addr_t function_address,
490 lldb::addr_t &void_arg,
491 bool stop_others,
492 bool try_all_threads,
Jim Inghamea9d4262010-11-05 19:25:48 +0000493 bool discard_on_error,
Sean Callanan841026f2010-07-23 00:16:21 +0000494 uint32_t single_thread_timeout_usec,
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000495 Stream &errors,
496 lldb::addr_t *this_arg)
Chris Lattner24943d22010-06-08 16:52:24 +0000497{
Jim Inghamea9d4262010-11-05 19:25:48 +0000498 lldb::ThreadPlanSP call_plan_sp(ClangFunction::GetThreadPlanToCallFunction(exe_ctx, function_address, void_arg,
499 errors, stop_others, discard_on_error,
500 this_arg));
Chris Lattner24943d22010-06-08 16:52:24 +0000501 if (call_plan_sp == NULL)
Greg Clayton427f2902010-12-14 02:59:59 +0000502 return lldb::eExecutionSetupError;
Sean Callanan841026f2010-07-23 00:16:21 +0000503
Chris Lattner24943d22010-06-08 16:52:24 +0000504 call_plan_sp->SetPrivate(true);
Jim Ingham3ae449a2010-11-17 02:32:00 +0000505
Jim Ingham360f53f2010-11-30 02:22:11 +0000506 return exe_ctx.process->RunThreadPlan (exe_ctx, call_plan_sp, stop_others, try_all_threads, discard_on_error,
507 single_thread_timeout_usec, errors);
Sean Callanan841026f2010-07-23 00:16:21 +0000508}
Chris Lattner24943d22010-06-08 16:52:24 +0000509
Greg Clayton427f2902010-12-14 02:59:59 +0000510lldb::ExecutionResults
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 Clayton427f2902010-12-14 02:59:59 +0000522 lldb::ExecutionResults return_value = lldb::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 Clayton427f2902010-12-14 02:59:59 +0000532 return lldb::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 Clayton427f2902010-12-14 02:59:59 +0000537 return lldb::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 Clayton427f2902010-12-14 02:59:59 +0000552 if (return_value != lldb::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 Clayton427f2902010-12-14 02:59:59 +0000560 return lldb::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}