Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame^] | 1 | //===-- ClangFunction.cpp ---------------------------------------*- C++ -*-===// |
| 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 | |
| 10 | |
| 11 | // C Includes |
| 12 | // C++ Includes |
| 13 | // Other libraries and framework includes |
| 14 | #include "clang/Frontend/CodeGenAction.h" |
| 15 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
| 16 | #include "clang/Frontend/CompilerInstance.h" |
| 17 | #include "clang/AST/ASTContext.h" |
| 18 | #include "clang/AST/RecordLayout.h" |
| 19 | #include "llvm/ExecutionEngine/JIT.h" |
| 20 | #include "llvm/Module.h" |
| 21 | #include "clang/CodeGen/ModuleBuilder.h" |
| 22 | #include "llvm/ADT/StringRef.h" |
| 23 | |
| 24 | // Project includes |
| 25 | #include "lldb/Expression/ClangFunction.h" |
| 26 | #include "lldb/Symbol/Type.h" |
| 27 | #include "lldb/Core/DataExtractor.h" |
| 28 | #include "lldb/Core/ValueObject.h" |
| 29 | #include "lldb/Core/ValueObjectList.h" |
| 30 | #include "lldb/Interpreter/CommandReturnObject.h" |
| 31 | #include "lldb/Symbol/ClangASTContext.h" |
| 32 | #include "lldb/Symbol/Function.h" |
| 33 | #include "lldb/Target/ExecutionContext.h" |
| 34 | #include "lldb/Target/Process.h" |
| 35 | #include "lldb/Target/Thread.h" |
| 36 | #include "lldb/Target/ThreadPlan.h" |
| 37 | #include "lldb/Target/ThreadPlanCallFunction.h" |
| 38 | #include "lldb/Core/Log.h" |
| 39 | |
| 40 | using namespace lldb_private; |
| 41 | //---------------------------------------------------------------------- |
| 42 | // ClangFunction constructor |
| 43 | //---------------------------------------------------------------------- |
| 44 | ClangFunction::ClangFunction(const char *target_triple, ClangASTContext *ast_context, void *return_qualtype, const Address& functionAddress, const ValueList &arg_value_list) : |
| 45 | ClangExpression (target_triple, NULL), |
| 46 | m_function_addr (functionAddress), |
| 47 | m_function_ptr (NULL), |
| 48 | m_arg_values (arg_value_list), |
| 49 | m_clang_ast_context (ast_context), |
| 50 | m_function_return_qual_type(return_qualtype), |
| 51 | m_wrapper_function_name ("__lldb_caller_function"), |
| 52 | m_wrapper_struct_name ("__lldb_caller_struct"), |
| 53 | m_return_offset(0), |
| 54 | m_compiled (false), |
| 55 | m_JITted (false) |
| 56 | { |
| 57 | } |
| 58 | |
| 59 | ClangFunction::ClangFunction(const char *target_triple, Function &function, ClangASTContext *ast_context, const ValueList &arg_value_list) : |
| 60 | ClangExpression (target_triple, NULL), |
| 61 | m_function_ptr (&function), |
| 62 | m_arg_values (arg_value_list), |
| 63 | m_clang_ast_context (ast_context), |
| 64 | m_function_return_qual_type (NULL), |
| 65 | m_wrapper_function_name ("__lldb_function_caller"), |
| 66 | m_wrapper_struct_name ("__lldb_caller_struct"), |
| 67 | m_return_offset(0), |
| 68 | m_compiled (false), |
| 69 | m_JITted (false) |
| 70 | { |
| 71 | m_function_addr = m_function_ptr->GetAddressRange().GetBaseAddress(); |
| 72 | m_function_return_qual_type = m_function_ptr->GetReturnType().GetOpaqueClangQualType(); |
| 73 | } |
| 74 | |
| 75 | //---------------------------------------------------------------------- |
| 76 | // Destructor |
| 77 | //---------------------------------------------------------------------- |
| 78 | ClangFunction::~ClangFunction() |
| 79 | { |
| 80 | } |
| 81 | |
| 82 | unsigned |
| 83 | ClangFunction::CompileFunction (Stream &errors) |
| 84 | { |
| 85 | // FIXME: How does clang tell us there's no return value? We need to handle that case. |
| 86 | unsigned num_errors = 0; |
| 87 | |
| 88 | if (!m_compiled) |
| 89 | { |
| 90 | std::string return_type_str = ClangASTContext::GetTypeName(m_function_return_qual_type); |
| 91 | |
| 92 | // Cons up the function we're going to wrap our call in, then compile it... |
| 93 | // We declare the function "extern "C"" because the compiler might be in C++ |
| 94 | // mode which would mangle the name and then we couldn't find it again... |
| 95 | std::string expression; |
| 96 | expression.append ("extern \"C\" void "); |
| 97 | expression.append (m_wrapper_function_name); |
| 98 | expression.append (" (void *input)\n{\n struct "); |
| 99 | expression.append (m_wrapper_struct_name); |
| 100 | expression.append (" \n {\n"); |
| 101 | expression.append (" "); |
| 102 | expression.append (return_type_str); |
| 103 | expression.append (" (*fn_ptr) ("); |
| 104 | |
| 105 | // Get the number of arguments. If we have a function type and it is prototyped, |
| 106 | // trust that, otherwise use the values we were given. |
| 107 | |
| 108 | // FIXME: This will need to be extended to handle Variadic functions. We'll need |
| 109 | // to pull the defined arguments out of the function, then add the types from the |
| 110 | // arguments list for the variable arguments. |
| 111 | |
| 112 | size_t num_args = -1; |
| 113 | bool trust_function = false; |
| 114 | // GetArgumentCount returns -1 for an unprototyped function. |
| 115 | if (m_function_ptr) |
| 116 | { |
| 117 | num_args = m_function_ptr->GetArgumentCount(); |
| 118 | if (num_args != -1) |
| 119 | trust_function = true; |
| 120 | } |
| 121 | |
| 122 | if (num_args == -1) |
| 123 | num_args = m_arg_values.GetSize(); |
| 124 | |
| 125 | std::string args_buffer; // This one stores the definition of all the args in "struct caller". |
| 126 | std::string args_list_buffer; // This one stores the argument list called from the structure. |
| 127 | for (int i = 0; i < num_args; i++) |
| 128 | { |
| 129 | const char *type_string; |
| 130 | std::string type_stdstr; |
| 131 | |
| 132 | if (trust_function) |
| 133 | { |
| 134 | type_string = m_function_ptr->GetArgumentTypeAtIndex(i).GetName().AsCString(); |
| 135 | } |
| 136 | else |
| 137 | { |
| 138 | Value *arg_value = m_arg_values.GetValueAtIndex(i); |
| 139 | void *clang_qual_type = arg_value->GetOpaqueClangQualType (); |
| 140 | if (clang_qual_type != NULL) |
| 141 | { |
| 142 | type_stdstr = ClangASTContext::GetTypeName(clang_qual_type); |
| 143 | type_string = type_stdstr.c_str(); |
| 144 | } |
| 145 | else |
| 146 | { |
| 147 | errors.Printf("Could not determine type of input value %d.", i); |
| 148 | return 1; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | |
| 153 | expression.append (type_string); |
| 154 | if (i < num_args - 1) |
| 155 | expression.append (", "); |
| 156 | |
| 157 | char arg_buf[32]; |
| 158 | args_buffer.append (" "); |
| 159 | args_buffer.append (type_string); |
| 160 | snprintf(arg_buf, 31, "arg_%d", i); |
| 161 | args_buffer.push_back (' '); |
| 162 | args_buffer.append (arg_buf); |
| 163 | args_buffer.append (";\n"); |
| 164 | |
| 165 | args_list_buffer.append ("__lldb_fn_data->"); |
| 166 | args_list_buffer.append (arg_buf); |
| 167 | if (i < num_args - 1) |
| 168 | args_list_buffer.append (", "); |
| 169 | |
| 170 | } |
| 171 | expression.append (");\n"); // Close off the function calling prototype. |
| 172 | |
| 173 | expression.append (args_buffer); |
| 174 | |
| 175 | expression.append (" "); |
| 176 | expression.append (return_type_str); |
| 177 | expression.append (" return_value;"); |
| 178 | expression.append ("\n };\n struct "); |
| 179 | expression.append (m_wrapper_struct_name); |
| 180 | expression.append ("* __lldb_fn_data = (struct "); |
| 181 | expression.append (m_wrapper_struct_name); |
| 182 | expression.append (" *) input;\n"); |
| 183 | |
| 184 | expression.append (" __lldb_fn_data->return_value = __lldb_fn_data->fn_ptr ("); |
| 185 | expression.append (args_list_buffer); |
| 186 | expression.append (");\n}\n"); |
| 187 | |
| 188 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP); |
| 189 | if (log) |
| 190 | log->Printf ("Expression: \n\n%s\n\n", expression.c_str()); |
| 191 | |
| 192 | // Okay, now compile this expression: |
| 193 | num_errors = ParseBareExpression (expression.c_str(), errors); |
| 194 | m_compiled = (num_errors == 0); |
| 195 | |
| 196 | if (m_compiled) |
| 197 | { |
| 198 | using namespace clang; |
| 199 | CompilerInstance *compiler_instance = GetCompilerInstance(); |
| 200 | ASTContext &ast_context = compiler_instance->getASTContext(); |
| 201 | |
| 202 | DeclarationName wrapper_func_name(&ast_context.Idents.get(m_wrapper_function_name.c_str())); |
| 203 | FunctionDecl::lookup_result func_lookup = ast_context.getTranslationUnitDecl()->lookup(wrapper_func_name); |
| 204 | if (func_lookup.first == func_lookup.second) |
| 205 | return false; |
| 206 | |
| 207 | FunctionDecl *wrapper_func = dyn_cast<FunctionDecl> (*(func_lookup.first)); |
| 208 | if (!wrapper_func) |
| 209 | return false; |
| 210 | |
| 211 | DeclarationName wrapper_struct_name(&ast_context.Idents.get(m_wrapper_struct_name.c_str())); |
| 212 | RecordDecl::lookup_result struct_lookup = wrapper_func->lookup(wrapper_struct_name); |
| 213 | if (struct_lookup.first == struct_lookup.second) |
| 214 | return false; |
| 215 | |
| 216 | RecordDecl *wrapper_struct = dyn_cast<RecordDecl>(*(struct_lookup.first)); |
| 217 | |
| 218 | if (!wrapper_struct) |
| 219 | return false; |
| 220 | |
| 221 | m_struct_layout = &ast_context.getASTRecordLayout (wrapper_struct); |
| 222 | if (!m_struct_layout) |
| 223 | { |
| 224 | m_compiled = false; |
| 225 | return 1; |
| 226 | } |
| 227 | m_return_offset = m_struct_layout->getFieldOffset(m_struct_layout->getFieldCount() - 1); |
| 228 | m_return_size = (m_struct_layout->getDataSize() - m_return_offset)/8; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | return num_errors; |
| 233 | } |
| 234 | |
| 235 | bool |
| 236 | ClangFunction::WriteFunctionWrapper (ExecutionContext &exc_context, Stream &errors) |
| 237 | { |
| 238 | Process *process = exc_context.process; |
| 239 | |
| 240 | if (process == NULL) |
| 241 | return false; |
| 242 | |
| 243 | if (!m_JITted) |
| 244 | { |
| 245 | // Next we should JIT it and insert the result into the target program. |
| 246 | if (!JITFunction (exc_context, m_wrapper_function_name.c_str())) |
| 247 | return false; |
| 248 | |
| 249 | if (!WriteJITCode (exc_context)) |
| 250 | return false; |
| 251 | |
| 252 | m_JITted = true; |
| 253 | } |
| 254 | |
| 255 | // Next get the call address for the function: |
| 256 | m_wrapper_fun_addr = GetFunctionAddress (m_wrapper_function_name.c_str()); |
| 257 | if (m_wrapper_fun_addr == LLDB_INVALID_ADDRESS) |
| 258 | return false; |
| 259 | |
| 260 | return true; |
| 261 | } |
| 262 | |
| 263 | bool |
| 264 | ClangFunction::WriteFunctionArguments (ExecutionContext &exc_context, lldb::addr_t &args_addr_ref, Stream &errors) |
| 265 | { |
| 266 | return WriteFunctionArguments(exc_context, args_addr_ref, m_function_addr, m_arg_values, errors); |
| 267 | } |
| 268 | |
| 269 | // FIXME: Assure that the ValueList we were passed in is consistent with the one that defined this function. |
| 270 | |
| 271 | bool |
| 272 | ClangFunction::WriteFunctionArguments (ExecutionContext &exc_context, lldb::addr_t &args_addr_ref, Address function_address, ValueList &arg_values, Stream &errors) |
| 273 | { |
| 274 | // Otherwise, allocate space for the argument passing struct, and write it. |
| 275 | // We use the information in the expression parser AST to |
| 276 | // figure out how to do this... |
| 277 | // We should probably transcode this in this object so we can ditch the compiler instance |
| 278 | // and all its associated data, and just keep the JITTed bytes. |
| 279 | |
| 280 | Error error; |
| 281 | using namespace clang; |
| 282 | ExecutionResults return_value = eExecutionSetupError; |
| 283 | |
| 284 | Process *process = exc_context.process; |
| 285 | |
| 286 | if (process == NULL) |
| 287 | return return_value; |
| 288 | |
| 289 | uint64_t struct_size = m_struct_layout->getSize()/8; // Clang returns sizes in bytes. |
| 290 | |
| 291 | if (args_addr_ref == LLDB_INVALID_ADDRESS) |
| 292 | { |
| 293 | args_addr_ref = process->AllocateMemory(struct_size, lldb::ePermissionsReadable|lldb::ePermissionsWritable, error); |
| 294 | if (args_addr_ref == LLDB_INVALID_ADDRESS) |
| 295 | return false; |
| 296 | m_wrapper_args_addrs.push_back (args_addr_ref); |
| 297 | } |
| 298 | else |
| 299 | { |
| 300 | // Make sure this is an address that we've already handed out. |
| 301 | if (find (m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(), args_addr_ref) == m_wrapper_args_addrs.end()) |
| 302 | { |
| 303 | return false; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | // FIXME: This is fake, and just assumes that it matches that architecture. |
| 308 | // Make a data extractor and put the address into the right byte order & size. |
| 309 | |
| 310 | uint64_t fun_addr = function_address.GetLoadAddress(exc_context.process); |
| 311 | int first_offset = m_struct_layout->getFieldOffset(0)/8; |
| 312 | process->WriteMemory(args_addr_ref + first_offset, &fun_addr, 8, error); |
| 313 | |
| 314 | // FIXME: We will need to extend this for Variadic functions. |
| 315 | |
| 316 | Error value_error; |
| 317 | |
| 318 | size_t num_args = arg_values.GetSize(); |
| 319 | if (num_args != m_arg_values.GetSize()) |
| 320 | { |
| 321 | errors.Printf ("Wrong number of arguments - was: %d should be: %d", num_args, m_arg_values.GetSize()); |
| 322 | return false; |
| 323 | } |
| 324 | |
| 325 | for (int i = 0; i < num_args; i++) |
| 326 | { |
| 327 | // FIXME: We should sanity check sizes. |
| 328 | |
| 329 | int offset = m_struct_layout->getFieldOffset(i+1)/8; // Clang sizes are in bytes. |
| 330 | Value *arg_value = arg_values.GetValueAtIndex(i); |
| 331 | |
| 332 | // FIXME: For now just do scalars: |
| 333 | |
| 334 | // Special case: if it's a pointer, don't do anything (the ABI supports passing cstrings) |
| 335 | |
| 336 | if (arg_value->GetValueType() == Value::eValueTypeHostAddress && |
| 337 | arg_value->GetContextType() == Value::eContextTypeOpaqueClangQualType && |
| 338 | ClangASTContext::IsPointerType(arg_value->GetValueOpaqueClangQualType())) |
| 339 | continue; |
| 340 | |
| 341 | const Scalar &arg_scalar = arg_value->ResolveValue(&exc_context, m_clang_ast_context->getASTContext()); |
| 342 | |
| 343 | int byte_size = arg_scalar.GetByteSize(); |
| 344 | std::vector<uint8_t> buffer; |
| 345 | buffer.resize(byte_size); |
| 346 | DataExtractor value_data; |
| 347 | arg_scalar.GetData (value_data); |
| 348 | value_data.ExtractBytes(0, byte_size, process->GetByteOrder(), buffer.data()); |
| 349 | process->WriteMemory(args_addr_ref + offset, buffer.data(), byte_size, error); |
| 350 | } |
| 351 | |
| 352 | return true; |
| 353 | } |
| 354 | |
| 355 | bool |
| 356 | ClangFunction::InsertFunction (ExecutionContext &exc_context, lldb::addr_t &args_addr_ref, Stream &errors) |
| 357 | { |
| 358 | using namespace clang; |
| 359 | |
| 360 | if (CompileFunction(errors) != 0) |
| 361 | return false; |
| 362 | if (!WriteFunctionWrapper(exc_context, errors)) |
| 363 | return false; |
| 364 | if (!WriteFunctionArguments(exc_context, args_addr_ref, errors)) |
| 365 | return false; |
| 366 | |
| 367 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP); |
| 368 | if (log) |
| 369 | log->Printf ("Call Address: 0x%llx Struct Address: 0x%llx.\n", m_wrapper_fun_addr, args_addr_ref); |
| 370 | |
| 371 | return true; |
| 372 | } |
| 373 | |
| 374 | ThreadPlan * |
| 375 | ClangFunction::GetThreadPlanToCallFunction (ExecutionContext &exc_context, lldb::addr_t &args_addr, Stream &errors, bool stop_others, bool discard_on_error) |
| 376 | { |
| 377 | // FIXME: Use the errors Stream for better error reporting. |
| 378 | |
| 379 | Process *process = exc_context.process; |
| 380 | |
| 381 | if (process == NULL) |
| 382 | { |
| 383 | errors.Printf("Can't call a function without a process."); |
| 384 | return NULL; |
| 385 | } |
| 386 | |
| 387 | // Okay, now run the function: |
| 388 | |
| 389 | Address wrapper_address (NULL, m_wrapper_fun_addr); |
| 390 | ThreadPlan *new_plan = new ThreadPlanCallFunction (*exc_context.thread, |
| 391 | wrapper_address, |
| 392 | args_addr, |
| 393 | stop_others, discard_on_error); |
| 394 | return new_plan; |
| 395 | } |
| 396 | |
| 397 | bool |
| 398 | ClangFunction::FetchFunctionResults (ExecutionContext &exc_context, lldb::addr_t args_addr, Value &ret_value) |
| 399 | { |
| 400 | // Read the return value - it is the last field in the struct: |
| 401 | // FIXME: How does clang tell us there's no return value? We need to handle that case. |
| 402 | |
| 403 | std::vector<uint8_t> data_buffer; |
| 404 | data_buffer.resize(m_return_size); |
| 405 | Process *process = exc_context.process; |
| 406 | Error error; |
| 407 | size_t bytes_read = process->ReadMemory(args_addr + m_return_offset/8, data_buffer.data(), m_return_size, error); |
| 408 | |
| 409 | if (bytes_read == 0) |
| 410 | { |
| 411 | return false; |
| 412 | } |
| 413 | |
| 414 | if (bytes_read < m_return_size) |
| 415 | return false; |
| 416 | |
| 417 | DataExtractor data(data_buffer.data(), m_return_size, process->GetByteOrder(), process->GetAddressByteSize()); |
| 418 | // FIXME: Assuming an integer scalar for now: |
| 419 | |
| 420 | uint32_t offset = 0; |
| 421 | uint64_t return_integer = data.GetMaxU64(&offset, m_return_size); |
| 422 | |
| 423 | ret_value.SetContext (Value::eContextTypeOpaqueClangQualType, m_function_return_qual_type); |
| 424 | ret_value.SetValueType(Value::eValueTypeScalar); |
| 425 | ret_value.GetScalar() = return_integer; |
| 426 | return true; |
| 427 | } |
| 428 | |
| 429 | void |
| 430 | ClangFunction::DeallocateFunctionResults (ExecutionContext &exc_context, lldb::addr_t args_addr) |
| 431 | { |
| 432 | std::list<lldb::addr_t>::iterator pos; |
| 433 | pos = std::find(m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(), args_addr); |
| 434 | if (pos != m_wrapper_args_addrs.end()) |
| 435 | m_wrapper_args_addrs.erase(pos); |
| 436 | |
| 437 | exc_context.process->DeallocateMemory(args_addr); |
| 438 | } |
| 439 | |
| 440 | ClangFunction::ExecutionResults |
| 441 | ClangFunction::ExecuteFunction(ExecutionContext &exc_context, Stream &errors, Value &results) |
| 442 | { |
| 443 | return ExecuteFunction (exc_context, errors, 1000, true, results); |
| 444 | } |
| 445 | |
| 446 | ClangFunction::ExecutionResults |
| 447 | ClangFunction::ExecuteFunction(ExecutionContext &exc_context, Stream &errors, bool stop_others, Value &results) |
| 448 | { |
| 449 | return ExecuteFunction (exc_context, NULL, errors, stop_others, NULL, false, results); |
| 450 | } |
| 451 | |
| 452 | ClangFunction::ExecutionResults |
| 453 | ClangFunction::ExecuteFunction( |
| 454 | ExecutionContext &exc_context, |
| 455 | Stream &errors, |
| 456 | uint32_t single_thread_timeout_usec, |
| 457 | bool try_all_threads, |
| 458 | Value &results) |
| 459 | { |
| 460 | return ExecuteFunction (exc_context, NULL, errors, true, single_thread_timeout_usec, try_all_threads, results); |
| 461 | } |
| 462 | |
| 463 | ClangFunction::ExecutionResults |
| 464 | ClangFunction::ExecuteFunction( |
| 465 | ExecutionContext &exc_context, |
| 466 | lldb::addr_t *args_addr_ptr, |
| 467 | Stream &errors, |
| 468 | bool stop_others, |
| 469 | uint32_t single_thread_timeout_usec, |
| 470 | bool try_all_threads, |
| 471 | Value &results) |
| 472 | { |
| 473 | using namespace clang; |
| 474 | ExecutionResults return_value = eExecutionSetupError; |
| 475 | Process *process = exc_context.process; |
| 476 | |
| 477 | lldb::addr_t args_addr; |
| 478 | |
| 479 | if (args_addr_ptr != NULL) |
| 480 | args_addr = *args_addr_ptr; |
| 481 | else |
| 482 | args_addr = LLDB_INVALID_ADDRESS; |
| 483 | |
| 484 | if (CompileFunction(errors) != 0) |
| 485 | return eExecutionSetupError; |
| 486 | |
| 487 | if (args_addr == LLDB_INVALID_ADDRESS) |
| 488 | { |
| 489 | if (!InsertFunction(exc_context, args_addr, errors)) |
| 490 | return eExecutionSetupError; |
| 491 | } |
| 492 | |
| 493 | |
| 494 | lldb::ThreadPlanSP call_plan_sp(GetThreadPlanToCallFunction(exc_context, args_addr, errors, stop_others, false)); |
| 495 | |
| 496 | ThreadPlanCallFunction *call_plan_ptr = static_cast<ThreadPlanCallFunction *> (call_plan_sp.get()); |
| 497 | |
| 498 | if (args_addr_ptr != NULL) |
| 499 | *args_addr_ptr = args_addr; |
| 500 | |
| 501 | if (call_plan_sp == NULL) |
| 502 | return return_value; |
| 503 | |
| 504 | call_plan_sp->SetPrivate(true); |
| 505 | exc_context.thread->QueueThreadPlan(call_plan_sp, true); |
| 506 | |
| 507 | // We need to call the function synchronously, so spin waiting for it to return. |
| 508 | // If we get interrupted while executing, we're going to lose our context, and |
| 509 | // won't be able to gather the result at this point. |
| 510 | |
| 511 | TimeValue* timeout_ptr = NULL; |
| 512 | TimeValue real_timeout; |
| 513 | if (single_thread_timeout_usec != 0) |
| 514 | { |
| 515 | real_timeout = TimeValue::Now(); |
| 516 | real_timeout.OffsetWithMicroSeconds(single_thread_timeout_usec); |
| 517 | timeout_ptr = &real_timeout; |
| 518 | } |
| 519 | process->Resume (); |
| 520 | |
| 521 | |
| 522 | while (1) |
| 523 | { |
| 524 | lldb::EventSP event_sp; |
| 525 | |
| 526 | // Now wait for the process to stop again: |
| 527 | // FIXME: Probably want a time out. |
| 528 | lldb::StateType stop_state = process->WaitForStateChangedEvents (timeout_ptr, event_sp); |
| 529 | if (stop_state == lldb::eStateInvalid && timeout_ptr != NULL) |
| 530 | { |
| 531 | // Right now this is the only way to tell we've timed out... |
| 532 | // We should interrupt the process here... |
| 533 | // Not really sure what to do if Halt fails here... |
| 534 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP); |
| 535 | if (log) |
| 536 | log->Printf ("Running function with timeout: %d timed out, trying with all threads enabled.", single_thread_timeout_usec); |
| 537 | |
| 538 | if (process->Halt().Success()) |
| 539 | { |
| 540 | timeout_ptr = NULL; |
| 541 | |
| 542 | lldb::StateType stop_state = process->WaitForStateChangedEvents (timeout_ptr, event_sp); |
| 543 | if (stop_state == lldb::eStateInvalid) |
| 544 | { |
| 545 | errors.Printf ("Got an invalid stop state after halt."); |
| 546 | } |
| 547 | else if (stop_state != lldb::eStateStopped) |
| 548 | { |
| 549 | StreamString s; |
| 550 | event_sp->Dump (&s); |
| 551 | |
| 552 | errors.Printf("Didn't get a stopped event after Halting the target, got: \"%s\"", s.GetData()); |
| 553 | } |
| 554 | |
| 555 | if (try_all_threads) |
| 556 | { |
| 557 | // Between the time that we got the timeout and the time we halted, but target |
| 558 | // might have actually completed the plan. If so, we're done. |
| 559 | if (exc_context.thread->IsThreadPlanDone (call_plan_sp.get())) |
| 560 | { |
| 561 | return_value = eExecutionCompleted; |
| 562 | break; |
| 563 | } |
| 564 | |
| 565 | |
| 566 | call_plan_ptr->SetStopOthers (false); |
| 567 | process->Resume(); |
| 568 | continue; |
| 569 | } |
| 570 | else |
| 571 | return eExecutionInterrupted; |
| 572 | } |
| 573 | } |
| 574 | if (stop_state == lldb::eStateRunning || stop_state == lldb::eStateStepping) |
| 575 | continue; |
| 576 | |
| 577 | if (exc_context.thread->IsThreadPlanDone (call_plan_sp.get())) |
| 578 | { |
| 579 | return_value = eExecutionCompleted; |
| 580 | break; |
| 581 | } |
| 582 | else if (exc_context.thread->WasThreadPlanDiscarded (call_plan_sp.get())) |
| 583 | { |
| 584 | return_value = eExecutionDiscarded; |
| 585 | break; |
| 586 | } |
| 587 | else |
| 588 | { |
| 589 | return_value = eExecutionInterrupted; |
| 590 | break; |
| 591 | } |
| 592 | |
| 593 | } |
| 594 | |
| 595 | if (return_value != eExecutionCompleted) |
| 596 | return return_value; |
| 597 | |
| 598 | FetchFunctionResults(exc_context, args_addr, results); |
| 599 | |
| 600 | if (args_addr_ptr == NULL) |
| 601 | DeallocateFunctionResults(exc_context, args_addr); |
| 602 | |
| 603 | return eExecutionCompleted; |
| 604 | } |
| 605 | |
| 606 | ClangFunction::ExecutionResults |
| 607 | ClangFunction::ExecuteFunctionWithABI(ExecutionContext &exc_context, Stream &errors, Value &results) |
| 608 | { |
| 609 | // FIXME: Use the errors Stream for better error reporting. |
| 610 | using namespace clang; |
| 611 | ExecutionResults return_value = eExecutionSetupError; |
| 612 | |
| 613 | Process *process = exc_context.process; |
| 614 | |
| 615 | if (process == NULL) |
| 616 | { |
| 617 | errors.Printf("Can't call a function without a process."); |
| 618 | return return_value; |
| 619 | } |
| 620 | |
| 621 | //unsigned int num_args = m_arg_values.GetSize(); |
| 622 | //unsigned int arg_index; |
| 623 | |
| 624 | //for (arg_index = 0; arg_index < num_args; ++arg_index) |
| 625 | // m_arg_values.GetValueAtIndex(arg_index)->ResolveValue(&exc_context, GetASTContext()); |
| 626 | |
| 627 | ThreadPlan *call_plan = exc_context.thread->QueueThreadPlanForCallFunction (false, |
| 628 | m_function_addr, |
| 629 | m_arg_values, |
| 630 | true); |
| 631 | if (call_plan == NULL) |
| 632 | return return_value; |
| 633 | |
| 634 | call_plan->SetPrivate(true); |
| 635 | |
| 636 | // We need to call the function synchronously, so spin waiting for it to return. |
| 637 | // If we get interrupted while executing, we're going to lose our context, and |
| 638 | // won't be able to gather the result at this point. |
| 639 | |
| 640 | process->Resume (); |
| 641 | |
| 642 | while (1) |
| 643 | { |
| 644 | lldb::EventSP event_sp; |
| 645 | |
| 646 | // Now wait for the process to stop again: |
| 647 | // FIXME: Probably want a time out. |
| 648 | lldb::StateType stop_state = process->WaitForStateChangedEvents (NULL, event_sp); |
| 649 | if (stop_state == lldb::eStateRunning || stop_state == lldb::eStateStepping) |
| 650 | continue; |
| 651 | |
| 652 | if (exc_context.thread->IsThreadPlanDone (call_plan)) |
| 653 | { |
| 654 | return_value = eExecutionCompleted; |
| 655 | break; |
| 656 | } |
| 657 | else if (exc_context.thread->WasThreadPlanDiscarded (call_plan)) |
| 658 | { |
| 659 | return_value = eExecutionDiscarded; |
| 660 | break; |
| 661 | } |
| 662 | else |
| 663 | { |
| 664 | return_value = eExecutionInterrupted; |
| 665 | break; |
| 666 | } |
| 667 | |
| 668 | } |
| 669 | |
| 670 | return eExecutionCompleted; |
| 671 | } |