blob: acfc71c146e93b1984cd2c47ef7094eb515ce3b1 [file] [log] [blame]
Jim Ingham151c0322015-09-15 21:13:50 +00001//===-- FunctionCaller.cpp ---------------------------------------*- C++ -*-===//
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002//
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 Lattner30fdc8d2010-06-08 16:52:24 +000014#include "clang/AST/ASTContext.h"
15#include "clang/AST/RecordLayout.h"
Greg Claytond2d60ce2010-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 Clayton514487e2011-02-15 21:59:32 +000020#include "llvm/ADT/Triple.h"
Greg Claytond2d60ce2010-07-02 18:39:06 +000021#include "llvm/ExecutionEngine/ExecutionEngine.h"
Chandler Carruth1e157582013-01-02 12:20:07 +000022#include "llvm/IR/Module.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023
24// Project includes
Greg Clayton23f8c952014-03-24 23:10:19 +000025#include "lldb/Core/DataExtractor.h"
26#include "lldb/Core/Log.h"
27#include "lldb/Core/Module.h"
28#include "lldb/Core/State.h"
29#include "lldb/Core/ValueObject.h"
30#include "lldb/Core/ValueObjectList.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000031#include "lldb/Expression/ASTStructExtractor.h"
32#include "lldb/Expression/ClangExpressionParser.h"
Jim Ingham151c0322015-09-15 21:13:50 +000033#include "lldb/Expression/FunctionCaller.h"
Greg Claytone01e07b2013-04-18 18:10:51 +000034#include "lldb/Expression/IRExecutionUnit.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035#include "lldb/Interpreter/CommandReturnObject.h"
36#include "lldb/Symbol/ClangASTContext.h"
37#include "lldb/Symbol/Function.h"
Greg Clayton23f8c952014-03-24 23:10:19 +000038#include "lldb/Symbol/Type.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039#include "lldb/Target/ExecutionContext.h"
40#include "lldb/Target/Process.h"
Sean Callananebf77072010-07-23 00:16:21 +000041#include "lldb/Target/RegisterContext.h"
Greg Clayton514487e2011-02-15 21:59:32 +000042#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043#include "lldb/Target/Thread.h"
44#include "lldb/Target/ThreadPlan.h"
45#include "lldb/Target/ThreadPlanCallFunction.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046
47using namespace lldb_private;
Sean Callanan1a8d4092010-08-27 01:01:44 +000048
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049//----------------------------------------------------------------------
Jim Ingham151c0322015-09-15 21:13:50 +000050// FunctionCaller constructor
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051//----------------------------------------------------------------------
Jim Ingham151c0322015-09-15 21:13:50 +000052FunctionCaller::FunctionCaller
Greg Clayton7b462cc2010-10-15 22:48:33 +000053(
Jim Ingham35944dd2011-03-17 20:02:56 +000054 ExecutionContextScope &exe_scope,
Greg Claytona1e5dc82015-08-11 22:53:00 +000055 const CompilerType &return_type,
Greg Clayton7b462cc2010-10-15 22:48:33 +000056 const Address& functionAddress,
Jim Ingham23ef27c2014-04-22 01:42:22 +000057 const ValueList &arg_value_list,
58 const char *name
Greg Clayton7b462cc2010-10-15 22:48:33 +000059) :
Jim Ingham151c0322015-09-15 21:13:50 +000060 Expression (exe_scope),
Greg Clayton23f8c952014-03-24 23:10:19 +000061 m_execution_unit_sp(),
Duncan P. N. Exon Smithf3079d22014-12-10 04:59:18 +000062 m_parser(),
Greg Clayton23f8c952014-03-24 23:10:19 +000063 m_jit_module_wp(),
Jim Ingham23ef27c2014-04-22 01:42:22 +000064 m_name (name ? name : "<unknown>"),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000065 m_function_ptr (NULL),
Greg Claytonc982c762010-07-09 20:39:50 +000066 m_function_addr (functionAddress),
Greg Clayton57ee3062013-07-11 22:46:58 +000067 m_function_return_type(return_type),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000068 m_wrapper_function_name ("__lldb_caller_function"),
69 m_wrapper_struct_name ("__lldb_caller_struct"),
Greg Claytonc982c762010-07-09 20:39:50 +000070 m_wrapper_args_addrs (),
Greg Claytonc982c762010-07-09 20:39:50 +000071 m_arg_values (arg_value_list),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072 m_compiled (false),
73 m_JITted (false)
74{
Enrico Granatadfc88a02012-09-18 00:08:47 +000075 m_jit_process_wp = lldb::ProcessWP(exe_scope.CalculateProcess());
Jim Ingham151c0322015-09-15 21:13:50 +000076 // Can't make a FunctionCaller without a process.
Enrico Granatadfc88a02012-09-18 00:08:47 +000077 assert (m_jit_process_wp.lock());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000078}
79
Chris Lattner30fdc8d2010-06-08 16:52:24 +000080//----------------------------------------------------------------------
81// Destructor
82//----------------------------------------------------------------------
Jim Ingham151c0322015-09-15 21:13:50 +000083FunctionCaller::~FunctionCaller()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000084{
Greg Clayton23f8c952014-03-24 23:10:19 +000085 lldb::ProcessSP process_sp (m_jit_process_wp.lock());
86 if (process_sp)
87 {
88 lldb::ModuleSP jit_module_sp (m_jit_module_wp.lock());
89 if (jit_module_sp)
90 process_sp->GetTarget().GetImages().Remove(jit_module_sp);
91 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000092}
93
Chris Lattner30fdc8d2010-06-08 16:52:24 +000094bool
Jim Ingham151c0322015-09-15 21:13:50 +000095FunctionCaller::WriteFunctionWrapper (ExecutionContext &exe_ctx, Stream &errors)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000096{
Greg Claytonc14ee322011-09-22 04:58:26 +000097 Process *process = exe_ctx.GetProcessPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000098
Sean Callanan1a8d4092010-08-27 01:01:44 +000099 if (!process)
100 return false;
Enrico Granatadfc88a02012-09-18 00:08:47 +0000101
102 lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
103
104 if (process != jit_process_sp.get())
Jim Ingham35944dd2011-03-17 20:02:56 +0000105 return false;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000106
107 if (!m_compiled)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000108 return false;
109
Sean Callanan1a8d4092010-08-27 01:01:44 +0000110 if (m_JITted)
111 return true;
Sean Callanan1582ee62013-04-18 22:06:33 +0000112
113 bool can_interpret = false; // should stay that way
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000114
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000115 Error jit_error (m_parser->PrepareForExecution (m_jit_start_addr,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000116 m_jit_end_addr,
Greg Clayton23f8c952014-03-24 23:10:19 +0000117 m_execution_unit_sp,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000118 exe_ctx,
Sean Callanan1582ee62013-04-18 22:06:33 +0000119 can_interpret,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000120 eExecutionPolicyAlways));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000121
122 if (!jit_error.Success())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000123 return false;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000124
Greg Clayton23f8c952014-03-24 23:10:19 +0000125 if (m_parser->GetGenerateDebugInfo())
126 {
127 lldb::ModuleSP jit_module_sp ( m_execution_unit_sp->GetJITModule());
128
129 if (jit_module_sp)
130 {
131 ConstString const_func_name(FunctionName());
132 FileSpec jit_file;
133 jit_file.GetFilename() = const_func_name;
134 jit_module_sp->SetFileSpecAndObjectName (jit_file, ConstString());
135 m_jit_module_wp = jit_module_sp;
136 process->GetTarget().GetImages().Append(jit_module_sp);
137 }
138 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000139 if (process && m_jit_start_addr)
Greg Clayton23f8c952014-03-24 23:10:19 +0000140 m_jit_process_wp = process->shared_from_this();
Sean Callanand14fac12013-01-14 21:45:38 +0000141
142 m_JITted = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000143
144 return true;
145}
146
147bool
Jim Ingham151c0322015-09-15 21:13:50 +0000148FunctionCaller::WriteFunctionArguments (ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref, Stream &errors)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000149{
Jim Ingham151c0322015-09-15 21:13:50 +0000150 return WriteFunctionArguments(exe_ctx, args_addr_ref, m_arg_values, errors);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000151}
152
153// FIXME: Assure that the ValueList we were passed in is consistent with the one that defined this function.
154
155bool
Jim Ingham151c0322015-09-15 21:13:50 +0000156FunctionCaller::WriteFunctionArguments (ExecutionContext &exe_ctx,
Jim Inghambaae1682010-09-10 23:07:48 +0000157 lldb::addr_t &args_addr_ref,
Jim Ingham151c0322015-09-15 21:13:50 +0000158 ValueList &arg_values,
Jim Inghambaae1682010-09-10 23:07:48 +0000159 Stream &errors)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000160{
Sean Callanan1a8d4092010-08-27 01:01:44 +0000161 // All the information to reconstruct the struct is provided by the
162 // StructExtractor.
163 if (!m_struct_valid)
164 {
165 errors.Printf("Argument information was not correctly parsed, so the function cannot be called.");
166 return false;
167 }
168
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000169 Error error;
170 using namespace clang;
Jim Ingham8646d3c2014-05-05 02:47:44 +0000171 lldb::ExpressionResults return_value = lldb::eExpressionSetupError;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000172
Greg Claytonc14ee322011-09-22 04:58:26 +0000173 Process *process = exe_ctx.GetProcessPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000174
175 if (process == NULL)
176 return return_value;
Jim Ingham35944dd2011-03-17 20:02:56 +0000177
Enrico Granatadfc88a02012-09-18 00:08:47 +0000178 lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
179
180 if (process != jit_process_sp.get())
Jim Ingham35944dd2011-03-17 20:02:56 +0000181 return false;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000182
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000183 if (args_addr_ref == LLDB_INVALID_ADDRESS)
184 {
Sean Callanan1a8d4092010-08-27 01:01:44 +0000185 args_addr_ref = process->AllocateMemory(m_struct_size, lldb::ePermissionsReadable|lldb::ePermissionsWritable, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000186 if (args_addr_ref == LLDB_INVALID_ADDRESS)
187 return false;
188 m_wrapper_args_addrs.push_back (args_addr_ref);
189 }
190 else
191 {
192 // Make sure this is an address that we've already handed out.
193 if (find (m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(), args_addr_ref) == m_wrapper_args_addrs.end())
194 {
195 return false;
196 }
197 }
198
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000199 // TODO: verify fun_addr needs to be a callable address
Jim Ingham151c0322015-09-15 21:13:50 +0000200 Scalar fun_addr (m_function_addr.GetCallableLoadAddress(exe_ctx.GetTargetPtr()));
Greg Claytonc7bece562013-01-25 18:06:21 +0000201 uint64_t first_offset = m_member_offsets[0];
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000202 process->WriteScalarToMemory(args_addr_ref + first_offset, fun_addr, process->GetAddressByteSize(), error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000203
204 // FIXME: We will need to extend this for Variadic functions.
205
206 Error value_error;
207
208 size_t num_args = arg_values.GetSize();
209 if (num_args != m_arg_values.GetSize())
210 {
Greg Clayton6fea17e2014-03-03 19:15:20 +0000211 errors.Printf ("Wrong number of arguments - was: %" PRIu64 " should be: %" PRIu64 "", (uint64_t)num_args, (uint64_t)m_arg_values.GetSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000212 return false;
213 }
214
Greg Claytonc982c762010-07-09 20:39:50 +0000215 for (size_t i = 0; i < num_args; i++)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000216 {
217 // FIXME: We should sanity check sizes.
218
Greg Claytonc7bece562013-01-25 18:06:21 +0000219 uint64_t offset = m_member_offsets[i+1]; // Clang sizes are in bytes.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000220 Value *arg_value = arg_values.GetValueAtIndex(i);
221
222 // FIXME: For now just do scalars:
223
224 // Special case: if it's a pointer, don't do anything (the ABI supports passing cstrings)
225
226 if (arg_value->GetValueType() == Value::eValueTypeHostAddress &&
Greg Clayton57ee3062013-07-11 22:46:58 +0000227 arg_value->GetContextType() == Value::eContextTypeInvalid &&
Greg Clayton99558cc42015-08-24 23:46:31 +0000228 arg_value->GetCompilerType().IsPointerType())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000229 continue;
230
Greg Clayton57ee3062013-07-11 22:46:58 +0000231 const Scalar &arg_scalar = arg_value->ResolveValue(&exe_ctx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000232
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000233 if (!process->WriteScalarToMemory(args_addr_ref + offset, arg_scalar, arg_scalar.GetByteSize(), error))
234 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000235 }
236
237 return true;
238}
239
240bool
Jim Ingham151c0322015-09-15 21:13:50 +0000241FunctionCaller::InsertFunction (ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref, Stream &errors)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000242{
243 using namespace clang;
244
245 if (CompileFunction(errors) != 0)
246 return false;
Sean Callanan138e74e2010-07-26 22:14:36 +0000247 if (!WriteFunctionWrapper(exe_ctx, errors))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000248 return false;
Sean Callanan138e74e2010-07-26 22:14:36 +0000249 if (!WriteFunctionArguments(exe_ctx, args_addr_ref, errors))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000250 return false;
251
Greg Clayton5160ce52013-03-27 23:08:40 +0000252 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000253 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000254 log->Printf ("Call Address: 0x%" PRIx64 " Struct Address: 0x%" PRIx64 ".\n", m_jit_start_addr, args_addr_ref);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000255
256 return true;
257}
258
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000259lldb::ThreadPlanSP
Jim Ingham151c0322015-09-15 21:13:50 +0000260FunctionCaller::GetThreadPlanToCallFunction (ExecutionContext &exe_ctx,
Sean Callanana464f3d2013-11-08 01:14:26 +0000261 lldb::addr_t args_addr,
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000262 const EvaluateExpressionOptions &options,
263 Stream &errors)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000264{
Greg Clayton5160ce52013-03-27 23:08:40 +0000265 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callananf58b12d2013-03-06 19:57:25 +0000266
267 if (log)
Jim Ingham151c0322015-09-15 21:13:50 +0000268 log->Printf("-- [FunctionCaller::GetThreadPlanToCallFunction] Creating thread plan to call function \"%s\" --", m_name.c_str());
Sean Callananf58b12d2013-03-06 19:57:25 +0000269
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000270 // FIXME: Use the errors Stream for better error reporting.
Greg Claytonc14ee322011-09-22 04:58:26 +0000271 Thread *thread = exe_ctx.GetThreadPtr();
272 if (thread == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000273 {
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000274 errors.Printf("Can't call a function without a valid thread.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000275 return NULL;
276 }
277
278 // Okay, now run the function:
279
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000280 Address wrapper_address (m_jit_start_addr);
Sean Callanana464f3d2013-11-08 01:14:26 +0000281
282 lldb::addr_t args = { args_addr };
283
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000284 lldb::ThreadPlanSP new_plan_sp (new ThreadPlanCallFunction (*thread,
Sean Callanan17827832010-12-13 22:46:15 +0000285 wrapper_address,
Greg Claytona1e5dc82015-08-11 22:53:00 +0000286 CompilerType(),
Sean Callanana464f3d2013-11-08 01:14:26 +0000287 args,
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000288 options));
289 new_plan_sp->SetIsMasterPlan(true);
290 new_plan_sp->SetOkayToDiscard (false);
291 return new_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000292}
293
294bool
Jim Ingham151c0322015-09-15 21:13:50 +0000295FunctionCaller::FetchFunctionResults (ExecutionContext &exe_ctx, lldb::addr_t args_addr, Value &ret_value)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000296{
297 // Read the return value - it is the last field in the struct:
298 // FIXME: How does clang tell us there's no return value? We need to handle that case.
Greg Claytona1e5dc82015-08-11 22:53:00 +0000299 // FIXME: Create our ThreadPlanCallFunction with the return CompilerType, and then use GetReturnValueObject
Jim Inghamef651602011-12-22 19:12:40 +0000300 // to fetch the value. That way we can fetch any values we need.
Sean Callananf58b12d2013-03-06 19:57:25 +0000301
Greg Clayton5160ce52013-03-27 23:08:40 +0000302 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
Sean Callananf58b12d2013-03-06 19:57:25 +0000303
304 if (log)
Jim Ingham151c0322015-09-15 21:13:50 +0000305 log->Printf("-- [FunctionCaller::FetchFunctionResults] Fetching function results for \"%s\"--", m_name.c_str());
Sean Callananf58b12d2013-03-06 19:57:25 +0000306
Greg Claytonc14ee322011-09-22 04:58:26 +0000307 Process *process = exe_ctx.GetProcessPtr();
Jim Ingham35944dd2011-03-17 20:02:56 +0000308
309 if (process == NULL)
310 return false;
Enrico Granatadfc88a02012-09-18 00:08:47 +0000311
312 lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
313
314 if (process != jit_process_sp.get())
Jim Ingham35944dd2011-03-17 20:02:56 +0000315 return false;
316
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000317 Error error;
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000318 ret_value.GetScalar() = process->ReadUnsignedIntegerFromMemory (args_addr + m_return_offset, m_return_size, 0, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000319
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000320 if (error.Fail())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000321 return false;
322
Greg Clayton99558cc42015-08-24 23:46:31 +0000323 ret_value.SetCompilerType(m_function_return_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000324 ret_value.SetValueType(Value::eValueTypeScalar);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000325 return true;
326}
327
328void
Jim Ingham151c0322015-09-15 21:13:50 +0000329FunctionCaller::DeallocateFunctionResults (ExecutionContext &exe_ctx, lldb::addr_t args_addr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000330{
331 std::list<lldb::addr_t>::iterator pos;
332 pos = std::find(m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(), args_addr);
333 if (pos != m_wrapper_args_addrs.end())
334 m_wrapper_args_addrs.erase(pos);
335
Greg Claytonc14ee322011-09-22 04:58:26 +0000336 exe_ctx.GetProcessRef().DeallocateMemory(args_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000337}
338
Jim Ingham1624a2d2014-05-05 02:26:40 +0000339lldb::ExpressionResults
Jim Ingham151c0322015-09-15 21:13:50 +0000340FunctionCaller::ExecuteFunction(
Sean Callanan138e74e2010-07-26 22:14:36 +0000341 ExecutionContext &exe_ctx,
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000342 lldb::addr_t *args_addr_ptr,
343 const EvaluateExpressionOptions &options,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000344 Stream &errors,
Sean Callananebf77072010-07-23 00:16:21 +0000345 Value &results)
346{
347 using namespace clang;
Jim Ingham8646d3c2014-05-05 02:47:44 +0000348 lldb::ExpressionResults return_value = lldb::eExpressionSetupError;
Sean Callananebf77072010-07-23 00:16:21 +0000349
Jim Ingham151c0322015-09-15 21:13:50 +0000350 // FunctionCaller::ExecuteFunction execution is always just to get the result. Do make sure we ignore
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000351 // breakpoints, unwind on error, and don't try to debug it.
352 EvaluateExpressionOptions real_options = options;
353 real_options.SetDebug(false);
354 real_options.SetUnwindOnError(true);
355 real_options.SetIgnoreBreakpoints(true);
356
Sean Callananebf77072010-07-23 00:16:21 +0000357 lldb::addr_t args_addr;
358
359 if (args_addr_ptr != NULL)
360 args_addr = *args_addr_ptr;
361 else
362 args_addr = LLDB_INVALID_ADDRESS;
363
364 if (CompileFunction(errors) != 0)
Jim Ingham8646d3c2014-05-05 02:47:44 +0000365 return lldb::eExpressionSetupError;
Sean Callananebf77072010-07-23 00:16:21 +0000366
367 if (args_addr == LLDB_INVALID_ADDRESS)
368 {
Sean Callanan138e74e2010-07-26 22:14:36 +0000369 if (!InsertFunction(exe_ctx, args_addr, errors))
Jim Ingham8646d3c2014-05-05 02:47:44 +0000370 return lldb::eExpressionSetupError;
Sean Callananebf77072010-07-23 00:16:21 +0000371 }
Sean Callananebf77072010-07-23 00:16:21 +0000372
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000373 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
374
375 if (log)
Jim Ingham151c0322015-09-15 21:13:50 +0000376 log->Printf("== [FunctionCaller::ExecuteFunction] Executing function \"%s\" ==", m_name.c_str());
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000377
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000378 lldb::ThreadPlanSP call_plan_sp = GetThreadPlanToCallFunction (exe_ctx,
379 args_addr,
380 real_options,
381 errors);
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000382 if (!call_plan_sp)
Jim Ingham8646d3c2014-05-05 02:47:44 +0000383 return lldb::eExpressionSetupError;
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000384
Jim Ingham23b95f02014-03-12 23:43:15 +0000385 // We need to make sure we record the fact that we are running an expression here
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000386 // otherwise this fact will fail to be recorded when fetching an Objective-C object description
387 if (exe_ctx.GetProcessPtr())
388 exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
389
390 return_value = exe_ctx.GetProcessRef().RunThreadPlan (exe_ctx,
391 call_plan_sp,
392 real_options,
393 errors);
394
395 if (log)
396 {
Jim Ingham8646d3c2014-05-05 02:47:44 +0000397 if (return_value != lldb::eExpressionCompleted)
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000398 {
Jim Ingham151c0322015-09-15 21:13:50 +0000399 log->Printf("== [FunctionCaller::ExecuteFunction] Execution of \"%s\" completed abnormally ==", m_name.c_str());
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000400 }
401 else
402 {
Jim Ingham151c0322015-09-15 21:13:50 +0000403 log->Printf("== [FunctionCaller::ExecuteFunction] Execution of \"%s\" completed normally ==", m_name.c_str());
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000404 }
405 }
406
407 if (exe_ctx.GetProcessPtr())
408 exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
409
Sean Callananebf77072010-07-23 00:16:21 +0000410 if (args_addr_ptr != NULL)
411 *args_addr_ptr = args_addr;
412
Jim Ingham8646d3c2014-05-05 02:47:44 +0000413 if (return_value != lldb::eExpressionCompleted)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000414 return return_value;
415
Sean Callanan138e74e2010-07-26 22:14:36 +0000416 FetchFunctionResults(exe_ctx, args_addr, results);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000417
418 if (args_addr_ptr == NULL)
Sean Callanan138e74e2010-07-26 22:14:36 +0000419 DeallocateFunctionResults(exe_ctx, args_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000420
Jim Ingham8646d3c2014-05-05 02:47:44 +0000421 return lldb::eExpressionCompleted;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000422}