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