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