1*0b57cec5SDimitry Andric //===-- FunctionCaller.cpp ------------------------------------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric
9*0b57cec5SDimitry Andric
10*0b57cec5SDimitry Andric #include "lldb/Expression/FunctionCaller.h"
11*0b57cec5SDimitry Andric #include "lldb/Core/Module.h"
12*0b57cec5SDimitry Andric #include "lldb/Core/ValueObject.h"
13*0b57cec5SDimitry Andric #include "lldb/Core/ValueObjectList.h"
14*0b57cec5SDimitry Andric #include "lldb/Expression/DiagnosticManager.h"
15*0b57cec5SDimitry Andric #include "lldb/Expression/IRExecutionUnit.h"
16*0b57cec5SDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h"
17*0b57cec5SDimitry Andric #include "lldb/Symbol/Function.h"
18*0b57cec5SDimitry Andric #include "lldb/Symbol/Type.h"
19*0b57cec5SDimitry Andric #include "lldb/Target/ExecutionContext.h"
20*0b57cec5SDimitry Andric #include "lldb/Target/Process.h"
21*0b57cec5SDimitry Andric #include "lldb/Target/RegisterContext.h"
22*0b57cec5SDimitry Andric #include "lldb/Target/Target.h"
23*0b57cec5SDimitry Andric #include "lldb/Target/Thread.h"
24*0b57cec5SDimitry Andric #include "lldb/Target/ThreadPlan.h"
25*0b57cec5SDimitry Andric #include "lldb/Target/ThreadPlanCallFunction.h"
26*0b57cec5SDimitry Andric #include "lldb/Utility/DataExtractor.h"
27*0b57cec5SDimitry Andric #include "lldb/Utility/LLDBLog.h"
28*0b57cec5SDimitry Andric #include "lldb/Utility/Log.h"
29*0b57cec5SDimitry Andric #include "lldb/Utility/State.h"
30*0b57cec5SDimitry Andric
31*0b57cec5SDimitry Andric using namespace lldb_private;
32*0b57cec5SDimitry Andric
33*0b57cec5SDimitry Andric char FunctionCaller::ID;
34*0b57cec5SDimitry Andric
35*0b57cec5SDimitry Andric // FunctionCaller constructor
FunctionCaller(ExecutionContextScope & exe_scope,const CompilerType & return_type,const Address & functionAddress,const ValueList & arg_value_list,const char * name)36*0b57cec5SDimitry Andric FunctionCaller::FunctionCaller(ExecutionContextScope &exe_scope,
37*0b57cec5SDimitry Andric const CompilerType &return_type,
38*0b57cec5SDimitry Andric const Address &functionAddress,
39*0b57cec5SDimitry Andric const ValueList &arg_value_list,
40*0b57cec5SDimitry Andric const char *name)
41*0b57cec5SDimitry Andric : Expression(exe_scope), m_execution_unit_sp(), m_parser(),
42*0b57cec5SDimitry Andric m_jit_module_wp(), m_name(name ? name : "<unknown>"),
43*0b57cec5SDimitry Andric m_function_ptr(nullptr), m_function_addr(functionAddress),
44*0b57cec5SDimitry Andric m_function_return_type(return_type),
45*0b57cec5SDimitry Andric m_wrapper_function_name("__lldb_caller_function"),
46*0b57cec5SDimitry Andric m_wrapper_struct_name("__lldb_caller_struct"), m_wrapper_args_addrs(),
47*0b57cec5SDimitry Andric m_struct_valid(false), m_struct_size(0), m_return_size(0),
48*0b57cec5SDimitry Andric m_return_offset(0), m_arg_values(arg_value_list), m_compiled(false),
49*0b57cec5SDimitry Andric m_JITted(false) {
50*0b57cec5SDimitry Andric m_jit_process_wp = lldb::ProcessWP(exe_scope.CalculateProcess());
51*0b57cec5SDimitry Andric // Can't make a FunctionCaller without a process.
52*0b57cec5SDimitry Andric assert(m_jit_process_wp.lock());
53*0b57cec5SDimitry Andric }
54*0b57cec5SDimitry Andric
55*0b57cec5SDimitry Andric // Destructor
~FunctionCaller()56*0b57cec5SDimitry Andric FunctionCaller::~FunctionCaller() {
57*0b57cec5SDimitry Andric lldb::ProcessSP process_sp(m_jit_process_wp.lock());
58*0b57cec5SDimitry Andric if (process_sp) {
59*0b57cec5SDimitry Andric lldb::ModuleSP jit_module_sp(m_jit_module_wp.lock());
60*0b57cec5SDimitry Andric if (jit_module_sp)
61*0b57cec5SDimitry Andric process_sp->GetTarget().GetImages().Remove(jit_module_sp);
62*0b57cec5SDimitry Andric }
63*0b57cec5SDimitry Andric }
64*0b57cec5SDimitry Andric
WriteFunctionWrapper(ExecutionContext & exe_ctx,DiagnosticManager & diagnostic_manager)65*0b57cec5SDimitry Andric bool FunctionCaller::WriteFunctionWrapper(
66*0b57cec5SDimitry Andric ExecutionContext &exe_ctx, DiagnosticManager &diagnostic_manager) {
67*0b57cec5SDimitry Andric Process *process = exe_ctx.GetProcessPtr();
68*0b57cec5SDimitry Andric
69*0b57cec5SDimitry Andric if (!process) {
70*0b57cec5SDimitry Andric diagnostic_manager.Printf(eDiagnosticSeverityError, "no process.");
71*0b57cec5SDimitry Andric return false;
72*0b57cec5SDimitry Andric }
73*0b57cec5SDimitry Andric
74*0b57cec5SDimitry Andric lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
75*0b57cec5SDimitry Andric
76*0b57cec5SDimitry Andric if (process != jit_process_sp.get()) {
77*0b57cec5SDimitry Andric diagnostic_manager.Printf(eDiagnosticSeverityError,
78*0b57cec5SDimitry Andric "process does not match the stored process.");
79*0b57cec5SDimitry Andric return false;
80*0b57cec5SDimitry Andric }
81*0b57cec5SDimitry Andric
82*0b57cec5SDimitry Andric if (process->GetState() != lldb::eStateStopped) {
83*0b57cec5SDimitry Andric diagnostic_manager.Printf(eDiagnosticSeverityError,
84*0b57cec5SDimitry Andric "process is not stopped");
85*0b57cec5SDimitry Andric return false;
86*0b57cec5SDimitry Andric }
87*0b57cec5SDimitry Andric
88*0b57cec5SDimitry Andric if (!m_compiled) {
89*0b57cec5SDimitry Andric diagnostic_manager.Printf(eDiagnosticSeverityError,
90*0b57cec5SDimitry Andric "function not compiled");
91*0b57cec5SDimitry Andric return false;
92*0b57cec5SDimitry Andric }
93*0b57cec5SDimitry Andric
94*0b57cec5SDimitry Andric if (m_JITted)
95*0b57cec5SDimitry Andric return true;
96*0b57cec5SDimitry Andric
97*0b57cec5SDimitry Andric bool can_interpret = false; // should stay that way
98*0b57cec5SDimitry Andric
99*0b57cec5SDimitry Andric Status jit_error(m_parser->PrepareForExecution(
100*0b57cec5SDimitry Andric m_jit_start_addr, m_jit_end_addr, m_execution_unit_sp, exe_ctx,
101*0b57cec5SDimitry Andric can_interpret, eExecutionPolicyAlways));
102*0b57cec5SDimitry Andric
103*0b57cec5SDimitry Andric if (!jit_error.Success()) {
104*0b57cec5SDimitry Andric diagnostic_manager.Printf(eDiagnosticSeverityError,
105*0b57cec5SDimitry Andric "Error in PrepareForExecution: %s.",
106*0b57cec5SDimitry Andric jit_error.AsCString());
107*0b57cec5SDimitry Andric return false;
108*0b57cec5SDimitry Andric }
109*0b57cec5SDimitry Andric
110*0b57cec5SDimitry Andric if (m_parser->GetGenerateDebugInfo()) {
111*0b57cec5SDimitry Andric lldb::ModuleSP jit_module_sp(m_execution_unit_sp->GetJITModule());
112*0b57cec5SDimitry Andric
113*0b57cec5SDimitry Andric if (jit_module_sp) {
114*0b57cec5SDimitry Andric ConstString const_func_name(FunctionName());
115*0b57cec5SDimitry Andric FileSpec jit_file;
116*0b57cec5SDimitry Andric jit_file.SetFilename(const_func_name);
117*0b57cec5SDimitry Andric jit_module_sp->SetFileSpecAndObjectName(jit_file, ConstString());
118*0b57cec5SDimitry Andric m_jit_module_wp = jit_module_sp;
119*0b57cec5SDimitry Andric process->GetTarget().GetImages().Append(jit_module_sp,
120*0b57cec5SDimitry Andric true /* notify */);
121*0b57cec5SDimitry Andric }
122*0b57cec5SDimitry Andric }
123*0b57cec5SDimitry Andric if (process && m_jit_start_addr)
124*0b57cec5SDimitry Andric m_jit_process_wp = process->shared_from_this();
125*0b57cec5SDimitry Andric
126*0b57cec5SDimitry Andric m_JITted = true;
127*0b57cec5SDimitry Andric
128*0b57cec5SDimitry Andric return true;
129*0b57cec5SDimitry Andric }
130*0b57cec5SDimitry Andric
WriteFunctionArguments(ExecutionContext & exe_ctx,lldb::addr_t & args_addr_ref,DiagnosticManager & diagnostic_manager)131*0b57cec5SDimitry Andric bool FunctionCaller::WriteFunctionArguments(
132*0b57cec5SDimitry Andric ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref,
133*0b57cec5SDimitry Andric DiagnosticManager &diagnostic_manager) {
134*0b57cec5SDimitry Andric return WriteFunctionArguments(exe_ctx, args_addr_ref, m_arg_values,
135*0b57cec5SDimitry Andric diagnostic_manager);
136*0b57cec5SDimitry Andric }
137*0b57cec5SDimitry Andric
138*0b57cec5SDimitry Andric // FIXME: Assure that the ValueList we were passed in is consistent with the one
139*0b57cec5SDimitry Andric // that defined this function.
140*0b57cec5SDimitry Andric
WriteFunctionArguments(ExecutionContext & exe_ctx,lldb::addr_t & args_addr_ref,ValueList & arg_values,DiagnosticManager & diagnostic_manager)141*0b57cec5SDimitry Andric bool FunctionCaller::WriteFunctionArguments(
142*0b57cec5SDimitry Andric ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref,
143*0b57cec5SDimitry Andric ValueList &arg_values, DiagnosticManager &diagnostic_manager) {
144*0b57cec5SDimitry Andric // All the information to reconstruct the struct is provided by the
145*0b57cec5SDimitry Andric // StructExtractor.
146*0b57cec5SDimitry Andric if (!m_struct_valid) {
147*0b57cec5SDimitry Andric diagnostic_manager.PutString(eDiagnosticSeverityError,
148*0b57cec5SDimitry Andric "Argument information was not correctly "
149*0b57cec5SDimitry Andric "parsed, so the function cannot be called.");
150*0b57cec5SDimitry Andric return false;
151*0b57cec5SDimitry Andric }
152*0b57cec5SDimitry Andric
153*0b57cec5SDimitry Andric Status error;
154*0b57cec5SDimitry Andric lldb::ExpressionResults return_value = lldb::eExpressionSetupError;
155*0b57cec5SDimitry Andric
156*0b57cec5SDimitry Andric Process *process = exe_ctx.GetProcessPtr();
157*0b57cec5SDimitry Andric
158*0b57cec5SDimitry Andric if (process == nullptr)
159*0b57cec5SDimitry Andric return return_value;
160*0b57cec5SDimitry Andric
161*0b57cec5SDimitry Andric lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
162*0b57cec5SDimitry Andric
163*0b57cec5SDimitry Andric if (process != jit_process_sp.get())
164*0b57cec5SDimitry Andric return false;
165*0b57cec5SDimitry Andric
166*0b57cec5SDimitry Andric if (args_addr_ref == LLDB_INVALID_ADDRESS) {
167*0b57cec5SDimitry Andric args_addr_ref = process->AllocateMemory(
168*0b57cec5SDimitry Andric m_struct_size, lldb::ePermissionsReadable | lldb::ePermissionsWritable,
169*0b57cec5SDimitry Andric error);
170*0b57cec5SDimitry Andric if (args_addr_ref == LLDB_INVALID_ADDRESS)
171*0b57cec5SDimitry Andric return false;
172*0b57cec5SDimitry Andric m_wrapper_args_addrs.push_back(args_addr_ref);
173*0b57cec5SDimitry Andric } else {
174*0b57cec5SDimitry Andric // Make sure this is an address that we've already handed out.
175*0b57cec5SDimitry Andric if (find(m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(),
176*0b57cec5SDimitry Andric args_addr_ref) == m_wrapper_args_addrs.end()) {
177*0b57cec5SDimitry Andric return false;
178*0b57cec5SDimitry Andric }
179*0b57cec5SDimitry Andric }
180*0b57cec5SDimitry Andric
181*0b57cec5SDimitry Andric // TODO: verify fun_addr needs to be a callable address
182*0b57cec5SDimitry Andric Scalar fun_addr(
183*0b57cec5SDimitry Andric m_function_addr.GetCallableLoadAddress(exe_ctx.GetTargetPtr()));
184*0b57cec5SDimitry Andric uint64_t first_offset = m_member_offsets[0];
185*0b57cec5SDimitry Andric process->WriteScalarToMemory(args_addr_ref + first_offset, fun_addr,
186*0b57cec5SDimitry Andric process->GetAddressByteSize(), error);
187*0b57cec5SDimitry Andric
188*0b57cec5SDimitry Andric // FIXME: We will need to extend this for Variadic functions.
189*0b57cec5SDimitry Andric
190*0b57cec5SDimitry Andric Status value_error;
191*0b57cec5SDimitry Andric
192*0b57cec5SDimitry Andric size_t num_args = arg_values.GetSize();
193*0b57cec5SDimitry Andric if (num_args != m_arg_values.GetSize()) {
194*0b57cec5SDimitry Andric diagnostic_manager.Printf(
195*0b57cec5SDimitry Andric eDiagnosticSeverityError,
196*0b57cec5SDimitry Andric "Wrong number of arguments - was: %" PRIu64 " should be: %" PRIu64 "",
197*0b57cec5SDimitry Andric (uint64_t)num_args, (uint64_t)m_arg_values.GetSize());
198*0b57cec5SDimitry Andric return false;
199*0b57cec5SDimitry Andric }
200*0b57cec5SDimitry Andric
201*0b57cec5SDimitry Andric for (size_t i = 0; i < num_args; i++) {
202*0b57cec5SDimitry Andric // FIXME: We should sanity check sizes.
203*0b57cec5SDimitry Andric
204*0b57cec5SDimitry Andric uint64_t offset = m_member_offsets[i + 1]; // Clang sizes are in bytes.
205*0b57cec5SDimitry Andric Value *arg_value = arg_values.GetValueAtIndex(i);
206*0b57cec5SDimitry Andric
207*0b57cec5SDimitry Andric // FIXME: For now just do scalars:
208*0b57cec5SDimitry Andric
209*0b57cec5SDimitry Andric // Special case: if it's a pointer, don't do anything (the ABI supports
210*0b57cec5SDimitry Andric // passing cstrings)
211*0b57cec5SDimitry Andric
212*0b57cec5SDimitry Andric if (arg_value->GetValueType() == Value::ValueType::HostAddress &&
213*0b57cec5SDimitry Andric arg_value->GetContextType() == Value::ContextType::Invalid &&
214*0b57cec5SDimitry Andric arg_value->GetCompilerType().IsPointerType())
215*0b57cec5SDimitry Andric continue;
216*0b57cec5SDimitry Andric
217*0b57cec5SDimitry Andric const Scalar &arg_scalar = arg_value->ResolveValue(&exe_ctx);
218*0b57cec5SDimitry Andric
219*0b57cec5SDimitry Andric if (!process->WriteScalarToMemory(args_addr_ref + offset, arg_scalar,
220*0b57cec5SDimitry Andric arg_scalar.GetByteSize(), error))
221*0b57cec5SDimitry Andric return false;
222*0b57cec5SDimitry Andric }
223*0b57cec5SDimitry Andric
224*0b57cec5SDimitry Andric return true;
225*0b57cec5SDimitry Andric }
226*0b57cec5SDimitry Andric
InsertFunction(ExecutionContext & exe_ctx,lldb::addr_t & args_addr_ref,DiagnosticManager & diagnostic_manager)227*0b57cec5SDimitry Andric bool FunctionCaller::InsertFunction(ExecutionContext &exe_ctx,
228*0b57cec5SDimitry Andric lldb::addr_t &args_addr_ref,
229*0b57cec5SDimitry Andric DiagnosticManager &diagnostic_manager) {
230*0b57cec5SDimitry Andric // Since we might need to call allocate memory and maybe call code to make
231*0b57cec5SDimitry Andric // the caller, we need to be stopped.
232*0b57cec5SDimitry Andric Process *process = exe_ctx.GetProcessPtr();
233*0b57cec5SDimitry Andric if (!process) {
234*0b57cec5SDimitry Andric diagnostic_manager.PutString(eDiagnosticSeverityError, "no process");
235*0b57cec5SDimitry Andric return false;
236*0b57cec5SDimitry Andric }
237*0b57cec5SDimitry Andric if (process->GetState() != lldb::eStateStopped) {
238*0b57cec5SDimitry Andric diagnostic_manager.PutString(eDiagnosticSeverityError, "process running");
239*0b57cec5SDimitry Andric return false;
240*0b57cec5SDimitry Andric }
241*0b57cec5SDimitry Andric if (CompileFunction(exe_ctx.GetThreadSP(), diagnostic_manager) != 0)
242*0b57cec5SDimitry Andric return false;
243*0b57cec5SDimitry Andric if (!WriteFunctionWrapper(exe_ctx, diagnostic_manager))
244*0b57cec5SDimitry Andric return false;
245*0b57cec5SDimitry Andric if (!WriteFunctionArguments(exe_ctx, args_addr_ref, diagnostic_manager))
246*0b57cec5SDimitry Andric return false;
247*0b57cec5SDimitry Andric
248*0b57cec5SDimitry Andric Log *log = GetLog(LLDBLog::Step);
249*0b57cec5SDimitry Andric LLDB_LOGF(log, "Call Address: 0x%" PRIx64 " Struct Address: 0x%" PRIx64 ".\n",
250*0b57cec5SDimitry Andric m_jit_start_addr, args_addr_ref);
251*0b57cec5SDimitry Andric
252*0b57cec5SDimitry Andric return true;
253*0b57cec5SDimitry Andric }
254*0b57cec5SDimitry Andric
GetThreadPlanToCallFunction(ExecutionContext & exe_ctx,lldb::addr_t args_addr,const EvaluateExpressionOptions & options,DiagnosticManager & diagnostic_manager)255*0b57cec5SDimitry Andric lldb::ThreadPlanSP FunctionCaller::GetThreadPlanToCallFunction(
256*0b57cec5SDimitry Andric ExecutionContext &exe_ctx, lldb::addr_t args_addr,
257*0b57cec5SDimitry Andric const EvaluateExpressionOptions &options,
258*0b57cec5SDimitry Andric DiagnosticManager &diagnostic_manager) {
259*0b57cec5SDimitry Andric Log *log(GetLog(LLDBLog::Expressions | LLDBLog::Step));
260*0b57cec5SDimitry Andric
261*0b57cec5SDimitry Andric LLDB_LOGF(log,
262*0b57cec5SDimitry Andric "-- [FunctionCaller::GetThreadPlanToCallFunction] Creating "
263*0b57cec5SDimitry Andric "thread plan to call function \"%s\" --",
264*0b57cec5SDimitry Andric m_name.c_str());
265*0b57cec5SDimitry Andric
266*0b57cec5SDimitry Andric // FIXME: Use the errors Stream for better error reporting.
267*0b57cec5SDimitry Andric Thread *thread = exe_ctx.GetThreadPtr();
268*0b57cec5SDimitry Andric if (thread == nullptr) {
269*0b57cec5SDimitry Andric diagnostic_manager.PutString(
270*0b57cec5SDimitry Andric eDiagnosticSeverityError,
271*0b57cec5SDimitry Andric "Can't call a function without a valid thread.");
272*0b57cec5SDimitry Andric return nullptr;
273*0b57cec5SDimitry Andric }
274*0b57cec5SDimitry Andric
275*0b57cec5SDimitry Andric // Okay, now run the function:
276*0b57cec5SDimitry Andric
277*0b57cec5SDimitry Andric Address wrapper_address(m_jit_start_addr);
278*0b57cec5SDimitry Andric
279*0b57cec5SDimitry Andric lldb::addr_t args = {args_addr};
280*0b57cec5SDimitry Andric
281*0b57cec5SDimitry Andric lldb::ThreadPlanSP new_plan_sp(new ThreadPlanCallFunction(
282*0b57cec5SDimitry Andric *thread, wrapper_address, CompilerType(), args, options));
283*0b57cec5SDimitry Andric new_plan_sp->SetIsControllingPlan(true);
284*0b57cec5SDimitry Andric new_plan_sp->SetOkayToDiscard(false);
285*0b57cec5SDimitry Andric return new_plan_sp;
286*0b57cec5SDimitry Andric }
287*0b57cec5SDimitry Andric
FetchFunctionResults(ExecutionContext & exe_ctx,lldb::addr_t args_addr,Value & ret_value)288*0b57cec5SDimitry Andric bool FunctionCaller::FetchFunctionResults(ExecutionContext &exe_ctx,
289*0b57cec5SDimitry Andric lldb::addr_t args_addr,
290*0b57cec5SDimitry Andric Value &ret_value) {
291*0b57cec5SDimitry Andric // Read the return value - it is the last field in the struct:
292*0b57cec5SDimitry Andric // FIXME: How does clang tell us there's no return value? We need to handle
293*0b57cec5SDimitry Andric // that case.
294*0b57cec5SDimitry Andric // FIXME: Create our ThreadPlanCallFunction with the return CompilerType, and
295*0b57cec5SDimitry Andric // then use GetReturnValueObject
296*0b57cec5SDimitry Andric // to fetch the value. That way we can fetch any values we need.
297*0b57cec5SDimitry Andric
298*0b57cec5SDimitry Andric Log *log(GetLog(LLDBLog::Expressions | LLDBLog::Step));
299*0b57cec5SDimitry Andric
300*0b57cec5SDimitry Andric LLDB_LOGF(log,
301*0b57cec5SDimitry Andric "-- [FunctionCaller::FetchFunctionResults] Fetching function "
302*0b57cec5SDimitry Andric "results for \"%s\"--",
303*0b57cec5SDimitry Andric m_name.c_str());
304*0b57cec5SDimitry Andric
305*0b57cec5SDimitry Andric Process *process = exe_ctx.GetProcessPtr();
306*0b57cec5SDimitry Andric
307*0b57cec5SDimitry Andric if (process == nullptr)
308*0b57cec5SDimitry Andric return false;
309*0b57cec5SDimitry Andric
310*0b57cec5SDimitry Andric lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
311*0b57cec5SDimitry Andric
312*0b57cec5SDimitry Andric if (process != jit_process_sp.get())
313*0b57cec5SDimitry Andric return false;
314*0b57cec5SDimitry Andric
315*0b57cec5SDimitry Andric Status error;
316*0b57cec5SDimitry Andric ret_value.GetScalar() = process->ReadUnsignedIntegerFromMemory(
317*0b57cec5SDimitry Andric args_addr + m_return_offset, m_return_size, 0, error);
318*0b57cec5SDimitry Andric
319*0b57cec5SDimitry Andric if (error.Fail())
320*0b57cec5SDimitry Andric return false;
321*0b57cec5SDimitry Andric
322*0b57cec5SDimitry Andric ret_value.SetCompilerType(m_function_return_type);
323*0b57cec5SDimitry Andric ret_value.SetValueType(Value::ValueType::Scalar);
324*0b57cec5SDimitry Andric return true;
325*0b57cec5SDimitry Andric }
326*0b57cec5SDimitry Andric
DeallocateFunctionResults(ExecutionContext & exe_ctx,lldb::addr_t args_addr)327*0b57cec5SDimitry Andric void FunctionCaller::DeallocateFunctionResults(ExecutionContext &exe_ctx,
328*0b57cec5SDimitry Andric lldb::addr_t args_addr) {
329*0b57cec5SDimitry Andric std::list<lldb::addr_t>::iterator pos;
330*0b57cec5SDimitry Andric pos = std::find(m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(),
331*0b57cec5SDimitry Andric args_addr);
332*0b57cec5SDimitry Andric if (pos != m_wrapper_args_addrs.end())
333*0b57cec5SDimitry Andric m_wrapper_args_addrs.erase(pos);
334*0b57cec5SDimitry Andric
335*0b57cec5SDimitry Andric exe_ctx.GetProcessRef().DeallocateMemory(args_addr);
336*0b57cec5SDimitry Andric }
337*0b57cec5SDimitry Andric
ExecuteFunction(ExecutionContext & exe_ctx,lldb::addr_t * args_addr_ptr,const EvaluateExpressionOptions & options,DiagnosticManager & diagnostic_manager,Value & results)338*0b57cec5SDimitry Andric lldb::ExpressionResults FunctionCaller::ExecuteFunction(
339*0b57cec5SDimitry Andric ExecutionContext &exe_ctx, lldb::addr_t *args_addr_ptr,
340*0b57cec5SDimitry Andric const EvaluateExpressionOptions &options,
341*0b57cec5SDimitry Andric DiagnosticManager &diagnostic_manager, Value &results) {
342*0b57cec5SDimitry Andric lldb::ExpressionResults return_value = lldb::eExpressionSetupError;
343*0b57cec5SDimitry Andric
344*0b57cec5SDimitry Andric // FunctionCaller::ExecuteFunction execution is always just to get the
345*0b57cec5SDimitry Andric // result. Unless explicitly asked for, ignore breakpoints and unwind on
346*0b57cec5SDimitry Andric // error.
347*0b57cec5SDimitry Andric const bool enable_debugging =
348*0b57cec5SDimitry Andric exe_ctx.GetTargetPtr() &&
349*0b57cec5SDimitry Andric exe_ctx.GetTargetPtr()->GetDebugUtilityExpression();
350*0b57cec5SDimitry Andric EvaluateExpressionOptions real_options = options;
351*0b57cec5SDimitry Andric real_options.SetDebug(false); // This halts the expression for debugging.
352*0b57cec5SDimitry Andric real_options.SetGenerateDebugInfo(enable_debugging);
353*0b57cec5SDimitry Andric real_options.SetUnwindOnError(!enable_debugging);
354*0b57cec5SDimitry Andric real_options.SetIgnoreBreakpoints(!enable_debugging);
355*0b57cec5SDimitry Andric
356*0b57cec5SDimitry Andric lldb::addr_t args_addr;
357*0b57cec5SDimitry Andric
358*0b57cec5SDimitry Andric if (args_addr_ptr != nullptr)
359*0b57cec5SDimitry Andric args_addr = *args_addr_ptr;
360*0b57cec5SDimitry Andric else
361*0b57cec5SDimitry Andric args_addr = LLDB_INVALID_ADDRESS;
362*0b57cec5SDimitry Andric
363*0b57cec5SDimitry Andric if (CompileFunction(exe_ctx.GetThreadSP(), diagnostic_manager) != 0)
364*0b57cec5SDimitry Andric return lldb::eExpressionSetupError;
365*0b57cec5SDimitry Andric
366*0b57cec5SDimitry Andric if (args_addr == LLDB_INVALID_ADDRESS) {
367*0b57cec5SDimitry Andric if (!InsertFunction(exe_ctx, args_addr, diagnostic_manager))
368*0b57cec5SDimitry Andric return lldb::eExpressionSetupError;
369*0b57cec5SDimitry Andric }
370*0b57cec5SDimitry Andric
371*0b57cec5SDimitry Andric Log *log(GetLog(LLDBLog::Expressions | LLDBLog::Step));
372*0b57cec5SDimitry Andric
373*0b57cec5SDimitry Andric LLDB_LOGF(log,
374*0b57cec5SDimitry Andric "== [FunctionCaller::ExecuteFunction] Executing function \"%s\" ==",
375*0b57cec5SDimitry Andric m_name.c_str());
376*0b57cec5SDimitry Andric
377*0b57cec5SDimitry Andric lldb::ThreadPlanSP call_plan_sp = GetThreadPlanToCallFunction(
378*0b57cec5SDimitry Andric exe_ctx, args_addr, real_options, diagnostic_manager);
379*0b57cec5SDimitry Andric if (!call_plan_sp)
380*0b57cec5SDimitry Andric return lldb::eExpressionSetupError;
381*0b57cec5SDimitry Andric
382*0b57cec5SDimitry Andric // We need to make sure we record the fact that we are running an expression
383*0b57cec5SDimitry Andric // here otherwise this fact will fail to be recorded when fetching an
384*0b57cec5SDimitry Andric // Objective-C object description
385*0b57cec5SDimitry Andric if (exe_ctx.GetProcessPtr())
386*0b57cec5SDimitry Andric exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
387*0b57cec5SDimitry Andric
388*0b57cec5SDimitry Andric return_value = exe_ctx.GetProcessRef().RunThreadPlan(
389*0b57cec5SDimitry Andric exe_ctx, call_plan_sp, real_options, diagnostic_manager);
390*0b57cec5SDimitry Andric
391 if (log) {
392 if (return_value != lldb::eExpressionCompleted) {
393 LLDB_LOGF(log,
394 "== [FunctionCaller::ExecuteFunction] Execution of \"%s\" "
395 "completed abnormally: %s ==",
396 m_name.c_str(),
397 Process::ExecutionResultAsCString(return_value));
398 } else {
399 LLDB_LOGF(log,
400 "== [FunctionCaller::ExecuteFunction] Execution of \"%s\" "
401 "completed normally ==",
402 m_name.c_str());
403 }
404 }
405
406 if (exe_ctx.GetProcessPtr())
407 exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
408
409 if (args_addr_ptr != nullptr)
410 *args_addr_ptr = args_addr;
411
412 if (return_value != lldb::eExpressionCompleted)
413 return return_value;
414
415 FetchFunctionResults(exe_ctx, args_addr, results);
416
417 if (args_addr_ptr == nullptr)
418 DeallocateFunctionResults(exe_ctx, args_addr);
419
420 return lldb::eExpressionCompleted;
421 }
422