blob: fa156dcf9ca852bcd78322994a10d621eeab3a2a [file] [log] [blame]
Zachary Turnere2411fa2016-11-12 19:12:56 +00001//===-- FunctionCaller.cpp ---------------------------------------*- C++-*-===//
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
Chris Lattner30fdc8d2010-06-08 16:52:24 +00009
Sean Callanan579e70c2016-03-19 00:03:59 +000010#include "lldb/Expression/FunctionCaller.h"
Greg Clayton23f8c952014-03-24 23:10:19 +000011#include "lldb/Core/Module.h"
Greg Clayton23f8c952014-03-24 23:10:19 +000012#include "lldb/Core/ValueObject.h"
13#include "lldb/Core/ValueObjectList.h"
Sean Callanan579e70c2016-03-19 00:03:59 +000014#include "lldb/Expression/DiagnosticManager.h"
Greg Claytone01e07b2013-04-18 18:10:51 +000015#include "lldb/Expression/IRExecutionUnit.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Interpreter/CommandReturnObject.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Symbol/Function.h"
Greg Clayton23f8c952014-03-24 23:10:19 +000018#include "lldb/Symbol/Type.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/Target/ExecutionContext.h"
20#include "lldb/Target/Process.h"
Sean Callananebf77072010-07-23 00:16:21 +000021#include "lldb/Target/RegisterContext.h"
Greg Clayton514487e2011-02-15 21:59:32 +000022#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "lldb/Target/Thread.h"
24#include "lldb/Target/ThreadPlan.h"
25#include "lldb/Target/ThreadPlanCallFunction.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000026#include "lldb/Utility/DataExtractor.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000027#include "lldb/Utility/Log.h"
Pavel Labathd821c992018-08-07 11:07:21 +000028#include "lldb/Utility/State.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029
30using namespace lldb_private;
Sean Callanan1a8d4092010-08-27 01:01:44 +000031
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032//----------------------------------------------------------------------
Jim Ingham151c0322015-09-15 21:13:50 +000033// FunctionCaller constructor
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000035FunctionCaller::FunctionCaller(ExecutionContextScope &exe_scope,
36 const CompilerType &return_type,
37 const Address &functionAddress,
38 const ValueList &arg_value_list,
39 const char *name)
Adrian Prantl7e34d782019-03-13 19:46:30 +000040 : Expression(exe_scope, eKindFunctionCaller),
41 m_execution_unit_sp(), m_parser(),
Kate Stoneb9c1b512016-09-06 20:57:50 +000042 m_jit_module_wp(), m_name(name ? name : "<unknown>"),
43 m_function_ptr(NULL), m_function_addr(functionAddress),
44 m_function_return_type(return_type),
45 m_wrapper_function_name("__lldb_caller_function"),
46 m_wrapper_struct_name("__lldb_caller_struct"), m_wrapper_args_addrs(),
Jason Molendaa6922412018-05-31 20:01:15 +000047 m_struct_valid(false), m_arg_values(arg_value_list), m_compiled(false),
48 m_JITted(false) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000049 m_jit_process_wp = lldb::ProcessWP(exe_scope.CalculateProcess());
50 // Can't make a FunctionCaller without a process.
51 assert(m_jit_process_wp.lock());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052}
53
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054//----------------------------------------------------------------------
55// Destructor
56//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000057FunctionCaller::~FunctionCaller() {
58 lldb::ProcessSP process_sp(m_jit_process_wp.lock());
59 if (process_sp) {
60 lldb::ModuleSP jit_module_sp(m_jit_module_wp.lock());
61 if (jit_module_sp)
62 process_sp->GetTarget().GetImages().Remove(jit_module_sp);
63 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000064}
65
Kate Stoneb9c1b512016-09-06 20:57:50 +000066bool FunctionCaller::WriteFunctionWrapper(
67 ExecutionContext &exe_ctx, DiagnosticManager &diagnostic_manager) {
68 Process *process = exe_ctx.GetProcessPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000069
Kate Stoneb9c1b512016-09-06 20:57:50 +000070 if (!process)
71 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072
Kate Stoneb9c1b512016-09-06 20:57:50 +000073 lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000074
Kate Stoneb9c1b512016-09-06 20:57:50 +000075 if (process != jit_process_sp.get())
76 return false;
77
78 if (!m_compiled)
79 return false;
80
81 if (m_JITted)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000082 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000083
Kate Stoneb9c1b512016-09-06 20:57:50 +000084 bool can_interpret = false; // should stay that way
Chris Lattner30fdc8d2010-06-08 16:52:24 +000085
Zachary Turner97206d52017-05-12 04:51:55 +000086 Status jit_error(m_parser->PrepareForExecution(
Kate Stoneb9c1b512016-09-06 20:57:50 +000087 m_jit_start_addr, m_jit_end_addr, m_execution_unit_sp, exe_ctx,
88 can_interpret, eExecutionPolicyAlways));
Chris Lattner30fdc8d2010-06-08 16:52:24 +000089
Jim Ingham0d231f72018-06-28 20:02:11 +000090 if (!jit_error.Success()) {
91 diagnostic_manager.Printf(eDiagnosticSeverityError,
92 "Error in PrepareForExecution: %s.",
93 jit_error.AsCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +000094 return false;
Jim Ingham0d231f72018-06-28 20:02:11 +000095 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000096
97 if (m_parser->GetGenerateDebugInfo()) {
98 lldb::ModuleSP jit_module_sp(m_execution_unit_sp->GetJITModule());
99
100 if (jit_module_sp) {
101 ConstString const_func_name(FunctionName());
102 FileSpec jit_file;
103 jit_file.GetFilename() = const_func_name;
104 jit_module_sp->SetFileSpecAndObjectName(jit_file, ConstString());
105 m_jit_module_wp = jit_module_sp;
Jason Molenda1724a172019-04-08 23:03:02 +0000106 process->GetTarget().GetImages().Append(jit_module_sp,
107 true /* notify */);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000108 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109 }
110 if (process && m_jit_start_addr)
111 m_jit_process_wp = process->shared_from_this();
Sean Callanan579e70c2016-03-19 00:03:59 +0000112
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113 m_JITted = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000114
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115 return true;
116}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000117
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118bool FunctionCaller::WriteFunctionArguments(
119 ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref,
120 DiagnosticManager &diagnostic_manager) {
121 return WriteFunctionArguments(exe_ctx, args_addr_ref, m_arg_values,
122 diagnostic_manager);
123}
Jim Ingham35944dd2011-03-17 20:02:56 +0000124
Kate Stoneb9c1b512016-09-06 20:57:50 +0000125// FIXME: Assure that the ValueList we were passed in is consistent with the one
126// that defined this function.
127
128bool FunctionCaller::WriteFunctionArguments(
129 ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref,
130 ValueList &arg_values, DiagnosticManager &diagnostic_manager) {
131 // All the information to reconstruct the struct is provided by the
132 // StructExtractor.
133 if (!m_struct_valid) {
Zachary Turnere2411fa2016-11-12 19:12:56 +0000134 diagnostic_manager.PutString(eDiagnosticSeverityError,
135 "Argument information was not correctly "
136 "parsed, so the function cannot be called.");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000137 return false;
138 }
139
Zachary Turner97206d52017-05-12 04:51:55 +0000140 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141 lldb::ExpressionResults return_value = lldb::eExpressionSetupError;
142
143 Process *process = exe_ctx.GetProcessPtr();
144
145 if (process == NULL)
146 return return_value;
147
148 lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
149
150 if (process != jit_process_sp.get())
151 return false;
152
153 if (args_addr_ref == LLDB_INVALID_ADDRESS) {
154 args_addr_ref = process->AllocateMemory(
155 m_struct_size, lldb::ePermissionsReadable | lldb::ePermissionsWritable,
156 error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000157 if (args_addr_ref == LLDB_INVALID_ADDRESS)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000158 return false;
159 m_wrapper_args_addrs.push_back(args_addr_ref);
160 } else {
161 // Make sure this is an address that we've already handed out.
162 if (find(m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(),
163 args_addr_ref) == m_wrapper_args_addrs.end()) {
164 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000166 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000167
Kate Stoneb9c1b512016-09-06 20:57:50 +0000168 // TODO: verify fun_addr needs to be a callable address
169 Scalar fun_addr(
170 m_function_addr.GetCallableLoadAddress(exe_ctx.GetTargetPtr()));
171 uint64_t first_offset = m_member_offsets[0];
172 process->WriteScalarToMemory(args_addr_ref + first_offset, fun_addr,
173 process->GetAddressByteSize(), error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000174
Kate Stoneb9c1b512016-09-06 20:57:50 +0000175 // FIXME: We will need to extend this for Variadic functions.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000176
Zachary Turner97206d52017-05-12 04:51:55 +0000177 Status value_error;
Sean Callanan579e70c2016-03-19 00:03:59 +0000178
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179 size_t num_args = arg_values.GetSize();
180 if (num_args != m_arg_values.GetSize()) {
181 diagnostic_manager.Printf(
182 eDiagnosticSeverityError,
183 "Wrong number of arguments - was: %" PRIu64 " should be: %" PRIu64 "",
184 (uint64_t)num_args, (uint64_t)m_arg_values.GetSize());
185 return false;
186 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000187
Kate Stoneb9c1b512016-09-06 20:57:50 +0000188 for (size_t i = 0; i < num_args; i++) {
189 // FIXME: We should sanity check sizes.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000190
Kate Stoneb9c1b512016-09-06 20:57:50 +0000191 uint64_t offset = m_member_offsets[i + 1]; // Clang sizes are in bytes.
192 Value *arg_value = arg_values.GetValueAtIndex(i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000193
Kate Stoneb9c1b512016-09-06 20:57:50 +0000194 // FIXME: For now just do scalars:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000195
Kate Stoneb9c1b512016-09-06 20:57:50 +0000196 // Special case: if it's a pointer, don't do anything (the ABI supports
197 // passing cstrings)
198
199 if (arg_value->GetValueType() == Value::eValueTypeHostAddress &&
200 arg_value->GetContextType() == Value::eContextTypeInvalid &&
201 arg_value->GetCompilerType().IsPointerType())
202 continue;
203
204 const Scalar &arg_scalar = arg_value->ResolveValue(&exe_ctx);
205
206 if (!process->WriteScalarToMemory(args_addr_ref + offset, arg_scalar,
207 arg_scalar.GetByteSize(), error))
208 return false;
209 }
210
211 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000212}
213
Kate Stoneb9c1b512016-09-06 20:57:50 +0000214bool FunctionCaller::InsertFunction(ExecutionContext &exe_ctx,
215 lldb::addr_t &args_addr_ref,
216 DiagnosticManager &diagnostic_manager) {
217 if (CompileFunction(exe_ctx.GetThreadSP(), diagnostic_manager) != 0)
218 return false;
219 if (!WriteFunctionWrapper(exe_ctx, diagnostic_manager))
220 return false;
221 if (!WriteFunctionArguments(exe_ctx, args_addr_ref, diagnostic_manager))
222 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000223
Kate Stoneb9c1b512016-09-06 20:57:50 +0000224 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
225 if (log)
226 log->Printf("Call Address: 0x%" PRIx64 " Struct Address: 0x%" PRIx64 ".\n",
227 m_jit_start_addr, args_addr_ref);
228
229 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000230}
231
Kate Stoneb9c1b512016-09-06 20:57:50 +0000232lldb::ThreadPlanSP FunctionCaller::GetThreadPlanToCallFunction(
233 ExecutionContext &exe_ctx, lldb::addr_t args_addr,
234 const EvaluateExpressionOptions &options,
235 DiagnosticManager &diagnostic_manager) {
236 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_EXPRESSIONS |
237 LIBLLDB_LOG_STEP));
Sean Callanan579e70c2016-03-19 00:03:59 +0000238
Kate Stoneb9c1b512016-09-06 20:57:50 +0000239 if (log)
240 log->Printf("-- [FunctionCaller::GetThreadPlanToCallFunction] Creating "
241 "thread plan to call function \"%s\" --",
242 m_name.c_str());
243
244 // FIXME: Use the errors Stream for better error reporting.
245 Thread *thread = exe_ctx.GetThreadPtr();
246 if (thread == NULL) {
Zachary Turnere2411fa2016-11-12 19:12:56 +0000247 diagnostic_manager.PutString(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000248 eDiagnosticSeverityError,
249 "Can't call a function without a valid thread.");
250 return NULL;
251 }
252
253 // Okay, now run the function:
254
255 Address wrapper_address(m_jit_start_addr);
256
257 lldb::addr_t args = {args_addr};
258
259 lldb::ThreadPlanSP new_plan_sp(new ThreadPlanCallFunction(
260 *thread, wrapper_address, CompilerType(), args, options));
261 new_plan_sp->SetIsMasterPlan(true);
262 new_plan_sp->SetOkayToDiscard(false);
263 return new_plan_sp;
264}
265
266bool FunctionCaller::FetchFunctionResults(ExecutionContext &exe_ctx,
267 lldb::addr_t args_addr,
268 Value &ret_value) {
269 // Read the return value - it is the last field in the struct:
270 // FIXME: How does clang tell us there's no return value? We need to handle
271 // that case.
272 // FIXME: Create our ThreadPlanCallFunction with the return CompilerType, and
273 // then use GetReturnValueObject
274 // to fetch the value. That way we can fetch any values we need.
275
276 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_EXPRESSIONS |
277 LIBLLDB_LOG_STEP));
278
279 if (log)
280 log->Printf("-- [FunctionCaller::FetchFunctionResults] Fetching function "
281 "results for \"%s\"--",
282 m_name.c_str());
283
284 Process *process = exe_ctx.GetProcessPtr();
285
286 if (process == NULL)
287 return false;
288
289 lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
290
291 if (process != jit_process_sp.get())
292 return false;
293
Zachary Turner97206d52017-05-12 04:51:55 +0000294 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000295 ret_value.GetScalar() = process->ReadUnsignedIntegerFromMemory(
296 args_addr + m_return_offset, m_return_size, 0, error);
297
298 if (error.Fail())
299 return false;
300
301 ret_value.SetCompilerType(m_function_return_type);
302 ret_value.SetValueType(Value::eValueTypeScalar);
303 return true;
304}
305
306void FunctionCaller::DeallocateFunctionResults(ExecutionContext &exe_ctx,
307 lldb::addr_t args_addr) {
308 std::list<lldb::addr_t>::iterator pos;
309 pos = std::find(m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(),
310 args_addr);
311 if (pos != m_wrapper_args_addrs.end())
312 m_wrapper_args_addrs.erase(pos);
313
314 exe_ctx.GetProcessRef().DeallocateMemory(args_addr);
315}
316
317lldb::ExpressionResults FunctionCaller::ExecuteFunction(
318 ExecutionContext &exe_ctx, lldb::addr_t *args_addr_ptr,
319 const EvaluateExpressionOptions &options,
320 DiagnosticManager &diagnostic_manager, Value &results) {
321 lldb::ExpressionResults return_value = lldb::eExpressionSetupError;
322
Adrian Prantl05097242018-04-30 16:49:04 +0000323 // FunctionCaller::ExecuteFunction execution is always just to get the
324 // result. Do make sure we ignore breakpoints, unwind on error, and don't try
325 // to debug it.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000326 EvaluateExpressionOptions real_options = options;
327 real_options.SetDebug(false);
328 real_options.SetUnwindOnError(true);
329 real_options.SetIgnoreBreakpoints(true);
330
331 lldb::addr_t args_addr;
332
333 if (args_addr_ptr != NULL)
334 args_addr = *args_addr_ptr;
335 else
336 args_addr = LLDB_INVALID_ADDRESS;
337
338 if (CompileFunction(exe_ctx.GetThreadSP(), diagnostic_manager) != 0)
339 return lldb::eExpressionSetupError;
340
341 if (args_addr == LLDB_INVALID_ADDRESS) {
342 if (!InsertFunction(exe_ctx, args_addr, diagnostic_manager))
343 return lldb::eExpressionSetupError;
344 }
345
346 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_EXPRESSIONS |
347 LIBLLDB_LOG_STEP));
348
349 if (log)
350 log->Printf(
351 "== [FunctionCaller::ExecuteFunction] Executing function \"%s\" ==",
352 m_name.c_str());
353
354 lldb::ThreadPlanSP call_plan_sp = GetThreadPlanToCallFunction(
355 exe_ctx, args_addr, real_options, diagnostic_manager);
356 if (!call_plan_sp)
357 return lldb::eExpressionSetupError;
358
359 // We need to make sure we record the fact that we are running an expression
Adrian Prantl05097242018-04-30 16:49:04 +0000360 // here otherwise this fact will fail to be recorded when fetching an
361 // Objective-C object description
Kate Stoneb9c1b512016-09-06 20:57:50 +0000362 if (exe_ctx.GetProcessPtr())
363 exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
364
365 return_value = exe_ctx.GetProcessRef().RunThreadPlan(
366 exe_ctx, call_plan_sp, real_options, diagnostic_manager);
367
368 if (log) {
369 if (return_value != lldb::eExpressionCompleted) {
370 log->Printf("== [FunctionCaller::ExecuteFunction] Execution of \"%s\" "
371 "completed abnormally ==",
372 m_name.c_str());
373 } else {
374 log->Printf("== [FunctionCaller::ExecuteFunction] Execution of \"%s\" "
375 "completed normally ==",
376 m_name.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000377 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000378 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000379
Kate Stoneb9c1b512016-09-06 20:57:50 +0000380 if (exe_ctx.GetProcessPtr())
381 exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000382
Kate Stoneb9c1b512016-09-06 20:57:50 +0000383 if (args_addr_ptr != NULL)
384 *args_addr_ptr = args_addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000385
Kate Stoneb9c1b512016-09-06 20:57:50 +0000386 if (return_value != lldb::eExpressionCompleted)
387 return return_value;
Enrico Granatadfc88a02012-09-18 00:08:47 +0000388
Kate Stoneb9c1b512016-09-06 20:57:50 +0000389 FetchFunctionResults(exe_ctx, args_addr, results);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000390
Kate Stoneb9c1b512016-09-06 20:57:50 +0000391 if (args_addr_ptr == NULL)
392 DeallocateFunctionResults(exe_ctx, args_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000393
Kate Stoneb9c1b512016-09-06 20:57:50 +0000394 return lldb::eExpressionCompleted;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000395}