blob: 21acb5b331820eea51ca6b79d40ffe0dbf456483 [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(
Jim Ingham53bfd062011-03-17 20:02:56 +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) :
Chris Lattner24943d22010-06-08 16:52:24 +000059 m_function_ptr (NULL),
Greg Clayton54e7afa2010-07-09 20:39:50 +000060 m_function_addr (functionAddress),
Chris Lattner24943d22010-06-08 16:52:24 +000061 m_function_return_qual_type(return_qualtype),
Greg Clayton54e7afa2010-07-09 20:39:50 +000062 m_clang_ast_context (ast_context),
Chris Lattner24943d22010-06-08 16:52:24 +000063 m_wrapper_function_name ("__lldb_caller_function"),
64 m_wrapper_struct_name ("__lldb_caller_struct"),
Greg Clayton54e7afa2010-07-09 20:39:50 +000065 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{
Jim Ingham53bfd062011-03-17 20:02:56 +000070 Process *process = exe_scope.CalculateProcess();
71 // Can't make a ClangFunction without a process.
72 assert (process != NULL);
73
74 m_jit_process_sp = process->GetSP();
Chris Lattner24943d22010-06-08 16:52:24 +000075}
76
Greg Clayton8de27c72010-10-15 22:48:33 +000077ClangFunction::ClangFunction
78(
Jim Ingham53bfd062011-03-17 20:02:56 +000079 ExecutionContextScope &exe_scope,
Greg Clayton8de27c72010-10-15 22:48:33 +000080 Function &function,
81 ClangASTContext *ast_context,
82 const ValueList &arg_value_list
83) :
Chris Lattner24943d22010-06-08 16:52:24 +000084 m_function_ptr (&function),
Greg Clayton54e7afa2010-07-09 20:39:50 +000085 m_function_addr (),
86 m_function_return_qual_type (),
Chris Lattner24943d22010-06-08 16:52:24 +000087 m_clang_ast_context (ast_context),
Chris Lattner24943d22010-06-08 16:52:24 +000088 m_wrapper_function_name ("__lldb_function_caller"),
89 m_wrapper_struct_name ("__lldb_caller_struct"),
Greg Clayton54e7afa2010-07-09 20:39:50 +000090 m_wrapper_args_addrs (),
Greg Clayton54e7afa2010-07-09 20:39:50 +000091 m_arg_values (arg_value_list),
Chris Lattner24943d22010-06-08 16:52:24 +000092 m_compiled (false),
93 m_JITted (false)
94{
Jim Ingham53bfd062011-03-17 20:02:56 +000095 Process *process = exe_scope.CalculateProcess();
96 // Can't make a ClangFunction without a process.
97 assert (process != NULL);
98
99 m_jit_process_sp = process->GetSP();
Greg Clayton395fc332011-02-15 21:59:32 +0000100
Chris Lattner24943d22010-06-08 16:52:24 +0000101 m_function_addr = m_function_ptr->GetAddressRange().GetBaseAddress();
Greg Clayton04c9c7b2011-02-16 23:00:21 +0000102 m_function_return_qual_type = m_function_ptr->GetReturnClangType();
Chris Lattner24943d22010-06-08 16:52:24 +0000103}
104
105//----------------------------------------------------------------------
106// Destructor
107//----------------------------------------------------------------------
108ClangFunction::~ClangFunction()
109{
110}
111
112unsigned
113ClangFunction::CompileFunction (Stream &errors)
114{
Sean Callanan65dafa82010-08-27 01:01:44 +0000115 if (m_compiled)
116 return 0;
117
Chris Lattner24943d22010-06-08 16:52:24 +0000118 // FIXME: How does clang tell us there's no return value? We need to handle that case.
119 unsigned num_errors = 0;
120
Greg Claytonb302b2f2011-06-30 02:28:26 +0000121 std::string return_type_str (ClangASTType::GetTypeNameForOpaqueQualType (m_function_return_qual_type));
Sean Callanan65dafa82010-08-27 01:01:44 +0000122
123 // Cons up the function we're going to wrap our call in, then compile it...
124 // We declare the function "extern "C"" because the compiler might be in C++
125 // mode which would mangle the name and then we couldn't find it again...
126 m_wrapper_function_text.clear();
127 m_wrapper_function_text.append ("extern \"C\" void ");
128 m_wrapper_function_text.append (m_wrapper_function_name);
129 m_wrapper_function_text.append (" (void *input)\n{\n struct ");
130 m_wrapper_function_text.append (m_wrapper_struct_name);
131 m_wrapper_function_text.append (" \n {\n");
132 m_wrapper_function_text.append (" ");
133 m_wrapper_function_text.append (return_type_str);
134 m_wrapper_function_text.append (" (*fn_ptr) (");
135
136 // Get the number of arguments. If we have a function type and it is prototyped,
137 // trust that, otherwise use the values we were given.
138
139 // FIXME: This will need to be extended to handle Variadic functions. We'll need
140 // to pull the defined arguments out of the function, then add the types from the
141 // arguments list for the variable arguments.
142
143 uint32_t num_args = UINT32_MAX;
144 bool trust_function = false;
145 // GetArgumentCount returns -1 for an unprototyped function.
146 if (m_function_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +0000147 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000148 int num_func_args = m_function_ptr->GetArgumentCount();
149 if (num_func_args >= 0)
150 trust_function = true;
151 else
152 num_args = num_func_args;
153 }
Chris Lattner24943d22010-06-08 16:52:24 +0000154
Sean Callanan65dafa82010-08-27 01:01:44 +0000155 if (num_args == UINT32_MAX)
156 num_args = m_arg_values.GetSize();
Chris Lattner24943d22010-06-08 16:52:24 +0000157
Sean Callanan65dafa82010-08-27 01:01:44 +0000158 std::string args_buffer; // This one stores the definition of all the args in "struct caller".
159 std::string args_list_buffer; // This one stores the argument list called from the structure.
160 for (size_t i = 0; i < num_args; i++)
161 {
Greg Clayton04c9c7b2011-02-16 23:00:21 +0000162 std::string type_name;
Chris Lattner24943d22010-06-08 16:52:24 +0000163
Sean Callanan65dafa82010-08-27 01:01:44 +0000164 if (trust_function)
Chris Lattner24943d22010-06-08 16:52:24 +0000165 {
Greg Clayton04c9c7b2011-02-16 23:00:21 +0000166 lldb::clang_type_t arg_clang_type = m_function_ptr->GetArgumentTypeAtIndex(i);
Greg Claytonb302b2f2011-06-30 02:28:26 +0000167 type_name = ClangASTType::GetTypeNameForOpaqueQualType (arg_clang_type);
Chris Lattner24943d22010-06-08 16:52:24 +0000168 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000169 else
Chris Lattner24943d22010-06-08 16:52:24 +0000170 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000171 Value *arg_value = m_arg_values.GetValueAtIndex(i);
Greg Claytonb302b2f2011-06-30 02:28:26 +0000172 lldb::clang_type_t clang_qual_type = arg_value->GetClangType ();
Sean Callanan65dafa82010-08-27 01:01:44 +0000173 if (clang_qual_type != NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000174 {
Greg Claytonb302b2f2011-06-30 02:28:26 +0000175 type_name = ClangASTType::GetTypeNameForOpaqueQualType (clang_qual_type);
Chris Lattner24943d22010-06-08 16:52:24 +0000176 }
177 else
Sean Callanan65dafa82010-08-27 01:01:44 +0000178 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000179 errors.Printf("Could not determine type of input value %lu.", i);
Chris Lattner24943d22010-06-08 16:52:24 +0000180 return 1;
181 }
Chris Lattner24943d22010-06-08 16:52:24 +0000182 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000183
Greg Clayton04c9c7b2011-02-16 23:00:21 +0000184 m_wrapper_function_text.append (type_name);
Sean Callanan65dafa82010-08-27 01:01:44 +0000185 if (i < num_args - 1)
186 m_wrapper_function_text.append (", ");
187
188 char arg_buf[32];
189 args_buffer.append (" ");
Greg Clayton04c9c7b2011-02-16 23:00:21 +0000190 args_buffer.append (type_name);
Sean Callanan65dafa82010-08-27 01:01:44 +0000191 snprintf(arg_buf, 31, "arg_%zd", i);
192 args_buffer.push_back (' ');
193 args_buffer.append (arg_buf);
194 args_buffer.append (";\n");
195
196 args_list_buffer.append ("__lldb_fn_data->");
197 args_list_buffer.append (arg_buf);
198 if (i < num_args - 1)
199 args_list_buffer.append (", ");
200
Chris Lattner24943d22010-06-08 16:52:24 +0000201 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000202 m_wrapper_function_text.append (");\n"); // Close off the function calling prototype.
203
204 m_wrapper_function_text.append (args_buffer);
205
206 m_wrapper_function_text.append (" ");
207 m_wrapper_function_text.append (return_type_str);
208 m_wrapper_function_text.append (" return_value;");
209 m_wrapper_function_text.append ("\n };\n struct ");
210 m_wrapper_function_text.append (m_wrapper_struct_name);
211 m_wrapper_function_text.append ("* __lldb_fn_data = (struct ");
212 m_wrapper_function_text.append (m_wrapper_struct_name);
213 m_wrapper_function_text.append (" *) input;\n");
214
215 m_wrapper_function_text.append (" __lldb_fn_data->return_value = __lldb_fn_data->fn_ptr (");
216 m_wrapper_function_text.append (args_list_buffer);
217 m_wrapper_function_text.append (");\n}\n");
218
Jim Ingham7812e012011-01-18 22:20:08 +0000219 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan65dafa82010-08-27 01:01:44 +0000220 if (log)
221 log->Printf ("Expression: \n\n%s\n\n", m_wrapper_function_text.c_str());
222
223 // Okay, now compile this expression
Jim Ingham53bfd062011-03-17 20:02:56 +0000224
225 m_parser.reset(new ClangExpressionParser(m_jit_process_sp.get(), *this));
Sean Callanan65dafa82010-08-27 01:01:44 +0000226
227 num_errors = m_parser->Parse (errors);
228
229 m_compiled = (num_errors == 0);
230
231 if (!m_compiled)
232 return num_errors;
Chris Lattner24943d22010-06-08 16:52:24 +0000233
234 return num_errors;
235}
236
237bool
Sean Callananc78d6482010-07-26 22:14:36 +0000238ClangFunction::WriteFunctionWrapper (ExecutionContext &exe_ctx, Stream &errors)
Chris Lattner24943d22010-06-08 16:52:24 +0000239{
Sean Callananc78d6482010-07-26 22:14:36 +0000240 Process *process = exe_ctx.process;
Chris Lattner24943d22010-06-08 16:52:24 +0000241
Sean Callanan65dafa82010-08-27 01:01:44 +0000242 if (!process)
243 return false;
Jim Ingham53bfd062011-03-17 20:02:56 +0000244
245 if (process != m_jit_process_sp.get())
246 return false;
Sean Callanan65dafa82010-08-27 01:01:44 +0000247
248 if (!m_compiled)
Chris Lattner24943d22010-06-08 16:52:24 +0000249 return false;
250
Sean Callanan65dafa82010-08-27 01:01:44 +0000251 if (m_JITted)
252 return true;
253
Sean Callanan696cf5f2011-05-07 01:06:41 +0000254 lldb::ClangExpressionVariableSP const_result;
255
Sean Callanan47dc4572011-09-15 02:13:07 +0000256 bool evaluated_statically = false; // should stay that way
257
258 Error jit_error (m_parser->PrepareForExecution (m_jit_alloc,
259 m_jit_start_addr,
260 m_jit_end_addr,
261 exe_ctx,
262 NULL,
263 evaluated_statically,
264 const_result,
265 eExecutionPolicyAlways));
Sean Callanan65dafa82010-08-27 01:01:44 +0000266
267 if (!jit_error.Success())
Chris Lattner24943d22010-06-08 16:52:24 +0000268 return false;
Greg Claytond0882d02011-01-19 23:00:49 +0000269 if (exe_ctx.process && m_jit_alloc != LLDB_INVALID_ADDRESS)
270 m_jit_process_sp = exe_ctx.process->GetSP();
Chris Lattner24943d22010-06-08 16:52:24 +0000271
272 return true;
273}
274
275bool
Sean Callananc78d6482010-07-26 22:14:36 +0000276ClangFunction::WriteFunctionArguments (ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref, Stream &errors)
Chris Lattner24943d22010-06-08 16:52:24 +0000277{
Sean Callananc78d6482010-07-26 22:14:36 +0000278 return WriteFunctionArguments(exe_ctx, args_addr_ref, m_function_addr, m_arg_values, errors);
Chris Lattner24943d22010-06-08 16:52:24 +0000279}
280
281// FIXME: Assure that the ValueList we were passed in is consistent with the one that defined this function.
282
283bool
Jim Ingham681778e2010-09-10 23:07:48 +0000284ClangFunction::WriteFunctionArguments (ExecutionContext &exe_ctx,
285 lldb::addr_t &args_addr_ref,
286 Address function_address,
287 ValueList &arg_values,
288 Stream &errors)
Chris Lattner24943d22010-06-08 16:52:24 +0000289{
Sean Callanan65dafa82010-08-27 01:01:44 +0000290 // All the information to reconstruct the struct is provided by the
291 // StructExtractor.
292 if (!m_struct_valid)
293 {
294 errors.Printf("Argument information was not correctly parsed, so the function cannot be called.");
295 return false;
296 }
297
Chris Lattner24943d22010-06-08 16:52:24 +0000298 Error error;
299 using namespace clang;
Greg Claytonb3448432011-03-24 21:19:54 +0000300 ExecutionResults return_value = eExecutionSetupError;
Chris Lattner24943d22010-06-08 16:52:24 +0000301
Sean Callananc78d6482010-07-26 22:14:36 +0000302 Process *process = exe_ctx.process;
Chris Lattner24943d22010-06-08 16:52:24 +0000303
304 if (process == NULL)
305 return return_value;
Jim Ingham53bfd062011-03-17 20:02:56 +0000306
307 if (process != m_jit_process_sp.get())
308 return false;
Sean Callanan65dafa82010-08-27 01:01:44 +0000309
Chris Lattner24943d22010-06-08 16:52:24 +0000310 if (args_addr_ref == LLDB_INVALID_ADDRESS)
311 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000312 args_addr_ref = process->AllocateMemory(m_struct_size, lldb::ePermissionsReadable|lldb::ePermissionsWritable, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000313 if (args_addr_ref == LLDB_INVALID_ADDRESS)
314 return false;
315 m_wrapper_args_addrs.push_back (args_addr_ref);
316 }
317 else
318 {
319 // Make sure this is an address that we've already handed out.
320 if (find (m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(), args_addr_ref) == m_wrapper_args_addrs.end())
321 {
322 return false;
323 }
324 }
325
Greg Claytonc0fa5332011-05-22 22:46:53 +0000326 // TODO: verify fun_addr needs to be a callable address
327 Scalar fun_addr (function_address.GetCallableLoadAddress(exe_ctx.target));
Sean Callanan65dafa82010-08-27 01:01:44 +0000328 int first_offset = m_member_offsets[0];
Greg Claytonc0fa5332011-05-22 22:46:53 +0000329 process->WriteScalarToMemory(args_addr_ref + first_offset, fun_addr, process->GetAddressByteSize(), error);
Chris Lattner24943d22010-06-08 16:52:24 +0000330
331 // FIXME: We will need to extend this for Variadic functions.
332
333 Error value_error;
334
335 size_t num_args = arg_values.GetSize();
336 if (num_args != m_arg_values.GetSize())
337 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000338 errors.Printf ("Wrong number of arguments - was: %lu should be: %lu", num_args, m_arg_values.GetSize());
Chris Lattner24943d22010-06-08 16:52:24 +0000339 return false;
340 }
341
Greg Clayton54e7afa2010-07-09 20:39:50 +0000342 for (size_t i = 0; i < num_args; i++)
Chris Lattner24943d22010-06-08 16:52:24 +0000343 {
344 // FIXME: We should sanity check sizes.
345
Sean Callanan65dafa82010-08-27 01:01:44 +0000346 int offset = m_member_offsets[i+1]; // Clang sizes are in bytes.
Chris Lattner24943d22010-06-08 16:52:24 +0000347 Value *arg_value = arg_values.GetValueAtIndex(i);
348
349 // FIXME: For now just do scalars:
350
351 // Special case: if it's a pointer, don't do anything (the ABI supports passing cstrings)
352
353 if (arg_value->GetValueType() == Value::eValueTypeHostAddress &&
Greg Clayton6916e352010-11-13 03:52:47 +0000354 arg_value->GetContextType() == Value::eContextTypeClangType &&
Greg Clayton462d4142010-09-29 01:12:09 +0000355 ClangASTContext::IsPointerType(arg_value->GetClangType()))
Chris Lattner24943d22010-06-08 16:52:24 +0000356 continue;
357
Sean Callananc78d6482010-07-26 22:14:36 +0000358 const Scalar &arg_scalar = arg_value->ResolveValue(&exe_ctx, m_clang_ast_context->getASTContext());
Chris Lattner24943d22010-06-08 16:52:24 +0000359
Greg Claytonc0fa5332011-05-22 22:46:53 +0000360 if (!process->WriteScalarToMemory(args_addr_ref + offset, arg_scalar, arg_scalar.GetByteSize(), error))
361 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000362 }
363
364 return true;
365}
366
367bool
Sean Callananc78d6482010-07-26 22:14:36 +0000368ClangFunction::InsertFunction (ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref, Stream &errors)
Chris Lattner24943d22010-06-08 16:52:24 +0000369{
370 using namespace clang;
371
372 if (CompileFunction(errors) != 0)
373 return false;
Sean Callananc78d6482010-07-26 22:14:36 +0000374 if (!WriteFunctionWrapper(exe_ctx, errors))
Chris Lattner24943d22010-06-08 16:52:24 +0000375 return false;
Sean Callananc78d6482010-07-26 22:14:36 +0000376 if (!WriteFunctionArguments(exe_ctx, args_addr_ref, errors))
Chris Lattner24943d22010-06-08 16:52:24 +0000377 return false;
378
Greg Claytone005f2c2010-11-06 01:53:30 +0000379 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000380 if (log)
Greg Claytond0882d02011-01-19 23:00:49 +0000381 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 +0000382
383 return true;
384}
385
386ThreadPlan *
Jim Inghamea9d4262010-11-05 19:25:48 +0000387ClangFunction::GetThreadPlanToCallFunction (ExecutionContext &exe_ctx,
388 lldb::addr_t func_addr,
389 lldb::addr_t &args_addr,
390 Stream &errors,
391 bool stop_others,
392 bool discard_on_error,
Sean Callanan3aa7da52010-12-13 22:46:15 +0000393 lldb::addr_t *this_arg,
394 lldb::addr_t *cmd_arg)
Chris Lattner24943d22010-06-08 16:52:24 +0000395{
396 // FIXME: Use the errors Stream for better error reporting.
397
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000398 if (exe_ctx.thread == NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000399 {
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000400 errors.Printf("Can't call a function without a valid thread.");
Chris Lattner24943d22010-06-08 16:52:24 +0000401 return NULL;
402 }
403
404 // Okay, now run the function:
405
Sean Callanan841026f2010-07-23 00:16:21 +0000406 Address wrapper_address (NULL, func_addr);
Sean Callananc78d6482010-07-26 22:14:36 +0000407 ThreadPlan *new_plan = new ThreadPlanCallFunction (*exe_ctx.thread,
Sean Callanan3aa7da52010-12-13 22:46:15 +0000408 wrapper_address,
409 args_addr,
410 stop_others,
411 discard_on_error,
412 this_arg,
413 cmd_arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000414 return new_plan;
415}
416
417bool
Sean Callananc78d6482010-07-26 22:14:36 +0000418ClangFunction::FetchFunctionResults (ExecutionContext &exe_ctx, lldb::addr_t args_addr, Value &ret_value)
Chris Lattner24943d22010-06-08 16:52:24 +0000419{
420 // Read the return value - it is the last field in the struct:
421 // FIXME: How does clang tell us there's no return value? We need to handle that case.
422
Sean Callananc78d6482010-07-26 22:14:36 +0000423 Process *process = exe_ctx.process;
Jim Ingham53bfd062011-03-17 20:02:56 +0000424
425 if (process == NULL)
426 return false;
427 if (process != m_jit_process_sp.get())
428 return false;
429
Chris Lattner24943d22010-06-08 16:52:24 +0000430 Error error;
Greg Claytonc0fa5332011-05-22 22:46:53 +0000431 ret_value.GetScalar() = process->ReadUnsignedIntegerFromMemory (args_addr + m_return_offset, m_return_size, 0, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000432
Greg Claytonc0fa5332011-05-22 22:46:53 +0000433 if (error.Fail())
Chris Lattner24943d22010-06-08 16:52:24 +0000434 return false;
435
Greg Clayton6916e352010-11-13 03:52:47 +0000436 ret_value.SetContext (Value::eContextTypeClangType, m_function_return_qual_type);
Chris Lattner24943d22010-06-08 16:52:24 +0000437 ret_value.SetValueType(Value::eValueTypeScalar);
Chris Lattner24943d22010-06-08 16:52:24 +0000438 return true;
439}
440
441void
Sean Callananc78d6482010-07-26 22:14:36 +0000442ClangFunction::DeallocateFunctionResults (ExecutionContext &exe_ctx, lldb::addr_t args_addr)
Chris Lattner24943d22010-06-08 16:52:24 +0000443{
444 std::list<lldb::addr_t>::iterator pos;
445 pos = std::find(m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(), args_addr);
446 if (pos != m_wrapper_args_addrs.end())
447 m_wrapper_args_addrs.erase(pos);
448
Sean Callananc78d6482010-07-26 22:14:36 +0000449 exe_ctx.process->DeallocateMemory(args_addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000450}
451
Greg Claytonb3448432011-03-24 21:19:54 +0000452ExecutionResults
Sean Callananc78d6482010-07-26 22:14:36 +0000453ClangFunction::ExecuteFunction(ExecutionContext &exe_ctx, Stream &errors, Value &results)
Chris Lattner24943d22010-06-08 16:52:24 +0000454{
Sean Callananc78d6482010-07-26 22:14:36 +0000455 return ExecuteFunction (exe_ctx, errors, 1000, true, results);
Chris Lattner24943d22010-06-08 16:52:24 +0000456}
457
Greg Claytonb3448432011-03-24 21:19:54 +0000458ExecutionResults
Sean Callananc78d6482010-07-26 22:14:36 +0000459ClangFunction::ExecuteFunction(ExecutionContext &exe_ctx, Stream &errors, bool stop_others, Value &results)
Chris Lattner24943d22010-06-08 16:52:24 +0000460{
Jim Inghamea9d4262010-11-05 19:25:48 +0000461 const bool try_all_threads = false;
462 const bool discard_on_error = true;
463 return ExecuteFunction (exe_ctx, NULL, errors, stop_others, NULL, try_all_threads, discard_on_error, results);
Chris Lattner24943d22010-06-08 16:52:24 +0000464}
465
Greg Claytonb3448432011-03-24 21:19:54 +0000466ExecutionResults
Chris Lattner24943d22010-06-08 16:52:24 +0000467ClangFunction::ExecuteFunction(
Sean Callananc78d6482010-07-26 22:14:36 +0000468 ExecutionContext &exe_ctx,
Chris Lattner24943d22010-06-08 16:52:24 +0000469 Stream &errors,
470 uint32_t single_thread_timeout_usec,
471 bool try_all_threads,
472 Value &results)
473{
Jim Inghamea9d4262010-11-05 19:25:48 +0000474 const bool stop_others = true;
475 const bool discard_on_error = true;
476 return ExecuteFunction (exe_ctx, NULL, errors, stop_others, single_thread_timeout_usec,
477 try_all_threads, discard_on_error, results);
Chris Lattner24943d22010-06-08 16:52:24 +0000478}
479
Sean Callanan841026f2010-07-23 00:16:21 +0000480// This is the static function
Greg Claytonb3448432011-03-24 21:19:54 +0000481ExecutionResults
Sean Callanan841026f2010-07-23 00:16:21 +0000482ClangFunction::ExecuteFunction (
Sean Callananc78d6482010-07-26 22:14:36 +0000483 ExecutionContext &exe_ctx,
Sean Callanan841026f2010-07-23 00:16:21 +0000484 lldb::addr_t function_address,
485 lldb::addr_t &void_arg,
486 bool stop_others,
487 bool try_all_threads,
Jim Inghamea9d4262010-11-05 19:25:48 +0000488 bool discard_on_error,
Sean Callanan841026f2010-07-23 00:16:21 +0000489 uint32_t single_thread_timeout_usec,
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000490 Stream &errors,
491 lldb::addr_t *this_arg)
Chris Lattner24943d22010-06-08 16:52:24 +0000492{
Jim Inghamea9d4262010-11-05 19:25:48 +0000493 lldb::ThreadPlanSP call_plan_sp(ClangFunction::GetThreadPlanToCallFunction(exe_ctx, function_address, void_arg,
494 errors, stop_others, discard_on_error,
495 this_arg));
Chris Lattner24943d22010-06-08 16:52:24 +0000496 if (call_plan_sp == NULL)
Greg Claytonb3448432011-03-24 21:19:54 +0000497 return eExecutionSetupError;
Sean Callanan841026f2010-07-23 00:16:21 +0000498
Chris Lattner24943d22010-06-08 16:52:24 +0000499 call_plan_sp->SetPrivate(true);
Jim Ingham3ae449a2010-11-17 02:32:00 +0000500
Jim Ingham360f53f2010-11-30 02:22:11 +0000501 return exe_ctx.process->RunThreadPlan (exe_ctx, call_plan_sp, stop_others, try_all_threads, discard_on_error,
502 single_thread_timeout_usec, errors);
Sean Callanan841026f2010-07-23 00:16:21 +0000503}
Chris Lattner24943d22010-06-08 16:52:24 +0000504
Greg Claytonb3448432011-03-24 21:19:54 +0000505ExecutionResults
Sean Callanan841026f2010-07-23 00:16:21 +0000506ClangFunction::ExecuteFunction(
Sean Callananc78d6482010-07-26 22:14:36 +0000507 ExecutionContext &exe_ctx,
Sean Callanan841026f2010-07-23 00:16:21 +0000508 lldb::addr_t *args_addr_ptr,
509 Stream &errors,
510 bool stop_others,
511 uint32_t single_thread_timeout_usec,
Jim Inghamea9d4262010-11-05 19:25:48 +0000512 bool try_all_threads,
513 bool discard_on_error,
Sean Callanan841026f2010-07-23 00:16:21 +0000514 Value &results)
515{
516 using namespace clang;
Greg Claytonb3448432011-03-24 21:19:54 +0000517 ExecutionResults return_value = eExecutionSetupError;
Sean Callanan841026f2010-07-23 00:16:21 +0000518
519 lldb::addr_t args_addr;
520
521 if (args_addr_ptr != NULL)
522 args_addr = *args_addr_ptr;
523 else
524 args_addr = LLDB_INVALID_ADDRESS;
525
526 if (CompileFunction(errors) != 0)
Greg Claytonb3448432011-03-24 21:19:54 +0000527 return eExecutionSetupError;
Sean Callanan841026f2010-07-23 00:16:21 +0000528
529 if (args_addr == LLDB_INVALID_ADDRESS)
530 {
Sean Callananc78d6482010-07-26 22:14:36 +0000531 if (!InsertFunction(exe_ctx, args_addr, errors))
Greg Claytonb3448432011-03-24 21:19:54 +0000532 return eExecutionSetupError;
Sean Callanan841026f2010-07-23 00:16:21 +0000533 }
534
Greg Claytond0882d02011-01-19 23:00:49 +0000535 return_value = ClangFunction::ExecuteFunction (exe_ctx,
536 m_jit_start_addr,
537 args_addr,
538 stop_others,
539 try_all_threads,
540 discard_on_error,
541 single_thread_timeout_usec,
542 errors);
Sean Callanan841026f2010-07-23 00:16:21 +0000543
544 if (args_addr_ptr != NULL)
545 *args_addr_ptr = args_addr;
546
Greg Claytonb3448432011-03-24 21:19:54 +0000547 if (return_value != eExecutionCompleted)
Chris Lattner24943d22010-06-08 16:52:24 +0000548 return return_value;
549
Sean Callananc78d6482010-07-26 22:14:36 +0000550 FetchFunctionResults(exe_ctx, args_addr, results);
Chris Lattner24943d22010-06-08 16:52:24 +0000551
552 if (args_addr_ptr == NULL)
Sean Callananc78d6482010-07-26 22:14:36 +0000553 DeallocateFunctionResults(exe_ctx, args_addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000554
Greg Claytonb3448432011-03-24 21:19:54 +0000555 return eExecutionCompleted;
Chris Lattner24943d22010-06-08 16:52:24 +0000556}
557
Sean Callanan65dafa82010-08-27 01:01:44 +0000558clang::ASTConsumer *
559ClangFunction::ASTTransformer (clang::ASTConsumer *passthrough)
Chris Lattner24943d22010-06-08 16:52:24 +0000560{
Sean Callanan65dafa82010-08-27 01:01:44 +0000561 return new ASTStructExtractor(passthrough, m_wrapper_struct_name.c_str(), *this);
Chris Lattner24943d22010-06-08 16:52:24 +0000562}